problem with timer 2 in atmega16

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi dears

i wanna write a simple code to flashing led in each 1 second.

i have a led connected to portB0.

i have a problem with below code:

1-this code works but when i add TCNT2==132 if condition part  to my code it doesnt work. why?  1 second is made of  32 overflow+ tcnt2=132 

#define F_CPU 1000000

#include <avr/io.h>
#include <avr/interrupt.h>

int overflow=0;

void init_timer2(){
	TCCR2 |=(1<<CS20)|(1<<CS22);      // Prescaler=128
	TIMSK |=(1<<TOIE2);              // Enable Timer2 Overflow
	TCNT2=0;                        // Initialize Timer/Counter2
	sei();
}


ISR (TIMER2_OVF_vect){
	overflow++;			    // Increase The Number Of Overflow
		if(overflow==30){
		//if(TCNT2==132){
			PORTB ^=(1<<0);	   // Toggle LED
			overflow=0;	  // Reset Overflow Counter
			TCNT2=0;	  // Reset Timer/Counter2 Variable
		//}
		}
}

void main(void)
{
	DDRB|=(1<<0);
	init_timer2();
	while (1)
	{
	}
}

 

Aasshh

Last Edited: Thu. Sep 12, 2019 - 09:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aashkanpasha wrote:
when i add TCNT2==132 if condition part  to my code it doesnt work. why?
Because you are in the "OVF" (that is "Overflow") interrupt. That occurs when the timer "reaches the TOP". So the TCNT is going to be nowhere near 132 at the time it enters that interrupt handler.

 

Anyway as soon as folks start messing with specific TCNT values like 132 or whatever the code is clearly screaming out "Use CTC mode! Use CTC mode!"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you.

your answer was clear.

but another question :

1-why this code not orking,when i write it in while(1)

#define F_CPU 1000000

#include <avr/io.h>
#include <avr/interrupt.h>

unsigned char overflow;

void init_timer2(){
	TCCR2 |=(1<<CS22)|(1<<CS20);      // Prescaler=256
	TIMSK |=(1<<TOIE2);              // Enable Timer2 Overflow
	TCNT2=0;                        // Initialize Timer/Counter2
	sei();
	overflow=0;
}


ISR (TIMER2_OVF_vect){
	overflow++;			    // Increase The Number Of Overflow
}


void main(void)
{
	DDRB|=(1<<0);
	init_timer2();
	while (1)
	{
			if(overflow>=30){
			PORTB ^=(1<<0);		 // Toggle LED
			TCNT2=0;		// Reset Timer/Counter2 Variable
			overflow=0;		// Reset Overflow Counter
			}
	}
}

 

Aasshh

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have to tell us what "not working" means.  No LED activity?  Blinking too fast?  Blinking too slow?  AVR goes up in flames?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LED DONT HAVE ACTIVITY AND IS off- i write first code in ISR led blink but if i write second code in while loop it doesnt work- led is off,

Aasshh

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Step back to first principles. What happens if you just try:

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB = (1 << 0);
    while(1) {
        PORTB ^= (1 << 0);
        _delay_ms(100);
    }
}

That is bound to work so if you see no LED flashing with that then the LED is not on PB0 or it is otherwise wired incorrectly.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

'overflow' needs to be declared as volatile

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TNAK YOU DEAR- YOUR ANSWER WAS COMPLETLY TRUE AND HELPED ME

Aasshh