Reads all the ADC ports available on the Nucleo F303K8 and prints the results in Value Read, Volts and 16 bit unsigned short.
Reads all ADC available on Nucleo-F303K8 and sends results to PC over USB UART.
main.cpp@2:9cc6c918161b, 2016-03-30 (annotated)
- Committer:
- joeata2wh
- Date:
- Wed Mar 30 14:49:33 2016 +0000
- Revision:
- 2:9cc6c918161b
- Parent:
- 1:a368dd17f04c
update to use MIT license
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joeata2wh | 0:fa185766e039 | 1 | /* Example of Reading all the ADC pins available on the Nucleo_F303K8. |
joeata2wh | 2:9cc6c918161b | 2 | |
joeata2wh | 2:9cc6c918161b | 3 | By Joseph Ellsworth CTO of A2WH |
joeata2wh | 2:9cc6c918161b | 4 | Take a look at A2WH.com Producing Water from Air using Solar Energy |
joeata2wh | 2:9cc6c918161b | 5 | March-2016 License: https://developer.mbed.org/handbook/MIT-Licence |
joeata2wh | 2:9cc6c918161b | 6 | */ |
joeata2wh | 0:fa185766e039 | 7 | |
joeata2wh | 1:a368dd17f04c | 8 | #include "mbed.h" |
joeata2wh | 1:a368dd17f04c | 9 | |
joeata2wh | 1:a368dd17f04c | 10 | // Host PC Communication channels |
joeata2wh | 1:a368dd17f04c | 11 | Serial pc(USBTX, USBRX); // tx, rx |
joeata2wh | 1:a368dd17f04c | 12 | |
joeata2wh | 0:fa185766e039 | 13 | |
joeata2wh | 1:a368dd17f04c | 14 | AnalogIn pa7(PA_7); |
joeata2wh | 1:a368dd17f04c | 15 | AnalogIn pa6(PA_6); |
joeata2wh | 1:a368dd17f04c | 16 | AnalogIn pa5(PA_5); |
joeata2wh | 1:a368dd17f04c | 17 | AnalogIn pa4(PA_4); |
joeata2wh | 1:a368dd17f04c | 18 | //AnalogIn pa2(PA_2); // conflicts with USB TX |
joeata2wh | 1:a368dd17f04c | 19 | //AnalogIn pa3(PA_3); // conflicts with USB RX |
joeata2wh | 1:a368dd17f04c | 20 | AnalogIn pa1(PA_1); |
joeata2wh | 1:a368dd17f04c | 21 | AnalogIn pa0(PA_0); |
joeata2wh | 1:a368dd17f04c | 22 | AnalogIn pb0(PB_0); |
joeata2wh | 1:a368dd17f04c | 23 | AnalogIn pb1(PB_1); |
joeata2wh | 0:fa185766e039 | 24 | |
joeata2wh | 0:fa185766e039 | 25 | DigitalOut myled(LED1); |
joeata2wh | 0:fa185766e039 | 26 | const float voltMeterARef = 3.3; |
joeata2wh | 0:fa185766e039 | 27 | |
joeata2wh | 0:fa185766e039 | 28 | float readPrint(AnalogIn ain, char *label) { |
joeata2wh | 0:fa185766e039 | 29 | float tval = ain.read(); |
joeata2wh | 0:fa185766e039 | 30 | float volts = tval * voltMeterARef; |
joeata2wh | 0:fa185766e039 | 31 | float perc = tval * 100.0; |
joeata2wh | 0:fa185766e039 | 32 | unsigned short tvalu16 = ain.read_u16 (); |
joeata2wh | 0:fa185766e039 | 33 | |
joeata2wh | 0:fa185766e039 | 34 | printf("adc %s R=%3.3f V=%3.3f%% U16=%u\r\n",label, tval, volts, tvalu16); |
joeata2wh | 0:fa185766e039 | 35 | |
joeata2wh | 0:fa185766e039 | 36 | return tval; |
joeata2wh | 0:fa185766e039 | 37 | } |
joeata2wh | 0:fa185766e039 | 38 | |
joeata2wh | 0:fa185766e039 | 39 | |
joeata2wh | 0:fa185766e039 | 40 | int main() { |
joeata2wh | 0:fa185766e039 | 41 | while(1) { |
joeata2wh | 0:fa185766e039 | 42 | myled = !myled; // toggle led |
joeata2wh | 1:a368dd17f04c | 43 | readPrint(pa7, "PA_7"); |
joeata2wh | 1:a368dd17f04c | 44 | readPrint(pa6, "PA_6"); |
joeata2wh | 1:a368dd17f04c | 45 | readPrint(pa5, "PA_5"); |
joeata2wh | 1:a368dd17f04c | 46 | readPrint(pa4, "PA_4"); |
joeata2wh | 1:a368dd17f04c | 47 | //readPrint(pa3, "PA_3"); |
joeata2wh | 1:a368dd17f04c | 48 | //readPrint(pa2, "PA_2"); |
joeata2wh | 1:a368dd17f04c | 49 | readPrint(pa1, "PA_1"); |
joeata2wh | 1:a368dd17f04c | 50 | readPrint(pa0, "PA_0"); |
joeata2wh | 0:fa185766e039 | 51 | |
joeata2wh | 1:a368dd17f04c | 52 | readPrint(pb0, "PB_0"); |
joeata2wh | 1:a368dd17f04c | 53 | readPrint(pb1, "PB_1"); |
joeata2wh | 0:fa185766e039 | 54 | printf("\r\n\r\n"); |
joeata2wh | 1:a368dd17f04c | 55 | wait(7.0); |
joeata2wh | 0:fa185766e039 | 56 | } |
joeata2wh | 0:fa185766e039 | 57 | } |