Kevin Anderson
/
DataCommFreedom
Using FRDM Board to output data and a clock for communications example.
Diff: main.cpp
- Revision:
- 9:77ae456366f7
- Parent:
- 8:2b61de165543
- Child:
- 10:90cc064f7082
--- a/main.cpp Mon Apr 13 17:11:10 2015 +0000 +++ b/main.cpp Wed Apr 22 10:49:08 2015 +0000 @@ -1,11 +1,13 @@ //project to include preamble, addressing, post amble, crc error control. +//after sending - set clock to 0, set timer and check clock pin - if clock pin goes high read in response - 4 bit ack? + #include "mbed.h" #include "stdio.h" #define MAX 100 //set the max size of the character data storage array #define BYTE 128 -#define NIBBLE 8 //used to set binary position of msb +#define NIBBLE 16 //used to set binary position of msb #define PREAMBLE 0x7E //preamble of 01111110 #define POSTAMBLE 0x81 //postamble of 10000001 //addressing 1st 4 bits indicate the network, 2nd 4 bits indicate the station id @@ -56,6 +58,20 @@ if(data[i] == '\n') { sent = 1; + //pad out sent data with 0's so crc is sent for all data. + while( (i%3) != 0) + { + //pad data with 0's + data[i] = 0x00; + send_byte(data[i], BYTE); + //store 0's into temp variable for crc calculation + crc_temp <<= 8; + crc_temp += data[i]; + i++; + } + //calculate and send crc + crc_value = calculate_crc(crc_temp); + send_byte(crc_value, NIBBLE); } //string still has data send next byte and handle crc. else @@ -63,13 +79,13 @@ //send the data send_byte(data[i], BYTE); //store the sent data into temp variable for crc calculation - crc_temp << 8; + crc_temp <<= 8; crc_temp += data[i]; //increment array pointer i++; //wait until i increments and then check to see if 3 bytes have been sent //i here is the actual count of bytes sent, not the array location that was just sent - //this also avoids sending a crc nibble after no data has been sent - can we figure logic for sending last crc if data not divisible by 3? + //this also avoids sending a crc nibble after no data has been sent if( (i % 3) == 0) { //calculate and send crc @@ -77,6 +93,7 @@ send_byte(crc_value, NIBBLE); //zero out crc temp variable crc_temp = 0; + crc_value = 0; } } } @@ -121,10 +138,10 @@ unsigned char calculate_crc(int crc_bytes) { //temp variable - unsigned char temp + unsigned char temp; - //shift crc over 4 spots - (crc_bytes << 4); + //shift crc over 5 spots + crc_bytes <<= 5; //return remainder temp = crc_bytes % CRC;