![](/media/cache/profiles/7c39a5f991bef4a1e34187677a65871d.jpg.50x50_q85.jpg)
Manchester receiver demo.
Dependencies: Manchester mbed CRC16 ManchesterMsg
main.cpp@4:649d9a4219c0, 2017-05-21 (annotated)
- Committer:
- hudakz
- Date:
- Sun May 21 19:28:00 2017 +0000
- Revision:
- 4:649d9a4219c0
- Parent:
- 3:0c6680f77855
- Child:
- 5:3dc7d8e25c89
Updated.
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 | 0:91017f5f5cb3 | 6 | |
hudakz | 0:91017f5f5cb3 | 7 | int main(void) { |
hudakz | 1:2ce05484e526 | 8 | Manchester man(p11, p12, 9600); // Tx pin, Rx pin, speed [bps] |
hudakz | 1:2ce05484e526 | 9 | ManchesterMsg msg(100); // Message container (max bytes) |
hudakz | 3:0c6680f77855 | 10 | char str[80]; // Storage for the received array of char |
hudakz | 3:0c6680f77855 | 11 | uint32_t val; // Storage for the value received |
hudakz | 3:0c6680f77855 | 12 | CRC16 crc16; // CRC16 object |
hudakz | 4:649d9a4219c0 | 13 | unsigned short recvCRC16; // CRC16 received in the message |
hudakz | 3:0c6680f77855 | 14 | unsigned short calcCRC16; // CRC16 calculated |
hudakz | 1:2ce05484e526 | 15 | |
hudakz | 1:2ce05484e526 | 16 | //pc.baud(9600); |
hudakz | 0:91017f5f5cb3 | 17 | |
hudakz | 0:91017f5f5cb3 | 18 | while(1) { |
hudakz | 0:91017f5f5cb3 | 19 | if(man.receive(msg)) { // Receive message |
hudakz | 4:649d9a4219c0 | 20 | |
hudakz | 1:2ce05484e526 | 21 | // Print data length and raw data bytes |
hudakz | 4:649d9a4219c0 | 22 | printf("\r\n----------------------------------------\r\n"); |
hudakz | 4:649d9a4219c0 | 23 | printf("Message length = %d, Raw data :\r\n", msg.len); |
hudakz | 1:2ce05484e526 | 24 | for(size_t i = 0; i < msg.len; i++) { |
hudakz | 1:2ce05484e526 | 25 | if((i + 1) % 10 == 0) |
hudakz | 1:2ce05484e526 | 26 | printf(" %.2x\r\n", msg.data[i]); |
hudakz | 1:2ce05484e526 | 27 | else |
hudakz | 1:2ce05484e526 | 28 | printf(" %.2x", msg.data[i]); |
hudakz | 1:2ce05484e526 | 29 | } |
hudakz | 4:649d9a4219c0 | 30 | printf("\r\n\r\n"); |
hudakz | 2:1cb83aa7b99a | 31 | |
hudakz | 4:649d9a4219c0 | 32 | // Calculate CRC16 (exclude CRC bytes (last two bytes) from calculation) |
hudakz | 3:0c6680f77855 | 33 | calcCRC16 = crc16.calc(msg.data, msg.len - 2); |
hudakz | 2:1cb83aa7b99a | 34 | |
hudakz | 3:0c6680f77855 | 35 | // Extract data and CRC16 from the message |
hudakz | 4:649d9a4219c0 | 36 | msg >> str >> val >> recvCRC16; |
hudakz | 2:1cb83aa7b99a | 37 | |
hudakz | 4:649d9a4219c0 | 38 | printf("Calculated CRC16 = %d\r\n", calcCRC16); |
hudakz | 4:649d9a4219c0 | 39 | printf("Received CRC16 = %d\r\n", recvCRC16); |
hudakz | 4:649d9a4219c0 | 40 | printf("\r\n"); |
hudakz | 2:1cb83aa7b99a | 41 | |
hudakz | 2:1cb83aa7b99a | 42 | |
hudakz | 4:649d9a4219c0 | 43 | if( calcCRC16 = recvCRC16) { |
hudakz | 4:649d9a4219c0 | 44 | printf("Received data :\r\n"); |
hudakz | 2:1cb83aa7b99a | 45 | printf(" str = %s\r\n", str); |
hudakz | 2:1cb83aa7b99a | 46 | printf(" val = 0x%x\r\n", val); |
hudakz | 2:1cb83aa7b99a | 47 | } |
hudakz | 4:649d9a4219c0 | 48 | else |
hudakz | 4:649d9a4219c0 | 49 | printf("CRC error\r\n"); |
hudakz | 0:91017f5f5cb3 | 50 | } |
hudakz | 0:91017f5f5cb3 | 51 | else |
hudakz | 0:91017f5f5cb3 | 52 | printf("Error\r\n"); |
hudakz | 0:91017f5f5cb3 | 53 | |
hudakz | 0:91017f5f5cb3 | 54 | led = !led; |
hudakz | 0:91017f5f5cb3 | 55 | } |
hudakz | 0:91017f5f5cb3 | 56 | } |