So I found some example code to generate a sine wave from PWM here:

PWM to Generate Analog Waveforms | mbed

I've modified it so that the duty cycle gets updated according to an analog input, but I was wondering if there was a way I could have the output track an analog input (from an ADC pin)? For example, if I input a sinewave with a specific +peak and -peak amplitude, I want to output the same sinewave after PWM conversion and filtering. I suspect I'd have to modify the for loop but I'm not sure how to go about doing that. Here's what I have so far:

#include "mbed.h" //Number of dutycycle steps for output wave #define SINE_STEPS 32 //Frequency of output sine in Hz #define SINE_OUT_FREQ 1000 //Constants to compute the sine waveform #define PI 3.141592f #define SINE_STEPS_RAD (2.0f * PI / (float)SINE_STEPS) //Frequency of Pulse Width Modulated signal in Hz #define PWM_FREQ 50000 AnalogIn adc0in(A0); //Table to generate the sine waveform using dutycycles float sine_duty[SINE_STEPS]; //PWM pin PwmOut PwmPin (D13); //Ticker to update the PWM dutycycle Ticker pwm_ticker; //Ticker calls this fucntion to update the PWM dutycycle void pwm_duty_updater() { static int idx=0; PwmPin.write(adc0in); // Set the dutycycle % to next value in array idx++; // Increment the idx if (idx == SINE_STEPS) idx=0; // Reset the idx when teh end has been reached } int main() { int i; // Init the duty cycle array for (i=0; i<SINE_STEPS; i++) { sine_duty[i] = ( sin(i * SINE_STEPS_RAD) + 1.0f ) / 2.0f; // convert sine (-1.0 .. +1.0) into dutycycle (0.0 .. 1.0) } // Set PWM frequency to 200 KHz (period = 5 us) PwmPin.period( 1.0f / (float) PWM_FREQ); // Init the Ticker to call the dutycyle updater at the required interval // The update should be at (SINE_STEPS * SINE_OUT_FREQ) pwm_ticker.attach(&pwm_duty_updater, 1.0f / (float)(SINE_STEPS * SINE_OUT_FREQ)); }

Hi Nunya Bruh,

OK, I understand your position. So the simplest way how to solve your task is first little bit study of the ADC and FTM peripherals. It is not so complicated as it looks like.

For the simplest implementation I suggest you to use Buc CLK as the source clock for ADC, then set the ADCLK to 4MHz (2-12MHz is working range). Set the continuous conversion on ADC, 16-bit resolution, short sample time, 4x average. Use ADC complete conversion interrupt. The ADC sampling frequency can be calculated by formula on page 858, Figure 35-92.

For the FTM please concentrate to three basic things - use the generated PWM frequency higher than ADC sampling frequency, catch in you mind the MOD - modulo value and ChVAL - channel value. The modulo value together with PWM CLK forms the generated PWM grequency. Then simply in the ADC interrupt routine get ADC value and put it into ChVAL register. It is simplest way to "copy" ADC input to PWM output. I haven't ready code for this task, but it will work, because I have tested it a time ago on other MCU.

The main point is to read the two peripherals description. Then you can improve the code to use DMA, PDB or PIT timers or other.

I wish you success, don't be afraid of it.

Best Regards,

Stano.