First publish Luci
Dependencies: Adafruit_RTCLib SoftI2C SoftSerial mbed
Diff: main.cpp
- Revision:
- 1:6cbf32f31179
- Parent:
- 0:99eede875063
- Child:
- 2:9d4438557eb7
diff -r 99eede875063 -r 6cbf32f31179 main.cpp --- a/main.cpp Tue May 23 11:15:38 2017 +0000 +++ b/main.cpp Thu Jun 08 15:26:56 2017 +0000 @@ -1,7 +1,12 @@ #include "mbed.h" #include "SoftSerial.h" +//#include "SoftI2C.h" //#include "DS1307.h" +#define typeIP 0 +#define typeIL 1 +#define typeIB 2 + //Init debug port Serial DBG(PA_9, PA_10); //tx, rx @@ -10,6 +15,7 @@ //Init rtc //I2C luciI2C (PB_7,PB_6); //sda, scl +//SoftI2C luciI2C (PB_7,PB_6); //sda, scl //RtcDs1307 luciRTC(luciI2C); //Init adc @@ -21,6 +27,24 @@ AnalogIn anIB(PB_0); AnalogIn anTE(PC_2); +//Declared unused adc as digital out +//PA0 used //PA1 used +//PA2 used //PA3 used +//PA4 UN //PA5 SPI UN +//PA6 SPI UN //PA7 SPI UN +//PB0 used //PB1 used +//PC0 Ser used //PC1 Ser used +//PC2 UN //PC3 UN +//PC4 UN //PC5 UN +DigitalOut PA4(PA_4); +DigitalOut PA5(PA_5); +DigitalOut PA6(PA_6); +DigitalOut PA7(PA_7); +DigitalOut PC2(PC_2); +DigitalOut PC3(PC_3); +DigitalOut PC4(PC_4); +DigitalOut PC5(PC_5); + //Sensor value float valVP,valIP; float valVL,valIL; @@ -38,7 +62,7 @@ float adcRead(AnalogIn an) { float s = 0; float sTotal = 0; - int adcMax = 10; + int adcMax = 10000; for(int i=0; i<adcMax; i++) { //Do adcMax readings s = an.read(); @@ -48,15 +72,21 @@ return sTotal/adcMax; } -float currentCalculate(float adcVal) { +float currentCalculate(float adcVal, int type) { float res = 0; adcVal = adcVal - 0.510980; //Remove offset - if(-0.001 < adcVal && adcVal < 0.001) { //No current + if(-0.0025 < adcVal && adcVal < 0.0025) { //No current res = 0; } else { res = (adcVal/13.2)*1000; } + + if(type == typeIP) { + if(res < 0) res = 0; + } else if(type == typeIL) { + res = -1*res; + } return res; } @@ -85,17 +115,26 @@ return bUpdated; } */ - int main() { DBG.baud(115200); XB.baud(9600); // Uncomment to update time + //DBG.printf("update time\r\n"); //if(rtcUpdate(luciRTC, (7*60*60) )) // GMT+7 // DBG.printf("Updated RTC to compiled time\r\n"); //DBG.printf("compiled %s %s\r\n",__DATE__,__TIME__); //DBG.printf("rtc clock is %s\r\n", (luciRTC.isRunning() ? "running" : "halted")); + PA4.write(0); + PA5.write(0); + PA6.write(0); + PA7.write(0); + PC2.write(0); + PC3.write(0); + PC4.write(0); + PC5.write(0); + while(1){ //Read sensor valVP = 0; @@ -110,9 +149,9 @@ valVP = adcRead(anVP)*52.8; valVL = adcRead(anVL)*52.8; valVB = adcRead(anVB)*52.8; - valIP = currentCalculate(adcRead(anIP)); - valIL = currentCalculate(adcRead(anIL)); - valIB = currentCalculate(adcRead(anIB)); + valIP = currentCalculate(adcRead(anIP),typeIP); + valIL = currentCalculate(adcRead(anIL),typeIL); + valIB = currentCalculate(adcRead(anIB),typeIB); valTE = adcRead(anTE); //Read time