Go Down

Topic: phase-shifted PWM (Read 845 times) previous topic - next topic

joepublic

Feb 19, 2008, 06:41 pm Last Edit: Feb 19, 2008, 06:42 pm by joepublic Reason: 1
I'm trying to make a program that quite simply, shifts the phase of PWM numbers.
So say I have two variables, var1 and var2.
var1 is moving up and down between 0 and 255. I want var2 to be 30 degrees out of phase of this,
so that when:
var1 = 0
var2 = 64

var 1 = 128
var2 = 192

But also,
var1 = 255
var2 = 192

Now this is getting into maths beyond my small brain's capability, especially seeing as I didn't do maths past GCSE.
Anyone care to lend a hand? Or a brain, rather?

CasNet

Greetings,

Doesn't something as simple as this work:

Code: [Select]
setup() {
   int var1 = 0;
   int var2 = 64;
}

loop () {
  var1 = (var1 + 1) % 256;
  var2 = (var2 + 1) % 256;
}



The variables start off with a fixed phase difference, and as long as they are incremented together, and wrap-around at the same point, they will stay in sync.

Regards,
David

Triffid Hunter

// stdint.h has types such as uint8_t and uint16_t
#include <stdint.h>

// inherent wrapping of limited data type does all the phase stuff you need during a simple add operation
// note that (255 + 1) & 255 = 0, which is exactly what happens when we work with 8 bit variables
uint8_t var, phase;

#define INITIAL_PHASE 30

// we recalculate the phase angle for 128 = 180 degrees, 256 = 360 degrees so simply adding does everything we need
// 30 degrees comes to about 21
for (var=0, phase=255 * INITIAL_PHASE / 360;;var++) {
 doSomethingWith(var);
 doSomethingWith(var + phase);
}

CasNet

That's clever. Thanks!
;D  :o  8-)

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!

Arduino
via Egeo 16
Torino, 10131
Italy