Thiago .
/
ANALOGIN
Reads Analog and sends to the computer every 0.01 seconds.
main.cpp@0:1b42934fe1a1, 2015-02-24 (annotated)
- Committer:
- Jamess
- Date:
- Tue Feb 24 23:06:58 2015 +0000
- Revision:
- 0:1b42934fe1a1
Hello Everybody.; My first published program, works perfectly so far =); I am going to use the FRDM KL 25z in order to measure an analog flux sensor and an acelerometer. This is the flux sensor part.; If you guys have any hints, I would like to hear.; TY
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jamess | 0:1b42934fe1a1 | 1 | // Sending Serial Values Read from AnalogIN |
Jamess | 0:1b42934fe1a1 | 2 | // by Thiago Rech |
Jamess | 0:1b42934fe1a1 | 3 | // |
Jamess | 0:1b42934fe1a1 | 4 | // When receiving the number '2', starts reading and sending the analog voltage in a 100Hz frequency* |
Jamess | 0:1b42934fe1a1 | 5 | // |
Jamess | 0:1b42934fe1a1 | 6 | // This is the first mbed project that I publish =D I would be thankfull if anyne give tips and point mistakes if any. |
Jamess | 0:1b42934fe1a1 | 7 | |
Jamess | 0:1b42934fe1a1 | 8 | |
Jamess | 0:1b42934fe1a1 | 9 | #include "mbed.h" |
Jamess | 0:1b42934fe1a1 | 10 | |
Jamess | 0:1b42934fe1a1 | 11 | AnalogIn ain(PTB3); |
Jamess | 0:1b42934fe1a1 | 12 | DigitalOut led2(LED2); // Led red |
Jamess | 0:1b42934fe1a1 | 13 | Ticker t1; // Timer to send data |
Jamess | 0:1b42934fe1a1 | 14 | Serial pc(USBTX, USBRX); |
Jamess | 0:1b42934fe1a1 | 15 | |
Jamess | 0:1b42934fe1a1 | 16 | void HandlerT1(void); |
Jamess | 0:1b42934fe1a1 | 17 | void rx_Handler(void); |
Jamess | 0:1b42934fe1a1 | 18 | |
Jamess | 0:1b42934fe1a1 | 19 | volatile bool STATUS = true; |
Jamess | 0:1b42934fe1a1 | 20 | char test = 0; |
Jamess | 0:1b42934fe1a1 | 21 | |
Jamess | 0:1b42934fe1a1 | 22 | int main() |
Jamess | 0:1b42934fe1a1 | 23 | { |
Jamess | 0:1b42934fe1a1 | 24 | |
Jamess | 0:1b42934fe1a1 | 25 | //Setup |
Jamess | 0:1b42934fe1a1 | 26 | pc.baud(9600); |
Jamess | 0:1b42934fe1a1 | 27 | pc.printf("Press 2 to Start"); |
Jamess | 0:1b42934fe1a1 | 28 | pc.attach(&rx_Handler, pc.RxIrq); |
Jamess | 0:1b42934fe1a1 | 29 | led2=0; |
Jamess | 0:1b42934fe1a1 | 30 | |
Jamess | 0:1b42934fe1a1 | 31 | while(1) { |
Jamess | 0:1b42934fe1a1 | 32 | |
Jamess | 0:1b42934fe1a1 | 33 | wait(0.5); // Debug |
Jamess | 0:1b42934fe1a1 | 34 | led2 = 0; |
Jamess | 0:1b42934fe1a1 | 35 | pc.putc(test); // Debug |
Jamess | 0:1b42934fe1a1 | 36 | |
Jamess | 0:1b42934fe1a1 | 37 | if (teste == '2') { |
Jamess | 0:1b42934fe1a1 | 38 | t1.attach(&HandlerT1, 0.01); |
Jamess | 0:1b42934fe1a1 | 39 | } |
Jamess | 0:1b42934fe1a1 | 40 | |
Jamess | 0:1b42934fe1a1 | 41 | while(test == '2') { |
Jamess | 0:1b42934fe1a1 | 42 | |
Jamess | 0:1b42934fe1a1 | 43 | if (STATUS == true) { |
Jamess | 0:1b42934fe1a1 | 44 | |
Jamess | 0:1b42934fe1a1 | 45 | STATUS = false; |
Jamess | 0:1b42934fe1a1 | 46 | led2 = 1; |
Jamess | 0:1b42934fe1a1 | 47 | pc.putc(analogValue); |
Jamess | 0:1b42934fe1a1 | 48 | pc.printf("0x%04X",ain.read_u16()); |
Jamess | 0:1b42934fe1a1 | 49 | } |
Jamess | 0:1b42934fe1a1 | 50 | |
Jamess | 0:1b42934fe1a1 | 51 | |
Jamess | 0:1b42934fe1a1 | 52 | } //end of while(test=='2'){} |
Jamess | 0:1b42934fe1a1 | 53 | |
Jamess | 0:1b42934fe1a1 | 54 | t1.detach(); // Detaches timer interruption when not needed |
Jamess | 0:1b42934fe1a1 | 55 | }//end of while(1) |
Jamess | 0:1b42934fe1a1 | 56 | }//end of main |
Jamess | 0:1b42934fe1a1 | 57 | |
Jamess | 0:1b42934fe1a1 | 58 | void HandlerT1(void) |
Jamess | 0:1b42934fe1a1 | 59 | { |
Jamess | 0:1b42934fe1a1 | 60 | STATUS = true; |
Jamess | 0:1b42934fe1a1 | 61 | } |
Jamess | 0:1b42934fe1a1 | 62 | |
Jamess | 0:1b42934fe1a1 | 63 | void rx_Handler(void) |
Jamess | 0:1b42934fe1a1 | 64 | { |
Jamess | 0:1b42934fe1a1 | 65 | test = pc.getc(); |
Jamess | 0:1b42934fe1a1 | 66 | pc.putc(test); |
Jamess | 0:1b42934fe1a1 | 67 | } |