for HTTP-3A
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #define VMAX 0.36f 00004 #define VMIN 0.05f 00005 #define AVGNUM 20 00006 00007 00008 Serial pc(USBTX, USBRX, 115200); 00009 00010 Ticker ticker1; 00011 00012 CAN can1(PB_8, PB_9); 00013 CANMessage can_msg_1; 00014 CANMessage can_msg_send; 00015 char data_msg[3] = {0x11,0x22,0x33}; 00016 00017 DigitalIn armSwitch(PF_7, PullDown); 00018 bool armed = false; 00019 00020 AnalogIn res(PA_0); 00021 float volt[AVGNUM]; 00022 float voltAVG = 0; 00023 int index = 0; 00024 00025 00026 //I2C i2c_lcd(D14,D15); // SDA, SCL 00027 I2C i2c_lcd(PF_0,PF_1); // SDA, SCL 00028 TextLCD_I2C lcd(&i2c_lcd, 0x27, TextLCD::LCD16x2); // I2C bus, PCF8574 Slaveaddress, LCD Type 00029 00030 const char fill[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; 00031 const char empty[] = {0x1F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x00}; 00032 const char cross[] = {0x00, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x00, 0x00}; 00033 00034 void showBar(void); 00035 00036 void showArm(void); 00037 00038 void CAN_RX1(void); 00039 00040 void sendCMD(void) 00041 { 00042 if(armed) 00043 { 00044 int CMD = 90 * (voltAVG-VMIN)/(VMAX-VMIN); 00045 if (CMD > 90) 00046 CMD = 90; 00047 else if (CMD < 0) 00048 CMD = 0; 00049 data_msg[0] = (char)CMD; 00050 data_msg[1] = (char)CMD; 00051 data_msg[2] = (char)CMD; 00052 can_msg_send = CANMessage(0x000,data_msg,3,CANData,CANStandard); 00053 can1.write(can_msg_send); 00054 } 00055 else 00056 { 00057 data_msg[0] = 0; 00058 data_msg[1] = 0; 00059 data_msg[2] = 0; 00060 can_msg_send = CANMessage(0x000,data_msg,3,CANData,CANStandard); 00061 can1.write(can_msg_send); 00062 } 00063 00064 //printf("res: %f\n", voltAVG); 00065 } 00066 00067 int main() 00068 { 00069 00070 can1.frequency(500000); 00071 can1.attach(&CAN_RX1, CAN::RxIrq); 00072 ticker1.attach(&sendCMD, 1); 00073 pc.printf("start\n"); 00074 00075 lcd.setCursor(TextLCD::CurOff_BlkOff); 00076 lcd.setBacklight(TextLCD::LightOn); 00077 lcd.setUDC(0, (char *) fill); 00078 lcd.setUDC(1, (char *) empty); 00079 lcd.setUDC(2, (char *) cross); 00080 pc.printf("set done\n\r"); 00081 00082 for(int i = 0; i<AVGNUM; i++) 00083 { 00084 volt[i] = 0; 00085 } 00086 00087 while(1) 00088 { 00089 volt[index] = res.read(); 00090 index++; 00091 if (index>=AVGNUM) 00092 index = 0; 00093 00094 float tmp = 0; 00095 for(int i = 0; i<AVGNUM; i++) 00096 { 00097 tmp = tmp + volt[i]; 00098 } 00099 voltAVG = tmp/AVGNUM; 00100 00101 00102 showBar(); 00103 showArm(); 00104 } 00105 00106 } 00107 00108 00109 00110 void CAN_RX1(void) 00111 { 00112 if(can1.read(can_msg_1)) 00113 { 00114 pc.printf("CAN RX %d\n", can_msg_1.id); 00115 00116 } 00117 } 00118 00119 void showBar(void) 00120 { 00121 int bars = 16 * (voltAVG-VMIN)/(VMAX-VMIN); 00122 if (bars > 16) 00123 bars = 16; 00124 else if( bars < 0 ) 00125 bars = 0; 00126 00127 if (armed) 00128 { 00129 for(int i = 0; i<bars ;i++) 00130 { 00131 lcd.locate(i,0); 00132 lcd.putc(0); 00133 } 00134 for(int i = bars; i<16 ;i++) 00135 { 00136 lcd.locate(i,0); 00137 lcd.putc(1); 00138 } 00139 } 00140 else 00141 { 00142 for(int i = 0; i<bars ;i++) 00143 { 00144 lcd.locate(i,0); 00145 lcd.putc(2); 00146 } 00147 for(int i = bars; i<16 ;i++) 00148 { 00149 lcd.locate(i,0); 00150 lcd.putc(1); 00151 } 00152 } 00153 } 00154 00155 void showArm(void) 00156 { 00157 if (armSwitch) 00158 armed = true; 00159 else 00160 armed = false; 00161 00162 if (armed) 00163 { 00164 lcd.locate(0,1); 00165 lcd.printf("ARMED "); 00166 } 00167 else 00168 { 00169 lcd.locate(0,1); 00170 lcd.printf("DISARMED"); 00171 } 00172 00173 }
Generated on Fri Jul 29 2022 11:06:18 by
1.7.2