Go Down

Topic: Timer4 on Leonardo (Read 3791 times) previous topic - next topic


It looks like there have been some reshuffling with the Leonardo board. Too bad, as this breaks compatability with some shields. Even worse, as I am a first time Arduino user and have been tring to fix this for quiet some time.
I am using AdaFruit Motor Shield (http://adafruit.com/products/81) which uses Timer0 and Timer2 on the Arduino Uno and pins 3 & 11 and 5 & 6.
It's easy to substitute Timer2 (Uno) on pins 3 & 11 with Timer0 (Leo), using on the same pins, but Timer 4 (Leo), that I want to use instead of Timer0 (Uno) is a bit more advanced and has different setup.
The code for the Uno, that I have is:
Code: [Select]

TCCR0A |= _BV(COM0B1) | _BV(WGM00) | _BV(WGM01); // fast PWM, turn on oc0a
OCR0B = 0;

The code, that I am going to try is:
Code: [Select]

TCCR4A |= _BV(COM4A0) | _BV(PWM4A); // fast PWM, turn on OC4A
TCCR4D |= _BV(WGM40) | _BV(WGM41);
OCR4A = 0;
pinMode(5, OUTPUT);

OC4A on Pin 6 is complimentary to P13. Does that mean, that both of them are set?
Is this the correct setup to use Timer4 on the Arduino Leonardo as a fast-PWM 8-bit timer, similar to Timer0 on the Uno?


Have you considered just using the MsTimer2 library which already handles Timer4 on Leonardo?


That is, using it on the Leonardo works fine, as it substitutes Timer4 for Timer2


I don't think I can directly do that. The code is part from the library for the Motor Shield. It expects PWM generated by timer on pins 3 & 11 and 5 & 6, which is achieved with this register manipulation.
I need a way to configure Timer4 similar to the way Timer0 is configured. The MsTimer2 code doesn't help much, as it uses Timer4 in high-speed mode.


For the future generations:
Timer4 works like e regular 8bit timer, unless the 10-bit registers are used. The only thing required to put in in Fast-PWM mode is to set the relevant COM4x1..0 and PWM4x.

There are several complimentary pins to the main one operated by the timer. For example OC4A is on P13, but it's complimentary is at P5. The documentation states that the OC4A complimentary - pin 5 is connected when COM4A1..0 is set to 01. But this way my shield, which was expecting PWM on P5 didn't work. Setting COM4A1..0 to 10 made it work...

So, this code, that sets the Mega328/128/etc Timer0 to FastPWM on OC0B:
Code: [Select]

TCCR0A |= _BV(COM0B1) | _BV(WGM00) | _BV(WGM01); // fast PWM, turn on oc0a
OCR0B = 0;

Is equivalent to this Mega32u4 code, that sets Timer4 to FastPWM on OC4A (same pin 5, as the above):
Code: [Select]

TCCR4A |= _BV(COM4A1) | _BV(PWM4A);
OCR4A = 0;

Turns out I have been setting WGM because of reading the datasheets for the 100th time in 4 in the morning ;)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131