- Tue Mar 01, 2011 3:11 pm
#121696
Hi,
I'm trying to understand the Timer1 and I have some questions on a tutorial code I'm using to learn the timer.
1. starting_edge will be set first time ICP is set
2. after that timer starts to count and may overflow
3. overflow quantities are being counted by ISR
4. when ICP is set again, duration is calculated
5. 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:
I would greatly appreciate the help to understand these moments.
I'm trying to understand the Timer1 and I have some questions on a tutorial code I'm using to learn the timer.
Code: Select all
I'm not sure if my understanding is right:#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)
{
;
}
}
1. starting_edge will be set first time ICP is set
2. after that timer starts to count and may overflow
3. overflow quantities are being counted by ISR
4. when ICP is set again, duration is calculated
5. 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:
Code: Select all
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?!period_out = ~(clocks / 750);
I would greatly appreciate the help to understand these moments.