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.

[SOLVED] Atmega32 timer2 problem

Status
Not open for further replies.

S.P.S

Member level 5
Member level 5
Joined
Oct 30, 2011
Messages
89
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Thodupuzha, India, India
Visit site
Activity points
1,742
hi,

i am doing a project in atmega32.

i am using timer0 as counter and timer 2 as timer with an 8 mhz crystal. i have an interrupt routine which wakes every 10 ms.

prescalar is set to 1024 for timer 2 and loaded a value for 10 ms delay and i count up to 1000 interrupts for a 10s delay.
but when i simulate the program i got the serial output after 33s which is expected to be 10s.serial routine and the interrupt routine works good .
what can be the reason

i am posting my code
Code:
#include <avr/atmega32.h>
#include <avr/interrupt.h>
unsigned char beat,temp;
unsigned int cnt=0;
int main(void)
{
	uart_init();
	 TCNT0=0;
	 TCNT2=178;	 
	 TCCR2=0x07;
	 TCCR0=0x06;
	 TIMSK=0x40;
	 sei ();	 
    while(1);
}
ISR (TIMER2_OVF_vect)
{
	cnt++;
	if(cnt==1000)
	{
	uart_tx(TCNT0);
	TCNT2=178;
	TCNT0=0;
	cnt=0;	
	}	 
}


i am using atmel studio6,winavr and proteus
i havn't tested it on hardware

please help me,
thanks in advance
 

make your cnt == 305 that will work

- - - Updated - - -

make your cnt == 305 that will work
It's calculation based on the F-CPU and in the link below you can have the idea also like you want an interval of 10 seconds
just enter in the real time (secs),
that will help you

https://eleccelerator.com/avr-timer-calculator/
 

thanks,
its works, But what am i doing wrong in the calculation

i set prescalar value to fosc/1024, that means the timer runs at 8/1024=7812.5 hz.
ie the timer takes 0.128ms for one count.
so for 10 ms the timer should count up to 10/.128=78

i had loaded timer with 256-78=178

so the timer should overflow after 10ms

so after 1000 overflow the real time will be 10 ms ,but i got 33 ms which means timer counts up to 256 counts.

how it happens in the above program,what is the mistake in my calculation?

thanks in advance.
 

that is only i told you i thought if you are not getting the calculations
so for ease I had given you the link
 

I think the problem is

Code:
ISR (TIMER2_OVF_vect)
{
	cnt++;
	if(cnt==1000)
	{
	uart_tx(TCNT0);
	TCNT2=178;
	TCNT0=0;
	cnt=0;	
	}	 
}

Each time you enter the interrupt you should set the timer to 178
Code:
TCNT2=178;
but you don't do that so after the first interrupt the timer counts from 0 all the way up.

try with
Code:
ISR (TIMER2_OVF_vect)
{
	[COLOR="#FF0000"]TCNT2=178;[/COLOR]
	cnt++;
	if(cnt==1000)
	{
	uart_tx(TCNT0);
        TCNT2=178;	
	TCNT0=0;
	cnt=0;	
	}	 
}
 
  • Like
Reactions: S.P.S

    S.P.S

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top