Dependencies: mbed
Fork of Nucleo-ADC_with_UART_output by
main.cpp@0:dade2710823b, 2015-03-05 (annotated)
- Committer:
- Foxnec
- Date:
- Thu Mar 05 22:54:26 2015 +0000
- Revision:
- 0:dade2710823b
- Child:
- 1:59ea769dee88
First commit. Nucleo ADC with UART output.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Foxnec | 0:dade2710823b | 1 | #include "mbed.h" |
Foxnec | 0:dade2710823b | 2 | |
Foxnec | 0:dade2710823b | 3 | //Nucleo F103,F303,F411 compatible |
Foxnec | 0:dade2710823b | 4 | |
Foxnec | 0:dade2710823b | 5 | AnalogIn analog_value(A0); |
Foxnec | 0:dade2710823b | 6 | |
Foxnec | 0:dade2710823b | 7 | Serial pc(SERIAL_TX, SERIAL_RX); |
Foxnec | 0:dade2710823b | 8 | |
Foxnec | 0:dade2710823b | 9 | DigitalOut myled(LED1); |
Foxnec | 0:dade2710823b | 10 | |
Foxnec | 0:dade2710823b | 11 | // Calculate the corresponding acquisition measure for a given value in mV |
Foxnec | 0:dade2710823b | 12 | #define rawmeas(x) ((0xFFFF*x)/3300) |
Foxnec | 0:dade2710823b | 13 | // Calculate the corresponding mV value from measured voltage |
Foxnec | 0:dade2710823b | 14 | #define mv(x) ((x*3300)/0xFFFF) |
Foxnec | 0:dade2710823b | 15 | |
Foxnec | 0:dade2710823b | 16 | int main() { |
Foxnec | 0:dade2710823b | 17 | |
Foxnec | 0:dade2710823b | 18 | pc.baud(115200); |
Foxnec | 0:dade2710823b | 19 | |
Foxnec | 0:dade2710823b | 20 | while(1) { |
Foxnec | 0:dade2710823b | 21 | uint16_t meas = analog_value.read_u16(); // Converts and read the analog input value |
Foxnec | 0:dade2710823b | 22 | if (meas > rawmeas(1000)) { // If the value is greater than 1000 mV toggle the LED |
Foxnec | 0:dade2710823b | 23 | myled = !myled; |
Foxnec | 0:dade2710823b | 24 | } |
Foxnec | 0:dade2710823b | 25 | |
Foxnec | 0:dade2710823b | 26 | //Prints the measured value in millivolts |
Foxnec | 0:dade2710823b | 27 | pc.printf("Measured value: %d\n", meas); |
Foxnec | 0:dade2710823b | 28 | pc.printf("%d mV\n", mv(meas)); |
Foxnec | 0:dade2710823b | 29 | |
Foxnec | 0:dade2710823b | 30 | wait(1); // 200 ms |
Foxnec | 0:dade2710823b | 31 | |
Foxnec | 0:dade2710823b | 32 | } |
Foxnec | 0:dade2710823b | 33 | } |