Go Down

Topic: Timer Interrupts and PWM Pins (Read 14120 times) previous topic - next topic

Zealot

I just started reading about Timers and Interrupts.

From what I understand the 328 has 3 timers (TIMER0, TIMER1, TIMER2), which is used in Arduino (UNO, etc.) to control the PWM pins:
- Pins 5 and 6: controlled by Timer 0
- Pins 9 and 10: controlled by timer 1
- Pins 11 and 3: controlled by timer 2

I always thought those PWM pins are different from the other I/O pins - but it seems they are not, no?

Furthermore, if I write my own routine using TIMER0, PWM Pins5 and 6 - and the delay() function! - will not work any more, correct?


Peter_n

Correct.

Those pins are normal digital pins. The timer input and outputs connected to those pins are something extra. The Timers will only use the pins if that is programmed in the registers.
Even the A0...A5 are normal digital pins, The analog section is an extra.
The interrupt pins, the I2C bus, the SPI bus are all something extra for normal digital pins..

The pinmapping drawing is very useful. Google for : arduino pinmapping

Arduino uses TIMER0 for the timing. You should not change that. You can do whatever you want with TIMER1 and TIMER2. Some functions or libraries use those timers (pwm, tone(), and many more), so you have to be careful how they are used.


Zealot

Thanks, I understand.

Do you know why they used all 3 timers to provide PWM functionality? I mean they could have used only one timer.

And why exactly those 6 pins? And not 4 pins. Or 8 pins.

Delta_G

#3
Jun 06, 2015, 01:24 pm Last Edit: Jun 06, 2015, 01:28 pm by Delta_G
Thanks, I understand.

Do you know why they used all 3 timers to provide PWM functionality? I mean they could have used only one timer.

And why exactly those 6 pins? And not 4 pins. Or 8 pins.
Because each of those timers has two compare registers, and so has the ability to control two pins with pwm.  It couldn't be done with only one timer the way those are built.

The pwm is hardware, it doesn't use the interrupt.  If you want to create a "software pwm" using the interrupt routine you could theoretically control more pins but you would have some limits to what you could output that way.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Zealot

OK, so lets make an example:

- I want to use delay() in my code -> I can't use TIMER0 for my project any more.
- I also want to use TMRpcm library (for PCM/WAV sound) -> I can't use TIMER1 for my project any more and PWM Pins 9 and 10 are also gone.

All that's left is TIMER2. But if I use that, I lose PWM Pins 3 and 11.

So finally I end up with only two available PWM Pins (Pins 5 and 6, controlled by TIMER0)

Is that correct?

Peter_n

#5
Jun 06, 2015, 02:05 pm Last Edit: Jun 06, 2015, 02:06 pm by Peter_n
Yes, the Arduino timing and pwm can both use TIMER0, that is why the PWM for TIMER0 has a different frequency.
http://www.arduino.cc/en/Reference/analogWrite

The pins are not gone for normal use (output or input pin), but they are gone for PWM.

Delta_G mentioned the software PWM. Such things are often used for Servo motors, frequency outputs, and so on. It uses a single timer with interrupt. Inside that interrupt the pins are set high and low. That way every pin can have a 'software' pwm.

Go Up