![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
aa
main.cpp@0:0311bb0b38f7, 2019-09-09 (annotated)
- Committer:
- M_souta
- Date:
- Mon Sep 09 00:16:13 2019 +0000
- Revision:
- 0:0311bb0b38f7
- Child:
- 1:fe3f80c4beeb
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
M_souta | 0:0311bb0b38f7 | 1 | #include "mbed.h" |
M_souta | 0:0311bb0b38f7 | 2 | #include "RingBuffer.h" |
M_souta | 0:0311bb0b38f7 | 3 | #include "SO1602A.h" |
M_souta | 0:0311bb0b38f7 | 4 | |
M_souta | 0:0311bb0b38f7 | 5 | //********** pin define **********// |
M_souta | 0:0311bb0b38f7 | 6 | #define SELECT1_0 PB_4 |
M_souta | 0:0311bb0b38f7 | 7 | #define SELECT1_1 PB_5 |
M_souta | 0:0311bb0b38f7 | 8 | #define SELECT1_2 PB_3 |
M_souta | 0:0311bb0b38f7 | 9 | #define SELECT1_3 PB_1 |
M_souta | 0:0311bb0b38f7 | 10 | #define SELECT2_0 PF_0 |
M_souta | 0:0311bb0b38f7 | 11 | #define SELECT2_1 PF_1 |
M_souta | 0:0311bb0b38f7 | 12 | #define SELECT2_2 PA_8 |
M_souta | 0:0311bb0b38f7 | 13 | #define SELECT2_3 PA_11 |
M_souta | 0:0311bb0b38f7 | 14 | #define SELECT3_0 PA_4 |
M_souta | 0:0311bb0b38f7 | 15 | #define SELECT3_1 PA_5 |
M_souta | 0:0311bb0b38f7 | 16 | #define SELECT3_2 PA_7 |
M_souta | 0:0311bb0b38f7 | 17 | #define SELECT3_3 PA_6 |
M_souta | 0:0311bb0b38f7 | 18 | #define Z0 PA_0 |
M_souta | 0:0311bb0b38f7 | 19 | #define Z1 PA_1 |
M_souta | 0:0311bb0b38f7 | 20 | #define Z2 PA_3 |
M_souta | 0:0311bb0b38f7 | 21 | #define SDA PB_7 |
M_souta | 0:0311bb0b38f7 | 22 | #define SCL PB_6 |
M_souta | 0:0311bb0b38f7 | 23 | #define UART_SE PA_2 |
M_souta | 0:0311bb0b38f7 | 24 | #define UARTTX PA_9 |
M_souta | 0:0311bb0b38f7 | 25 | #define UARTRX PA_10 |
M_souta | 0:0311bb0b38f7 | 26 | //********** pin define **********// |
M_souta | 0:0311bb0b38f7 | 27 | |
M_souta | 0:0311bb0b38f7 | 28 | //********** pin select **********// |
M_souta | 0:0311bb0b38f7 | 29 | AnalogIn y1(Z0); |
M_souta | 0:0311bb0b38f7 | 30 | AnalogIn y2(Z1); |
M_souta | 0:0311bb0b38f7 | 31 | AnalogIn y3(Z2); |
M_souta | 0:0311bb0b38f7 | 32 | |
M_souta | 0:0311bb0b38f7 | 33 | DigitalOut select1[]= { |
M_souta | 0:0311bb0b38f7 | 34 | DigitalOut(SELECT1_0), |
M_souta | 0:0311bb0b38f7 | 35 | DigitalOut(SELECT1_1), |
M_souta | 0:0311bb0b38f7 | 36 | DigitalOut(SELECT1_2), |
M_souta | 0:0311bb0b38f7 | 37 | DigitalOut(SELECT1_3), |
M_souta | 0:0311bb0b38f7 | 38 | }; |
M_souta | 0:0311bb0b38f7 | 39 | DigitalOut select2[]= { |
M_souta | 0:0311bb0b38f7 | 40 | DigitalOut(SELECT2_0), |
M_souta | 0:0311bb0b38f7 | 41 | DigitalOut(SELECT2_1), |
M_souta | 0:0311bb0b38f7 | 42 | DigitalOut(SELECT2_2), |
M_souta | 0:0311bb0b38f7 | 43 | DigitalOut(SELECT2_3), |
M_souta | 0:0311bb0b38f7 | 44 | }; |
M_souta | 0:0311bb0b38f7 | 45 | DigitalOut select3[]= { |
M_souta | 0:0311bb0b38f7 | 46 | DigitalOut(SELECT3_0), |
M_souta | 0:0311bb0b38f7 | 47 | DigitalOut(SELECT3_1), |
M_souta | 0:0311bb0b38f7 | 48 | DigitalOut(SELECT3_2), |
M_souta | 0:0311bb0b38f7 | 49 | DigitalOut(SELECT3_3), |
M_souta | 0:0311bb0b38f7 | 50 | }; |
M_souta | 0:0311bb0b38f7 | 51 | |
M_souta | 0:0311bb0b38f7 | 52 | DigitalOut uartSE(UART_SE); |
M_souta | 0:0311bb0b38f7 | 53 | |
M_souta | 0:0311bb0b38f7 | 54 | DigitalOut LED(LED3); |
M_souta | 0:0311bb0b38f7 | 55 | |
M_souta | 0:0311bb0b38f7 | 56 | //Serial uart(USBTX,USBRX); |
M_souta | 0:0311bb0b38f7 | 57 | Serial uart(UARTTX,UARTRX); |
M_souta | 0:0311bb0b38f7 | 58 | |
M_souta | 0:0311bb0b38f7 | 59 | I2C i2c(SDA,SCL); |
M_souta | 0:0311bb0b38f7 | 60 | SO1602A oled(i2c,0x78); |
M_souta | 0:0311bb0b38f7 | 61 | //********** pin select **********// |
M_souta | 0:0311bb0b38f7 | 62 | |
M_souta | 0:0311bb0b38f7 | 63 | //********** define **********// |
M_souta | 0:0311bb0b38f7 | 64 | #define BUFFER_SIZE 10 |
M_souta | 0:0311bb0b38f7 | 65 | char RS485Data[BUFFER_SIZE]; |
M_souta | 0:0311bb0b38f7 | 66 | RINGBUFFER::RingBuffer RS485uart(RS485Data,BUFFER_SIZE); |
M_souta | 0:0311bb0b38f7 | 67 | |
M_souta | 0:0311bb0b38f7 | 68 | #define SENDDATA_SIZE 8 |
M_souta | 0:0311bb0b38f7 | 69 | #define SENSORDATA_SIZE 48 |
M_souta | 0:0311bb0b38f7 | 70 | |
M_souta | 0:0311bb0b38f7 | 71 | const int pinDef[48] = { 0, 1, 2, 3, 4, 5, |
M_souta | 0:0311bb0b38f7 | 72 | 12,13,14,15, 7, 6, |
M_souta | 0:0311bb0b38f7 | 73 | 22,23, 8, 9,10,11, |
M_souta | 0:0311bb0b38f7 | 74 | 16,17,18,19,20,21, |
M_souta | 0:0311bb0b38f7 | 75 | 27,26,25,24,40,41, |
M_souta | 0:0311bb0b38f7 | 76 | 28,29,30,31,38,39, |
M_souta | 0:0311bb0b38f7 | 77 | 32,33,34,35,36,37, |
M_souta | 0:0311bb0b38f7 | 78 | 42,43,44,45,36,47}; |
M_souta | 0:0311bb0b38f7 | 79 | |
M_souta | 0:0311bb0b38f7 | 80 | const int weight[6] = { 2, 1, 0, 0,-1,-2}; |
M_souta | 0:0311bb0b38f7 | 81 | |
M_souta | 0:0311bb0b38f7 | 82 | char send[SENDDATA_SIZE] = {60,77,80,55,65,68,90,87}; |
M_souta | 0:0311bb0b38f7 | 83 | bool line[SENSORDATA_SIZE] = {0}; |
M_souta | 0:0311bb0b38f7 | 84 | |
M_souta | 0:0311bb0b38f7 | 85 | void Transmit(); |
M_souta | 0:0311bb0b38f7 | 86 | void PinSelect(int pin); |
M_souta | 0:0311bb0b38f7 | 87 | void SendDataUpdate(char sendData[], int dataSize); |
M_souta | 0:0311bb0b38f7 | 88 | void LineDataRead(bool sensorData[], int dataSize); |
M_souta | 0:0311bb0b38f7 | 89 | void SendDataMake(bool sensorData[], char sendData[]); |
M_souta | 0:0311bb0b38f7 | 90 | //********** define **********// |
M_souta | 0:0311bb0b38f7 | 91 | |
M_souta | 0:0311bb0b38f7 | 92 | void InitiaLize(void) |
M_souta | 0:0311bb0b38f7 | 93 | { |
M_souta | 0:0311bb0b38f7 | 94 | uartSE = 1; //送信固定 |
M_souta | 0:0311bb0b38f7 | 95 | |
M_souta | 0:0311bb0b38f7 | 96 | uart.baud(9600); |
M_souta | 0:0311bb0b38f7 | 97 | uart.attach(Transmit,Serial::TxIrq); |
M_souta | 0:0311bb0b38f7 | 98 | } |
M_souta | 0:0311bb0b38f7 | 99 | |
M_souta | 0:0311bb0b38f7 | 100 | int main(void) |
M_souta | 0:0311bb0b38f7 | 101 | { |
M_souta | 0:0311bb0b38f7 | 102 | InitiaLize(); |
M_souta | 0:0311bb0b38f7 | 103 | |
M_souta | 0:0311bb0b38f7 | 104 | while(1) |
M_souta | 0:0311bb0b38f7 | 105 | { |
M_souta | 0:0311bb0b38f7 | 106 | LineDataRead(line,SENSORDATA_SIZE); |
M_souta | 0:0311bb0b38f7 | 107 | SendDataMake(line,send); |
M_souta | 0:0311bb0b38f7 | 108 | SendDataUpdate(send,SENDDATA_SIZE); |
M_souta | 0:0311bb0b38f7 | 109 | } |
M_souta | 0:0311bb0b38f7 | 110 | } |
M_souta | 0:0311bb0b38f7 | 111 | |
M_souta | 0:0311bb0b38f7 | 112 | void Transmit() { |
M_souta | 0:0311bb0b38f7 | 113 | __disable_irq(); |
M_souta | 0:0311bb0b38f7 | 114 | //uart.printf("%d\n\r",RS485uart.GetData()); |
M_souta | 0:0311bb0b38f7 | 115 | uart.putc(RS485uart.GetData()); |
M_souta | 0:0311bb0b38f7 | 116 | __enable_irq(); |
M_souta | 0:0311bb0b38f7 | 117 | |
M_souta | 0:0311bb0b38f7 | 118 | } |
M_souta | 0:0311bb0b38f7 | 119 | |
M_souta | 0:0311bb0b38f7 | 120 | void SendDataUpdate(char sendData[], int dataSize) { |
M_souta | 0:0311bb0b38f7 | 121 | __disable_irq(); |
M_souta | 0:0311bb0b38f7 | 122 | if(!RS485uart.InAnyData()) { |
M_souta | 0:0311bb0b38f7 | 123 | RS485uart.PutData('S'); |
M_souta | 0:0311bb0b38f7 | 124 | for(int i = 0; i < dataSize; i++) { |
M_souta | 0:0311bb0b38f7 | 125 | RS485uart.PutData(sendData[i]); |
M_souta | 0:0311bb0b38f7 | 126 | } |
M_souta | 0:0311bb0b38f7 | 127 | RS485uart.PutData('F'); |
M_souta | 0:0311bb0b38f7 | 128 | } |
M_souta | 0:0311bb0b38f7 | 129 | __enable_irq(); |
M_souta | 0:0311bb0b38f7 | 130 | } |
M_souta | 0:0311bb0b38f7 | 131 | |
M_souta | 0:0311bb0b38f7 | 132 | void PinSelect(int pin) { |
M_souta | 0:0311bb0b38f7 | 133 | bool selectnum[16][4]= { |
M_souta | 0:0311bb0b38f7 | 134 | {0,0,0,0}, |
M_souta | 0:0311bb0b38f7 | 135 | {0,0,0,1}, |
M_souta | 0:0311bb0b38f7 | 136 | {0,0,1,0}, |
M_souta | 0:0311bb0b38f7 | 137 | {0,0,1,1}, |
M_souta | 0:0311bb0b38f7 | 138 | {0,1,0,0}, |
M_souta | 0:0311bb0b38f7 | 139 | {0,1,0,1}, |
M_souta | 0:0311bb0b38f7 | 140 | {0,1,1,0}, |
M_souta | 0:0311bb0b38f7 | 141 | {0,1,1,1}, |
M_souta | 0:0311bb0b38f7 | 142 | {1,0,0,0}, |
M_souta | 0:0311bb0b38f7 | 143 | {1,0,0,1}, |
M_souta | 0:0311bb0b38f7 | 144 | {1,0,1,0}, |
M_souta | 0:0311bb0b38f7 | 145 | {1,0,1,1}, |
M_souta | 0:0311bb0b38f7 | 146 | {1,1,0,0}, |
M_souta | 0:0311bb0b38f7 | 147 | {1,1,0,1}, |
M_souta | 0:0311bb0b38f7 | 148 | {1,1,1,0}, |
M_souta | 0:0311bb0b38f7 | 149 | {1,1,1,1}, |
M_souta | 0:0311bb0b38f7 | 150 | }; |
M_souta | 0:0311bb0b38f7 | 151 | if(pin < 16) { |
M_souta | 0:0311bb0b38f7 | 152 | select1[0]=selectnum[pin][3]; |
M_souta | 0:0311bb0b38f7 | 153 | select1[1]=selectnum[pin][2]; |
M_souta | 0:0311bb0b38f7 | 154 | select1[2]=selectnum[pin][1]; |
M_souta | 0:0311bb0b38f7 | 155 | select1[3]=selectnum[pin][0]; |
M_souta | 0:0311bb0b38f7 | 156 | for(int i=0; i<=3; i++) { |
M_souta | 0:0311bb0b38f7 | 157 | select2[i]=0; |
M_souta | 0:0311bb0b38f7 | 158 | select3[i]=0; |
M_souta | 0:0311bb0b38f7 | 159 | } |
M_souta | 0:0311bb0b38f7 | 160 | } else if(pin < 32) { |
M_souta | 0:0311bb0b38f7 | 161 | select2[0]=selectnum[pin-16][3]; |
M_souta | 0:0311bb0b38f7 | 162 | select2[1]=selectnum[pin-16][2]; |
M_souta | 0:0311bb0b38f7 | 163 | select2[2]=selectnum[pin-16][1]; |
M_souta | 0:0311bb0b38f7 | 164 | select2[3]=selectnum[pin-16][0]; |
M_souta | 0:0311bb0b38f7 | 165 | for(int i=0; i<=3; i++) { |
M_souta | 0:0311bb0b38f7 | 166 | select1[i]=0; |
M_souta | 0:0311bb0b38f7 | 167 | select3[i]=0; |
M_souta | 0:0311bb0b38f7 | 168 | } |
M_souta | 0:0311bb0b38f7 | 169 | } else if(pin < 48) { |
M_souta | 0:0311bb0b38f7 | 170 | select3[0]=selectnum[pin-32][3]; |
M_souta | 0:0311bb0b38f7 | 171 | select3[1]=selectnum[pin-32][2]; |
M_souta | 0:0311bb0b38f7 | 172 | select3[2]=selectnum[pin-32][1]; |
M_souta | 0:0311bb0b38f7 | 173 | select3[3]=selectnum[pin-32][0]; |
M_souta | 0:0311bb0b38f7 | 174 | for(int i=0; i<=3; i++) { |
M_souta | 0:0311bb0b38f7 | 175 | select1[i]=0; |
M_souta | 0:0311bb0b38f7 | 176 | select2[i]=0; |
M_souta | 0:0311bb0b38f7 | 177 | } |
M_souta | 0:0311bb0b38f7 | 178 | } |
M_souta | 0:0311bb0b38f7 | 179 | } |
M_souta | 0:0311bb0b38f7 | 180 | |
M_souta | 0:0311bb0b38f7 | 181 | void LineDataRead(bool sensorData[], int dataSize) { |
M_souta | 0:0311bb0b38f7 | 182 | int pin; |
M_souta | 0:0311bb0b38f7 | 183 | int readData = 0; |
M_souta | 0:0311bb0b38f7 | 184 | for(int i = 0; i < dataSize; i++) { |
M_souta | 0:0311bb0b38f7 | 185 | pin = pinDef[i]; |
M_souta | 0:0311bb0b38f7 | 186 | PinSelect(pin); |
M_souta | 0:0311bb0b38f7 | 187 | if(pin<=15) { |
M_souta | 0:0311bb0b38f7 | 188 | readData = y1.read(); |
M_souta | 0:0311bb0b38f7 | 189 | } else if(pin<=31) { |
M_souta | 0:0311bb0b38f7 | 190 | readData = y2.read(); |
M_souta | 0:0311bb0b38f7 | 191 | } else if(pin<=47) { |
M_souta | 0:0311bb0b38f7 | 192 | readData = y3.read(); |
M_souta | 0:0311bb0b38f7 | 193 | } |
M_souta | 0:0311bb0b38f7 | 194 | if(readData < 0.8f) { |
M_souta | 0:0311bb0b38f7 | 195 | sensorData[i] = 0; |
M_souta | 0:0311bb0b38f7 | 196 | } else { |
M_souta | 0:0311bb0b38f7 | 197 | sensorData[i] = 1; |
M_souta | 0:0311bb0b38f7 | 198 | } |
M_souta | 0:0311bb0b38f7 | 199 | |
M_souta | 0:0311bb0b38f7 | 200 | } |
M_souta | 0:0311bb0b38f7 | 201 | } |
M_souta | 0:0311bb0b38f7 | 202 | |
M_souta | 0:0311bb0b38f7 | 203 | void SendDataMake(bool sensorData[], char sendData[]) { |
M_souta | 0:0311bb0b38f7 | 204 | int data = 0; |
M_souta | 0:0311bb0b38f7 | 205 | bool ACflag = true; |
M_souta | 0:0311bb0b38f7 | 206 | bool NCflag = true; |
M_souta | 0:0311bb0b38f7 | 207 | int count = 0; |
M_souta | 0:0311bb0b38f7 | 208 | for(int i = 0; i < 8; i++) { |
M_souta | 0:0311bb0b38f7 | 209 | NCflag = true; |
M_souta | 0:0311bb0b38f7 | 210 | ACflag = true; |
M_souta | 0:0311bb0b38f7 | 211 | data = 0; |
M_souta | 0:0311bb0b38f7 | 212 | for(int j = 0; j < 6; j++) { |
M_souta | 0:0311bb0b38f7 | 213 | data += (!sensorData[count]) * weight[j]; |
M_souta | 0:0311bb0b38f7 | 214 | if(sensorData[count] == 1) ACflag = false; |
M_souta | 0:0311bb0b38f7 | 215 | else if(sensorData[count] == 0) NCflag = false; |
M_souta | 0:0311bb0b38f7 | 216 | count++; |
M_souta | 0:0311bb0b38f7 | 217 | } |
M_souta | 0:0311bb0b38f7 | 218 | if(NCflag) sendData[i] = 'N'; |
M_souta | 0:0311bb0b38f7 | 219 | else if(ACflag) sendData[i] = 'A'; |
M_souta | 0:0311bb0b38f7 | 220 | else sendData[i] = data; |
M_souta | 0:0311bb0b38f7 | 221 | } |
M_souta | 0:0311bb0b38f7 | 222 | } |