for HTTP-3A

Dependencies:   mbed TextLCD

Committer:
thomasya
Date:
Wed Oct 23 14:11:17 2019 +0000
Revision:
8:f6e17225d565
Parent:
7:e22e9fd1a526
gadifha

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thomasya 3:043e5e06f325 1 #include "mbed.h"
thomasya 4:c1438ffd88dd 2 #include "TextLCD.h"
thomasya 4:c1438ffd88dd 3 #define VMAX 0.36f
thomasya 4:c1438ffd88dd 4 #define VMIN 0.05f
thomasya 6:572219c7c378 5 #define AVGNUM 20
thomasya 3:043e5e06f325 6
thomasya 4:c1438ffd88dd 7
thomasya 3:043e5e06f325 8 Serial pc(USBTX, USBRX, 115200);
group-ST 0:6e8275981824 9
thomasya 3:043e5e06f325 10 Ticker ticker1;
thomasya 3:043e5e06f325 11
thomasya 4:c1438ffd88dd 12 CAN can1(PB_8, PB_9);
thomasya 3:043e5e06f325 13 CANMessage can_msg_1;
thomasya 3:043e5e06f325 14 CANMessage can_msg_send;
thomasya 4:c1438ffd88dd 15 char data_msg[3] = {0x11,0x22,0x33};
thomasya 4:c1438ffd88dd 16
thomasya 5:1d817b3c42f1 17 DigitalIn armSwitch(PF_7, PullDown);
thomasya 5:1d817b3c42f1 18 bool armed = false;
thomasya 4:c1438ffd88dd 19
thomasya 4:c1438ffd88dd 20 AnalogIn res(PA_0);
thomasya 6:572219c7c378 21 float volt[AVGNUM];
thomasya 6:572219c7c378 22 float voltAVG = 0;
thomasya 6:572219c7c378 23 int index = 0;
group-ST 0:6e8275981824 24
thomasya 3:043e5e06f325 25
thomasya 8:f6e17225d565 26 //I2C i2c_lcd(D14,D15); // SDA, SCL
thomasya 5:1d817b3c42f1 27 I2C i2c_lcd(PF_0,PF_1); // SDA, SCL
thomasya 8:f6e17225d565 28 TextLCD_I2C lcd(&i2c_lcd, 0x27, TextLCD::LCD16x2); // I2C bus, PCF8574 Slaveaddress, LCD Type
thomasya 4:c1438ffd88dd 29
thomasya 4:c1438ffd88dd 30 const char fill[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00};
thomasya 4:c1438ffd88dd 31 const char empty[] = {0x1F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x00};
thomasya 5:1d817b3c42f1 32 const char cross[] = {0x00, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x00, 0x00};
thomasya 4:c1438ffd88dd 33
thomasya 4:c1438ffd88dd 34 void showBar(void);
thomasya 3:043e5e06f325 35
thomasya 5:1d817b3c42f1 36 void showArm(void);
thomasya 5:1d817b3c42f1 37
thomasya 3:043e5e06f325 38 void CAN_RX1(void);
group-ST 0:6e8275981824 39
thomasya 3:043e5e06f325 40 void sendCMD(void)
thomasya 3:043e5e06f325 41 {
thomasya 4:c1438ffd88dd 42 if(armed)
thomasya 4:c1438ffd88dd 43 {
thomasya 6:572219c7c378 44 int CMD = 90 * (voltAVG-VMIN)/(VMAX-VMIN);
thomasya 6:572219c7c378 45 if (CMD > 90)
thomasya 6:572219c7c378 46 CMD = 90;
thomasya 6:572219c7c378 47 else if (CMD < 0)
thomasya 6:572219c7c378 48 CMD = 0;
thomasya 6:572219c7c378 49 data_msg[0] = (char)CMD;
thomasya 6:572219c7c378 50 data_msg[1] = (char)CMD;
thomasya 6:572219c7c378 51 data_msg[2] = (char)CMD;
thomasya 7:e22e9fd1a526 52 can_msg_send = CANMessage(0x000,data_msg,3,CANData,CANStandard);
thomasya 6:572219c7c378 53 can1.write(can_msg_send);
thomasya 6:572219c7c378 54 }
thomasya 6:572219c7c378 55 else
thomasya 6:572219c7c378 56 {
thomasya 6:572219c7c378 57 data_msg[0] = 0;
thomasya 6:572219c7c378 58 data_msg[1] = 0;
thomasya 6:572219c7c378 59 data_msg[2] = 0;
thomasya 7:e22e9fd1a526 60 can_msg_send = CANMessage(0x000,data_msg,3,CANData,CANStandard);
thomasya 4:c1438ffd88dd 61 can1.write(can_msg_send);
thomasya 4:c1438ffd88dd 62 }
thomasya 3:043e5e06f325 63
thomasya 6:572219c7c378 64 //printf("res: %f\n", voltAVG);
thomasya 3:043e5e06f325 65 }
group-ST 0:6e8275981824 66
group-ST 0:6e8275981824 67 int main()
group-ST 0:6e8275981824 68 {
thomasya 3:043e5e06f325 69
thomasya 4:c1438ffd88dd 70 can1.frequency(500000);
thomasya 4:c1438ffd88dd 71 can1.attach(&CAN_RX1, CAN::RxIrq);
thomasya 4:c1438ffd88dd 72 ticker1.attach(&sendCMD, 1);
thomasya 3:043e5e06f325 73 pc.printf("start\n");
thomasya 4:c1438ffd88dd 74
thomasya 4:c1438ffd88dd 75 lcd.setCursor(TextLCD::CurOff_BlkOff);
thomasya 4:c1438ffd88dd 76 lcd.setBacklight(TextLCD::LightOn);
thomasya 4:c1438ffd88dd 77 lcd.setUDC(0, (char *) fill);
thomasya 4:c1438ffd88dd 78 lcd.setUDC(1, (char *) empty);
thomasya 5:1d817b3c42f1 79 lcd.setUDC(2, (char *) cross);
thomasya 4:c1438ffd88dd 80 pc.printf("set done\n\r");
thomasya 4:c1438ffd88dd 81
thomasya 6:572219c7c378 82 for(int i = 0; i<AVGNUM; i++)
thomasya 6:572219c7c378 83 {
thomasya 6:572219c7c378 84 volt[i] = 0;
thomasya 6:572219c7c378 85 }
thomasya 6:572219c7c378 86
thomasya 3:043e5e06f325 87 while(1)
thomasya 4:c1438ffd88dd 88 {
thomasya 6:572219c7c378 89 volt[index] = res.read();
thomasya 6:572219c7c378 90 index++;
thomasya 6:572219c7c378 91 if (index>=AVGNUM)
thomasya 6:572219c7c378 92 index = 0;
thomasya 6:572219c7c378 93
thomasya 6:572219c7c378 94 float tmp = 0;
thomasya 6:572219c7c378 95 for(int i = 0; i<AVGNUM; i++)
thomasya 6:572219c7c378 96 {
thomasya 6:572219c7c378 97 tmp = tmp + volt[i];
thomasya 6:572219c7c378 98 }
thomasya 6:572219c7c378 99 voltAVG = tmp/AVGNUM;
thomasya 6:572219c7c378 100
thomasya 6:572219c7c378 101
thomasya 4:c1438ffd88dd 102 showBar();
thomasya 5:1d817b3c42f1 103 showArm();
thomasya 3:043e5e06f325 104 }
thomasya 3:043e5e06f325 105
thomasya 3:043e5e06f325 106 }
thomasya 3:043e5e06f325 107
thomasya 3:043e5e06f325 108
thomasya 3:043e5e06f325 109
thomasya 3:043e5e06f325 110 void CAN_RX1(void)
thomasya 3:043e5e06f325 111 {
thomasya 3:043e5e06f325 112 if(can1.read(can_msg_1))
thomasya 3:043e5e06f325 113 {
thomasya 3:043e5e06f325 114 pc.printf("CAN RX %d\n", can_msg_1.id);
thomasya 3:043e5e06f325 115
group-ST 0:6e8275981824 116 }
group-ST 0:6e8275981824 117 }
thomasya 4:c1438ffd88dd 118
thomasya 4:c1438ffd88dd 119 void showBar(void)
thomasya 4:c1438ffd88dd 120 {
thomasya 6:572219c7c378 121 int bars = 16 * (voltAVG-VMIN)/(VMAX-VMIN);
thomasya 4:c1438ffd88dd 122 if (bars > 16)
thomasya 4:c1438ffd88dd 123 bars = 16;
thomasya 4:c1438ffd88dd 124 else if( bars < 0 )
thomasya 4:c1438ffd88dd 125 bars = 0;
thomasya 4:c1438ffd88dd 126
thomasya 5:1d817b3c42f1 127 if (armed)
thomasya 4:c1438ffd88dd 128 {
thomasya 5:1d817b3c42f1 129 for(int i = 0; i<bars ;i++)
thomasya 5:1d817b3c42f1 130 {
thomasya 5:1d817b3c42f1 131 lcd.locate(i,0);
thomasya 5:1d817b3c42f1 132 lcd.putc(0);
thomasya 5:1d817b3c42f1 133 }
thomasya 5:1d817b3c42f1 134 for(int i = bars; i<16 ;i++)
thomasya 5:1d817b3c42f1 135 {
thomasya 5:1d817b3c42f1 136 lcd.locate(i,0);
thomasya 5:1d817b3c42f1 137 lcd.putc(1);
thomasya 5:1d817b3c42f1 138 }
thomasya 5:1d817b3c42f1 139 }
thomasya 5:1d817b3c42f1 140 else
thomasya 4:c1438ffd88dd 141 {
thomasya 5:1d817b3c42f1 142 for(int i = 0; i<bars ;i++)
thomasya 5:1d817b3c42f1 143 {
thomasya 5:1d817b3c42f1 144 lcd.locate(i,0);
thomasya 5:1d817b3c42f1 145 lcd.putc(2);
thomasya 5:1d817b3c42f1 146 }
thomasya 5:1d817b3c42f1 147 for(int i = bars; i<16 ;i++)
thomasya 5:1d817b3c42f1 148 {
thomasya 5:1d817b3c42f1 149 lcd.locate(i,0);
thomasya 5:1d817b3c42f1 150 lcd.putc(1);
thomasya 5:1d817b3c42f1 151 }
thomasya 4:c1438ffd88dd 152 }
thomasya 4:c1438ffd88dd 153 }
thomasya 5:1d817b3c42f1 154
thomasya 5:1d817b3c42f1 155 void showArm(void)
thomasya 5:1d817b3c42f1 156 {
thomasya 5:1d817b3c42f1 157 if (armSwitch)
thomasya 5:1d817b3c42f1 158 armed = true;
thomasya 5:1d817b3c42f1 159 else
thomasya 5:1d817b3c42f1 160 armed = false;
thomasya 5:1d817b3c42f1 161
thomasya 5:1d817b3c42f1 162 if (armed)
thomasya 5:1d817b3c42f1 163 {
thomasya 5:1d817b3c42f1 164 lcd.locate(0,1);
thomasya 5:1d817b3c42f1 165 lcd.printf("ARMED ");
thomasya 5:1d817b3c42f1 166 }
thomasya 5:1d817b3c42f1 167 else
thomasya 5:1d817b3c42f1 168 {
thomasya 5:1d817b3c42f1 169 lcd.locate(0,1);
thomasya 5:1d817b3c42f1 170 lcd.printf("DISARMED");
thomasya 5:1d817b3c42f1 171 }
thomasya 5:1d817b3c42f1 172
thomasya 5:1d817b3c42f1 173 }