
Sine from PWM dutycycle modulation
main.cpp@0:d274003b52e7, 2014-02-18 (annotated)
- Committer:
- wim
- Date:
- Tue Feb 18 20:08:15 2014 +0000
- Revision:
- 0:d274003b52e7
PWM SINE Test;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:d274003b52e7 | 1 | #include "mbed.h" |
wim | 0:d274003b52e7 | 2 | |
wim | 0:d274003b52e7 | 3 | //Number of dutycycle steps for output wave |
wim | 0:d274003b52e7 | 4 | #define SINE_STEPS 32 |
wim | 0:d274003b52e7 | 5 | //Frequency of output sine in Hz |
wim | 0:d274003b52e7 | 6 | #define SINE_OUT_FREQ 1000 |
wim | 0:d274003b52e7 | 7 | |
wim | 0:d274003b52e7 | 8 | //Constants to compute the sine waveform |
wim | 0:d274003b52e7 | 9 | #define PI 3.141592f |
wim | 0:d274003b52e7 | 10 | #define SINE_STEPS_RAD (2.0f * PI / (float)SINE_STEPS) |
wim | 0:d274003b52e7 | 11 | |
wim | 0:d274003b52e7 | 12 | //Table to generate the sine waveform using dutycycles |
wim | 0:d274003b52e7 | 13 | float sine_duty[SINE_STEPS]; |
wim | 0:d274003b52e7 | 14 | |
wim | 0:d274003b52e7 | 15 | |
wim | 0:d274003b52e7 | 16 | //Frequency of Pulse Width Modulated signal in Hz |
wim | 0:d274003b52e7 | 17 | #define PWM_FREQ 200000 |
wim | 0:d274003b52e7 | 18 | |
wim | 0:d274003b52e7 | 19 | //PWM pin |
wim | 0:d274003b52e7 | 20 | PwmOut PwmPin (p22); |
wim | 0:d274003b52e7 | 21 | |
wim | 0:d274003b52e7 | 22 | //Heartbeat LED |
wim | 0:d274003b52e7 | 23 | DigitalOut myled(LED1); |
wim | 0:d274003b52e7 | 24 | |
wim | 0:d274003b52e7 | 25 | |
wim | 0:d274003b52e7 | 26 | //Ticker to update the PWM dutycycle |
wim | 0:d274003b52e7 | 27 | Ticker pwm_ticker; |
wim | 0:d274003b52e7 | 28 | |
wim | 0:d274003b52e7 | 29 | //Ticker calls this fucntion to update the PWM dutycycle |
wim | 0:d274003b52e7 | 30 | void pwm_duty_updater() { |
wim | 0:d274003b52e7 | 31 | static int idx=0; |
wim | 0:d274003b52e7 | 32 | |
wim | 0:d274003b52e7 | 33 | PwmPin.write(sine_duty[idx]); // Set the dutycycle % to next value in array |
wim | 0:d274003b52e7 | 34 | idx++; // Increment the idx |
wim | 0:d274003b52e7 | 35 | if (idx == SINE_STEPS) idx=0; // Reset the idx when teh end has been reached |
wim | 0:d274003b52e7 | 36 | |
wim | 0:d274003b52e7 | 37 | } |
wim | 0:d274003b52e7 | 38 | |
wim | 0:d274003b52e7 | 39 | int main() { |
wim | 0:d274003b52e7 | 40 | int i; |
wim | 0:d274003b52e7 | 41 | |
wim | 0:d274003b52e7 | 42 | // Init the duty cycle array |
wim | 0:d274003b52e7 | 43 | for (i=0; i<SINE_STEPS; i++) { |
wim | 0:d274003b52e7 | 44 | sine_duty[i] = ( sin(i * SINE_STEPS_RAD) + 1.0f ) / 2.0f; // convert sine (-1.0 .. +1.0) into dutycycle (0.0 .. 1.0) |
wim | 0:d274003b52e7 | 45 | } |
wim | 0:d274003b52e7 | 46 | |
wim | 0:d274003b52e7 | 47 | // Set PWM frequency to 200 KHz (period = 5 us) |
wim | 0:d274003b52e7 | 48 | PwmPin.period( 1.0f / (float) PWM_FREQ); |
wim | 0:d274003b52e7 | 49 | |
wim | 0:d274003b52e7 | 50 | // Init the Ticker to call the dutycyle updater at the required interval |
wim | 0:d274003b52e7 | 51 | // The update should be at (SINE_STEPS * SINE_OUT_FREQ) |
wim | 0:d274003b52e7 | 52 | pwm_ticker.attach(&pwm_duty_updater, 1.0f / (float)(SINE_STEPS * SINE_OUT_FREQ)); |
wim | 0:d274003b52e7 | 53 | |
wim | 0:d274003b52e7 | 54 | while(1){ //infinite loop |
wim | 0:d274003b52e7 | 55 | myled = !myled; |
wim | 0:d274003b52e7 | 56 | wait(0.5); |
wim | 0:d274003b52e7 | 57 | } |
wim | 0:d274003b52e7 | 58 | |
wim | 0:d274003b52e7 | 59 | } |
wim | 0:d274003b52e7 | 60 |