Manchester receiver demo.

Dependencies:   Manchester mbed CRC16 ManchesterMsg

Committer:
hudakz
Date:
Thu May 18 11:07:36 2017 +0000
Revision:
3:0c6680f77855
Parent:
2:1cb83aa7b99a
Child:
4:649d9a4219c0
Updated.

Who changed what in which revision?

UserRevisionLine numberNew 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 3:0c6680f77855 10 char str[80]; // Storage for the received array of char
hudakz 3:0c6680f77855 11 uint32_t val; // Storage for the value received
hudakz 3:0c6680f77855 12 CRC16 crc16; // CRC16 object
hudakz 3:0c6680f77855 13 unsigned short msgCRC16; // CRC16 received in message
hudakz 3:0c6680f77855 14 unsigned short calcCRC16; // CRC16 calculated
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 3:0c6680f77855 32 // Calculate CRC16 (CRC bytes are excluded from calculation)
hudakz 3:0c6680f77855 33 calcCRC16 = crc16.calc(msg.data, msg.len - 2);
hudakz 2:1cb83aa7b99a 34
hudakz 3:0c6680f77855 35 // Extract data and CRC16 from the 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 }