counting of PWM pulses

Hello all,

I want to make a project using Timer 1 of Arduino Uno for counting 3000 PWM pulses (having 50% duty cycle). after counting 3000 PWM pulses at pin 5, it will generate a 100 microsecond pulse at pin 10 and then restart counting 3000 pulses.

I have tried but unable to code, Can anyone help about this?

Thanking you,

Post your best attempt. Describe what the code actually does. Describe how that is different from what you want.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code. If the code is too large to post it can be attached.

What frequency will that PWM have?

Does it have to be output on pin 10? If you use pin 9 you can leverage the output compare (channel A) function of Timer 1.

This works on the bench here. It assumes a reasonably stable PWM input. It also uses timer 1 at full speed; if you have a very slow PWM input you'll likely need to adjust the prescaler for timer 1 to slow its tick rate down (and you'd have to adjust the constant K_NUM_TICKS_100US...)

#define NUM_EDGES               3000u       //#         ticks of incoming rising edges before driving output
#define K_NUM_TICKS_100US       1600u       //#         ticks of 16MHz clock for 100uS output pulse

const uint8_t pinOC1A = 9;
const uint8_t pinInput = 8;
const uint8_t pinTest = 7;                  //generates a test signal that can be connected to pin 8 for bench testing

void setup() 
{
    pinMode( pinInput, INPUT );
    pinMode( pinOC1A, OUTPUT );
    pinMode( pinTest, OUTPUT );
    //set up timer 1 for input capture interrupt
    TCCR1A = 0;    
    TCCR1B = _BV(ICNC1) | _BV(ICES1) | _BV(CS10);          //noise canceller, rising edge, /1 prescaler
    TIMSK1 = _BV(ICIE1);                                //enable input capture interrupts   

}//setup

void loop() 
{
    //generate test waveform on pin 7
    digitalWrite( pinTest, HIGH );
    delayMicroseconds( 50 );    
    digitalWrite( pinTest, LOW );
    delayMicroseconds( 50 );

}//loop

ISR( TIMER1_CAPT_vect )
{
    static uint16_t
        period,
        timeLast,
        timeNow;    
    static uint16_t
        edgeCount = 0;

    //find the time since the last rising edge of the input
    period = ICR1 - timeLast;
    
    //count this edge
    edgeCount++;
    if( edgeCount == NUM_EDGES - 1 )
    {
        //if we're at edge 2999, set up the OC to set the output pin 'duration' from now
        //so the rising output edge corresponds to the incoming rising edge        
        TCCR1A &= ~_BV(COM1A1);             //set OC to set pin
        TCCR1A |= _BV(COM1A0);
        OCR1A = ICR1 + period;              //set time to the expected next incoming rising edge
        TIFR1 |= _BV(OCF1A);                //clear any outstanding OC flags
        
        //no interrupt is needed for this edge
        
    }//
    else if( edgeCount == NUM_EDGES )
    {        
        OCR1A = OCR1A + K_NUM_TICKS_100US;  //set OC1A to occur 100uS from time OC went high
        TCCR1A |= _BV(COM1A1);              //set OC to clear pin
        TCCR1A &= ~_BV(COM1A0);
        
        TIFR1 |= _BV(OCF1A);                //clear any outstanding OC flags
        TIMSK1 |= _BV(OCIE1A);              //set OC interrupt
        
        edgeCount = 0;                      //reset counter for next 3K count
        
    }//if

    //save time of this edge for on-going period measurements
    timeLast = ICR1;
    
}//IST input cap 1

ISR( TIMER1_OC1A_vect )
{
    //when OC occurs, revert pin to std I/O and halt OC interrupt
    TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
    TIMSK1 &= ~(_BV(OCIE1A));
    
}//ISR OC1A

dev31:
PWM pulses (having 50% duty cycle)

If the duty cycle is a constant 50% it's not PWM!

groundFungus:
Post your best attempt. Describe what the code actually does. Describe how that is different from what you want.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code. If the code is too large to post it can be attached.

Thank you for your valuable guidance. I will try to define the requirement properly in future.

Then add to the title "SOLVED" for now please