Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 9 months ago.
How to sample a 50Hz sine wave and store in an array??
Hi
I have a program here which I am measuring a 50Hz sine wave using a Ticker which is calling my sampling function every 0.000625 seconds i.e period of waveform is 0.02 then dividing this by 32 to get 32 samples per waveform it requires to sample every 0.000625 seconds. The Ticker seems to be calling the ADC function correctly 32 times per period but when I try to read the values from the sampling of 4 waveforms they are wrong could somebody tell me why my below code isn't displaying the correct values? Apologies about the format I'm not sure how to copy code in the question section
Thanks
<<#include "mbed.h">>
<<Serial pc(USBTX, USBRX);>>
**Global variables***
<<AnalogIn AinV(p16);>>
<<float ADC_Voltage_data;>>
<<float ADC_sampling_rate;>>
<<Ticker ADC_set;>>
<<float samples[128]; >>
<<volatile int ADC_Count;>>
<<DigitalOut sample_pulse(p17);>>
**Function prototype's
void ADC();
*Main Function**
<<int main() {>>
<<sample_pulse=0;>>
<<ADC_Count=0;>>
<<ADC_set.attach(&ADC,0.000625); >>
attach a ticker ADC-set to call ADC function at the sampling rate
calculated to allow 32 samples per period
<<while(ADC_Count <128) >>
{
}
<<for(int i=0;i<128;i++)>>
{
printf("%f\n\r", samples[ADC_Count]);
}
}
*function to display analog input as voltage on LCD***
<<void ADC() { >>
<<sample_pulse=1;>>
<<ADC_Voltage_data=AinV*3.3; >>
<<samples[ADC_Count]=ADC_Voltage_data; >>
<<ADC_Count++;>>
<<sample_pulse=0;>>
}