![](/media/cache/profiles/7c39a5f991bef4a1e34187677a65871d.jpg.50x50_q85.jpg)
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.
Diff: main.cpp
- Revision:
- 1:3b3e8080688f
- Parent:
- 0:a508b645fc7e
- Child:
- 2:5bf6cf198838
--- a/main.cpp Wed Nov 22 16:53:15 2017 +0000 +++ b/main.cpp Mon Jul 30 09:42:18 2018 +0000 @@ -2,52 +2,64 @@ #include "ManchesterUART.h" #include "CRC16.h" -DigitalOut led(LED1); -ManchesterUART man(p28, p27, 230400); // Tx pin name, Rx pin name, speed [bps] -ManchesterMsg msg(100); // Message container (max bytes) -char str[80]; // Storage for the received array of char -uint32_t val; // Storage for the value received -CRC16 crc16; // CRC16 object -unsigned short recvCRC16; // CRC16 received in the message -unsigned short calcCRC16; // CRC16 calculated +static DigitalOut led(LED1); +static ManchesterUART man(p28, p27, 115200); // Tx pin name, Rx pin name, speed [bps] +static ManchesterMsg msg(256); // Message container (max bytes) +static char str[256]; // Storage for the received array of char +static uint32_t binaryData; // Storage for the binary data received +static CRC16 crc16; // CRC16 object +static unsigned short recvCRC16; // CRC16 received with the message +static unsigned short calcCRC16; // CRC16 calculated -int main(void) { - while(1) { - if(man.receive(msg)) { // Receive message - +/** + * @brief + * @note + * @param + * @retval + */ +int main(void) +{ + while (1) + { + if (man.receive(msg)) // Receive message + { // Print data length and raw data bytes printf("\r\n----------------------------------------\r\n"); printf("Message length: %d\r\n", msg.len); printf("Raw data :\r\n"); - for(size_t i = 0; i < msg.len; i++) { - if((i + 1) % 10 == 0) - printf(" %.2x\r\n", msg.data[i]); + for (size_t i = 0; i < msg.len; i++) + { + if ((i + 1) % 10 == 0) + printf(" %.2X\r\n", msg.data[i]); else - printf(" %.2x", msg.data[i]); + printf(" %.2X", msg.data[i]); } - printf("\r\n\r\n"); - + + printf("\r\n"); + // Calculate CRC16. Exclude CRC bytes (last two bytes) from calculation. - calcCRC16 = crc16.calc(msg.data, msg.len - 2); - printf("Calculated CRC16 = %d\r\n", calcCRC16); + calcCRC16 = crc16.calc(msg.data, msg.len - 2); + //printf("Calculated CRC16 = %d\r\n", calcCRC16); + + //Extract data and CRC16 from the message + msg >> binaryData; + msg >> str; + msg >> recvCRC16; - // Extract data and CRC16 from the message - msg >> str >> val >> recvCRC16; -// - printf("Received CRC16 = %d\r\n", recvCRC16); - printf("\r\n"); - - if( calcCRC16 == recvCRC16) { - printf("Received data :\r\n"); - printf(" str = %s\r\n", str); - printf(" val = 0x%x\r\n", val); + //printf("Received CRC16 = %d\r\n", recvCRC16); + printf("\r\n"); + + if (calcCRC16 == recvCRC16) + { + printf("Decoded data :\r\n"); + printf(" binary = 0x%lX\r\n", binaryData); + printf(" string = %s\r\n", str); } else printf("CRC error\r\n"); + + led = !led; } - else - printf("Error = %d\r\n", man.lastError()); - - led = !led; } } +