A simple example for analog input and EPD usage.
Dependencies: GDEP015OC1 acn_nrf52_saadc aconno_bsp
Fork of acd52832_Car_battery_ch by
main.cpp@4:f6f94ef38e6a, 2017-06-29 (annotated)
- Committer:
- jurica238814
- Date:
- Thu Jun 29 16:21:53 2017 +0000
- Revision:
- 4:f6f94ef38e6a
- Parent:
- 3:4fb622e929d0
- Child:
- 5:6566725c8835
Mean calculation and voltage/current added. Should work.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 0:dc96b5c8e3f6 | 1 | /* Copyright (c) 2017 Aconno. All Rights Reserved. |
jurica238814 | 0:dc96b5c8e3f6 | 2 | * |
jurica238814 | 0:dc96b5c8e3f6 | 3 | * Licensees are granted free, non-transferable use of the information. NO |
jurica238814 | 0:dc96b5c8e3f6 | 4 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
jurica238814 | 0:dc96b5c8e3f6 | 5 | * the file. |
jurica238814 | 0:dc96b5c8e3f6 | 6 | */ |
jurica238814 | 0:dc96b5c8e3f6 | 7 | |
jurica238814 | 0:dc96b5c8e3f6 | 8 | #include "mbed.h" |
jurica238814 | 0:dc96b5c8e3f6 | 9 | #include "acd52832_bsp.h" |
jurica238814 | 0:dc96b5c8e3f6 | 10 | #include "GDEP015OC1.h" |
jurica238814 | 0:dc96b5c8e3f6 | 11 | |
jurica238814 | 4:f6f94ef38e6a | 12 | #define delay_time (1000) |
jurica238814 | 4:f6f94ef38e6a | 13 | #define ADC_MAX_VALUE (4092) |
jurica238814 | 4:f6f94ef38e6a | 14 | #define ADC_REF_VOLTAGE (3.6) |
jurica238814 | 4:f6f94ef38e6a | 15 | #define VOLTAGE_DIVIDER_RATION (130.0/30) |
jurica238814 | 4:f6f94ef38e6a | 16 | #define CURRENT_FACTOR (36.0) |
jurica238814 | 0:dc96b5c8e3f6 | 17 | |
jurica238814 | 0:dc96b5c8e3f6 | 18 | SPI spi(PIN_EPD_MOSI, NC, PIN_EPD_SCK, NC); |
jurica238814 | 0:dc96b5c8e3f6 | 19 | GDEP015OC1 epd = GDEP015OC1(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY); |
jurica238814 | 0:dc96b5c8e3f6 | 20 | |
jurica238814 | 0:dc96b5c8e3f6 | 21 | AnalogIn battery (p28); |
jurica238814 | 0:dc96b5c8e3f6 | 22 | AnalogIn usb1 (p29); |
jurica238814 | 0:dc96b5c8e3f6 | 23 | AnalogIn usb2 (p30); |
jurica238814 | 0:dc96b5c8e3f6 | 24 | |
jurica238814 | 0:dc96b5c8e3f6 | 25 | int main(){ |
jurica238814 | 0:dc96b5c8e3f6 | 26 | char buffer[25] = {0}; |
jurica238814 | 4:f6f94ef38e6a | 27 | float adc1_mean=0, adc2_mean=0, adc3_mean=0; |
jurica238814 | 4:f6f94ef38e6a | 28 | int count = 0; |
jurica238814 | 4:f6f94ef38e6a | 29 | |
jurica238814 | 0:dc96b5c8e3f6 | 30 | wait_ms(100); |
jurica238814 | 4:f6f94ef38e6a | 31 | NRF_SAADC->RESOLUTION = 0x00000002; // Set 12b resolution |
jurica238814 | 0:dc96b5c8e3f6 | 32 | while(true){ |
jurica238814 | 0:dc96b5c8e3f6 | 33 | |
jurica238814 | 4:f6f94ef38e6a | 34 | adc1_mean += battery.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 35 | adc2_mean += usb1.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 36 | adc3_mean += usb2.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 37 | count ++; |
jurica238814 | 0:dc96b5c8e3f6 | 38 | |
jurica238814 | 4:f6f94ef38e6a | 39 | if (count == 10){ |
jurica238814 | 4:f6f94ef38e6a | 40 | |
jurica238814 | 4:f6f94ef38e6a | 41 | adc1_mean /= 10; |
jurica238814 | 4:f6f94ef38e6a | 42 | adc2_mean /= 10; |
jurica238814 | 4:f6f94ef38e6a | 43 | adc3_mean /= 10; |
jurica238814 | 4:f6f94ef38e6a | 44 | |
jurica238814 | 4:f6f94ef38e6a | 45 | count = 0; |
jurica238814 | 4:f6f94ef38e6a | 46 | epd.empty(); |
jurica238814 | 4:f6f94ef38e6a | 47 | epd.writeFull(); |
jurica238814 | 4:f6f94ef38e6a | 48 | |
jurica238814 | 4:f6f94ef38e6a | 49 | sprintf(buffer, "Battery: %5.5fV", adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION); // Create a string |
jurica238814 | 4:f6f94ef38e6a | 50 | epd.writeString(buffer,25,70,0); // Write new data to the buffer |
jurica238814 | 4:f6f94ef38e6a | 51 | epd.write(); // Write string to the EPD |
jurica238814 | 4:f6f94ef38e6a | 52 | |
jurica238814 | 4:f6f94ef38e6a | 53 | |
jurica238814 | 4:f6f94ef38e6a | 54 | sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean); // Create a string |
jurica238814 | 4:f6f94ef38e6a | 55 | epd.writeString(buffer,25,90,0); // Write new data to the buffer |
jurica238814 | 4:f6f94ef38e6a | 56 | epd.write(); // Write string to the EPD |
jurica238814 | 4:f6f94ef38e6a | 57 | |
jurica238814 | 4:f6f94ef38e6a | 58 | sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean); // Create a string |
jurica238814 | 4:f6f94ef38e6a | 59 | epd.writeString(buffer,25,110,0); // Write new data to the buffer |
jurica238814 | 4:f6f94ef38e6a | 60 | epd.write(); // Write string to the EPD |
jurica238814 | 4:f6f94ef38e6a | 61 | |
jurica238814 | 4:f6f94ef38e6a | 62 | adc1_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 63 | adc2_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 64 | adc3_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 65 | } |
jurica238814 | 0:dc96b5c8e3f6 | 66 | } |
jurica238814 | 0:dc96b5c8e3f6 | 67 | } |