Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

avr microcontoller timers

Status
Not open for further replies.

Disha Karnataki

Full Member level 5
Full Member level 5
Joined
Jul 20, 2013
Messages
249
Helped
9
Reputation
18
Reaction score
8
Trophy points
18
Location
india
Visit site
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:
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?
 

A more robust way would be to setup an interrupt routine for the timer and use that to determine end of count. Using TCNTx directly is a bit tricky.

However since you ARE using TCNT0, then your if-loop will complete in 200 /12e6 secs or so.
And your for-loop with a count of ~90 will do this ~90 times giving a total delay of only 90 * 200 / 12 = 1500 MICRO-seconds, or 1.5 milli-seconds. Not 2 secs.
 
thank you sir..
i modified program got blinking effect
as we can see above in code:
Code:
for(i=x;i>0;i--)                
	{
	    
	     if(TCNT0<=255);           //loop till tcnt value becomes 190
		 TCNT0=0;                 //reset the tcnt value back to zero
    }
if i am changing the condition in if statement as
Code:
  if(TCNT0>=255);           //loop till tcnt value becomes 190
		 TCNT0=0;                 //reset the tcnt value back to zero
then too the blinking takes place but, whereas it should not blink at all in the second case right?
my explanation to this is: initially TCNT0=0 later as timer ticks TCNT0 reaches 255 so the second code if condition will always fail and will satisfy only when TCNT0=255. AM i right? so negligible delay.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top