Uses trigonometry to produce high quality 3 phase sinewave output waveworms on the PWM. Tested from 30 to 100Hz at 10kHz modulation. Changes frequency glitch free. Amplitude is scalable. Could be used as a basic invertor back-end. Not optimised for processor overhead, could be modified to use lookup tables if less accuracy required. Oscilloscope trace here: http://mbed.org/users/waynemcl/notebook/sine3-v2/
main.cpp@0:d040778370fc, 2010-11-28 (annotated)
- Committer:
- waynemcl
- Date:
- Sun Nov 28 22:39:32 2010 +0000
- Revision:
- 0:d040778370fc
v2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
waynemcl | 0:d040778370fc | 1 | #include "mbed.h" |
waynemcl | 0:d040778370fc | 2 | #include "stdio.h" |
waynemcl | 0:d040778370fc | 3 | |
waynemcl | 0:d040778370fc | 4 | #define PI 3.1416 |
waynemcl | 0:d040778370fc | 5 | |
waynemcl | 0:d040778370fc | 6 | // License: You may copy or modify and re-use this code, but you MUST credit me: |
waynemcl | 0:d040778370fc | 7 | // Wayne McLachlan, www.embeddedengineering.co.nz |
waynemcl | 0:d040778370fc | 8 | // You may add your name to the author list if you have done significant modifications or improvements |
waynemcl | 0:d040778370fc | 9 | |
waynemcl | 0:d040778370fc | 10 | PwmOut chA(p21); |
waynemcl | 0:d040778370fc | 11 | PwmOut chB(p22); |
waynemcl | 0:d040778370fc | 12 | PwmOut chC(p23); |
waynemcl | 0:d040778370fc | 13 | |
waynemcl | 0:d040778370fc | 14 | Serial pc(USBTX, USBRX); |
waynemcl | 0:d040778370fc | 15 | Ticker sine3ticker; |
waynemcl | 0:d040778370fc | 16 | |
waynemcl | 0:d040778370fc | 17 | #define SINE3_PWM_FREQ 10000 |
waynemcl | 0:d040778370fc | 18 | |
waynemcl | 0:d040778370fc | 19 | unsigned char sine3freq; |
waynemcl | 0:d040778370fc | 20 | float sine3mag; |
waynemcl | 0:d040778370fc | 21 | float sine3theta; |
waynemcl | 0:d040778370fc | 22 | |
waynemcl | 0:d040778370fc | 23 | void sine3tick() { |
waynemcl | 0:d040778370fc | 24 | // offset PWM for bipolar: 0.5 => 0V |
waynemcl | 0:d040778370fc | 25 | chA = 0.5 + (sine3mag * sinf(sine3theta) /2); |
waynemcl | 0:d040778370fc | 26 | chB = 0.5 + (sine3mag * sinf(sine3theta + 2*PI /3)/2); // + 120 degrees |
waynemcl | 0:d040778370fc | 27 | chC = 0.5 + (sine3mag * sinf(sine3theta + 2*PI*2/3)/2); // + 240 degrees => - 120 degrees |
waynemcl | 0:d040778370fc | 28 | |
waynemcl | 0:d040778370fc | 29 | sine3theta += 2*PI / (SINE3_PWM_FREQ / sine3freq); // one cycle / steps per cycle |
waynemcl | 0:d040778370fc | 30 | if (sine3theta >= 2*PI) { |
waynemcl | 0:d040778370fc | 31 | sine3theta = 0; |
waynemcl | 0:d040778370fc | 32 | } |
waynemcl | 0:d040778370fc | 33 | } |
waynemcl | 0:d040778370fc | 34 | |
waynemcl | 0:d040778370fc | 35 | |
waynemcl | 0:d040778370fc | 36 | void sine3_init() { |
waynemcl | 0:d040778370fc | 37 | chA.period_us( 1000000 / SINE3_PWM_FREQ ); // period in uS. Common to all PWM |
waynemcl | 0:d040778370fc | 38 | chA = 0.5; |
waynemcl | 0:d040778370fc | 39 | chB = 0.5; |
waynemcl | 0:d040778370fc | 40 | chC = 0.5; |
waynemcl | 0:d040778370fc | 41 | |
waynemcl | 0:d040778370fc | 42 | sine3mag = 0.9; // prevent overmodulation |
waynemcl | 0:d040778370fc | 43 | sine3freq = 55; // compromise between 50Hz and 60Hz |
waynemcl | 0:d040778370fc | 44 | sine3theta = 0; |
waynemcl | 0:d040778370fc | 45 | sine3ticker.attach_us(&sine3tick, (1000000 / SINE3_PWM_FREQ) ); // period in uS |
waynemcl | 0:d040778370fc | 46 | //pc.printf("\n\r init "); |
waynemcl | 0:d040778370fc | 47 | } |
waynemcl | 0:d040778370fc | 48 | |
waynemcl | 0:d040778370fc | 49 | |
waynemcl | 0:d040778370fc | 50 | int main() { |
waynemcl | 0:d040778370fc | 51 | sine3_init(); |
waynemcl | 0:d040778370fc | 52 | while (1) { |
waynemcl | 0:d040778370fc | 53 | sine3freq++; |
waynemcl | 0:d040778370fc | 54 | if (sine3freq > 100) { |
waynemcl | 0:d040778370fc | 55 | sine3freq = 30; |
waynemcl | 0:d040778370fc | 56 | pc.printf("\n\r"); |
waynemcl | 0:d040778370fc | 57 | } |
waynemcl | 0:d040778370fc | 58 | wait(0.2); |
waynemcl | 0:d040778370fc | 59 | pc.printf("%d ",sine3freq); |
waynemcl | 0:d040778370fc | 60 | } |
waynemcl | 0:d040778370fc | 61 | } |