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, 3 months ago.
Real time signal acquisition
Hi
Can I reconstruct a sine wave of 50hz If I input the same.As I am a beginner,I have tried with simple adc sampling codes with a sample time 20us between 2 samples.I am getting the values which are not accurate.Can you advise me how to do it
Regards
1 Answer
10 years, 3 months ago.
20us is too fast with the standard AnalogIn lib. FastAnalogIn can do it (search for it), but to be fair, if you have a 50Hz signal sampling at 1kHz is more than enough (so 1000us sample time).
This is the code I have written for the above problem (includes your suggestion)
- define SAMPLE_RATE 1000
- define LENGTH_RESULT 100
- include "mbed.h"
- include "adc.h"
Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1 ADC adc(SAMPLE_RATE, 1); AnalogIn s1(p20); ADC adc(SAMPLE_RATE, 1); Serial uart(USBTX, USBRX); tx, rx volatile int result;
int main() { Init UART uart.baud(256000); uart.printf("Requested max sample rate is %u, actual max sample rate is %u.\n", SAMPLE_RATE, adc.actual_sample_rate());
Set up ADC on pin 20 adc.setup(p20,1); Measure pin 20 adc.select(p20);
Vars
int count; Program
AD conversion
while(1) { Start ADC conversion adc.start(); Wait for it to complete while(!adc.done(p20)); result=3.3*adc.read(p20); uart.printf("%u\n\r", result); wait_us(1);
} }
Can we get accurate values between the range.
posted by 06 May 2014Use <<code>> and <</code>>
Also for this you might just as well use AnalogIn instead of a specific ADC lib. At the Handbook page you can find info on AnalogIn. But in principle from what I can see that should work, but do take into account your uart is taking quite some time to send the data, even at the increased speed. Also I assume adc.read returns a float (considering you multiply it by 3.3), however resutl is an integer. But in general just start by having a look at AnalogIn.
posted by 06 May 2014