Kevin Anderson
/
DataCommFreedom
Using FRDM Board to output data and a clock for communications example.
Diff: main.cpp
- Revision:
- 8:2b61de165543
- Parent:
- 7:aeeb441a68b8
- Child:
- 9:77ae456366f7
--- a/main.cpp Thu Apr 02 14:38:05 2015 +0000 +++ b/main.cpp Mon Apr 13 17:11:10 2015 +0000 @@ -1,37 +1,107 @@ -//project to include preamble, addressing, control, error control. -//currently fixed size data field but doesn't have to be. (post amble if not fixed size) +//project to include preamble, addressing, post amble, crc error control. #include "mbed.h" #include "stdio.h" -#define MAX 100 //set the size of the character data storage array +#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 PREAMBLE 0x7E //preamble of 01111110 -#define ADDRESS 0x02 //address of 00000010 +#define POSTAMBLE 0x81 //postamble of 10000001 +//addressing 1st 4 bits indicate the network, 2nd 4 bits indicate the station id +#define ADDRESS 0x12 //address of 00010010 network = 1, station id = 2 +#define BROADCAST 0x00 //broadcast address of 00000000 +#define MULTICAST 0x10 //broadcast to only network 1, all stations +#define CRC 0x13 //crc of 10011 or x^4+x+1 or crc-5 DigitalOut myled(LED1); // red led on board -DigitalOut clock_pin(D8), serial_out(D7); //send clock pulse and tx +DigitalOut clock_pin(D8), serial_out(D7); //send clock pulse and data Timer t; //timer for controlling the clock and data skew int msecs, sksecs; //clock time needed for data transfer and skew time -int skew_flag; //skew flag for while loop -unsigned char pre = PREAMBLE, add = ADDRESS; -unsigned char data[100] = "Hi!"; //data output -int done = 0,sent = 0 ,i = 0, j = 0; //increment variables -void send_byte(int byte); +unsigned char pre = PREAMBLE, add = BROADCAST, post = POSTAMBLE, crc_value = 0; //protocol overhead +unsigned char data[MAX] = "Hi Kyle!\n"; //data output +int crc_temp = 0; //temporary crc variable +unsigned char sent = 0, i = 0, j = 0, skew_flag; //increment variables and flags + +//function prototypes +//send a byte of data, for preamble, postamble, address, and data with BYTE, crc with NIBBLE +void send_byte(unsigned char byte, int position); + +//calculate the crc value to send. +unsigned char calculate_crc(int crc_bytes); + + +int main() +{ + + //turn on red led to show programming has worked + myled = 0; + //initialize output pins + clock_pin = 0; + serial_out = 0; + //skew flag + skew_flag = 1; + //set timers + msecs = 100; + sksecs = 80; -void send_byte(int byte) + //output preamble + send_byte(pre, BYTE); + //output address + send_byte(add, BYTE); + //output data + while(!sent) + { + //finished sending data when string termination found + if(data[i] == '\n') + { + sent = 1; + } + //string still has data send next byte and handle crc. + else + { + //send the data + send_byte(data[i], BYTE); + //store the sent data into temp variable for crc calculation + 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? + if( (i % 3) == 0) + { + //calculate and send crc + crc_value = calculate_crc(crc_temp); + send_byte(crc_value, NIBBLE); + //zero out crc temp variable + crc_temp = 0; + } + } + } + //output postamble + send_byte(post, BYTE); + //turn off red led to show sending has finished + myled = 1; +} + +void send_byte(unsigned char byte, int position) { - //not done(reset) - done = 0; - j = 128; + //starting bit position to send msb first + j = position; //output byte - while(!done) { + while(j>0) + { //start timer for clock t.start(); //wait until the timer has reached the set time. - while(t.read_ms() < msecs) { + while(t.read_ms() < msecs) + { //extract data just before clock goes high - if(!clock_pin && skew_flag && t.read_ms() > sksecs) { + if(!clock_pin && skew_flag && t.read_ms() > sksecs) + { //extract data bit serial_out = (byte / j) % 2; skew_flag = 0; @@ -45,47 +115,23 @@ clock_pin = !clock_pin; //reset skew flag skew_flag = 1; - //last preamble bit sent - reset/increment variables - if(j==0) { - done = 1; - } } } -int main() +unsigned char calculate_crc(int crc_bytes) { - - //turn on red led to show programming has worked - myled = 0; - //initialize output pins - clock_pin = 0; - serial_out = 0; - //skew flag - skew_flag = 1; - //set timers - msecs = 1000; - sksecs = 800; - - //output preamble - send_byte(pre); - //output address - send_byte(add); - //output data - i=0; - while(!sent) { - send_byte(data[i]); - //finished sending data - if(i>2) { - sent = 1; - } - i++; - } - //turn off red led to show sending has finished - myled = 1; + //temp variable + unsigned char temp + + //shift crc over 4 spots + (crc_bytes << 4); + + //return remainder + temp = crc_bytes % CRC; + return temp; } - //crc = x^4+x+1 -//put char data into unsigned short temp variable, then shift << 5. % this number by the decimal equivalent of binary representation of -//the crc code and save and send it as a separate 5 bits. On the receive side save each byte in the temp character and then the next five -//bytes in a temp variable - after receiving 13 bits stick data in unsigned short variable << 5 add the recieved 5 bits and then % by -// the crc decimal equivalent, if that equals 0, save the temp character in the permanent character array and receive the next bits. \ No newline at end of file +//put char data into unsigned int temp variable, then shift << 4. % this number by the decimal equivalent of binary representation of +//the crc code and save and send it as a separate 4 bits. On the receive side save each byte in the temp character and then the next five +//bytes in a temp variable - after receiving 24 bits stick data in unsigned int variable << 4 add the recieved 4 bits and then % by +//the crc decimal equivalent, if that equals 0, save the temp character in the permanent character array and receive the next bits. \ No newline at end of file