ADC Current Measurement via serial port
Dependencies: mbed
Diff: main.cpp
- Revision:
- 3:8de0f40cba05
- Parent:
- 2:08c13f9a3d5c
--- a/main.cpp Fri Nov 18 08:25:14 2016 +0000 +++ b/main.cpp Tue Apr 25 15:13:25 2017 +0000 @@ -4,7 +4,10 @@ // Hyperterminal configuration // 9600 bauds, 8-bit data, no parity //------------------------------------ +AnalogIn analog_value(A0); +AnalogIn analog_value2(A1); +DigitalOut led(LED1); Serial pc(SERIAL_TX, SERIAL_RX); DigitalOut myled(LED1); @@ -12,10 +15,40 @@ int main() { int i = 1; - pc.printf("Hello World !\n"); + int tim = 0; + float vbat; + float meas; + float average; + float sum; + float current; + //pc.printf("Hello World !\n"); + pc.printf("Time\t Vbat \t Current \n ", tim); while(1) { - wait(1); - pc.printf("This program runs since %d seconds.\n", i++); + + for (int i=0; i<1000; i++) { + + meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) + meas = meas * 3300; // Change the value to be in the 0 to 3300 range + //printf("measure = %.0f mV\n", meas); + sum=sum+meas; + + if (meas > 2000) { // If the value is greater than 2V then switch the LED on + led = 1; + } else { + led = 0; + } + wait_ms(1);// 200 ms + } + vbat = analog_value2.read(); // Converts and read the analog input value (value from 0.0 to 1.0) + vbat = vbat * 3.300*2; // Change the value to be in the 0 to 3300 range by 2 because of voltage divider + average=sum/1000; + sum=0; + current=(average /10);//-0.96; + tim+=1; + pc.printf(" %d \t", tim); + pc.printf(" %.2f \t", vbat); + pc.printf(" %.2f; \n", current); + myled = !myled; } }