Kazuhiro Hirasawa
/
STM32_180SENSOR_ADC4_RS_Ver2_CommCLD
WathchDog
main.cpp@5:efffadf6d0e0, 2019-08-15 (annotated)
- Committer:
- orain3
- Date:
- Thu Aug 15 14:09:24 2019 +0000
- Revision:
- 5:efffadf6d0e0
- Parent:
- 4:cb6587253326
- Child:
- 6:1948d19f854f
20190815
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
orain3 | 0:63c74ee8bffc | 1 | #include "mbed.h" |
orain3 | 0:63c74ee8bffc | 2 | |
orain3 | 4:cb6587253326 | 3 | //const int _ID = 4; |
orain3 | 3:b082ed02d08c | 4 | |
orain3 | 3:b082ed02d08c | 5 | void irqrecv(); |
orain3 | 4:cb6587253326 | 6 | int rs485_recv(float ,float ,float ,float ,float); |
orain3 | 3:b082ed02d08c | 7 | char rsgetc(); |
orain3 | 3:b082ed02d08c | 8 | int rslen(); |
orain3 | 3:b082ed02d08c | 9 | |
orain3 | 0:63c74ee8bffc | 10 | Serial pc(SERIAL_TX,SERIAL_RX); |
orain3 | 1:c896654c679b | 11 | Serial modem(D1,D0); |
orain3 | 1:c896654c679b | 12 | AnalogIn adc_1(A0); |
orain3 | 1:c896654c679b | 13 | AnalogIn adc_2(A1); |
orain3 | 1:c896654c679b | 14 | AnalogIn adc_3(A2); |
orain3 | 1:c896654c679b | 15 | AnalogIn adc_4(A3); |
orain3 | 0:63c74ee8bffc | 16 | |
orain3 | 0:63c74ee8bffc | 17 | DigitalOut led(LED1); |
orain3 | 1:c896654c679b | 18 | DigitalOut sokutei(D3); |
orain3 | 4:cb6587253326 | 19 | DigitalOut pos1(D4); |
orain3 | 4:cb6587253326 | 20 | DigitalOut pos2(D5); |
orain3 | 1:c896654c679b | 21 | DigitalIn in1(D9,PullNone); |
orain3 | 1:c896654c679b | 22 | DigitalIn in2(D10,PullNone); |
orain3 | 1:c896654c679b | 23 | DigitalIn kansyutu(D6,PullNone); |
orain3 | 1:c896654c679b | 24 | DigitalIn commandin(D7,PullNone); |
orain3 | 1:c896654c679b | 25 | DigitalIn jmp(D2,PullUp); |
orain3 | 4:cb6587253326 | 26 | DigitalIn pulse1(D9,PullNone); |
orain3 | 4:cb6587253326 | 27 | DigitalIn pulse2(D10,PullNone); |
orain3 | 4:cb6587253326 | 28 | |
orain3 | 4:cb6587253326 | 29 | Timer pulse_watcher; |
orain3 | 0:63c74ee8bffc | 30 | |
orain3 | 0:63c74ee8bffc | 31 | |
orain3 | 4:cb6587253326 | 32 | #define bufsize 1024 |
orain3 | 3:b082ed02d08c | 33 | char RxBuff[bufsize+1]; |
orain3 | 3:b082ed02d08c | 34 | int RxPtr; |
orain3 | 3:b082ed02d08c | 35 | int RdPtr; |
orain3 | 3:b082ed02d08c | 36 | |
orain3 | 4:cb6587253326 | 37 | int data[40]; |
orain3 | 4:cb6587253326 | 38 | |
orain3 | 4:cb6587253326 | 39 | |
orain3 | 3:b082ed02d08c | 40 | char rsgetc() { |
orain3 | 3:b082ed02d08c | 41 | // pc.printf("%c",RxBuff[RdPtr]); |
orain3 | 3:b082ed02d08c | 42 | |
orain3 | 3:b082ed02d08c | 43 | RdPtr++; |
orain3 | 3:b082ed02d08c | 44 | if(RdPtr >= bufsize-1) RdPtr = 0; |
orain3 | 3:b082ed02d08c | 45 | return(RxBuff[RdPtr]); |
orain3 | 3:b082ed02d08c | 46 | } |
orain3 | 3:b082ed02d08c | 47 | |
orain3 | 3:b082ed02d08c | 48 | int rslen() { |
orain3 | 3:b082ed02d08c | 49 | // if (RxPtr < RdPtr) { |
orain3 | 3:b082ed02d08c | 50 | // return(bufsize + RxPtr - RdPtr); |
orain3 | 3:b082ed02d08c | 51 | // } else { |
orain3 | 3:b082ed02d08c | 52 | // return(RxPtr - RdPtr); |
orain3 | 3:b082ed02d08c | 53 | // } |
orain3 | 3:b082ed02d08c | 54 | return(RxPtr - RdPtr); |
orain3 | 3:b082ed02d08c | 55 | } |
orain3 | 3:b082ed02d08c | 56 | |
orain3 | 3:b082ed02d08c | 57 | void irqrecv() { |
orain3 | 3:b082ed02d08c | 58 | if (modem.readable()) { |
orain3 | 3:b082ed02d08c | 59 | if (RxPtr <= bufsize - 1) { |
orain3 | 3:b082ed02d08c | 60 | RxBuff[++RxPtr] = modem.getc(); |
orain3 | 3:b082ed02d08c | 61 | } else { |
orain3 | 3:b082ed02d08c | 62 | RxPtr = 0; |
orain3 | 3:b082ed02d08c | 63 | RxBuff[RxPtr] = modem.getc(); |
orain3 | 3:b082ed02d08c | 64 | } |
orain3 | 3:b082ed02d08c | 65 | } |
orain3 | 3:b082ed02d08c | 66 | } |
orain3 | 3:b082ed02d08c | 67 | |
orain3 | 4:cb6587253326 | 68 | unsigned char buf485[512]; |
orain3 | 3:b082ed02d08c | 69 | int cnt485; |
orain3 | 3:b082ed02d08c | 70 | |
orain3 | 0:63c74ee8bffc | 71 | int main() |
orain3 | 0:63c74ee8bffc | 72 | { |
orain3 | 1:c896654c679b | 73 | float sum1,sum2,sum3,sum4; |
orain3 | 1:c896654c679b | 74 | float ad1,ad2,ad3,ad4; |
orain3 | 3:b082ed02d08c | 75 | static float temp;//,turb,s; |
orain3 | 0:63c74ee8bffc | 76 | int i,cnt; |
orain3 | 4:cb6587253326 | 77 | int pulse; |
orain3 | 0:63c74ee8bffc | 78 | |
orain3 | 1:c896654c679b | 79 | modem.baud(38400); |
orain3 | 1:c896654c679b | 80 | modem.format(8,Serial::None,1); |
orain3 | 3:b082ed02d08c | 81 | RxPtr = 0; |
orain3 | 3:b082ed02d08c | 82 | RdPtr = 0; |
orain3 | 3:b082ed02d08c | 83 | modem.attach(irqrecv,RawSerial::RxIrq); |
orain3 | 3:b082ed02d08c | 84 | cnt485 = 0; |
orain3 | 3:b082ed02d08c | 85 | buf485[0] = '\0'; |
orain3 | 0:63c74ee8bffc | 86 | |
orain3 | 0:63c74ee8bffc | 87 | pc.printf("\nSTM32 ADC internal channels reading example\r\n"); |
orain3 | 4:cb6587253326 | 88 | pos1 = 1; |
orain3 | 4:cb6587253326 | 89 | pos2 = 1; |
orain3 | 4:cb6587253326 | 90 | |
orain3 | 0:63c74ee8bffc | 91 | temp = 0; |
orain3 | 3:b082ed02d08c | 92 | // turb = 0; |
orain3 | 3:b082ed02d08c | 93 | // s = 0; |
orain3 | 1:c896654c679b | 94 | sokutei = 1; |
orain3 | 4:cb6587253326 | 95 | pulse_watcher.start(); |
orain3 | 4:cb6587253326 | 96 | pulse = pulse1 || pulse2; |
orain3 | 4:cb6587253326 | 97 | |
orain3 | 4:cb6587253326 | 98 | for (i = 0;i<40;i++) data[i] = 0; |
orain3 | 4:cb6587253326 | 99 | |
orain3 | 1:c896654c679b | 100 | |
orain3 | 0:63c74ee8bffc | 101 | while(1) { |
orain3 | 4:cb6587253326 | 102 | if (pulse_watcher.read() > 100) pulse_watcher.reset(); |
orain3 | 1:c896654c679b | 103 | |
orain3 | 4:cb6587253326 | 104 | // if (jmp) sokutei = 1; |
orain3 | 4:cb6587253326 | 105 | // else sokutei = 0; |
orain3 | 4:cb6587253326 | 106 | |
orain3 | 4:cb6587253326 | 107 | // if (jmp) { |
orain3 | 4:cb6587253326 | 108 | // pos1 = 1; |
orain3 | 4:cb6587253326 | 109 | // pos2 = 1; |
orain3 | 4:cb6587253326 | 110 | // } else { |
orain3 | 4:cb6587253326 | 111 | // pos1 = 0; |
orain3 | 4:cb6587253326 | 112 | // pos2 = 0; |
orain3 | 4:cb6587253326 | 113 | // } |
orain3 | 1:c896654c679b | 114 | |
orain3 | 0:63c74ee8bffc | 115 | sum1 = 0; |
orain3 | 0:63c74ee8bffc | 116 | sum2 = 0; |
orain3 | 1:c896654c679b | 117 | sum3 = 0; |
orain3 | 1:c896654c679b | 118 | sum4 = 0; |
orain3 | 0:63c74ee8bffc | 119 | cnt = 0; |
orain3 | 3:b082ed02d08c | 120 | for (i=0;i<1000;i++) { |
orain3 | 4:cb6587253326 | 121 | rs485_recv(temp,ad1,ad2,ad3,ad4); |
orain3 | 3:b082ed02d08c | 122 | |
orain3 | 0:63c74ee8bffc | 123 | sum1 += adc_1.read(); |
orain3 | 0:63c74ee8bffc | 124 | sum2 += adc_2.read(); |
orain3 | 1:c896654c679b | 125 | sum3 += adc_3.read(); |
orain3 | 1:c896654c679b | 126 | sum4 += adc_4.read(); |
orain3 | 0:63c74ee8bffc | 127 | cnt += 1; |
orain3 | 0:63c74ee8bffc | 128 | wait(0.001); |
orain3 | 0:63c74ee8bffc | 129 | |
orain3 | 2:d69d10b670c5 | 130 | // if (modem.readable()) { |
orain3 | 2:d69d10b670c5 | 131 | // pc.printf("%c\r",modem.getc()); |
orain3 | 2:d69d10b670c5 | 132 | // wait(0.01); |
orain3 | 2:d69d10b670c5 | 133 | // modem.printf("=04,PVAL,%01.04f,0,\r",temp); |
orain3 | 2:d69d10b670c5 | 134 | // } |
orain3 | 4:cb6587253326 | 135 | // pulse watch |
orain3 | 4:cb6587253326 | 136 | if ( (pulse1 || pulse2) != pulse ) { |
orain3 | 4:cb6587253326 | 137 | pulse_watcher.reset(); |
orain3 | 4:cb6587253326 | 138 | pos1 = 0; |
orain3 | 4:cb6587253326 | 139 | pos2 = 0; |
orain3 | 4:cb6587253326 | 140 | } |
orain3 | 4:cb6587253326 | 141 | pulse = pulse1 || pulse2; |
orain3 | 4:cb6587253326 | 142 | if ( pulse_watcher.read() > 10) { |
orain3 | 4:cb6587253326 | 143 | pos1 = 1; |
orain3 | 4:cb6587253326 | 144 | pos2 = 1; |
orain3 | 4:cb6587253326 | 145 | } |
orain3 | 4:cb6587253326 | 146 | |
orain3 | 4:cb6587253326 | 147 | // pulsewatch |
orain3 | 0:63c74ee8bffc | 148 | } |
orain3 | 0:63c74ee8bffc | 149 | if(cnt != 0) { |
orain3 | 0:63c74ee8bffc | 150 | ad1 = sum1/cnt; |
orain3 | 0:63c74ee8bffc | 151 | ad2 = sum2/cnt; |
orain3 | 1:c896654c679b | 152 | ad3 = sum3/cnt; |
orain3 | 1:c896654c679b | 153 | ad4 = sum4/cnt; |
orain3 | 0:63c74ee8bffc | 154 | } else { |
orain3 | 0:63c74ee8bffc | 155 | ad1 = adc_1.read(); |
orain3 | 0:63c74ee8bffc | 156 | ad2 = adc_2.read(); |
orain3 | 1:c896654c679b | 157 | ad3 = adc_3.read(); |
orain3 | 1:c896654c679b | 158 | ad4 = adc_4.read(); |
orain3 | 0:63c74ee8bffc | 159 | } |
orain3 | 0:63c74ee8bffc | 160 | |
orain3 | 3:b082ed02d08c | 161 | if (ad1 < 0.2435L) ad1 = 0.2435L; |
orain3 | 3:b082ed02d08c | 162 | if (ad1 > 0.8085L) ad1 = 0.8085L; |
orain3 | 3:b082ed02d08c | 163 | |
orain3 | 3:b082ed02d08c | 164 | temp = (double)(ad1 - 0.2435L) * 60.0L / (0.8085L - 0.2425L) - 10.0L; |
orain3 | 3:b082ed02d08c | 165 | // pc.printf("ADC = %f %f %f %f %f \r\n", ad1,ad2,ad3,ad4,temp); |
orain3 | 3:b082ed02d08c | 166 | // s = 0; |
orain3 | 0:63c74ee8bffc | 167 | |
orain3 | 4:cb6587253326 | 168 | // data set |
orain3 | 4:cb6587253326 | 169 | data[8] = ad1 * 0xffff; |
orain3 | 4:cb6587253326 | 170 | data[9] = ad2 * 0xffff; |
orain3 | 4:cb6587253326 | 171 | data[10] = ad3 * 0xffff; |
orain3 | 4:cb6587253326 | 172 | data[12] = ad4 * 0xffff; |
orain3 | 0:63c74ee8bffc | 173 | } |
orain3 | 0:63c74ee8bffc | 174 | } |
orain3 | 3:b082ed02d08c | 175 | |
orain3 | 3:b082ed02d08c | 176 | |
orain3 | 4:cb6587253326 | 177 | int rs485_recv(float temp,float ad1,float ad2,float ad3,float ad4) { |
orain3 | 3:b082ed02d08c | 178 | char chr; |
orain3 | 4:cb6587253326 | 179 | // char idbuf[8]; |
orain3 | 4:cb6587253326 | 180 | char sendbuf[512]; |
orain3 | 4:cb6587253326 | 181 | // int i; |
orain3 | 4:cb6587253326 | 182 | |
orain3 | 3:b082ed02d08c | 183 | if (rslen()) { |
orain3 | 3:b082ed02d08c | 184 | |
orain3 | 3:b082ed02d08c | 185 | chr = rsgetc(); |
orain3 | 3:b082ed02d08c | 186 | buf485[cnt485++] = chr; |
orain3 | 3:b082ed02d08c | 187 | buf485[cnt485] = '\0'; |
orain3 | 3:b082ed02d08c | 188 | if (chr == '?') cnt485 = 0; |
orain3 | 3:b082ed02d08c | 189 | |
orain3 | 4:cb6587253326 | 190 | if (chr == 0x0a) { |
orain3 | 5:efffadf6d0e0 | 191 | // pc.printf("CR\r\n"); |
orain3 | 4:cb6587253326 | 192 | |
orain3 | 4:cb6587253326 | 193 | if( ('C' == buf485[0]) ) { |
orain3 | 4:cb6587253326 | 194 | if (kansyutu) data[0] = 1; |
orain3 | 4:cb6587253326 | 195 | else data[0] = 0; |
orain3 | 3:b082ed02d08c | 196 | |
orain3 | 4:cb6587253326 | 197 | // sokutei on/off |
orain3 | 5:efffadf6d0e0 | 198 | if( buf485[2] & 0x05 ) { |
orain3 | 4:cb6587253326 | 199 | sokutei = 1; |
orain3 | 4:cb6587253326 | 200 | data[0] = data[0] | 64 | 128; |
orain3 | 4:cb6587253326 | 201 | } else { |
orain3 | 4:cb6587253326 | 202 | sokutei = 0; |
orain3 | 4:cb6587253326 | 203 | } |
orain3 | 5:efffadf6d0e0 | 204 | if( buf485[2] & 0x0a ) { |
orain3 | 4:cb6587253326 | 205 | wait(0.1); |
orain3 | 5:efffadf6d0e0 | 206 | modem.printf("?00,WIPE,1,\r"); |
orain3 | 4:cb6587253326 | 207 | } else { |
orain3 | 4:cb6587253326 | 208 | // answer |
orain3 | 4:cb6587253326 | 209 | sprintf(sendbuf,"=%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x\r\n", |
orain3 | 4:cb6587253326 | 210 | data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9], |
orain3 | 4:cb6587253326 | 211 | data[10],data[11],data[12],data[13],data[14],data[15],data[16],data[17],data[18],data[19], |
orain3 | 4:cb6587253326 | 212 | data[20],data[21],data[22],data[23],data[24],data[25],data[26],data[27],data[28],data[29], |
orain3 | 4:cb6587253326 | 213 | data[30],data[31],data[32],data[33],data[34],data[35],data[36],data[37],data[38],data[39] |
orain3 | 4:cb6587253326 | 214 | ); |
orain3 | 4:cb6587253326 | 215 | //modem.printf("=%c%c,PVAL,%01.04f,%01.04f,%01.04f,%01.04f,%01.04f,%01d\r", 0x30 | idbuf[0],0x30 | idbuf[1],temp,ad1,ad2,ad3,ad4,kan); |
orain3 | 4:cb6587253326 | 216 | |
orain3 | 4:cb6587253326 | 217 | wait(0.1); |
orain3 | 4:cb6587253326 | 218 | modem.printf("%s",sendbuf); |
orain3 | 4:cb6587253326 | 219 | pc.printf("%s",sendbuf); |
orain3 | 4:cb6587253326 | 220 | } |
orain3 | 3:b082ed02d08c | 221 | } |
orain3 | 4:cb6587253326 | 222 | RxPtr = 0; |
orain3 | 4:cb6587253326 | 223 | RdPtr = 0; |
orain3 | 4:cb6587253326 | 224 | RxBuff[0] = '\0'; |
orain3 | 4:cb6587253326 | 225 | buf485[0] = '\0'; // clear |
orain3 | 4:cb6587253326 | 226 | cnt485 = 0; |
orain3 | 3:b082ed02d08c | 227 | } |
orain3 | 3:b082ed02d08c | 228 | } |
orain3 | 3:b082ed02d08c | 229 | return(0); |
orain3 | 3:b082ed02d08c | 230 | } |
orain3 | 3:b082ed02d08c | 231 | |
orain3 | 3:b082ed02d08c | 232 |