Kazuhiro Hirasawa
/
STM32_180SENSOR_ADC4_RS_Ver2_CommCLD
WathchDog
main.cpp
- Committer:
- orain3
- Date:
- 2019-02-24
- Revision:
- 3:b082ed02d08c
- Parent:
- 2:d69d10b670c5
- Child:
- 4:cb6587253326
File content as of revision 3:b082ed02d08c:
#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); AnalogIn adc_2(A1); AnalogIn adc_3(A2); AnalogIn adc_4(A3); DigitalOut led(LED1); DigitalOut sokutei(D3); DigitalOut commndout(D4); DigitalIn in1(D9,PullNone); DigitalIn in2(D10,PullNone); DigitalIn kansyutu(D6,PullNone); DigitalIn commandin(D7,PullNone); 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; 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; sokutei = 1; while(1) { if (jmp) sokutei = 1; else sokutei = 0; sum1 = 0; sum2 = 0; sum3 = 0; sum4 = 0; cnt = 0; for (i=0;i<1000;i++) { rs485_recv(temp); sum1 += adc_1.read(); sum2 += adc_2.read(); sum3 += adc_3.read(); sum4 += adc_4.read(); cnt += 1; wait(0.001); // if (modem.readable()) { // pc.printf("%c\r",modem.getc()); // wait(0.01); // modem.printf("=04,PVAL,%01.04f,0,\r",temp); // } } if(cnt != 0) { ad1 = sum1/cnt; ad2 = sum2/cnt; ad3 = sum3/cnt; ad4 = sum4/cnt; } else { ad1 = adc_1.read(); ad2 = adc_2.read(); ad3 = adc_3.read(); ad4 = adc_4.read(); } 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); }