Hi,
I’m trying to understand the Timer1 and I have some questions on a tutorial code I’m using to learn the timer.
#include <mega16.h>
#define period_out PORTC
unsigned char ov_counter;
unsigned int starting_edge, ending_edge;
unsigned int clocks;
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
++ov_counter;
}
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{
ending_edge = 256*ICR1H+ICR1L;
clocks = (unsigned long) ending_edge + ((unsigned long) ov_counter * 65536) - (unsigned long) starting_edge;
period_out = ~(clocks / 750);
ov_counter = 0;
starting_edge = ending edge;
}
void main(void)
{
DDRC = 0xFF;
TCCR1A = 0xFF;
TCCR1B = 0xC2;
TIMSK = 0x24;
#asm("sei")
while(1)
{
;
}
}
I’m not sure if my understanding is right:
-
starting_edge will be set first time ICP is set
-
after that timer starts to count and may overflow
-
overflow quantities are being counted by ISR
-
when ICP is set again, duration is calculated
-
calculation is: the ending_edge time, when ICP goes set again, plus how many times overflow happened, minus the starting_edge when first ICP was set.
I also can’t understand this line:
period_out = ~(clocks / 750);
Well the timer is clocked by Fclk/8 (6MHz / 8 = 750) so 750 counts per ms, so we divide clocks by 750, but then what does the “~” operator there please?!
I would greatly appreciate the help to understand these moments.