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

Committer:
hudakz
Date:
2017-11-22
Revision:
0:a508b645fc7e
Child:
1:3b3e8080688f

File content as of revision 0:a508b645fc7e:

#include "mbed.h"
#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

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]);
                else
                    printf("  %.2x", msg.data[i]);
            }
            printf("\r\n\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);
            
            // 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);
            }
            else
                printf("CRC error\r\n");
        }
        else
            printf("Error = %d\r\n", man.lastError());

        led = !led;
    }
}