Receiver demo for the Manchester encoding library using UART's hardware.
Dependencies: CRC16 ManchesterMsg ManchesterUART mbed
Example of a Manchester code receiver using the ManchesterUART library.
main.cpp@1:3b3e8080688f, 2018-07-30 (annotated)
- Committer:
- hudakz
- Date:
- Mon Jul 30 09:42:18 2018 +0000
- Revision:
- 1:3b3e8080688f
- Parent:
- 0:a508b645fc7e
- Child:
- 2:5bf6cf198838
Updated.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:a508b645fc7e | 1 | #include "mbed.h" |
hudakz | 0:a508b645fc7e | 2 | #include "ManchesterUART.h" |
hudakz | 0:a508b645fc7e | 3 | #include "CRC16.h" |
hudakz | 0:a508b645fc7e | 4 | |
hudakz | 1:3b3e8080688f | 5 | static DigitalOut led(LED1); |
hudakz | 1:3b3e8080688f | 6 | static ManchesterUART man(p28, p27, 115200); // Tx pin name, Rx pin name, speed [bps] |
hudakz | 1:3b3e8080688f | 7 | static ManchesterMsg msg(256); // Message container (max bytes) |
hudakz | 1:3b3e8080688f | 8 | static char str[256]; // Storage for the received array of char |
hudakz | 1:3b3e8080688f | 9 | static uint32_t binaryData; // Storage for the binary data received |
hudakz | 1:3b3e8080688f | 10 | static CRC16 crc16; // CRC16 object |
hudakz | 1:3b3e8080688f | 11 | static unsigned short recvCRC16; // CRC16 received with the message |
hudakz | 1:3b3e8080688f | 12 | static unsigned short calcCRC16; // CRC16 calculated |
hudakz | 0:a508b645fc7e | 13 | |
hudakz | 1:3b3e8080688f | 14 | /** |
hudakz | 1:3b3e8080688f | 15 | * @brief |
hudakz | 1:3b3e8080688f | 16 | * @note |
hudakz | 1:3b3e8080688f | 17 | * @param |
hudakz | 1:3b3e8080688f | 18 | * @retval |
hudakz | 1:3b3e8080688f | 19 | */ |
hudakz | 1:3b3e8080688f | 20 | int main(void) |
hudakz | 1:3b3e8080688f | 21 | { |
hudakz | 1:3b3e8080688f | 22 | while (1) |
hudakz | 1:3b3e8080688f | 23 | { |
hudakz | 1:3b3e8080688f | 24 | if (man.receive(msg)) // Receive message |
hudakz | 1:3b3e8080688f | 25 | { |
hudakz | 0:a508b645fc7e | 26 | // Print data length and raw data bytes |
hudakz | 0:a508b645fc7e | 27 | printf("\r\n----------------------------------------\r\n"); |
hudakz | 0:a508b645fc7e | 28 | printf("Message length: %d\r\n", msg.len); |
hudakz | 0:a508b645fc7e | 29 | printf("Raw data :\r\n"); |
hudakz | 1:3b3e8080688f | 30 | for (size_t i = 0; i < msg.len; i++) |
hudakz | 1:3b3e8080688f | 31 | { |
hudakz | 1:3b3e8080688f | 32 | if ((i + 1) % 10 == 0) |
hudakz | 1:3b3e8080688f | 33 | printf(" %.2X\r\n", msg.data[i]); |
hudakz | 0:a508b645fc7e | 34 | else |
hudakz | 1:3b3e8080688f | 35 | printf(" %.2X", msg.data[i]); |
hudakz | 0:a508b645fc7e | 36 | } |
hudakz | 1:3b3e8080688f | 37 | |
hudakz | 1:3b3e8080688f | 38 | printf("\r\n"); |
hudakz | 1:3b3e8080688f | 39 | |
hudakz | 0:a508b645fc7e | 40 | // Calculate CRC16. Exclude CRC bytes (last two bytes) from calculation. |
hudakz | 1:3b3e8080688f | 41 | calcCRC16 = crc16.calc(msg.data, msg.len - 2); |
hudakz | 1:3b3e8080688f | 42 | //printf("Calculated CRC16 = %d\r\n", calcCRC16); |
hudakz | 1:3b3e8080688f | 43 | |
hudakz | 1:3b3e8080688f | 44 | //Extract data and CRC16 from the message |
hudakz | 1:3b3e8080688f | 45 | msg >> binaryData; |
hudakz | 1:3b3e8080688f | 46 | msg >> str; |
hudakz | 1:3b3e8080688f | 47 | msg >> recvCRC16; |
hudakz | 0:a508b645fc7e | 48 | |
hudakz | 1:3b3e8080688f | 49 | //printf("Received CRC16 = %d\r\n", recvCRC16); |
hudakz | 1:3b3e8080688f | 50 | printf("\r\n"); |
hudakz | 1:3b3e8080688f | 51 | |
hudakz | 1:3b3e8080688f | 52 | if (calcCRC16 == recvCRC16) |
hudakz | 1:3b3e8080688f | 53 | { |
hudakz | 1:3b3e8080688f | 54 | printf("Decoded data :\r\n"); |
hudakz | 1:3b3e8080688f | 55 | printf(" binary = 0x%lX\r\n", binaryData); |
hudakz | 1:3b3e8080688f | 56 | printf(" string = %s\r\n", str); |
hudakz | 0:a508b645fc7e | 57 | } |
hudakz | 0:a508b645fc7e | 58 | else |
hudakz | 0:a508b645fc7e | 59 | printf("CRC error\r\n"); |
hudakz | 1:3b3e8080688f | 60 | |
hudakz | 1:3b3e8080688f | 61 | led = !led; |
hudakz | 0:a508b645fc7e | 62 | } |
hudakz | 0:a508b645fc7e | 63 | } |
hudakz | 0:a508b645fc7e | 64 | } |
hudakz | 1:3b3e8080688f | 65 |