![](/media/cache/profiles/7c39a5f991bef4a1e34187677a65871d.jpg.50x50_q85.jpg)
Manchester receiver demo.
Dependencies: Manchester mbed CRC16 ManchesterMsg
main.cpp@2:1cb83aa7b99a, 2017-05-18 (annotated)
- Committer:
- hudakz
- Date:
- Thu May 18 10:50:53 2017 +0000
- Revision:
- 2:1cb83aa7b99a
- Parent:
- 1:2ce05484e526
- Child:
- 3:0c6680f77855
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 | 1:2ce05484e526 | 10 | char str[50]; // Array of char |
hudakz | 1:2ce05484e526 | 11 | uint32_t val; |
hudakz | 2:1cb83aa7b99a | 12 | CRC16 crc16; |
hudakz | 2:1cb83aa7b99a | 13 | unsigned short msgCRC16; |
hudakz | 2:1cb83aa7b99a | 14 | unsigned short calcCRC16; |
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 | 1:2ce05484e526 | 20 | // Print data length and raw data bytes |
hudakz | 0:91017f5f5cb3 | 21 | printf("\r\n----------------------\r\n"); |
hudakz | 1:2ce05484e526 | 22 | printf("Length = %d\r\n", msg.len); |
hudakz | 1:2ce05484e526 | 23 | printf("Raw data :\r\n"); |
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 | 1:2ce05484e526 | 30 | printf("\r\n"); |
hudakz | 2:1cb83aa7b99a | 31 | |
hudakz | 1:2ce05484e526 | 32 | |
hudakz | 2:1cb83aa7b99a | 33 | calcCRC16 = crc16.calc(msg.data, msg.len - 2); // exclude CRC bytes from calculation |
hudakz | 2:1cb83aa7b99a | 34 | |
hudakz | 2:1cb83aa7b99a | 35 | // Extract data from message |
hudakz | 2:1cb83aa7b99a | 36 | msg >> str >> val >> msgCRC16; |
hudakz | 2:1cb83aa7b99a | 37 | |
hudakz | 2:1cb83aa7b99a | 38 | printf("msgCRC16 = %d\r\n", msgCRC16); |
hudakz | 2:1cb83aa7b99a | 39 | printf("calcCRC16 = %d\r\n", calcCRC16); |
hudakz | 2:1cb83aa7b99a | 40 | |
hudakz | 2:1cb83aa7b99a | 41 | |
hudakz | 2:1cb83aa7b99a | 42 | if( calcCRC16 != msgCRC16) { |
hudakz | 2:1cb83aa7b99a | 43 | printf("CRC error\r\n"); |
hudakz | 2:1cb83aa7b99a | 44 | } |
hudakz | 2:1cb83aa7b99a | 45 | else { |
hudakz | 2:1cb83aa7b99a | 46 | printf("Extracted data :\r\n"); |
hudakz | 2:1cb83aa7b99a | 47 | printf(" str = %s\r\n", str); |
hudakz | 2:1cb83aa7b99a | 48 | printf(" val = 0x%x\r\n", val); |
hudakz | 2:1cb83aa7b99a | 49 | } |
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 | } |