problem on timer2 OVF interrupt

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

Hi;

i have written a simple code to toggle PB0 using timer2 overflow interrupt:

.
.
int main()
{
.
DDRB = (1<<DDB0);
TIMSK = (1<<TOIE2);
sei();
TCCR2 = (1<<CS22) | (1<<CS21) | (1<<CS20);//CLK/1024
.
.
}


ISR(TIMER2_OVF_vect)
{
	static int Tim2_counter = 0;
	Tim2_counter++;
	if (Tim2_counter == 160)//AFTER 5 SECONDS
    	{
    		PORTB ^= (1<<PB0);
    		Tim2_counter == 0;
    	}
}

my problem is that PB0 toggles only once and remains set

This topic has a solution.
Last Edited: Thu. Mar 22, 2018 - 07:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the code in

.
.
}

Do you have a while(1) {} statement in there?  If not, then the LED is probably being lit by the DDRB = (1<<DDB0) statement and then the program exits.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

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

larryvc wrote:
What is the code in...

 

Indeed.  There is a checklist of information needed.  Doesn't it make sense that all items on the list might be needed to answer a question?

 

--  Tell AVR model and clock speed and Vcc level.

--  Tell language and toochain and version and build settings (including optimization level)

--  Show a complete test program.  When an operation question, show a schematic.  In this case what is connected to PB0?

 

 

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.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sorry! I found the problem!

Tim2_counter == 0;

double "=" !!!

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

Reza, we are volunteers here, if we are taking our time to comment on your thread at least have the courtesy to respond to those comments.  

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

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

Sure his '.'s are just saying "there's other stuff above/below this but you don't need to see that as the issue is with the timer" ? I often use "..." when sketching out code to say "other stuff here"

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

clawson wrote:
Sure his '.'s are just saying "there's other stuff above/below this but you don't need to see that as the issue is with the timer" ?

IMO -- using the ellipsis when posting illustrative code is one thing, but much different when it is a case of "my xxx doesn't work".

 

a = 4;

...

printf (%d, a);

...

 

"What could be the problem?  The value printed is 123!"

 

 

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: 1

larryvc wrote:
Reza, we are volunteers here, if we are taking our time to comment on your thread at least have the courtesy to respond to those comments.

yes. you are right and i'm sorry. please forgive me.

I was checking out your solution and I noticed that I have used double "=" and

when I found my code problem and said it in post  #4 and marked it as solution I thought all comments and questions are answered.

sorry again

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

clawson wrote:

Sure his '.'s are just saying "there's other stuff above/below this but you don't need to see that as the issue is with the timer" ? I often use "..." when sketching out code to say "other stuff here"

yes you are right