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.

Timer2 as Timer Not Working Correctly on avr atmega32a

Status
Not open for further replies.

maxbayne

Newbie
Newbie level 2
Joined
Dec 12, 2022
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
19
hiii i try to config timer2 (8bit) inside atmega32a
same configration tested over timer0 and worked perfect but when i tried it over timer2 not working

i try to generate timer every 1 second so i used count with some times to overflow the timer like i overflow timer 61 count so it mean take 1 second

i used info to calc the overflow count from this site
https://eleccelerator.com/avr-timer-calculator/

prescaler.png



61.png


488.png


this code work fine with timer0 and generate 1 second timer but with timer2 it generates 125 ms
if i change the timer2Counter to checked with 488 it generate 1 second

why 61 working with timer0 and not working with timer2 while timer0 is same as timer2

any help plz where is the issue , i tested this with real hardware and same result

this is my code



C++:
#include "avr/interrupt.h"


int main(void)
{
  //Configuration ----------------------------------------------------


  //Config Port A as Output using some helper function
  GPIO::Port_Direction(GPIO_PORT_A,GPIO_DIRECTION_OUTPUT);
 
  //Set Timer Initial Value = 0
  TCNT2=0;

  //Set Timer Mode To Normal and Prescaler 1024
  TCCR2=0b00000101;

  //Enable Global Interrupt using register (SREG)
  BITWISE_SET_BIT(SREG,SREG_I);     //or use sei();

  //Enable Timer2 Interrupt using register (TIMSK)
  BITWISE_SET_BIT(TIMSK,TOIE2);


  //Loop ----------------------------------------------------
  while(1)
  {
     GPIO::Pin_Clear(GPIO_IO_PA1);

     delay_ms(1000);

     GPIO::Pin_Set(GPIO_IO_PA1);

     delay_ms(1000);

  }

}


int timer2Counter;
ISR(TIMER2_OVF_vect)
{
    timer2Counter++;

    //if i use 488 which (61*8) it work correct and generate 1 second timer , but if i use 61 generate 125ms this issue only with timer2 but with timer0 61 worked fine
    if(timer2Counter==61)
    {
        GPIO::Pin_Toggle(GPIO_IO_PA2);

        timer2Counter=0;
    }
    
}
 

thanks so much @KlausST
t
prescaler.png
this is my fault i thought that all timer with same clock source configurations but iam wrong and you are correct
many thanks to you ... i have 2 weeks research for it and bought real hardware to test , i said may be proteus wrong ... thanks again
 

What is the difference between TIMER0 and TIMER2?


Unlike other timers, TIMER2 offers us with a wide range of prescalers to choose from. In TIMER0/1 the prescalers available are 8, 64, 256 and 1024, whereas in TIMER2, we have 8, 32, 64, 128, 256 and 1024! Since we are choosing 256 as the prescaler, we choose the 7th option (110).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top