Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Nucleo-Analog_read by
main.cpp@0:242976b62902, 2015-03-12 (annotated)
- Committer:
- dousape2
- Date:
- Thu Mar 12 23:40:48 2015 +0000
- Revision:
- 0:242976b62902
- Child:
- 1:dc5a274adc7d
Analog read with 3 posibility
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| dousape2 | 0:242976b62902 | 1 | #include "mbed.h" |
| dousape2 | 0:242976b62902 | 2 | |
| dousape2 | 0:242976b62902 | 3 | #define voltage 3300 |
| dousape2 | 0:242976b62902 | 4 | #define mv1(x) (x*voltage) |
| dousape2 | 0:242976b62902 | 5 | #define mv2(x) ((x*voltage)/0xFFFF) |
| dousape2 | 0:242976b62902 | 6 | #define mv3(x) (x*voltage) |
| dousape2 | 0:242976b62902 | 7 | AnalogIn analog_value(A0); |
| dousape2 | 0:242976b62902 | 8 | |
| dousape2 | 0:242976b62902 | 9 | DigitalOut led(LED1); |
| dousape2 | 0:242976b62902 | 10 | |
| dousape2 | 0:242976b62902 | 11 | int main() |
| dousape2 | 0:242976b62902 | 12 | { |
| dousape2 | 0:242976b62902 | 13 | float meas1; |
| dousape2 | 0:242976b62902 | 14 | int meas2; |
| dousape2 | 0:242976b62902 | 15 | float meas3; |
| dousape2 | 0:242976b62902 | 16 | |
| dousape2 | 0:242976b62902 | 17 | printf("\nAnalogIn example\n"); |
| dousape2 | 0:242976b62902 | 18 | |
| dousape2 | 0:242976b62902 | 19 | while(1) { |
| dousape2 | 0:242976b62902 | 20 | meas1 = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
| dousape2 | 0:242976b62902 | 21 | meas1 = mv1(meas1); // Change the value to be in the 0 to 3300 range |
| dousape2 | 0:242976b62902 | 22 | |
| dousape2 | 0:242976b62902 | 23 | meas2 = analog_value.read_u16(); // Converts and read the analog input value (value from 0.0 to 0xFFFF) |
| dousape2 | 0:242976b62902 | 24 | meas2 = mv2(meas2); // Change the value to be in the 0 to 3300 range |
| dousape2 | 0:242976b62902 | 25 | |
| dousape2 | 0:242976b62902 | 26 | meas3 = analog_value; // Converts and read the analog input value (value from 0.0 to 1.0) |
| dousape2 | 0:242976b62902 | 27 | meas3 = mv3(meas3); // Change the value to be in the 0 to 3300 range |
| dousape2 | 0:242976b62902 | 28 | |
| dousape2 | 0:242976b62902 | 29 | printf("measure = %.0f mV, %d mV, %.0f mV\n", meas1,meas2,meas3); |
| dousape2 | 0:242976b62902 | 30 | if (meas1 > 2000 && meas2 > 2000 && meas3 > 2000) { // If the value is greater than 2V then switch the LED on |
| dousape2 | 0:242976b62902 | 31 | led = 1; |
| dousape2 | 0:242976b62902 | 32 | } else { |
| dousape2 | 0:242976b62902 | 33 | led = 0; |
| dousape2 | 0:242976b62902 | 34 | } |
| dousape2 | 0:242976b62902 | 35 | wait(0.2); // 200 ms |
| dousape2 | 0:242976b62902 | 36 | } |
| dousape2 | 0:242976b62902 | 37 | } |
