Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@0:d76a3b12c090, 2015-08-17 (annotated)
- Committer:
- VigneshRSR
- Date:
- Mon Aug 17 00:09:19 2015 +0000
- Revision:
- 0:d76a3b12c090
SawtoothWave
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| VigneshRSR | 0:d76a3b12c090 | 1 | #include "mbed.h" |
| VigneshRSR | 0:d76a3b12c090 | 2 | |
| VigneshRSR | 0:d76a3b12c090 | 3 | //Number of dutycycle steps for output wave |
| VigneshRSR | 0:d76a3b12c090 | 4 | #define SAW_STEPS 33 |
| VigneshRSR | 0:d76a3b12c090 | 5 | |
| VigneshRSR | 0:d76a3b12c090 | 6 | //Frequency of output sine in Hz |
| VigneshRSR | 0:d76a3b12c090 | 7 | #define SAW_OUT_FREQ 1 |
| VigneshRSR | 0:d76a3b12c090 | 8 | |
| VigneshRSR | 0:d76a3b12c090 | 9 | //Frequency of Pulse Width Modulated signal in Hz |
| VigneshRSR | 0:d76a3b12c090 | 10 | #define PWM_FREQ 6600 |
| VigneshRSR | 0:d76a3b12c090 | 11 | |
| VigneshRSR | 0:d76a3b12c090 | 12 | float saw_duty[SAW_STEPS]; |
| VigneshRSR | 0:d76a3b12c090 | 13 | |
| VigneshRSR | 0:d76a3b12c090 | 14 | //PWM pin |
| VigneshRSR | 0:d76a3b12c090 | 15 | PwmOut mypwm(D10); |
| VigneshRSR | 0:d76a3b12c090 | 16 | |
| VigneshRSR | 0:d76a3b12c090 | 17 | //Heartbeat LED |
| VigneshRSR | 0:d76a3b12c090 | 18 | DigitalOut myled(LED1); |
| VigneshRSR | 0:d76a3b12c090 | 19 | |
| VigneshRSR | 0:d76a3b12c090 | 20 | //Ticker to update the PWM dutycycle |
| VigneshRSR | 0:d76a3b12c090 | 21 | Ticker pwm_ticker; |
| VigneshRSR | 0:d76a3b12c090 | 22 | |
| VigneshRSR | 0:d76a3b12c090 | 23 | //Ticker calls this fucntion to update the PWM dutycycle |
| VigneshRSR | 0:d76a3b12c090 | 24 | void pwm_duty_updater() { |
| VigneshRSR | 0:d76a3b12c090 | 25 | static int idx = 0; |
| VigneshRSR | 0:d76a3b12c090 | 26 | mypwm.write(saw_duty[idx]); // Set the dutycycle % to next value in array |
| VigneshRSR | 0:d76a3b12c090 | 27 | idx++; // Increment the idx |
| VigneshRSR | 0:d76a3b12c090 | 28 | if (idx == SAW_STEPS) idx=0; // Reset the idx when the end has been reached |
| VigneshRSR | 0:d76a3b12c090 | 29 | } |
| VigneshRSR | 0:d76a3b12c090 | 30 | |
| VigneshRSR | 0:d76a3b12c090 | 31 | int main() { |
| VigneshRSR | 0:d76a3b12c090 | 32 | float saw[] = {0, |
| VigneshRSR | 0:d76a3b12c090 | 33 | 0.03125, |
| VigneshRSR | 0:d76a3b12c090 | 34 | 0.0625, |
| VigneshRSR | 0:d76a3b12c090 | 35 | 0.09375, |
| VigneshRSR | 0:d76a3b12c090 | 36 | 0.125, |
| VigneshRSR | 0:d76a3b12c090 | 37 | 0.15625, |
| VigneshRSR | 0:d76a3b12c090 | 38 | 0.1875, |
| VigneshRSR | 0:d76a3b12c090 | 39 | 0.21875, |
| VigneshRSR | 0:d76a3b12c090 | 40 | 0.25, |
| VigneshRSR | 0:d76a3b12c090 | 41 | 0.28125, |
| VigneshRSR | 0:d76a3b12c090 | 42 | 0.3125, |
| VigneshRSR | 0:d76a3b12c090 | 43 | 0.34375, |
| VigneshRSR | 0:d76a3b12c090 | 44 | 0.375, |
| VigneshRSR | 0:d76a3b12c090 | 45 | 0.40625, |
| VigneshRSR | 0:d76a3b12c090 | 46 | 0.4375, |
| VigneshRSR | 0:d76a3b12c090 | 47 | 0.46875, |
| VigneshRSR | 0:d76a3b12c090 | 48 | 0.5, |
| VigneshRSR | 0:d76a3b12c090 | 49 | 0.53125, |
| VigneshRSR | 0:d76a3b12c090 | 50 | 0.5625, |
| VigneshRSR | 0:d76a3b12c090 | 51 | 0.59375, |
| VigneshRSR | 0:d76a3b12c090 | 52 | 0.625, |
| VigneshRSR | 0:d76a3b12c090 | 53 | 0.65625, |
| VigneshRSR | 0:d76a3b12c090 | 54 | 0.6875, |
| VigneshRSR | 0:d76a3b12c090 | 55 | 0.71875, |
| VigneshRSR | 0:d76a3b12c090 | 56 | 0.75, |
| VigneshRSR | 0:d76a3b12c090 | 57 | 0.78125, |
| VigneshRSR | 0:d76a3b12c090 | 58 | 0.8125, |
| VigneshRSR | 0:d76a3b12c090 | 59 | 0.84375, |
| VigneshRSR | 0:d76a3b12c090 | 60 | 0.875, |
| VigneshRSR | 0:d76a3b12c090 | 61 | 0.90625, |
| VigneshRSR | 0:d76a3b12c090 | 62 | 0.9375, |
| VigneshRSR | 0:d76a3b12c090 | 63 | 0.96875, |
| VigneshRSR | 0:d76a3b12c090 | 64 | 1}; |
| VigneshRSR | 0:d76a3b12c090 | 65 | |
| VigneshRSR | 0:d76a3b12c090 | 66 | for (int i = 0; i < SAW_STEPS; i++) { |
| VigneshRSR | 0:d76a3b12c090 | 67 | saw_duty[i] = saw[i]; |
| VigneshRSR | 0:d76a3b12c090 | 68 | } |
| VigneshRSR | 0:d76a3b12c090 | 69 | |
| VigneshRSR | 0:d76a3b12c090 | 70 | // Set PWM frequency |
| VigneshRSR | 0:d76a3b12c090 | 71 | mypwm.period( 1.0f / (float) PWM_FREQ); |
| VigneshRSR | 0:d76a3b12c090 | 72 | |
| VigneshRSR | 0:d76a3b12c090 | 73 | // Init the Ticker to call the dutycyle updater at the required interval |
| VigneshRSR | 0:d76a3b12c090 | 74 | // The update should be at (SINE_STEPS * SINE_OUT_FREQ) |
| VigneshRSR | 0:d76a3b12c090 | 75 | pwm_ticker.attach(&pwm_duty_updater, 1.0f / (float)(SAW_STEPS * SAW_OUT_FREQ)); |
| VigneshRSR | 0:d76a3b12c090 | 76 | |
| VigneshRSR | 0:d76a3b12c090 | 77 | while(1){ //infinite loop |
| VigneshRSR | 0:d76a3b12c090 | 78 | myled = !myled; |
| VigneshRSR | 0:d76a3b12c090 | 79 | wait(0.5); |
| VigneshRSR | 0:d76a3b12c090 | 80 | } |
| VigneshRSR | 0:d76a3b12c090 | 81 | } |