Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 DigitalOut relay1(p24); 00004 DigitalOut chargeLED(p30); 00005 DigitalOut dumpLED(p29); 00006 AnalogIn load1 (p16); 00007 00008 00009 #define rRatio 6.6355 00010 #define EMPTY 12.2000 00011 #define UPPER_CHARGE_LIMIT 13.0 00012 00013 int justConnected = 0; 00014 00015 void flash(void) { 00016 for (int i=0;i<5;i++) { 00017 dumpLED = 1; 00018 chargeLED = 1; 00019 wait(0.5); 00020 dumpLED = 0; 00021 chargeLED = 0; 00022 wait(0.5); 00023 } 00024 00025 } 00026 00027 //chargeLED = RED 00028 //dumpLED = Green 00029 00030 void chargeOne() { 00031 00032 justConnected = 0; 00033 00034 //with latch 00035 relay1=1; 00036 chargeLED=1; 00037 dumpLED = 0; 00038 00039 while (1) { 00040 00041 float voltage1 = load1*rRatio*3.3000; 00042 00043 float delay = 0.2; 00044 if (UPPER_CHARGE_LIMIT > voltage1) { 00045 delay=0.2+(UPPER_CHARGE_LIMIT - voltage1); 00046 } 00047 00048 wait (delay); 00049 chargeLED=1; 00050 wait (0.25); 00051 chargeLED=0; 00052 00053 // When the battery is charged to the limit, switch the relay off and return 00054 voltage1 = load1*rRatio*3.3000; 00055 if (voltage1 >= UPPER_CHARGE_LIMIT ) { 00056 flash(); 00057 relay1=0; 00058 chargeLED=0; 00059 return; 00060 } 00061 00062 } //loop 00063 00064 } 00065 00066 void init(void) { 00067 for (int i=0;i<10;i++) { 00068 dumpLED = 1; 00069 wait(0.5); 00070 dumpLED = 0; 00071 chargeLED = 1; 00072 wait(0.5); 00073 chargeLED = 0; 00074 } 00075 for (int i=0;i<1;i++) { 00076 dumpLED = 1; 00077 chargeLED = 1; 00078 wait(0.5); 00079 dumpLED = 0; 00080 chargeLED = 0; 00081 wait(0.5); 00082 } 00083 00084 justConnected = 1; 00085 00086 } 00087 00088 00089 void dump() { 00090 00091 dumpLED = 1; 00092 wait(0.5); 00093 dumpLED = 0; 00094 00095 } 00096 00097 int main() { 00098 init(); 00099 while(1) { 00100 wait(1); 00101 float voltage1 = load1*rRatio*3.3000; 00102 if ((voltage1 <= EMPTY) || ((justConnected == 1) && (voltage1 < UPPER_CHARGE_LIMIT))) { 00103 chargeOne(); 00104 } else { 00105 dump(); 00106 } 00107 00108 } 00109 }
Generated on Wed Jul 20 2022 16:44:16 by
1.7.2