SOLVED: PWM on Mega: different behavior Aruino 0018 vs 0022


I've been using Arduino 0018 and recently switched to 0022. With some minor modifications, everything runs fine, I can compile the same code on 0018 and 0022.

The odd thing is that my PWM (on pin 2, 5,6) behaves weird when compiled with 0022; every 1 second the PWM switches off and on again. I suspect this is related to either millis(); delay(); analogreference() or analogread() commands that are repeated every 1 second.

The PWM frequency is higher than default: prescaler is set to 1 (no prescaling) and it's using 9-bits fast-PWM mode.

This problem doesn't occur when compiled with arduino 0018.

Any ideas? are millis() or delay() using a different timer in 0022 vs 0018?


Found the answer...

from wiring_analog.c (\hardware\arduino\cores\arduino) :

void analogWrite(uint8_t pin, int val) { pinMode(pin, OUTPUT); if (val == 0) { digitalWrite(pin, LOW); } else if (val == 255) { digitalWrite(pin, HIGH); }

this was changed somewhere in-between IDE 0018 and IDE 0022

I was using 9-bit PWM, where 511 = high. A value of 50% (255) gave me the problems.

IMHO, This is a pretty dirty method used here. Time to implement some proper Arduino-proof methods of setting the different PWM resolutions (8,9,10-bit, Fast PWM vs Phase-correct; different prescalers?)

Who's up for the challenge?