Pages: [1]   Go Down
Author Topic: Timer/PWM Issues  (Read 347 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone! I have recently started using buttons in one of my projects, and I found a nice way to poll the buttons to make sure I always catch the keypress (I got the code from adafruit) Anyway, the code involves using timer2 to poll the buttons every 15 millis. To do that it uses the following bit of code to trigger interrupts:

  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;
  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;

It works extremely well, but now none of my PWM outputs are working. I tried pins on and off of timer2 to no avail. Is it not possible to use this type of interrupt and pwm in the same program?
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2386
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Polling the button from loop is all you need, assuming you don't use delay().

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unfortunately my code loop takes anywhere from 300 - 1200 ms to complete, so it seems I need some kind of interrupt driven button detection to reliably grab button presses. Anyone know why PWM stops working when I set the overflow interrupt?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4363
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Timer 2 is only used for PWM pins 3 and 11, at least it is on the UNO

I had a problem today when I added an interrupt driven library to a program using PWM on pin 3 and PWM on that pin stopped.  Changing to using PWM on pin 9 solved my problem.

Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont know if my uno is just messed up or something, but I was using pin 10 when that code killed it. I also tried 9 6 and 3, and it is all the same for me. Can anyone verify? Just use the fade example, paste in those lines, and try it on the different pins (change the default to another timer like pin 10). I just cant figure out whats going on.
Logged

Pages: [1]   Go Up
Jump to: