I have a problem:
Since the interrupt for the input capture starts to late, i can only reset TNCT1 to 0 after a certain time. So there are some counts that are lost. This equals to 2-4us.
Shouldnt the interrupt start right away? Or is there a solution to have TCNT1 set to 0 as there is a input edge?
#define NANOSECONDS_PER_TIC 500 #define MICROS_TO_TICS(mics)( mics * (unsigned long) 1000 / NANOSECONDS_PER_TIC) #define TICS_TO_MICROS(tics)( tics * (unsigned long) NANOSECONDS_PER_TIC / 1000) void setup(){ Serial.begin(57600); delay(300); Serial.println(__DATE__); Serial.println(__TIME__); //all on pin 4 on ProMicro (ICP1) DDRB &= ~(1 << PB0); //testpin 6 DDRD |= (1 << PD7); //testpin 2 DDRD |= (1 << PD1); //testpin 3 DDRD |= (1 << PD0); TCCR1A = 0; TCCR1B = 0; TIMSK1 = 0; TCCR1A = 0; //bit to one, raising edge! TCCR1B |= (1 << ICES1); //enable interrupt TIMSK1 |= (1 << ICIE1); //Timer overflow interrupt enable TIMSK1 |= (1 << TOIE1); TCCR1B |= (1 << CS11); // full counter to FFFF / 8 prescaler, so 1 tic is 500ns } int difference = 0; void loop(){ Serial.println(difference); delay(100); } /* * ca. 3us 10.01.2019 * starts about 2us after edge change * ca. 10us 14.01.2019 0:36 (ohne delay(); welches zur Besseren Sichtbarkeit fürs oszi ist!) * ca. 8us 19.01.2019 21:51 */ ISR(TIMER1_CAPT_vect){ byte oldSREG = SREG; PORTD |= (1 << PD7); unsigned int tcnt1 = TCNT1; TCNT1 = 0; //reset count from this edge on //get counter value unsigned int currICP = ICR1; difference = tcnt1 - currICP; // toggle interrupt: next time with the other edge TCCR1B ^= (1 << ICES1); SREG = oldSREG; PORTD &= ~(1 << PD7); } ISR(TIMER1_OVF_vect){ byte oldSREG = SREG; SREG = oldSREG; }