ADC Current Measurement via serial port
Dependencies: mbed
main.cpp@3:8de0f40cba05, 2017-04-25 (annotated)
- Committer:
- debag
- Date:
- Tue Apr 25 15:13:25 2017 +0000
- Revision:
- 3:8de0f40cba05
- Parent:
- 2:08c13f9a3d5c
ADC-Serial Recording
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:028fac66239d | 1 | #include "mbed.h" |
bcostm | 1:e9d1c42a73ae | 2 | |
bcostm | 1:e9d1c42a73ae | 3 | //------------------------------------ |
bcostm | 1:e9d1c42a73ae | 4 | // Hyperterminal configuration |
bcostm | 1:e9d1c42a73ae | 5 | // 9600 bauds, 8-bit data, no parity |
bcostm | 1:e9d1c42a73ae | 6 | //------------------------------------ |
debag | 3:8de0f40cba05 | 7 | AnalogIn analog_value(A0); |
debag | 3:8de0f40cba05 | 8 | AnalogIn analog_value2(A1); |
bcostm | 1:e9d1c42a73ae | 9 | |
debag | 3:8de0f40cba05 | 10 | DigitalOut led(LED1); |
bcostm | 0:028fac66239d | 11 | Serial pc(SERIAL_TX, SERIAL_RX); |
bcostm | 2:08c13f9a3d5c | 12 | |
bcostm | 0:028fac66239d | 13 | DigitalOut myled(LED1); |
bcostm | 2:08c13f9a3d5c | 14 | |
bcostm | 2:08c13f9a3d5c | 15 | int main() |
bcostm | 2:08c13f9a3d5c | 16 | { |
bcostm | 2:08c13f9a3d5c | 17 | int i = 1; |
debag | 3:8de0f40cba05 | 18 | int tim = 0; |
debag | 3:8de0f40cba05 | 19 | float vbat; |
debag | 3:8de0f40cba05 | 20 | float meas; |
debag | 3:8de0f40cba05 | 21 | float average; |
debag | 3:8de0f40cba05 | 22 | float sum; |
debag | 3:8de0f40cba05 | 23 | float current; |
debag | 3:8de0f40cba05 | 24 | //pc.printf("Hello World !\n"); |
debag | 3:8de0f40cba05 | 25 | pc.printf("Time\t Vbat \t Current \n ", tim); |
bcostm | 2:08c13f9a3d5c | 26 | while(1) { |
debag | 3:8de0f40cba05 | 27 | |
debag | 3:8de0f40cba05 | 28 | for (int i=0; i<1000; i++) { |
debag | 3:8de0f40cba05 | 29 | |
debag | 3:8de0f40cba05 | 30 | meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
debag | 3:8de0f40cba05 | 31 | meas = meas * 3300; // Change the value to be in the 0 to 3300 range |
debag | 3:8de0f40cba05 | 32 | //printf("measure = %.0f mV\n", meas); |
debag | 3:8de0f40cba05 | 33 | sum=sum+meas; |
debag | 3:8de0f40cba05 | 34 | |
debag | 3:8de0f40cba05 | 35 | if (meas > 2000) { // If the value is greater than 2V then switch the LED on |
debag | 3:8de0f40cba05 | 36 | led = 1; |
debag | 3:8de0f40cba05 | 37 | } else { |
debag | 3:8de0f40cba05 | 38 | led = 0; |
debag | 3:8de0f40cba05 | 39 | } |
debag | 3:8de0f40cba05 | 40 | wait_ms(1);// 200 ms |
debag | 3:8de0f40cba05 | 41 | } |
debag | 3:8de0f40cba05 | 42 | vbat = analog_value2.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
debag | 3:8de0f40cba05 | 43 | vbat = vbat * 3.300*2; // Change the value to be in the 0 to 3300 range by 2 because of voltage divider |
debag | 3:8de0f40cba05 | 44 | average=sum/1000; |
debag | 3:8de0f40cba05 | 45 | sum=0; |
debag | 3:8de0f40cba05 | 46 | current=(average /10);//-0.96; |
debag | 3:8de0f40cba05 | 47 | tim+=1; |
debag | 3:8de0f40cba05 | 48 | pc.printf(" %d \t", tim); |
debag | 3:8de0f40cba05 | 49 | pc.printf(" %.2f \t", vbat); |
debag | 3:8de0f40cba05 | 50 | pc.printf(" %.2f; \n", current); |
debag | 3:8de0f40cba05 | 51 | |
bcostm | 2:08c13f9a3d5c | 52 | myled = !myled; |
bcostm | 2:08c13f9a3d5c | 53 | } |
bcostm | 0:028fac66239d | 54 | } |