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.

Committer:
hudakz
Date:
Wed Nov 22 16:53:15 2017 +0000
Revision:
0:a508b645fc7e
Child:
1:3b3e8080688f
Initial release.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:a508b645fc7e 5 DigitalOut led(LED1);
hudakz 0:a508b645fc7e 6 ManchesterUART man(p28, p27, 230400); // Tx pin name, Rx pin name, speed [bps]
hudakz 0:a508b645fc7e 7 ManchesterMsg msg(100); // Message container (max bytes)
hudakz 0:a508b645fc7e 8 char str[80]; // Storage for the received array of char
hudakz 0:a508b645fc7e 9 uint32_t val; // Storage for the value received
hudakz 0:a508b645fc7e 10 CRC16 crc16; // CRC16 object
hudakz 0:a508b645fc7e 11 unsigned short recvCRC16; // CRC16 received in the message
hudakz 0:a508b645fc7e 12 unsigned short calcCRC16; // CRC16 calculated
hudakz 0:a508b645fc7e 13
hudakz 0:a508b645fc7e 14 int main(void) {
hudakz 0:a508b645fc7e 15 while(1) {
hudakz 0:a508b645fc7e 16 if(man.receive(msg)) { // Receive message
hudakz 0:a508b645fc7e 17
hudakz 0:a508b645fc7e 18 // Print data length and raw data bytes
hudakz 0:a508b645fc7e 19 printf("\r\n----------------------------------------\r\n");
hudakz 0:a508b645fc7e 20 printf("Message length: %d\r\n", msg.len);
hudakz 0:a508b645fc7e 21 printf("Raw data :\r\n");
hudakz 0:a508b645fc7e 22 for(size_t i = 0; i < msg.len; i++) {
hudakz 0:a508b645fc7e 23 if((i + 1) % 10 == 0)
hudakz 0:a508b645fc7e 24 printf(" %.2x\r\n", msg.data[i]);
hudakz 0:a508b645fc7e 25 else
hudakz 0:a508b645fc7e 26 printf(" %.2x", msg.data[i]);
hudakz 0:a508b645fc7e 27 }
hudakz 0:a508b645fc7e 28 printf("\r\n\r\n");
hudakz 0:a508b645fc7e 29
hudakz 0:a508b645fc7e 30 // Calculate CRC16. Exclude CRC bytes (last two bytes) from calculation.
hudakz 0:a508b645fc7e 31 calcCRC16 = crc16.calc(msg.data, msg.len - 2);
hudakz 0:a508b645fc7e 32 printf("Calculated CRC16 = %d\r\n", calcCRC16);
hudakz 0:a508b645fc7e 33
hudakz 0:a508b645fc7e 34 // Extract data and CRC16 from the message
hudakz 0:a508b645fc7e 35 msg >> str >> val >> recvCRC16;
hudakz 0:a508b645fc7e 36 //
hudakz 0:a508b645fc7e 37 printf("Received CRC16 = %d\r\n", recvCRC16);
hudakz 0:a508b645fc7e 38 printf("\r\n");
hudakz 0:a508b645fc7e 39
hudakz 0:a508b645fc7e 40 if( calcCRC16 == recvCRC16) {
hudakz 0:a508b645fc7e 41 printf("Received data :\r\n");
hudakz 0:a508b645fc7e 42 printf(" str = %s\r\n", str);
hudakz 0:a508b645fc7e 43 printf(" val = 0x%x\r\n", val);
hudakz 0:a508b645fc7e 44 }
hudakz 0:a508b645fc7e 45 else
hudakz 0:a508b645fc7e 46 printf("CRC error\r\n");
hudakz 0:a508b645fc7e 47 }
hudakz 0:a508b645fc7e 48 else
hudakz 0:a508b645fc7e 49 printf("Error = %d\r\n", man.lastError());
hudakz 0:a508b645fc7e 50
hudakz 0:a508b645fc7e 51 led = !led;
hudakz 0:a508b645fc7e 52 }
hudakz 0:a508b645fc7e 53 }