Руслан Бредун
/
stm32-sensor-base2
test
main.cpp@8:c3cffab85b0d, 2020-07-28 (annotated)
- Committer:
- ommpy
- Date:
- Tue Jul 28 14:25:33 2020 +0000
- Revision:
- 8:c3cffab85b0d
- Parent:
- 7:2f218add711e
- Child:
- 9:859bcb293e46
modbus byte array added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ommpy | 0:d383e2dee0f7 | 1 | #include <global.h> |
ommpy | 0:d383e2dee0f7 | 2 | |
ommpy | 6:a760ce6defbe | 3 | RS485 RS485(UART2_TX,UART2_RX,DE_TXD_2); // Tx, Rx , !RE and DE MAX485 pin |
ommpy | 6:a760ce6defbe | 4 | |
ommpy | 8:c3cffab85b0d | 5 | volatile bool serialArrived = false; |
ommpy | 8:c3cffab85b0d | 6 | int n=0; |
ommpy | 8:c3cffab85b0d | 7 | char a; |
ommpy | 8:c3cffab85b0d | 8 | bool send_flag = false; |
ommpy | 8:c3cffab85b0d | 9 | |
nestedslk | 4:f6e22dd39313 | 10 | Timer timer; |
ommpy | 6:a760ce6defbe | 11 | typedef uint8_t byte; |
ommpy | 0:d383e2dee0f7 | 12 | |
ommpy | 8:c3cffab85b0d | 13 | DigitalOut select(DE_TXD_1); |
ommpy | 8:c3cffab85b0d | 14 | byte regvalue[1] ; |
ommpy | 8:c3cffab85b0d | 15 | byte data[6]; |
ommpy | 6:a760ce6defbe | 16 | const unsigned char CRC7_POLY = 0x91; |
ommpy | 6:a760ce6defbe | 17 | |
ommpy | 6:a760ce6defbe | 18 | unsigned char getCRC(unsigned char message[], unsigned char length) |
ommpy | 6:a760ce6defbe | 19 | { |
ommpy | 6:a760ce6defbe | 20 | unsigned char i, j, crc = 0; |
ommpy | 6:a760ce6defbe | 21 | |
ommpy | 6:a760ce6defbe | 22 | for (i = 0; i < length; i++) |
ommpy | 6:a760ce6defbe | 23 | { |
ommpy | 6:a760ce6defbe | 24 | crc ^= message[i]; |
ommpy | 6:a760ce6defbe | 25 | for (j = 0; j < 8; j++) |
ommpy | 6:a760ce6defbe | 26 | { |
ommpy | 6:a760ce6defbe | 27 | if (crc & 1) |
ommpy | 6:a760ce6defbe | 28 | crc ^= CRC7_POLY; |
ommpy | 6:a760ce6defbe | 29 | crc >>= 1; |
ommpy | 6:a760ce6defbe | 30 | } |
ommpy | 6:a760ce6defbe | 31 | } |
ommpy | 6:a760ce6defbe | 32 | return crc; |
ommpy | 6:a760ce6defbe | 33 | } |
nestedslk | 4:f6e22dd39313 | 34 | int main() |
nestedslk | 4:f6e22dd39313 | 35 | { |
ommpy | 8:c3cffab85b0d | 36 | # if DEMO_CODE |
nestedslk | 5:97117a837d2c | 37 | |
ommpy | 6:a760ce6defbe | 38 | JSN_SR04 sensor1(TIM1_CH2, TRIG_PA8_OUT); |
ommpy | 6:a760ce6defbe | 39 | E18_D80NK infared1 (IR1_PB12_OUT); |
ommpy | 6:a760ce6defbe | 40 | E18_D80NK infared2 (IR2_PB13_OUT); |
ommpy | 8:c3cffab85b0d | 41 | |
ommpy | 8:c3cffab85b0d | 42 | select = 1; |
ommpy | 6:a760ce6defbe | 43 | |
ommpy | 7:2f218add711e | 44 | sensor1.setRanges(10, 200); |
ommpy | 6:a760ce6defbe | 45 | |
ommpy | 8:c3cffab85b0d | 46 | memset(data,0,sizeof(data)); |
ommpy | 8:c3cffab85b0d | 47 | float distance; |
ommpy | 8:c3cffab85b0d | 48 | |
ommpy | 8:c3cffab85b0d | 49 | while(true) { |
ommpy | 8:c3cffab85b0d | 50 | if(RS485.readable() >0){ |
ommpy | 8:c3cffab85b0d | 51 | //memset(regvalue,0,sizeof(regvalue)); |
ommpy | 8:c3cffab85b0d | 52 | //wait_ms(100); |
ommpy | 8:c3cffab85b0d | 53 | RS485.recvMsg(regvalue,sizeof(uint8_t),30); |
ommpy | 8:c3cffab85b0d | 54 | } |
ommpy | 8:c3cffab85b0d | 55 | //printf("Done\n"); |
ommpy | 6:a760ce6defbe | 56 | |
ommpy | 8:c3cffab85b0d | 57 | if((int)regvalue[1] == 4 ) |
ommpy | 8:c3cffab85b0d | 58 | { |
ommpy | 8:c3cffab85b0d | 59 | select = 1; |
ommpy | 8:c3cffab85b0d | 60 | timer.reset(); |
ommpy | 8:c3cffab85b0d | 61 | timer.start(); |
ommpy | 8:c3cffab85b0d | 62 | sensor1.startMeasurement(); |
ommpy | 8:c3cffab85b0d | 63 | |
ommpy | 8:c3cffab85b0d | 64 | distance = sensor1.getDistance_cm(); |
ommpy | 8:c3cffab85b0d | 65 | data[0] = (uint8_t)(distance >> 24) ; |
ommpy | 8:c3cffab85b0d | 66 | data[1] = (uint8_t)(distance >> 16) ; |
ommpy | 8:c3cffab85b0d | 67 | data[2] = (uint8_t)(distance >> 8) ; |
ommpy | 8:c3cffab85b0d | 68 | data[3] = (uint8_t)distance ; |
ommpy | 8:c3cffab85b0d | 69 | data[4] = (uint8_t)infared1.checkObstacle(); |
ommpy | 8:c3cffab85b0d | 70 | data[5] = (uint8_t)infared2.checkObstacle(); |
ommpy | 8:c3cffab85b0d | 71 | RS485.sendMsg(data,sizeof(data)); |
ommpy | 8:c3cffab85b0d | 72 | timer.stop(); |
ommpy | 8:c3cffab85b0d | 73 | send_flag = false; |
ommpy | 8:c3cffab85b0d | 74 | wait_ms(20); |
ommpy | 8:c3cffab85b0d | 75 | |
ommpy | 8:c3cffab85b0d | 76 | select = 0 ; |
ommpy | 8:c3cffab85b0d | 77 | } |
ommpy | 6:a760ce6defbe | 78 | |
ommpy | 6:a760ce6defbe | 79 | } |
ommpy | 7:2f218add711e | 80 | |
ommpy | 6:a760ce6defbe | 81 | |
ommpy | 6:a760ce6defbe | 82 | |
ommpy | 6:a760ce6defbe | 83 | #endif |
ommpy | 6:a760ce6defbe | 84 | |
ommpy | 0:d383e2dee0f7 | 85 | } |
ommpy | 0:d383e2dee0f7 | 86 |