Disha Karnataki
Full Member level 5
- Joined
- Jul 20, 2013
- Messages
- 249
- Helped
- 9
- Reputation
- 18
- Reaction score
- 8
- Trophy points
- 18
- Location
- india
- Activity points
- 2,231
i am now getting started with AVR atmega8 12mhz crystal
so as i was learning about timers.
my doubt is :
suppose i want use timer-0 with no prescaling then total counter value=255.
so the max delay is 255*(1/12mhz)=21.25microsec.
suppose i want to blink leds on portb with delay of say 2 seconds so can i just do it without using prescalers like this:
as seen here for loop repeats say for 2seconds 94 times(94 constant value has to be passed)
i know actual value is somewhat 94.11764 which microcontoller doesnot support and hence there is error.
That is fine for this example but, the problem is there is no blinking of leds but only there is dimming effect.
what may be wrong?
so as i was learning about timers.
my doubt is :
suppose i want use timer-0 with no prescaling then total counter value=255.
so the max delay is 255*(1/12mhz)=21.25microsec.
suppose i want to blink leds on portb with delay of say 2 seconds so can i just do it without using prescalers like this:
Code:
#include <avr/io.h>
void tim0(unsigned int);
int main(void)
{
DDRB=0X0F; //making lower nibble o/p port
PORTB=0X00;
TCCR0=0X01; //timer with no prescaler
TCNT0=0X00; //initial count value=0x00
while(1)
{
PORTB=0X0A; //portb has 0x0a value
tim0(90); //set some delay
PORTB=0X00;
tim0(94);
PORTB=0X05; //portb value =0x00
tim0(94); //set some delaY
}
}
void tim0(unsigned int x) //sub routine for delay
{
unsigned int i;
for(i=x;i>0;i--)
{
if(TCNT0<=199); //loop till tcnt value becomes 190
TCNT0=0; //reset the tcnt value back to zero
}
return; //continue this loop till i=0
}
as seen here for loop repeats say for 2seconds 94 times(94 constant value has to be passed)
i know actual value is somewhat 94.11764 which microcontoller doesnot support and hence there is error.
That is fine for this example but, the problem is there is no blinking of leds but only there is dimming effect.
what may be wrong?