Triggering timer counter inside an interrupt

Hey guys

I have a question: is it possible to trigger a counting on timer1 for example inside an interrupt which was called by attachInterrupt()?

In my situation, I need to wait for a signal and then trigger a counting until a certain value in order to generate a pulse, like a delay.

My code:

#include <TimerOne.h>


int intPin = 2;


void setup(){
    DDRD = B11111011; //D2 as input
    DDRB = B00000001; //D8 as output
    
    TCCR1A = 0; // normal operation
    TCCR1B = 0; // clear register
    TCNT1 = 0; //zero counter
    
    OCR1A = 3124; //16MHz/(PSC*f)-1 (<65536)    f => 1000ms = 1Hz    PSC: defined via datasheet
    TIMSK1 |= (1<<OCIE1A); //when TCNT1 reaches OCR1A, TIMSK1A flags
    TCCR1B |= (1<<CS10)|(1<<CS12)|(1<<WGM12); //PSC setup and defines CTC mode.
    attachInterrupt(digitalPinToInterrupt(intPin),int_0,RISING);
   

}


void loop(){
 
  
      
    
}

void int_0(){

    
     
}

ISR(TIMER1_COMPA_vect){
  PORTB |= (1<<PORTB0);
  delayMicroseconds(50);
  PORTB |= (0<<PORTB0);
  delayMicroseconds(50);
}

In my case, the pinD2 waits for a RISING edge to trigger the interrupt int_0(). I would like to do the task on ISR(TIMER1_COMPA_vect) only after int_0 was called. I thoight about putting TCNT1=0 and OCR1A inside int_0()

your delayMicroseconds(50);should work

if you want to toggle PORTB0, you might want to use PINB (you can toggle any pin by writing a 1 to the corresponding PIN register position), it will be faster than using |= (1 clock cycle)

I have a question: is it possible to trigger a counting on timer1 for example inside an interrupt which was called by attachInterrupt()?

In my situation, I need to wait for a signal and then trigger a counting until a certain value in order to generate a pulse, like a delay.

Looks very similar to an ac phase control application where a delayed turn on is triggered by a zero cross interrupt.

See Arduino Playground - ACPhaseControl

@J-M-L can u please explain difference in toggling portb and pinb.
Or link to known article about that its interesting.
Thanks

surepic:
@J-M-L can u please explain difference in toggling portb and pinb.

well you do this

 PORTB |= (1<<PORTB0);
  delayMicroseconds(50);
  PORTB |= (0<<PORTB0);
  delayMicroseconds(50);

and I thought what you wanted to do is turn ON and OFF PORTB0 (which it does not because a OR won’t write a 0, you need a AND with bits all at 1 except the one you want to set to 0)

writing 0b00000001 in PINB will toggle the first bit (in modern AVRs - and when the pin is an output)

it’s documented in AVR reference, but you can find many reference to that online, for example here