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@8:c9f0aea93832, 2017-07-05 (annotated)
- Committer:
- jurica238814
- Date:
- Wed Jul 05 13:04:57 2017 +0000
- Revision:
- 8:c9f0aea93832
- Parent:
- 6:e848c82b5248
- Child:
- 9:eef3ea422bfe
A battery voltage and a USB current measurement 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 | 5:6566725c8835 | 11 | #include "pictures.h" |
jurica238814 | 0:dc96b5c8e3f6 | 12 | |
jurica238814 | 6:e848c82b5248 | 13 | #define DEBUG (0) |
jurica238814 | 6:e848c82b5248 | 14 | #define ADC_MAX_VALUE (4095) |
jurica238814 | 5:6566725c8835 | 15 | #define ADC_REF_VOLTAGE (3.6) |
jurica238814 | 8:c9f0aea93832 | 16 | #define VOLTAGE_DIVIDER_RATION (4.42) |
jurica238814 | 8:c9f0aea93832 | 17 | #define VOLTAGE_LOSS (0.4) |
jurica238814 | 5:6566725c8835 | 18 | #define CURRENT_FACTOR (36.0) |
jurica238814 | 8:c9f0aea93832 | 19 | #define BATTERY_MAX_V (12.6) |
jurica238814 | 8:c9f0aea93832 | 20 | #define BATTERY_MIN_V (11.85) |
jurica238814 | 8:c9f0aea93832 | 21 | #define BATTERY_STEP (0.15) |
jurica238814 | 6:e848c82b5248 | 22 | #define QUICK_CURRENT (float)(0.8) |
jurica238814 | 6:e848c82b5248 | 23 | #define LOW_CURRENT (float)(0.4) |
jurica238814 | 6:e848c82b5248 | 24 | #define NO_CURRENT (float)(0.01) |
jurica238814 | 6:e848c82b5248 | 25 | #define NUM_OF_MEASUREMENTS (20) |
jurica238814 | 6:e848c82b5248 | 26 | #define NO_CURRENT_STATE (0) |
jurica238814 | 6:e848c82b5248 | 27 | #define LOW_CURRENT_STATE (1) |
jurica238814 | 6:e848c82b5248 | 28 | #define QUICK_CURRENT_STATE (2) |
jurica238814 | 5:6566725c8835 | 29 | |
jurica238814 | 6:e848c82b5248 | 30 | #define _BS_5 (5) |
jurica238814 | 6:e848c82b5248 | 31 | #define _BS_4 (4) |
jurica238814 | 6:e848c82b5248 | 32 | #define _BS_3 (3) |
jurica238814 | 6:e848c82b5248 | 33 | #define _BS_2 (2) |
jurica238814 | 6:e848c82b5248 | 34 | #define _BS_1 (1) |
jurica238814 | 6:e848c82b5248 | 35 | #define _BS_E (0) |
jurica238814 | 0:dc96b5c8e3f6 | 36 | |
jurica238814 | 0:dc96b5c8e3f6 | 37 | SPI spi(PIN_EPD_MOSI, NC, PIN_EPD_SCK, NC); |
jurica238814 | 0:dc96b5c8e3f6 | 38 | GDEP015OC1 epd = GDEP015OC1(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY); |
jurica238814 | 0:dc96b5c8e3f6 | 39 | |
jurica238814 | 0:dc96b5c8e3f6 | 40 | AnalogIn battery (p28); |
jurica238814 | 0:dc96b5c8e3f6 | 41 | AnalogIn usb1 (p29); |
jurica238814 | 0:dc96b5c8e3f6 | 42 | AnalogIn usb2 (p30); |
jurica238814 | 0:dc96b5c8e3f6 | 43 | |
jurica238814 | 6:e848c82b5248 | 44 | uint8_t get_battery_level(float battery_voltage){ |
jurica238814 | 6:e848c82b5248 | 45 | if(battery_voltage > BATTERY_MAX_V - BATTERY_STEP){ |
jurica238814 | 6:e848c82b5248 | 46 | return _BS_5; |
jurica238814 | 6:e848c82b5248 | 47 | } |
jurica238814 | 6:e848c82b5248 | 48 | else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 2) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 1)){ |
jurica238814 | 6:e848c82b5248 | 49 | return _BS_4; |
jurica238814 | 6:e848c82b5248 | 50 | } |
jurica238814 | 6:e848c82b5248 | 51 | else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 3) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 2)){ |
jurica238814 | 6:e848c82b5248 | 52 | return _BS_3; |
jurica238814 | 6:e848c82b5248 | 53 | } |
jurica238814 | 6:e848c82b5248 | 54 | else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 4) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 3)){ |
jurica238814 | 6:e848c82b5248 | 55 | return _BS_2; |
jurica238814 | 6:e848c82b5248 | 56 | } |
jurica238814 | 6:e848c82b5248 | 57 | else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 5) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 4)){ |
jurica238814 | 6:e848c82b5248 | 58 | return _BS_1; |
jurica238814 | 6:e848c82b5248 | 59 | } |
jurica238814 | 6:e848c82b5248 | 60 | else if(battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 5){ |
jurica238814 | 6:e848c82b5248 | 61 | return _BS_E; |
jurica238814 | 6:e848c82b5248 | 62 | } |
jurica238814 | 6:e848c82b5248 | 63 | return 0; |
jurica238814 | 6:e848c82b5248 | 64 | } |
jurica238814 | 6:e848c82b5248 | 65 | |
jurica238814 | 6:e848c82b5248 | 66 | uint8_t get_current_level(float current_value, uint8_t usb_state){ |
jurica238814 | 6:e848c82b5248 | 67 | if(current_value < NO_CURRENT) |
jurica238814 | 6:e848c82b5248 | 68 | return NO_CURRENT_STATE; |
jurica238814 | 6:e848c82b5248 | 69 | else if(current_value > NO_CURRENT && current_value < LOW_CURRENT) |
jurica238814 | 6:e848c82b5248 | 70 | return LOW_CURRENT_STATE; |
jurica238814 | 6:e848c82b5248 | 71 | else if(current_value > LOW_CURRENT && current_value < QUICK_CURRENT){ |
jurica238814 | 6:e848c82b5248 | 72 | // Determine current charger state depends on previous state |
jurica238814 | 6:e848c82b5248 | 73 | if(usb_state == LOW_CURRENT_STATE) |
jurica238814 | 6:e848c82b5248 | 74 | return LOW_CURRENT_STATE; |
jurica238814 | 6:e848c82b5248 | 75 | else if(usb_state == QUICK_CURRENT_STATE) |
jurica238814 | 6:e848c82b5248 | 76 | return QUICK_CURRENT_STATE; |
jurica238814 | 6:e848c82b5248 | 77 | } |
jurica238814 | 6:e848c82b5248 | 78 | else if(current_value > QUICK_CURRENT) |
jurica238814 | 6:e848c82b5248 | 79 | return QUICK_CURRENT_STATE; |
jurica238814 | 6:e848c82b5248 | 80 | return NO_CURRENT_STATE; |
jurica238814 | 6:e848c82b5248 | 81 | } |
jurica238814 | 6:e848c82b5248 | 82 | |
jurica238814 | 0:dc96b5c8e3f6 | 83 | int main(){ |
jurica238814 | 5:6566725c8835 | 84 | char low_string[25] = "LOW"; |
jurica238814 | 5:6566725c8835 | 85 | char quick_string[25] = "QUICK"; |
jurica238814 | 4:f6f94ef38e6a | 86 | float adc1_mean=0, adc2_mean=0, adc3_mean=0; |
jurica238814 | 5:6566725c8835 | 87 | float battery_voltage = 0; |
jurica238814 | 5:6566725c8835 | 88 | float usb1_current = 0, usb2_current = 0; |
jurica238814 | 4:f6f94ef38e6a | 89 | int count = 0; |
jurica238814 | 6:e848c82b5248 | 90 | uint8_t battery_level = 0; |
jurica238814 | 6:e848c82b5248 | 91 | uint8_t old_battery_level; |
jurica238814 | 6:e848c82b5248 | 92 | uint8_t usb1_current_state = 0, usb2_current_state = 0; |
jurica238814 | 6:e848c82b5248 | 93 | uint8_t usb1_old_state = 0, usb2_old_state = 0; |
jurica238814 | 6:e848c82b5248 | 94 | uint8_t change_flag = 1; |
jurica238814 | 6:e848c82b5248 | 95 | |
jurica238814 | 6:e848c82b5248 | 96 | #if DEBUG |
jurica238814 | 6:e848c82b5248 | 97 | char buffer[25] = {0}; |
jurica238814 | 8:c9f0aea93832 | 98 | float old_battery_voltage = 0; |
jurica238814 | 8:c9f0aea93832 | 99 | float old_usb1_current = 0; |
jurica238814 | 8:c9f0aea93832 | 100 | float old_usb2_current = 0; |
jurica238814 | 6:e848c82b5248 | 101 | #endif |
jurica238814 | 4:f6f94ef38e6a | 102 | |
jurica238814 | 4:f6f94ef38e6a | 103 | NRF_SAADC->RESOLUTION = 0x00000002; // Set 12b resolution |
jurica238814 | 5:6566725c8835 | 104 | |
jurica238814 | 5:6566725c8835 | 105 | epd.empty(); |
jurica238814 | 5:6566725c8835 | 106 | epd.writeFull(); |
jurica238814 | 6:e848c82b5248 | 107 | |
jurica238814 | 0:dc96b5c8e3f6 | 108 | while(true){ |
jurica238814 | 0:dc96b5c8e3f6 | 109 | |
jurica238814 | 4:f6f94ef38e6a | 110 | adc1_mean += battery.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 111 | adc2_mean += usb1.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 112 | adc3_mean += usb2.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 113 | count ++; |
jurica238814 | 8:c9f0aea93832 | 114 | |
jurica238814 | 6:e848c82b5248 | 115 | if (count == NUM_OF_MEASUREMENTS){ |
jurica238814 | 6:e848c82b5248 | 116 | |
jurica238814 | 6:e848c82b5248 | 117 | adc1_mean /= NUM_OF_MEASUREMENTS; |
jurica238814 | 6:e848c82b5248 | 118 | adc2_mean /= NUM_OF_MEASUREMENTS; |
jurica238814 | 6:e848c82b5248 | 119 | adc3_mean /= NUM_OF_MEASUREMENTS; |
jurica238814 | 5:6566725c8835 | 120 | count = 0; |
jurica238814 | 8:c9f0aea93832 | 121 | |
jurica238814 | 8:c9f0aea93832 | 122 | battery_voltage = adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION + VOLTAGE_LOSS; |
jurica238814 | 8:c9f0aea93832 | 123 | usb1_current = (adc2_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)) * CURRENT_FACTOR; |
jurica238814 | 8:c9f0aea93832 | 124 | usb2_current = (adc3_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)) * CURRENT_FACTOR; |
jurica238814 | 6:e848c82b5248 | 125 | |
jurica238814 | 6:e848c82b5248 | 126 | battery_level = get_battery_level(battery_voltage); |
jurica238814 | 8:c9f0aea93832 | 127 | if(battery_level != old_battery_level || change_flag){ |
jurica238814 | 8:c9f0aea93832 | 128 | |
jurica238814 | 6:e848c82b5248 | 129 | old_battery_level = battery_level; |
jurica238814 | 6:e848c82b5248 | 130 | change_flag = 1; |
jurica238814 | 6:e848c82b5248 | 131 | switch(battery_level){ |
jurica238814 | 6:e848c82b5248 | 132 | case(0):{ |
jurica238814 | 6:e848c82b5248 | 133 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 6:e848c82b5248 | 134 | epd.fill(BS_E[x], x); |
jurica238814 | 6:e848c82b5248 | 135 | break; |
jurica238814 | 6:e848c82b5248 | 136 | } |
jurica238814 | 6:e848c82b5248 | 137 | case(1):{ |
jurica238814 | 6:e848c82b5248 | 138 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 6:e848c82b5248 | 139 | epd.fill(BS_1[x], x); |
jurica238814 | 6:e848c82b5248 | 140 | break; |
jurica238814 | 6:e848c82b5248 | 141 | } |
jurica238814 | 6:e848c82b5248 | 142 | case(2):{ |
jurica238814 | 6:e848c82b5248 | 143 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 6:e848c82b5248 | 144 | epd.fill(BS_2[x], x); |
jurica238814 | 6:e848c82b5248 | 145 | break; |
jurica238814 | 6:e848c82b5248 | 146 | } |
jurica238814 | 6:e848c82b5248 | 147 | case(3):{ |
jurica238814 | 6:e848c82b5248 | 148 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 6:e848c82b5248 | 149 | epd.fill(BS_3[x], x); |
jurica238814 | 6:e848c82b5248 | 150 | break; |
jurica238814 | 6:e848c82b5248 | 151 | } |
jurica238814 | 6:e848c82b5248 | 152 | case(4):{ |
jurica238814 | 6:e848c82b5248 | 153 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 6:e848c82b5248 | 154 | epd.fill(BS_4[x], x); |
jurica238814 | 6:e848c82b5248 | 155 | break; |
jurica238814 | 6:e848c82b5248 | 156 | } |
jurica238814 | 6:e848c82b5248 | 157 | case(5):{ |
jurica238814 | 6:e848c82b5248 | 158 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 6:e848c82b5248 | 159 | epd.fill(BS_5[x], x); |
jurica238814 | 6:e848c82b5248 | 160 | break; |
jurica238814 | 6:e848c82b5248 | 161 | } |
jurica238814 | 6:e848c82b5248 | 162 | default: break; |
jurica238814 | 6:e848c82b5248 | 163 | } |
jurica238814 | 8:c9f0aea93832 | 164 | |
jurica238814 | 5:6566725c8835 | 165 | epd.write(); |
jurica238814 | 5:6566725c8835 | 166 | } |
jurica238814 | 5:6566725c8835 | 167 | |
jurica238814 | 6:e848c82b5248 | 168 | usb1_current_state = get_current_level(usb1_current, usb1_old_state); |
jurica238814 | 6:e848c82b5248 | 169 | usb2_current_state = get_current_level(usb2_current, usb2_old_state); |
jurica238814 | 6:e848c82b5248 | 170 | |
jurica238814 | 6:e848c82b5248 | 171 | if((usb1_old_state != usb1_current_state) || change_flag){ |
jurica238814 | 6:e848c82b5248 | 172 | usb1_old_state = usb1_current_state; |
jurica238814 | 6:e848c82b5248 | 173 | switch(usb1_current_state){ |
jurica238814 | 6:e848c82b5248 | 174 | case(NO_CURRENT_STATE):{ |
jurica238814 | 6:e848c82b5248 | 175 | epd.writeString(quick_string, 25, 180, 1); // Delete the old state |
jurica238814 | 6:e848c82b5248 | 176 | epd.writeString(low_string, 25, 180, 1); |
jurica238814 | 6:e848c82b5248 | 177 | break; |
jurica238814 | 6:e848c82b5248 | 178 | } |
jurica238814 | 6:e848c82b5248 | 179 | case(LOW_CURRENT_STATE):{ |
jurica238814 | 6:e848c82b5248 | 180 | epd.writeString(quick_string, 25, 180, 1); // Delete the old state |
jurica238814 | 6:e848c82b5248 | 181 | epd.writeString(low_string, 25, 180, 0); |
jurica238814 | 6:e848c82b5248 | 182 | break; |
jurica238814 | 6:e848c82b5248 | 183 | } |
jurica238814 | 6:e848c82b5248 | 184 | case(QUICK_CURRENT_STATE):{ |
jurica238814 | 6:e848c82b5248 | 185 | epd.writeString(low_string, 25, 180, 1); // Delete the old state |
jurica238814 | 6:e848c82b5248 | 186 | epd.writeString(quick_string, 25, 180, 0); |
jurica238814 | 6:e848c82b5248 | 187 | break; |
jurica238814 | 6:e848c82b5248 | 188 | } |
jurica238814 | 6:e848c82b5248 | 189 | default: break; |
jurica238814 | 6:e848c82b5248 | 190 | } |
jurica238814 | 6:e848c82b5248 | 191 | epd.write(); // Write string to the EPD |
jurica238814 | 5:6566725c8835 | 192 | } |
jurica238814 | 5:6566725c8835 | 193 | |
jurica238814 | 6:e848c82b5248 | 194 | if((usb2_old_state != usb2_current_state) || change_flag){ |
jurica238814 | 6:e848c82b5248 | 195 | usb2_old_state = usb2_current_state; |
jurica238814 | 6:e848c82b5248 | 196 | switch(usb2_current_state){ |
jurica238814 | 6:e848c82b5248 | 197 | case(NO_CURRENT_STATE):{ |
jurica238814 | 6:e848c82b5248 | 198 | epd.writeString(low_string, 135, 180, 1); // Delete the old state |
jurica238814 | 6:e848c82b5248 | 199 | epd.writeString(quick_string, 135, 180, 1); // Delete the old state |
jurica238814 | 6:e848c82b5248 | 200 | break; |
jurica238814 | 6:e848c82b5248 | 201 | } |
jurica238814 | 6:e848c82b5248 | 202 | case(LOW_CURRENT_STATE):{ |
jurica238814 | 6:e848c82b5248 | 203 | epd.writeString(quick_string, 135, 180, 1); // Delete the old state |
jurica238814 | 6:e848c82b5248 | 204 | epd.writeString(low_string, 135, 180, 0); |
jurica238814 | 6:e848c82b5248 | 205 | break; |
jurica238814 | 6:e848c82b5248 | 206 | } |
jurica238814 | 6:e848c82b5248 | 207 | case(QUICK_CURRENT_STATE):{ |
jurica238814 | 6:e848c82b5248 | 208 | epd.writeString(low_string, 135, 180, 1); // Delete the old state |
jurica238814 | 6:e848c82b5248 | 209 | epd.writeString(quick_string, 135, 180, 0); |
jurica238814 | 6:e848c82b5248 | 210 | break; |
jurica238814 | 6:e848c82b5248 | 211 | } |
jurica238814 | 6:e848c82b5248 | 212 | default: break; |
jurica238814 | 6:e848c82b5248 | 213 | } |
jurica238814 | 6:e848c82b5248 | 214 | epd.write(); // Write string to the EPD |
jurica238814 | 6:e848c82b5248 | 215 | change_flag = 0; |
jurica238814 | 5:6566725c8835 | 216 | } |
jurica238814 | 6:e848c82b5248 | 217 | |
jurica238814 | 4:f6f94ef38e6a | 218 | |
jurica238814 | 8:c9f0aea93832 | 219 | #if DEBUG |
jurica238814 | 5:6566725c8835 | 220 | // Print voltage and current values in debug mode |
jurica238814 | 8:c9f0aea93832 | 221 | sprintf(buffer, "Battery: %5.5fV", old_battery_voltage); // Create a string |
jurica238814 | 8:c9f0aea93832 | 222 | epd.writeString(buffer,25,95,1); // Write new data to the buffer |
jurica238814 | 8:c9f0aea93832 | 223 | epd.write(); // Write string to the EPD |
jurica238814 | 8:c9f0aea93832 | 224 | old_battery_voltage = battery_voltage; |
jurica238814 | 8:c9f0aea93832 | 225 | |
jurica238814 | 8:c9f0aea93832 | 226 | sprintf(buffer, "Battery: %5.5fV", battery_voltage); // Create a string |
jurica238814 | 5:6566725c8835 | 227 | epd.writeString(buffer,25,95,0); // Write new data to the buffer |
jurica238814 | 8:c9f0aea93832 | 228 | |
jurica238814 | 8:c9f0aea93832 | 229 | sprintf(buffer, "USB1: %5.5f", old_usb1_current); // Create a string |
jurica238814 | 8:c9f0aea93832 | 230 | epd.writeString(buffer,5,190,1); // Write new data to the buffer |
jurica238814 | 8:c9f0aea93832 | 231 | old_usb1_current = usb1_current; |
jurica238814 | 8:c9f0aea93832 | 232 | sprintf(buffer, "USB1: %5.5f", usb1_current); // Create a string |
jurica238814 | 5:6566725c8835 | 233 | epd.writeString(buffer,5,190,0); // Write new data to the buffer |
jurica238814 | 6:e848c82b5248 | 234 | |
jurica238814 | 8:c9f0aea93832 | 235 | sprintf(buffer, "USB2: %5.5fA", old_usb2_current); // Create a string |
jurica238814 | 8:c9f0aea93832 | 236 | epd.writeString(buffer,105,190,1); // Write new data to the buffer |
jurica238814 | 8:c9f0aea93832 | 237 | old_usb2_current = usb2_current; |
jurica238814 | 8:c9f0aea93832 | 238 | sprintf(buffer, "USB2: %5.5fA", usb2_current); // Create a string |
jurica238814 | 5:6566725c8835 | 239 | epd.writeString(buffer,105,190,0); // Write new data to the buffer |
jurica238814 | 8:c9f0aea93832 | 240 | |
jurica238814 | 6:e848c82b5248 | 241 | epd.write(); |
jurica238814 | 5:6566725c8835 | 242 | #endif |
jurica238814 | 4:f6f94ef38e6a | 243 | |
jurica238814 | 4:f6f94ef38e6a | 244 | adc1_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 245 | adc2_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 246 | adc3_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 247 | } |
jurica238814 | 0:dc96b5c8e3f6 | 248 | } |
jurica238814 | 0:dc96b5c8e3f6 | 249 | } |