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.
main.cpp
00001 #include "mbed.h" 00002 00003 //Number of dutycycle steps for output wave 00004 #define SINE_STEPS 32 00005 //Frequency of output sine in Hz 00006 #define SINE_OUT_FREQ 1000 00007 00008 //Constants to compute the sine waveform 00009 #define PI 3.141592f 00010 #define SINE_STEPS_RAD (2.0f * PI / (float)SINE_STEPS) 00011 00012 //Table to generate the sine waveform using dutycycles 00013 float sine_duty[SINE_STEPS]; 00014 00015 00016 //Frequency of Pulse Width Modulated signal in Hz 00017 #define PWM_FREQ 200000 00018 00019 //PWM pin 00020 PwmOut PwmPin (p22); 00021 00022 //Heartbeat LED 00023 DigitalOut myled(LED1); 00024 00025 00026 //Ticker to update the PWM dutycycle 00027 Ticker pwm_ticker; 00028 00029 //Ticker calls this fucntion to update the PWM dutycycle 00030 void pwm_duty_updater() { 00031 static int idx=0; 00032 00033 PwmPin.write(sine_duty[idx]); // Set the dutycycle % to next value in array 00034 idx++; // Increment the idx 00035 if (idx == SINE_STEPS) idx=0; // Reset the idx when teh end has been reached 00036 00037 } 00038 00039 int main() { 00040 int i; 00041 00042 // Init the duty cycle array 00043 for (i=0; i<SINE_STEPS; i++) { 00044 sine_duty[i] = ( sin(i * SINE_STEPS_RAD) + 1.0f ) / 2.0f; // convert sine (-1.0 .. +1.0) into dutycycle (0.0 .. 1.0) 00045 } 00046 00047 // Set PWM frequency to 200 KHz (period = 5 us) 00048 PwmPin.period( 1.0f / (float) PWM_FREQ); 00049 00050 // Init the Ticker to call the dutycyle updater at the required interval 00051 // The update should be at (SINE_STEPS * SINE_OUT_FREQ) 00052 pwm_ticker.attach(&pwm_duty_updater, 1.0f / (float)(SINE_STEPS * SINE_OUT_FREQ)); 00053 00054 while(1){ //infinite loop 00055 myled = !myled; 00056 wait(0.5); 00057 } 00058 00059 } 00060
Generated on Wed Jul 13 2022 20:04:57 by
1.7.2