A simple program to read an analoge channel on the Freedom Kl25Z using the ticker object to control timing. The data is written into a circular buffer, which is read by the main loop and sent down the serial line.

Dependencies:   FastAnalogIn mbed

Note that the Baud rate is set as 57600 rather than the default 9600.

Committer:
sas37
Date:
Wed Jul 23 16:58:23 2014 +0000
Revision:
1:c424a90749e1
Parent:
0:eb0dc2b5bc51
Updated to correct version of FastAnalogIn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sas37 0:eb0dc2b5bc51 1 #include "mbed.h"
sas37 0:eb0dc2b5bc51 2 #include "FastAnalogIn.h"
sas37 0:eb0dc2b5bc51 3
sas37 0:eb0dc2b5bc51 4 /*
sas37 0:eb0dc2b5bc51 5 Simple program which uses a ticker object to record and analoge value and store it in a cicular buffer.
sas37 0:eb0dc2b5bc51 6 The ticker delay sets the sampling rate.
sas37 0:eb0dc2b5bc51 7
sas37 0:eb0dc2b5bc51 8 The FastAnalogIn Library is used (http://mbed.org/users/Sissors/code/FastAnalogIn/) to rapidly sample
sas37 0:eb0dc2b5bc51 9 a single channel.
sas37 0:eb0dc2b5bc51 10
sas37 0:eb0dc2b5bc51 11 In the main loop a busy loop is used to send read the data from the cicular buffer and send down the serial line
sas37 0:eb0dc2b5bc51 12
sas37 0:eb0dc2b5bc51 13
sas37 0:eb0dc2b5bc51 14 */
sas37 0:eb0dc2b5bc51 15
sas37 0:eb0dc2b5bc51 16 //size of cicular buffer
sas37 0:eb0dc2b5bc51 17 #define BuffSize 255
sas37 0:eb0dc2b5bc51 18
sas37 0:eb0dc2b5bc51 19 using namespace mbed;
sas37 0:eb0dc2b5bc51 20
sas37 0:eb0dc2b5bc51 21 FastAnalogIn Ain(PTC2);
sas37 0:eb0dc2b5bc51 22
sas37 0:eb0dc2b5bc51 23 Serial pc(USBTX, USBRX);
sas37 0:eb0dc2b5bc51 24 Timer t;
sas37 0:eb0dc2b5bc51 25 Ticker tock;
sas37 0:eb0dc2b5bc51 26
sas37 0:eb0dc2b5bc51 27 //set the delay used by the ticker
sas37 0:eb0dc2b5bc51 28 float delay = 0.01;
sas37 0:eb0dc2b5bc51 29
sas37 0:eb0dc2b5bc51 30 //some circular buffers
sas37 0:eb0dc2b5bc51 31
sas37 0:eb0dc2b5bc51 32 float data[BuffSize];
sas37 0:eb0dc2b5bc51 33 float time_[BuffSize];
sas37 0:eb0dc2b5bc51 34 long i =0;
sas37 0:eb0dc2b5bc51 35 int j=0;
sas37 0:eb0dc2b5bc51 36 long k =0;
sas37 0:eb0dc2b5bc51 37
sas37 0:eb0dc2b5bc51 38 void ana()
sas37 0:eb0dc2b5bc51 39 {
sas37 0:eb0dc2b5bc51 40 if(i<k+BuffSize)
sas37 0:eb0dc2b5bc51 41 {j = i%BuffSize;
sas37 0:eb0dc2b5bc51 42 //time_[j]=t.read();
sas37 0:eb0dc2b5bc51 43 data[j]=Ain.read();
sas37 0:eb0dc2b5bc51 44 time_[j]=t.read();
sas37 0:eb0dc2b5bc51 45 i++;
sas37 0:eb0dc2b5bc51 46 }
sas37 0:eb0dc2b5bc51 47 }
sas37 0:eb0dc2b5bc51 48
sas37 0:eb0dc2b5bc51 49
sas37 0:eb0dc2b5bc51 50 int main()
sas37 0:eb0dc2b5bc51 51 {
sas37 0:eb0dc2b5bc51 52 pc.baud(57600);
sas37 0:eb0dc2b5bc51 53 t.start();
sas37 0:eb0dc2b5bc51 54 tock.attach(&ana,delay);
sas37 0:eb0dc2b5bc51 55 while (true) {
sas37 0:eb0dc2b5bc51 56 if(k<i)
sas37 0:eb0dc2b5bc51 57 {
sas37 0:eb0dc2b5bc51 58 pc.printf("%f,%f\n",time_[k%BuffSize],data[k%BuffSize]);
sas37 0:eb0dc2b5bc51 59 k++;
sas37 0:eb0dc2b5bc51 60 }
sas37 0:eb0dc2b5bc51 61
sas37 0:eb0dc2b5bc51 62
sas37 0:eb0dc2b5bc51 63 }
sas37 0:eb0dc2b5bc51 64 }