![](/media/cache/profiles/7c39a5f991bef4a1e34187677a65871d.jpg.50x50_q85.jpg)
Manchester receiver demo.
Dependencies: Manchester mbed CRC16 ManchesterMsg
main.cpp@6:d1191c39b418, 2018-10-14 (annotated)
- Committer:
- hudakz
- Date:
- Sun Oct 14 09:46:53 2018 +0000
- Revision:
- 6:d1191c39b418
- Parent:
- 5:3dc7d8e25c89
Number of sync pulses in preamble can be set.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:91017f5f5cb3 | 1 | #include "mbed.h" |
hudakz | 0:91017f5f5cb3 | 2 | #include "Manchester.h" |
hudakz | 2:1cb83aa7b99a | 3 | #include "CRC16.h" |
hudakz | 0:91017f5f5cb3 | 4 | |
hudakz | 0:91017f5f5cb3 | 5 | DigitalOut led(LED1); |
hudakz | 6:d1191c39b418 | 6 | Manchester man(D3, D4, 9600); // Tx pin, Rx pin, speed [bps] |
hudakz | 5:3dc7d8e25c89 | 7 | ManchesterMsg msg(100); // Message container (max bytes) |
hudakz | 5:3dc7d8e25c89 | 8 | char str[80]; // Storage for the received array of char |
hudakz | 5:3dc7d8e25c89 | 9 | uint32_t val; // Storage for the value received |
hudakz | 5:3dc7d8e25c89 | 10 | CRC16 crc16; // CRC16 object |
hudakz | 5:3dc7d8e25c89 | 11 | unsigned short recvCRC16; // CRC16 received in the message |
hudakz | 5:3dc7d8e25c89 | 12 | unsigned short calcCRC16; // CRC16 calculated |
hudakz | 0:91017f5f5cb3 | 13 | |
hudakz | 0:91017f5f5cb3 | 14 | int main(void) { |
hudakz | 6:d1191c39b418 | 15 | man.setPreamble(5); // Number of sync pulses in preamble |
hudakz | 0:91017f5f5cb3 | 16 | while(1) { |
hudakz | 6:d1191c39b418 | 17 | if(man.receive(msg)) { // Receive message |
hudakz | 4:649d9a4219c0 | 18 | |
hudakz | 1:2ce05484e526 | 19 | // Print data length and raw data bytes |
hudakz | 4:649d9a4219c0 | 20 | printf("\r\n----------------------------------------\r\n"); |
hudakz | 4:649d9a4219c0 | 21 | printf("Message length = %d, Raw data :\r\n", msg.len); |
hudakz | 1:2ce05484e526 | 22 | for(size_t i = 0; i < msg.len; i++) { |
hudakz | 1:2ce05484e526 | 23 | if((i + 1) % 10 == 0) |
hudakz | 1:2ce05484e526 | 24 | printf(" %.2x\r\n", msg.data[i]); |
hudakz | 1:2ce05484e526 | 25 | else |
hudakz | 1:2ce05484e526 | 26 | printf(" %.2x", msg.data[i]); |
hudakz | 1:2ce05484e526 | 27 | } |
hudakz | 4:649d9a4219c0 | 28 | printf("\r\n\r\n"); |
hudakz | 2:1cb83aa7b99a | 29 | |
hudakz | 5:3dc7d8e25c89 | 30 | // Calculate CRC16. Exclude CRC bytes (last two bytes) from calculation. |
hudakz | 5:3dc7d8e25c89 | 31 | calcCRC16 = crc16.calc(msg.data, msg.len - 2); |
hudakz | 5:3dc7d8e25c89 | 32 | printf("Calculated CRC16 = %d\r\n", calcCRC16); |
hudakz | 2:1cb83aa7b99a | 33 | |
hudakz | 3:0c6680f77855 | 34 | // Extract data and CRC16 from the message |
hudakz | 4:649d9a4219c0 | 35 | msg >> str >> val >> recvCRC16; |
hudakz | 2:1cb83aa7b99a | 36 | |
hudakz | 4:649d9a4219c0 | 37 | printf("Received CRC16 = %d\r\n", recvCRC16); |
hudakz | 5:3dc7d8e25c89 | 38 | printf("\r\n"); |
hudakz | 2:1cb83aa7b99a | 39 | |
hudakz | 5:3dc7d8e25c89 | 40 | if( calcCRC16 == recvCRC16) { |
hudakz | 4:649d9a4219c0 | 41 | printf("Received data :\r\n"); |
hudakz | 2:1cb83aa7b99a | 42 | printf(" str = %s\r\n", str); |
hudakz | 2:1cb83aa7b99a | 43 | printf(" val = 0x%x\r\n", val); |
hudakz | 2:1cb83aa7b99a | 44 | } |
hudakz | 4:649d9a4219c0 | 45 | else |
hudakz | 4:649d9a4219c0 | 46 | printf("CRC error\r\n"); |
hudakz | 0:91017f5f5cb3 | 47 | } |
hudakz | 0:91017f5f5cb3 | 48 | else |
hudakz | 0:91017f5f5cb3 | 49 | printf("Error\r\n"); |
hudakz | 0:91017f5f5cb3 | 50 | |
hudakz | 0:91017f5f5cb3 | 51 | led = !led; |
hudakz | 0:91017f5f5cb3 | 52 | } |
hudakz | 0:91017f5f5cb3 | 53 | } |