WathchDog

Dependencies:   mbed WDT

Committer:
orain3
Date:
Sun Feb 24 08:57:06 2019 +0000
Revision:
3:b082ed02d08c
Parent:
2:d69d10b670c5
Child:
4:cb6587253326
rev1 rs outou

Who changed what in which revision?

UserRevisionLine numberNew contents of line
orain3 0:63c74ee8bffc 1 #include "mbed.h"
orain3 0:63c74ee8bffc 2
orain3 3:b082ed02d08c 3 const int _ID = 4;
orain3 3:b082ed02d08c 4
orain3 3:b082ed02d08c 5 void irqrecv();
orain3 3:b082ed02d08c 6 int rs485_recv(float ph);
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 1:c896654c679b 19 DigitalOut commndout(D4);
orain3 1:c896654c679b 20 DigitalIn in1(D9,PullNone);
orain3 1:c896654c679b 21 DigitalIn in2(D10,PullNone);
orain3 1:c896654c679b 22 DigitalIn kansyutu(D6,PullNone);
orain3 1:c896654c679b 23 DigitalIn commandin(D7,PullNone);
orain3 1:c896654c679b 24 DigitalIn jmp(D2,PullUp);
orain3 0:63c74ee8bffc 25
orain3 0:63c74ee8bffc 26
orain3 3:b082ed02d08c 27 #define bufsize 64
orain3 3:b082ed02d08c 28 char RxBuff[bufsize+1];
orain3 3:b082ed02d08c 29 int RxPtr;
orain3 3:b082ed02d08c 30 int RdPtr;
orain3 3:b082ed02d08c 31
orain3 3:b082ed02d08c 32 char rsgetc() {
orain3 3:b082ed02d08c 33 // pc.printf("%c",RxBuff[RdPtr]);
orain3 3:b082ed02d08c 34
orain3 3:b082ed02d08c 35 RdPtr++;
orain3 3:b082ed02d08c 36 if(RdPtr >= bufsize-1) RdPtr = 0;
orain3 3:b082ed02d08c 37 return(RxBuff[RdPtr]);
orain3 3:b082ed02d08c 38 }
orain3 3:b082ed02d08c 39
orain3 3:b082ed02d08c 40 int rslen() {
orain3 3:b082ed02d08c 41 // if (RxPtr < RdPtr) {
orain3 3:b082ed02d08c 42 // return(bufsize + RxPtr - RdPtr);
orain3 3:b082ed02d08c 43 // } else {
orain3 3:b082ed02d08c 44 // return(RxPtr - RdPtr);
orain3 3:b082ed02d08c 45 // }
orain3 3:b082ed02d08c 46 return(RxPtr - RdPtr);
orain3 3:b082ed02d08c 47 }
orain3 3:b082ed02d08c 48
orain3 3:b082ed02d08c 49 void irqrecv() {
orain3 3:b082ed02d08c 50 if (modem.readable()) {
orain3 3:b082ed02d08c 51 if (RxPtr <= bufsize - 1) {
orain3 3:b082ed02d08c 52 RxBuff[++RxPtr] = modem.getc();
orain3 3:b082ed02d08c 53 } else {
orain3 3:b082ed02d08c 54 RxPtr = 0;
orain3 3:b082ed02d08c 55 RxBuff[RxPtr] = modem.getc();
orain3 3:b082ed02d08c 56 }
orain3 3:b082ed02d08c 57 }
orain3 3:b082ed02d08c 58 }
orain3 3:b082ed02d08c 59
orain3 3:b082ed02d08c 60 unsigned char buf485[32];
orain3 3:b082ed02d08c 61 int cnt485;
orain3 3:b082ed02d08c 62
orain3 0:63c74ee8bffc 63 int main()
orain3 0:63c74ee8bffc 64 {
orain3 1:c896654c679b 65 float sum1,sum2,sum3,sum4;
orain3 1:c896654c679b 66 float ad1,ad2,ad3,ad4;
orain3 3:b082ed02d08c 67 static float temp;//,turb,s;
orain3 0:63c74ee8bffc 68 int i,cnt;
orain3 0:63c74ee8bffc 69
orain3 1:c896654c679b 70 modem.baud(38400);
orain3 1:c896654c679b 71 modem.format(8,Serial::None,1);
orain3 3:b082ed02d08c 72 RxPtr = 0;
orain3 3:b082ed02d08c 73 RdPtr = 0;
orain3 3:b082ed02d08c 74 modem.attach(irqrecv,RawSerial::RxIrq);
orain3 3:b082ed02d08c 75 cnt485 = 0;
orain3 3:b082ed02d08c 76 buf485[0] = '\0';
orain3 0:63c74ee8bffc 77
orain3 0:63c74ee8bffc 78 pc.printf("\nSTM32 ADC internal channels reading example\r\n");
orain3 0:63c74ee8bffc 79 temp = 0;
orain3 3:b082ed02d08c 80 // turb = 0;
orain3 3:b082ed02d08c 81 // s = 0;
orain3 1:c896654c679b 82
orain3 1:c896654c679b 83 sokutei = 1;
orain3 1:c896654c679b 84
orain3 0:63c74ee8bffc 85 while(1) {
orain3 1:c896654c679b 86
orain3 1:c896654c679b 87 if (jmp) sokutei = 1;
orain3 1:c896654c679b 88 else sokutei = 0;
orain3 1:c896654c679b 89
orain3 0:63c74ee8bffc 90 sum1 = 0;
orain3 0:63c74ee8bffc 91 sum2 = 0;
orain3 1:c896654c679b 92 sum3 = 0;
orain3 1:c896654c679b 93 sum4 = 0;
orain3 0:63c74ee8bffc 94 cnt = 0;
orain3 3:b082ed02d08c 95 for (i=0;i<1000;i++) {
orain3 3:b082ed02d08c 96 rs485_recv(temp);
orain3 3:b082ed02d08c 97
orain3 0:63c74ee8bffc 98 sum1 += adc_1.read();
orain3 0:63c74ee8bffc 99 sum2 += adc_2.read();
orain3 1:c896654c679b 100 sum3 += adc_3.read();
orain3 1:c896654c679b 101 sum4 += adc_4.read();
orain3 0:63c74ee8bffc 102 cnt += 1;
orain3 0:63c74ee8bffc 103 wait(0.001);
orain3 0:63c74ee8bffc 104
orain3 2:d69d10b670c5 105 // if (modem.readable()) {
orain3 2:d69d10b670c5 106 // pc.printf("%c\r",modem.getc());
orain3 2:d69d10b670c5 107 // wait(0.01);
orain3 2:d69d10b670c5 108 // modem.printf("=04,PVAL,%01.04f,0,\r",temp);
orain3 2:d69d10b670c5 109 // }
orain3 0:63c74ee8bffc 110 }
orain3 0:63c74ee8bffc 111 if(cnt != 0) {
orain3 0:63c74ee8bffc 112 ad1 = sum1/cnt;
orain3 0:63c74ee8bffc 113 ad2 = sum2/cnt;
orain3 1:c896654c679b 114 ad3 = sum3/cnt;
orain3 1:c896654c679b 115 ad4 = sum4/cnt;
orain3 0:63c74ee8bffc 116 } else {
orain3 0:63c74ee8bffc 117 ad1 = adc_1.read();
orain3 0:63c74ee8bffc 118 ad2 = adc_2.read();
orain3 1:c896654c679b 119 ad3 = adc_3.read();
orain3 1:c896654c679b 120 ad4 = adc_4.read();
orain3 0:63c74ee8bffc 121 }
orain3 0:63c74ee8bffc 122
orain3 3:b082ed02d08c 123 if (ad1 < 0.2435L) ad1 = 0.2435L;
orain3 3:b082ed02d08c 124 if (ad1 > 0.8085L) ad1 = 0.8085L;
orain3 3:b082ed02d08c 125
orain3 3:b082ed02d08c 126 temp = (double)(ad1 - 0.2435L) * 60.0L / (0.8085L - 0.2425L) - 10.0L;
orain3 3:b082ed02d08c 127 // pc.printf("ADC = %f %f %f %f %f \r\n", ad1,ad2,ad3,ad4,temp);
orain3 3:b082ed02d08c 128 // s = 0;
orain3 0:63c74ee8bffc 129
orain3 0:63c74ee8bffc 130 }
orain3 0:63c74ee8bffc 131 }
orain3 3:b082ed02d08c 132
orain3 3:b082ed02d08c 133
orain3 3:b082ed02d08c 134 int rs485_recv(float ph) {
orain3 3:b082ed02d08c 135 char chr;
orain3 3:b082ed02d08c 136 char idbuf[8];
orain3 3:b082ed02d08c 137
orain3 3:b082ed02d08c 138 if (rslen()) {
orain3 3:b082ed02d08c 139 // pc.printf("%03d\r\n",rslen());
orain3 3:b082ed02d08c 140 // wait(0.01);
orain3 3:b082ed02d08c 141 // while(rslen()) rsgetc();
orain3 3:b082ed02d08c 142
orain3 3:b082ed02d08c 143 chr = rsgetc();
orain3 3:b082ed02d08c 144 buf485[cnt485++] = chr;
orain3 3:b082ed02d08c 145 buf485[cnt485] = '\0';
orain3 3:b082ed02d08c 146 if (chr == '?') cnt485 = 0;
orain3 3:b082ed02d08c 147
orain3 3:b082ed02d08c 148 // if (chr == '?') pc.printf("?\r\n");
orain3 3:b082ed02d08c 149 // if (chr == '0') pc.printf("0\r\n");
orain3 3:b082ed02d08c 150
orain3 3:b082ed02d08c 151
orain3 3:b082ed02d08c 152
orain3 3:b082ed02d08c 153 if (chr == 0x0d) {
orain3 3:b082ed02d08c 154 // pc.printf("Recv CR:%s\r\n",buf485);
orain3 3:b082ed02d08c 155 sprintf(idbuf,"%02d",_ID);
orain3 3:b082ed02d08c 156 // idbuf[0] = '0';
orain3 3:b082ed02d08c 157 // idbuf[1] = '4';
orain3 3:b082ed02d08c 158 // idbuf[2] = '\0';
orain3 3:b082ed02d08c 159 // if((idbuf[0] == buf485[1]) && (idbuf[1] == buf485[2])) {
orain3 3:b082ed02d08c 160
orain3 3:b082ed02d08c 161 if((idbuf[0] == buf485[0]) && (idbuf[1] == buf485[1]) && (',' == buf485[2]) && ('P' == buf485[3])) {
orain3 3:b082ed02d08c 162 // answer
orain3 3:b082ed02d08c 163 // konnkaiha modemunanode huyou wait(0.02);
orain3 3:b082ed02d08c 164 // RE = 1;//0;
orain3 3:b082ed02d08c 165 // DE = 1;
orain3 3:b082ed02d08c 166 // pc.printf("Ans:=%c%c,PVAL,%01.04f,0\r\n", 0x30 | idbuf[0],0x30 | idbuf[1],ph);
orain3 3:b082ed02d08c 167 modem.printf("=%c%c,PVAL,%01.04f,0\r", 0x30 | idbuf[0],0x30 | idbuf[1],ph);
orain3 3:b082ed02d08c 168 // konkaiha modemunanode huyou wait(0.01);
orain3 3:b082ed02d08c 169 // RE = 0;//1;
orain3 3:b082ed02d08c 170 // DE = 0;
orain3 3:b082ed02d08c 171 // while(rslen()) rsgetc();
orain3 3:b082ed02d08c 172 RxPtr = 0;
orain3 3:b082ed02d08c 173 RdPtr = 0;
orain3 3:b082ed02d08c 174 RxBuff[0] = '\0';
orain3 3:b082ed02d08c 175 buf485[0] = '\0'; // clear
orain3 3:b082ed02d08c 176 cnt485 = 0;
orain3 3:b082ed02d08c 177 }
orain3 3:b082ed02d08c 178 }
orain3 3:b082ed02d08c 179 }
orain3 3:b082ed02d08c 180 return(0);
orain3 3:b082ed02d08c 181 }
orain3 3:b082ed02d08c 182
orain3 3:b082ed02d08c 183