Hi Tejas,
I have posted some extracts below to help, BUT this is my first mbed program and I searched and cut and paste from lots of other peoples code to get this working! So all due recognition to them.
I have also deleted bits and renamed variables in the code extract to make it 'more' understandable, but cannot say that it will function as is...
This is within main, which basically attaches a 'ticker' to occur at the set time interval which calls the function ADC_read every 0.1S.
A timer is also set up that calls the function Duration_end at the end of the set time period of 30S.
//--------------------------------------------------------------------
Convert.attach(&ADC_read, 0.1); //do ADC_read function every 0.1 second
SamplingDuration.attach(&Duration_end, 30); //do adc reads over 'duration' // currently 30 seconds
//---------------------------------------------------------------------------------
Before 'main' I have the following functions and declarations which I have commented to help you understand.
Timeout SamplingDuration; // set up a timeout for adc reading duration.
// my understanding is this declares SamplingDuration as a type Timeout
Ticker Convert; // set up a ticker to do timed adc covertions.
AnalogIn Vinput(p20); //Vin pin
int ADC_Count;
float Voltage;
float Voltage_total;
float samples[1024]; // array to save averaged sample data
void Duration_end()
{
Convert.detach();
}
void ADC_read()
{
ADC_Count++;
Voltage_total =0;
for (int i=0; i<25; i++) // do 25 readings
{
Voltage = Vinput.read();
Voltage_total = Voltage_total+ Voltage; //Note Vinput.read can be summed then averaged
wait_us(10);
}
Voltage=Voltage_total/25;
samples[ADC_Count] = Voltage; //Save averaged reading within an array
}
The following links from my notebook may also help:
http://www.nxp.com/documents/application_note/AN10974.pdf Useful reference for current design
http://mbed.org/handbook/Timeout
http://mbed.org/handbook/Ticker
So I hope the above helps you with your code development.
sampling rate of ADC can be changed? IS there any available library for that? if i want change it then how it is possible?