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@1:a368dd17f04c, 2016-03-21 (annotated)
- Committer:
- joeata2wh
- Date:
- Mon Mar 21 00:05:14 2016 +0000
- Revision:
- 1:a368dd17f04c
- Parent:
- 0:fa185766e039
- Child:
- 2:9cc6c918161b
Fix to remove conflict with USB RX, TX pins
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 | 0:fa185766e039 | 2 | by Joseph Ellsworth CTO A2WH - Free for all but no warranty, no promises */ |
joeata2wh | 0:fa185766e039 | 3 | |
joeata2wh | 1:a368dd17f04c | 4 | #include "mbed.h" |
joeata2wh | 1:a368dd17f04c | 5 | |
joeata2wh | 1:a368dd17f04c | 6 | // Host PC Communication channels |
joeata2wh | 1:a368dd17f04c | 7 | Serial pc(USBTX, USBRX); // tx, rx |
joeata2wh | 1:a368dd17f04c | 8 | |
joeata2wh | 0:fa185766e039 | 9 | |
joeata2wh | 1:a368dd17f04c | 10 | AnalogIn pa7(PA_7); |
joeata2wh | 1:a368dd17f04c | 11 | AnalogIn pa6(PA_6); |
joeata2wh | 1:a368dd17f04c | 12 | AnalogIn pa5(PA_5); |
joeata2wh | 1:a368dd17f04c | 13 | AnalogIn pa4(PA_4); |
joeata2wh | 1:a368dd17f04c | 14 | //AnalogIn pa2(PA_2); // conflicts with USB TX |
joeata2wh | 1:a368dd17f04c | 15 | //AnalogIn pa3(PA_3); // conflicts with USB RX |
joeata2wh | 1:a368dd17f04c | 16 | AnalogIn pa1(PA_1); |
joeata2wh | 1:a368dd17f04c | 17 | AnalogIn pa0(PA_0); |
joeata2wh | 1:a368dd17f04c | 18 | AnalogIn pb0(PB_0); |
joeata2wh | 1:a368dd17f04c | 19 | AnalogIn pb1(PB_1); |
joeata2wh | 0:fa185766e039 | 20 | |
joeata2wh | 0:fa185766e039 | 21 | DigitalOut myled(LED1); |
joeata2wh | 0:fa185766e039 | 22 | const float voltMeterARef = 3.3; |
joeata2wh | 0:fa185766e039 | 23 | |
joeata2wh | 0:fa185766e039 | 24 | float readPrint(AnalogIn ain, char *label) { |
joeata2wh | 0:fa185766e039 | 25 | float tval = ain.read(); |
joeata2wh | 0:fa185766e039 | 26 | float volts = tval * voltMeterARef; |
joeata2wh | 0:fa185766e039 | 27 | float perc = tval * 100.0; |
joeata2wh | 0:fa185766e039 | 28 | unsigned short tvalu16 = ain.read_u16 (); |
joeata2wh | 0:fa185766e039 | 29 | |
joeata2wh | 0:fa185766e039 | 30 | printf("adc %s R=%3.3f V=%3.3f%% U16=%u\r\n",label, tval, volts, tvalu16); |
joeata2wh | 0:fa185766e039 | 31 | |
joeata2wh | 0:fa185766e039 | 32 | return tval; |
joeata2wh | 0:fa185766e039 | 33 | } |
joeata2wh | 0:fa185766e039 | 34 | |
joeata2wh | 0:fa185766e039 | 35 | |
joeata2wh | 0:fa185766e039 | 36 | int main() { |
joeata2wh | 0:fa185766e039 | 37 | while(1) { |
joeata2wh | 0:fa185766e039 | 38 | myled = !myled; // toggle led |
joeata2wh | 1:a368dd17f04c | 39 | readPrint(pa7, "PA_7"); |
joeata2wh | 1:a368dd17f04c | 40 | readPrint(pa6, "PA_6"); |
joeata2wh | 1:a368dd17f04c | 41 | readPrint(pa5, "PA_5"); |
joeata2wh | 1:a368dd17f04c | 42 | readPrint(pa4, "PA_4"); |
joeata2wh | 1:a368dd17f04c | 43 | //readPrint(pa3, "PA_3"); |
joeata2wh | 1:a368dd17f04c | 44 | //readPrint(pa2, "PA_2"); |
joeata2wh | 1:a368dd17f04c | 45 | readPrint(pa1, "PA_1"); |
joeata2wh | 1:a368dd17f04c | 46 | readPrint(pa0, "PA_0"); |
joeata2wh | 0:fa185766e039 | 47 | |
joeata2wh | 1:a368dd17f04c | 48 | readPrint(pb0, "PB_0"); |
joeata2wh | 1:a368dd17f04c | 49 | readPrint(pb1, "PB_1"); |
joeata2wh | 0:fa185766e039 | 50 | printf("\r\n\r\n"); |
joeata2wh | 1:a368dd17f04c | 51 | wait(7.0); |
joeata2wh | 0:fa185766e039 | 52 | } |
joeata2wh | 0:fa185766e039 | 53 | } |