Go Down

Topic: Timer interrupt and PWM (Read 509 times) previous topic - next topic

Mazen21

Mar 18, 2012, 04:47 pm Last Edit: Mar 18, 2012, 04:52 pm by Mazen21 Reason: 1
Hello,
can I use analogWrite() function and Timer interrupt in the same code?
Thank you.

robtillaart

which Timer, arduino has more....

Easiest way is just to try -

What is your goal?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Nick Gammon


can I use analogWrite() function and Timer interrupt in the same code?


Yes.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

MarkT

If you change the setup of the timer it can affect analogWrite, but you can add an interrupt handler for timers 1 and 2.

timer0 already has an interrupt handler that maintains the millis(), micros() and delay() time information, so its not wise to use this interrupt.

timer0 is used for PWM pins 5 and 6,
timer1 is used for PWM pins 9 and 10,
timer2 is used for PWM pins 11 and 3

(Arduino Mega's are different, note)
[ I won't respond to messages, use the forum please ]

Mazen21

Thank you all for your answers.
I'm using arduino mega adk, and already using analogWrite() in pin 2 and 3.
I'm trying to do some code with a 5ms periode, The best way, I think, is using the timer interrupt, but I don't want to affect the analogWrite function.
Please if you have any links or answers send it to me.

Nick Gammon

Here is a link:

http://gammon.com.au/forum/?id=11504
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up