tow INA226 devices
Dependencies: C12832_lcd app-board-logger-v1 INA226 LM75B MMA7660 MSCFileSystem mbed
main.cpp@1:a8c8b3ba71ff, 2013-06-19 (annotated)
- Committer:
- hirox
- Date:
- Wed Jun 19 12:05:56 2013 +0000
- Revision:
- 1:a8c8b3ba71ff
- Parent:
- 0:a30d570f6e08
add tmp date
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hirox | 0:a30d570f6e08 | 1 | #include "mbed.h" |
hirox | 0:a30d570f6e08 | 2 | #include "MSCFileSystem.h" |
hirox | 0:a30d570f6e08 | 3 | #include "INA226.hpp" |
hirox | 0:a30d570f6e08 | 4 | //#include "EthernetInterface.h" |
hirox | 0:a30d570f6e08 | 5 | //#include "NTPClient.h" |
hirox | 0:a30d570f6e08 | 6 | #include "C12832_lcd.h" |
hirox | 0:a30d570f6e08 | 7 | #include "MMA7660.h" |
hirox | 0:a30d570f6e08 | 8 | #include "LM75B.h" |
hirox | 0:a30d570f6e08 | 9 | |
hirox | 0:a30d570f6e08 | 10 | Serial debug(USBTX,USBRX); |
hirox | 0:a30d570f6e08 | 11 | DigitalOut ntpLed(LED1); |
hirox | 0:a30d570f6e08 | 12 | DigitalOut mmaLed(LED2); |
hirox | 0:a30d570f6e08 | 13 | DigitalOut inaLed(LED3); |
hirox | 0:a30d570f6e08 | 14 | DigitalOut usbLed(LED4); |
hirox | 0:a30d570f6e08 | 15 | |
hirox | 0:a30d570f6e08 | 16 | time_t ctTime; |
hirox | 0:a30d570f6e08 | 17 | |
hirox | 0:a30d570f6e08 | 18 | // gloval variavle |
hirox | 0:a30d570f6e08 | 19 | unsigned short val; |
hirox | 0:a30d570f6e08 | 20 | double V1,V2,C1,C2; |
hirox | 0:a30d570f6e08 | 21 | char strTimeMsg[16]; |
hirox | 0:a30d570f6e08 | 22 | float xyz[3]; |
hirox | 0:a30d570f6e08 | 23 | float r_V1,r_V2,r_C1,r_C2; |
hirox | 0:a30d570f6e08 | 24 | float r_rpm; |
hirox | 0:a30d570f6e08 | 25 | |
hirox | 0:a30d570f6e08 | 26 | // INA226 |
hirox | 0:a30d570f6e08 | 27 | I2C i2c(p28,p27); |
hirox | 0:a30d570f6e08 | 28 | INA226 VCmonitor1(i2c,0x80); |
hirox | 0:a30d570f6e08 | 29 | INA226 VCmonitor2(i2c,0x83); |
hirox | 0:a30d570f6e08 | 30 | |
hirox | 0:a30d570f6e08 | 31 | // Analog In |
hirox | 0:a30d570f6e08 | 32 | AnalogIn rpm_in(p17); |
hirox | 0:a30d570f6e08 | 33 | |
hirox | 0:a30d570f6e08 | 34 | // mbed application board LCD |
hirox | 0:a30d570f6e08 | 35 | C12832_LCD lcd; |
hirox | 0:a30d570f6e08 | 36 | |
hirox | 0:a30d570f6e08 | 37 | // for NTP Client |
hirox | 0:a30d570f6e08 | 38 | //EthernetInterface eth; |
hirox | 0:a30d570f6e08 | 39 | //NTPClient ntp; |
hirox | 0:a30d570f6e08 | 40 | |
hirox | 0:a30d570f6e08 | 41 | // MMA7660 accelerometer 0x98 |
hirox | 0:a30d570f6e08 | 42 | MMA7660 MMA(p28,p27); |
hirox | 0:a30d570f6e08 | 43 | |
hirox | 0:a30d570f6e08 | 44 | // LM75B Temperture |
hirox | 0:a30d570f6e08 | 45 | LM75B tmp(p28,p27); |
hirox | 0:a30d570f6e08 | 46 | |
hirox | 0:a30d570f6e08 | 47 | //USB Flash |
hirox | 0:a30d570f6e08 | 48 | MSCFileSystem msc("fs"); |
hirox | 0:a30d570f6e08 | 49 | |
hirox | 0:a30d570f6e08 | 50 | // set INA226 |
hirox | 0:a30d570f6e08 | 51 | void setINA226(){ |
hirox | 0:a30d570f6e08 | 52 | lcd.cls(); |
hirox | 0:a30d570f6e08 | 53 | lcd.locate(0,0); |
hirox | 0:a30d570f6e08 | 54 | lcd.printf("Please waite...INA226"); |
hirox | 0:a30d570f6e08 | 55 | inaLed = 1; |
hirox | 0:a30d570f6e08 | 56 | |
hirox | 0:a30d570f6e08 | 57 | // VCmonitor1 |
hirox | 0:a30d570f6e08 | 58 | if ( !VCmonitor1.isExist() ){ |
hirox | 0:a30d570f6e08 | 59 | lcd.printf("VCmonitor1 INA226 Not Found.\n"); |
hirox | 0:a30d570f6e08 | 60 | inaLed = 0; |
hirox | 0:a30d570f6e08 | 61 | } |
hirox | 0:a30d570f6e08 | 62 | lcd.printf("VCmonitor1 INA226 Found\n"); |
hirox | 0:a30d570f6e08 | 63 | val = 0; |
hirox | 0:a30d570f6e08 | 64 | if ( VCmonitor1.rawRead(0x00,&val) != 0){ |
hirox | 0:a30d570f6e08 | 65 | lcd.printf("VCmonitor1 INA226 Read Error\n"); |
hirox | 0:a30d570f6e08 | 66 | debug.printf("VCmonitor1 INA226 Read Error\n"); |
hirox | 0:a30d570f6e08 | 67 | } |
hirox | 0:a30d570f6e08 | 68 | lcd.printf("VCmonitor1 Reg 0x00 : 0x%04x\n",val); |
hirox | 0:a30d570f6e08 | 69 | debug.printf("VCmonitor1 Reg 0x00 : 0x%04x\r\n",val); |
hirox | 0:a30d570f6e08 | 70 | |
hirox | 0:a30d570f6e08 | 71 | VCmonitor1.setCurrentCalibration(); |
hirox | 0:a30d570f6e08 | 72 | |
hirox | 0:a30d570f6e08 | 73 | // VCmonitor2 |
hirox | 0:a30d570f6e08 | 74 | if ( !VCmonitor2.isExist() ){ |
hirox | 0:a30d570f6e08 | 75 | lcd.printf("VCmonitor2 INA226 Not Found.\n"); |
hirox | 0:a30d570f6e08 | 76 | inaLed = 0; |
hirox | 0:a30d570f6e08 | 77 | } |
hirox | 0:a30d570f6e08 | 78 | lcd.printf("VCmonitor2 INA226 Found\n"); |
hirox | 0:a30d570f6e08 | 79 | val = 0; |
hirox | 0:a30d570f6e08 | 80 | if ( VCmonitor2.rawRead(0x00,&val) != 0){ |
hirox | 0:a30d570f6e08 | 81 | lcd.printf("VCmonitor2 INA226 Read Error\n"); |
hirox | 0:a30d570f6e08 | 82 | debug.printf("VCmonitor2 INA226 Read Error\n"); |
hirox | 0:a30d570f6e08 | 83 | } |
hirox | 0:a30d570f6e08 | 84 | lcd.printf("VCmonitor2 Reg 0x00 : 0x04x\n",val); |
hirox | 0:a30d570f6e08 | 85 | debug.printf("VCmonitor2 Reg 0x00 : 0x04x\r\n",val); |
hirox | 0:a30d570f6e08 | 86 | |
hirox | 0:a30d570f6e08 | 87 | VCmonitor2.setCurrentCalibration(); |
hirox | 0:a30d570f6e08 | 88 | } |
hirox | 0:a30d570f6e08 | 89 | |
hirox | 0:a30d570f6e08 | 90 | // set MMA7660 |
hirox | 0:a30d570f6e08 | 91 | void setMMA7660(){ |
hirox | 0:a30d570f6e08 | 92 | if ( MMA.testConnection() ){ |
hirox | 0:a30d570f6e08 | 93 | mmaLed = 1; |
hirox | 0:a30d570f6e08 | 94 | } |
hirox | 0:a30d570f6e08 | 95 | } |
hirox | 0:a30d570f6e08 | 96 | |
hirox | 0:a30d570f6e08 | 97 | // update LCD Display |
hirox | 0:a30d570f6e08 | 98 | void updateLCD() { |
hirox | 0:a30d570f6e08 | 99 | |
hirox | 0:a30d570f6e08 | 100 | lcd.cls(); |
hirox | 0:a30d570f6e08 | 101 | lcd.locate(0,0); |
hirox | 0:a30d570f6e08 | 102 | lcd.printf("%s",strTimeMsg); |
hirox | 0:a30d570f6e08 | 103 | |
hirox | 0:a30d570f6e08 | 104 | lcd.locate(0,10); |
hirox | 0:a30d570f6e08 | 105 | lcd.printf("%3.2f ",xyz[0]); |
hirox | 0:a30d570f6e08 | 106 | lcd.printf("%3.2f ",xyz[1]); |
hirox | 0:a30d570f6e08 | 107 | lcd.printf("%3.2f ",xyz[2]); |
hirox | 0:a30d570f6e08 | 108 | |
hirox | 0:a30d570f6e08 | 109 | lcd.locate(0,20); |
hirox | 0:a30d570f6e08 | 110 | lcd.printf("%3.2f ",r_V1); |
hirox | 0:a30d570f6e08 | 111 | lcd.printf("%3.2f ",r_C1); |
hirox | 0:a30d570f6e08 | 112 | lcd.printf("%3.2f ",r_rpm); |
hirox | 0:a30d570f6e08 | 113 | |
hirox | 1:a8c8b3ba71ff | 114 | debug.printf("%s,%4.3f,%4.3f,%4.3f,%f,%f,%f,%f,%f,%f\r\n",strTimeMsg,xyz[0],xyz[1],xyz[2],r_V1,r_C1,r_V2,r_C2,r_rpm,tmp.read()); |
hirox | 0:a30d570f6e08 | 115 | |
hirox | 0:a30d570f6e08 | 116 | } |
hirox | 0:a30d570f6e08 | 117 | |
hirox | 0:a30d570f6e08 | 118 | /* ----- Set RTC by NTP ----- */ |
hirox | 0:a30d570f6e08 | 119 | /* ---------------- |
hirox | 0:a30d570f6e08 | 120 | void setRTC_NTP() |
hirox | 0:a30d570f6e08 | 121 | { |
hirox | 0:a30d570f6e08 | 122 | |
hirox | 0:a30d570f6e08 | 123 | lcd.cls(); |
hirox | 0:a30d570f6e08 | 124 | lcd.locate(0,0); |
hirox | 0:a30d570f6e08 | 125 | lcd.printf("Get IP addr ..."); |
hirox | 0:a30d570f6e08 | 126 | |
hirox | 0:a30d570f6e08 | 127 | EthernetErr ethErr = eth.setup(); |
hirox | 0:a30d570f6e08 | 128 | |
hirox | 0:a30d570f6e08 | 129 | if (ethErr) { |
hirox | 0:a30d570f6e08 | 130 | lcd.cls(); |
hirox | 0:a30d570f6e08 | 131 | lcd.printf("Network Error \n\r %d",ethErr); |
hirox | 0:a30d570f6e08 | 132 | return ; |
hirox | 0:a30d570f6e08 | 133 | } |
hirox | 0:a30d570f6e08 | 134 | |
hirox | 0:a30d570f6e08 | 135 | Host server(IpAddr(), 123, "0,pool.ntp.org"); |
hirox | 0:a30d570f6e08 | 136 | ntp.setTime(server); |
hirox | 0:a30d570f6e08 | 137 | lcd.printf("Set time successfully"); |
hirox | 0:a30d570f6e08 | 138 | ctTime = time(NULL); |
hirox | 0:a30d570f6e08 | 139 | ntpLed = 1; |
hirox | 0:a30d570f6e08 | 140 | // eth.disconnect(); |
hirox | 0:a30d570f6e08 | 141 | } |
hirox | 0:a30d570f6e08 | 142 | ----------- */ |
hirox | 0:a30d570f6e08 | 143 | |
hirox | 0:a30d570f6e08 | 144 | int main() { |
hirox | 0:a30d570f6e08 | 145 | |
hirox | 0:a30d570f6e08 | 146 | char strTimeFile[8]; |
hirox | 0:a30d570f6e08 | 147 | int intTimeFile=0; |
hirox | 0:a30d570f6e08 | 148 | char filepath[256]; |
hirox | 0:a30d570f6e08 | 149 | double V,C; |
hirox | 0:a30d570f6e08 | 150 | |
hirox | 0:a30d570f6e08 | 151 | lcd.cls(); |
hirox | 0:a30d570f6e08 | 152 | lcd.locate(0,0); |
hirox | 0:a30d570f6e08 | 153 | lcd.printf("init..."); |
hirox | 0:a30d570f6e08 | 154 | |
hirox | 0:a30d570f6e08 | 155 | time_t ctTime; |
hirox | 0:a30d570f6e08 | 156 | |
hirox | 0:a30d570f6e08 | 157 | // set RTC by NTP |
hirox | 0:a30d570f6e08 | 158 | // setRTC_NTP(); |
hirox | 0:a30d570f6e08 | 159 | |
hirox | 0:a30d570f6e08 | 160 | // set INA226 |
hirox | 0:a30d570f6e08 | 161 | setINA226(); |
hirox | 0:a30d570f6e08 | 162 | |
hirox | 0:a30d570f6e08 | 163 | // set MMA7660 |
hirox | 0:a30d570f6e08 | 164 | setMMA7660(); |
hirox | 0:a30d570f6e08 | 165 | |
hirox | 0:a30d570f6e08 | 166 | while(1){ |
hirox | 0:a30d570f6e08 | 167 | |
hirox | 0:a30d570f6e08 | 168 | ctTime = time(NULL) + 32400; |
hirox | 0:a30d570f6e08 | 169 | strftime(strTimeFile,8,"%d%H%M%S",localtime(&ctTime)); |
hirox | 0:a30d570f6e08 | 170 | |
hirox | 0:a30d570f6e08 | 171 | lcd.cls(); |
hirox | 0:a30d570f6e08 | 172 | lcd.printf("USB File Create...\n"); |
hirox | 0:a30d570f6e08 | 173 | debug.printf("%s\r\n",strTimeFile); |
hirox | 0:a30d570f6e08 | 174 | |
hirox | 0:a30d570f6e08 | 175 | intTimeFile = atoi(strTimeFile); |
hirox | 0:a30d570f6e08 | 176 | sprintf(filepath,"/fs/%08u.csv",intTimeFile); |
hirox | 0:a30d570f6e08 | 177 | lcd.printf("%.16s ",filepath); |
hirox | 0:a30d570f6e08 | 178 | debug.printf("filepath %s\r\n",filepath); |
hirox | 0:a30d570f6e08 | 179 | FILE *fp = fopen(filepath,"w"); |
hirox | 0:a30d570f6e08 | 180 | usbLed = 1; |
hirox | 0:a30d570f6e08 | 181 | if (fp == NULL ) { |
hirox | 0:a30d570f6e08 | 182 | lcd.locate(0,12); |
hirox | 0:a30d570f6e08 | 183 | lcd.printf("Create Failed.") ; |
hirox | 0:a30d570f6e08 | 184 | // exit(0); |
hirox | 0:a30d570f6e08 | 185 | } |
hirox | 0:a30d570f6e08 | 186 | |
hirox | 0:a30d570f6e08 | 187 | for (int i=0; i<600; i++) { |
hirox | 0:a30d570f6e08 | 188 | VCmonitor1.getVoltage(&V1); |
hirox | 0:a30d570f6e08 | 189 | VCmonitor1.getCurrent(&C1); |
hirox | 0:a30d570f6e08 | 190 | r_V1 = V1 / 1000 ; |
hirox | 0:a30d570f6e08 | 191 | r_C1 = C1 / 1000 ; |
hirox | 0:a30d570f6e08 | 192 | VCmonitor2.getVoltage(&V2); |
hirox | 0:a30d570f6e08 | 193 | VCmonitor2.getCurrent(&C2); |
hirox | 0:a30d570f6e08 | 194 | r_V2 = V2 / 1000 ; |
hirox | 0:a30d570f6e08 | 195 | r_C2 = C2 / 1000 ; |
hirox | 0:a30d570f6e08 | 196 | |
hirox | 0:a30d570f6e08 | 197 | r_rpm = rpm_in.read() * 18500 ; |
hirox | 0:a30d570f6e08 | 198 | |
hirox | 0:a30d570f6e08 | 199 | xyz[0] = MMA.x(); |
hirox | 0:a30d570f6e08 | 200 | xyz[1] = MMA.y(); |
hirox | 0:a30d570f6e08 | 201 | xyz[2] = MMA.z(); |
hirox | 0:a30d570f6e08 | 202 | |
hirox | 0:a30d570f6e08 | 203 | ctTime = time(NULL) + 32400; |
hirox | 0:a30d570f6e08 | 204 | strftime(strTimeMsg,16,"%m/%d %H:%M:%S",localtime(&ctTime)); |
hirox | 0:a30d570f6e08 | 205 | |
hirox | 1:a8c8b3ba71ff | 206 | fprintf(fp,"%s,%4.3f,%4.3f,%4.3f,%f,%f,%f,%f,%f,%f\r\n",strTimeMsg,xyz[0],xyz[1],xyz[2],r_V1,r_C1,r_V2,r_C2,r_rpm,tmp.read()); |
hirox | 0:a30d570f6e08 | 207 | updateLCD(); |
hirox | 0:a30d570f6e08 | 208 | |
hirox | 0:a30d570f6e08 | 209 | wait(1); |
hirox | 0:a30d570f6e08 | 210 | } |
hirox | 0:a30d570f6e08 | 211 | |
hirox | 0:a30d570f6e08 | 212 | fclose(fp); |
hirox | 0:a30d570f6e08 | 213 | usbLed = 0; |
hirox | 0:a30d570f6e08 | 214 | } |
hirox | 0:a30d570f6e08 | 215 | } |