avr - Measuring the period of a square wave using microcontroller -


i new microcontroller. following code measures period of square wave. have marked lines haven't understood. code follows:

#include <avr/io.h> #include <avr/interrupt.h>  isr(timer1_capt_vect) {     int counter_value = icr1; //16 bit value     portb = (counter_value >> 7); // has been done here?     tcnt1 = 0; // why line?  }  int main(void) { ddrb = 0xff; tccr1a = 0x00; tccr1b = 0b11000010; timsk = 0b00100000; sei(); while(1); cli(); } 

what has been done in lines?

isr(timer1_capt_vect) {     int counter_value = icr1; //16 bit value     portb = (counter_value >> 7); // has been done here? 

portb set of 8 output lines. presumably, connected bus device haven't mentioned. maybe set of leds display binary number.

the result counter 16 bits. significant bits, shift result right discard less significant bits. (this operation loses precision, have 8 bits of output, not 16.) why shift 7 instead of 8, or why unsigned value of counter saved signed int first, don't know. suspect mistake. have done portb = (icr1 >> 8); instead.

    tcnt1 = 0; // why line? 

since have recorded time of capture , sent out portb, want reset timer next capture.

} 

Comments