Kazuhiro Hirasawa
/
STM32_180SENSOR_ADC4_RS_Ver2_CommCLD
WathchDog
Diff: main.cpp
- Revision:
- 3:b082ed02d08c
- Parent:
- 2:d69d10b670c5
- Child:
- 4:cb6587253326
diff -r d69d10b670c5 -r b082ed02d08c main.cpp --- a/main.cpp Sat Feb 23 09:11:23 2019 +0000 +++ b/main.cpp Sun Feb 24 08:57:06 2019 +0000 @@ -1,5 +1,12 @@ #include "mbed.h" +const int _ID = 4; + +void irqrecv(); +int rs485_recv(float ph); +char rsgetc(); +int rslen(); + Serial pc(SERIAL_TX,SERIAL_RX); Serial modem(D1,D0); AnalogIn adc_1(A0); @@ -17,20 +24,61 @@ DigitalIn jmp(D2,PullUp); +#define bufsize 64 +char RxBuff[bufsize+1]; +int RxPtr; +int RdPtr; + +char rsgetc() { +// pc.printf("%c",RxBuff[RdPtr]); + + RdPtr++; + if(RdPtr >= bufsize-1) RdPtr = 0; + return(RxBuff[RdPtr]); +} + +int rslen() { +// if (RxPtr < RdPtr) { +// return(bufsize + RxPtr - RdPtr); +// } else { +// return(RxPtr - RdPtr); +// } + return(RxPtr - RdPtr); +} + +void irqrecv() { + if (modem.readable()) { + if (RxPtr <= bufsize - 1) { + RxBuff[++RxPtr] = modem.getc(); + } else { + RxPtr = 0; + RxBuff[RxPtr] = modem.getc(); + } + } +} + +unsigned char buf485[32]; +int cnt485; + int main() { float sum1,sum2,sum3,sum4; float ad1,ad2,ad3,ad4; - float temp,turb,s; + static float temp;//,turb,s; int i,cnt; modem.baud(38400); modem.format(8,Serial::None,1); + RxPtr = 0; + RdPtr = 0; + modem.attach(irqrecv,RawSerial::RxIrq); + cnt485 = 0; + buf485[0] = '\0'; pc.printf("\nSTM32 ADC internal channels reading example\r\n"); temp = 0; - turb = 0; - s = 0; +// turb = 0; +// s = 0; sokutei = 1; @@ -44,7 +92,9 @@ sum3 = 0; sum4 = 0; cnt = 0; - for (i=0;i<10;i++) { + for (i=0;i<1000;i++) { + rs485_recv(temp); + sum1 += adc_1.read(); sum2 += adc_2.read(); sum3 += adc_3.read(); @@ -70,10 +120,64 @@ ad4 = adc_4.read(); } - pc.printf("ADC = %f %f %f %f \r\n", ad1,ad2,ad3,ad4); - temp = ad1*66-30+0.3; - turb = 2.1*((ad2-0.166)/0.6642*1000+25.5); - s = 0; + if (ad1 < 0.2435L) ad1 = 0.2435L; + if (ad1 > 0.8085L) ad1 = 0.8085L; + + temp = (double)(ad1 - 0.2435L) * 60.0L / (0.8085L - 0.2425L) - 10.0L; +// pc.printf("ADC = %f %f %f %f %f \r\n", ad1,ad2,ad3,ad4,temp); +// s = 0; } } + + +int rs485_recv(float ph) { + char chr; + char idbuf[8]; + + if (rslen()) { +// pc.printf("%03d\r\n",rslen()); +// wait(0.01); +// while(rslen()) rsgetc(); + + chr = rsgetc(); + buf485[cnt485++] = chr; + buf485[cnt485] = '\0'; + if (chr == '?') cnt485 = 0; + +// if (chr == '?') pc.printf("?\r\n"); +// if (chr == '0') pc.printf("0\r\n"); + + + + if (chr == 0x0d) { +// pc.printf("Recv CR:%s\r\n",buf485); + sprintf(idbuf,"%02d",_ID); +// idbuf[0] = '0'; +// idbuf[1] = '4'; +// idbuf[2] = '\0'; +// if((idbuf[0] == buf485[1]) && (idbuf[1] == buf485[2])) { + + if((idbuf[0] == buf485[0]) && (idbuf[1] == buf485[1]) && (',' == buf485[2]) && ('P' == buf485[3])) { + // answer +// konnkaiha modemunanode huyou wait(0.02); +// RE = 1;//0; +// DE = 1; +// pc.printf("Ans:=%c%c,PVAL,%01.04f,0\r\n", 0x30 | idbuf[0],0x30 | idbuf[1],ph); + modem.printf("=%c%c,PVAL,%01.04f,0\r", 0x30 | idbuf[0],0x30 | idbuf[1],ph); +// konkaiha modemunanode huyou wait(0.01); +// RE = 0;//1; +// DE = 0; +// while(rslen()) rsgetc(); + RxPtr = 0; + RdPtr = 0; + RxBuff[0] = '\0'; + buf485[0] = '\0'; // clear + cnt485 = 0; + } + } + } + return(0); +} + +