Growing stuff and doing things

dstroy0

Zeroes and Ones
something something monsters ahead

It's prolly fine. Who needs memory?

Capture.PNG

lol

I'm running through refactoring things today. So I'll probably end up doing regression testing later today or tomorrow.

Simple debugging and refactoring. I just run bits of code and time them, move class members up or down and rerun, maybe try and rearrange formulas to not have to deal with float division which takes an eternity on this microcontroller.
 

dstroy0

Zeroes and Ones
I fixed a fun little problem yesterday,

On the arduino mega, they used two different types of pwm by default. TCCR0 is the timer that generates millis(), delay() etc. It's one you don't want to mess with unless you don't need certain built in functionality. It uses fast PWM which counts up to 255 and then gets reset to 0.

The other timers use phase-correct PWM differs from fast PWM in a couple of ways but importantly it counts up to 255, then counts back down to 0. but not always. Sometimes it misses a cycle? I'm not really sure whats happening. You can observe the behavior on an oscilloscope, running PWM on pins from TCCR0 and any of the other TCC2-5. What it does is make like a little beat flicker when it's out of phase.

It's ridiculously easy to fix. You just set any other TCC you have connected to fast PWM and any flicker will be gone, or outside the refresh rate of your eyeballs.

For me, I'm using pin 4 (TCC0) and pin 9 (TCC2), I had to set TCCR2 to fast PWM.

Code:
  //arduino mega2560
  //non-inverting fast pwm pin 9/10
  bitSet(TCCR2A, COM2A1);
  bitClear(TCCR2A, COM2A0);
  bitSet(TCCR2A, COM2B1);
  bitClear(TCCR2A, COM2B0);
  bitSet(TCCR2A, WGM21);
  bitSet(TCCR2A, WGM20);
  bitClear(TCCR2B, WGM22);
 

Deebs

The Sentient Naturewalker
Staff member
Administrator
Moderator
Cool stuff, just starting to read/play with the pulse width modulation stuff...hope you don't mind the question :)

Does phase correct PWM divide the frequency by two compared to fast PWM? because the timer is going both up and down. Would you divide by 255 or 256? I think I read something about a 1 off rule..
 

dstroy0

Zeroes and Ones
Cool stuff, just starting to read/play with the pulse width modulation stuff...hope you don't mind the question :)

Does phase correct PWM divide the frequency by two compared to fast PWM? because the timer is going both up and down. Would you divide by 255 or 256? I think I read something about a 1 off rule..

pp 174 in the datasheet for an atmega2650

1585834339212.png

The N variable represents the prescale factor (1, 8, 32, 64, 128, 256, or 1024).
 
Top Bottom