Same as LPC, but with correct Address
Dependencies: C12832 mbed DataCommLPC2
Revision 10:f26deacef98d, committed 2015-05-08
- Comitter:
- n02655194
- Date:
- Fri May 08 14:31:44 2015 +0000
- Parent:
- 9:c907be24e49e
- Commit message:
- final
Changed in this revision
DataCommLPC2.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r c907be24e49e -r f26deacef98d DataCommLPC2.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DataCommLPC2.lib Fri May 08 14:31:44 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/n02655194/code/DataCommLPC2/#c907be24e49e
diff -r c907be24e49e -r f26deacef98d main.cpp --- a/main.cpp Wed May 06 05:24:41 2015 +0000 +++ b/main.cpp Fri May 08 14:31:44 2015 +0000 @@ -1,158 +1,138 @@ #include "mbed.h" #include "stdio.h" +#include "math.h" #include "C12832.h" +//To Do: +//Fix crc_passed logic + +//Improvements: +//Increase bandwidth by reducing wasted nibbles from CRC + +//Changes: +//unsigned chars moved +//removed nibble +//added CRC + +//multicast using 4bit address / 4 bit network. network = 4 msb, address = 4 lsb. broadcast = all 0's. multicast = network id & address of 0's. #define MAX 100 //set the size of the character data storage array #define BYTE 8 -#define NIBBLE 4 //used to set size of data read #define PREAMBLE 0x7E //preamble of 01111110 #define POSTAMBLE 0x81 //postamble of 10000001 #define ADDRESS 0x11 //address of 00010010 - network of 1, id of 2. -#define NETWORK 0x10 //network portion of the address -#define ID 0x02 //id portion of the address #define BROADCAST 0x00 //address of 00000000 -//multicast using 4bit address / 4 bit network. network = 4 msb, address = 4 lsb. broadcast = all 0's. multicast = network id & address of 0's. #define CRC 0x13 //crc of 10011 or x^4+x+1 or crc-5 - +Timer t; //timer for pausing after data RXed before displaying data +DigitalOut myled(LED1); // red led on board C12832 lcd(p5, p7, p6, p8, p11); //LCD structure -DigitalOut myled(LED1), myled2(LED2), myled3(LED3), myled4(LED4); //variables to access the four blue leds -DigitalIn clock_pin(p21), serial_in(p22); //clock pulse input and data input pins -Timer t; //timer for pausing after postamble received before displaying data -unsigned char temp, data[MAX], crc_calc=0; //temp byte storage, storage array, transmitted crc value -char c[1];//temp_data moved from unsigned char +int msecs, sksecs; //clock time needed for data transfer and skew time +DigitalIn clock_pin(p21); //clock pulse input and data input pins +DigitalInOut serial_in(p22);//Recieve, send for ACK + +unsigned char temp, crc_calc; //temp byte storage, storage array, transmitted crc value +char c[1];//Used for troubleshooting to send a char to LCD +char data[MAX];//data array unsigned char preamble, address, i, j, k, temp_data,FCS; //increment variables -unsigned char data_flag, rflag, d_flag, done; //data flags -int crc_passed = 0; //crc flag -int temp_crc = 0; //stores values for crc check. +unsigned char data_flag, rflag, done_flag1, done_flag2; //data flags +int crc_passed = 0, temp_crc = 0; //CRC Flag, stores values for crc check. //funtion prototypes -void check_byte(int value); //stays inside the function until the received byte matches the value passed into the function (PREAMBLE) -int check_abyte();//after preamble received checks the next byte for address. Returns 1 if address received matches ADDRESS, BROADCAST, or multicast; 0 if not. -int read_byte(int size); //reads received data and returns it a byte at a time. -int get_crc(int temp_crc); - -//Improvements -//if crc correct display correct crc -//if crc correct send ACK(1) to Master -//if crc wrong, display incorrect crc -//if crc wrong, send NoACK(0) to Master -//if crc wrong adjust for retransmit - +void check_byte(int value); //stays inside the function until the RXed byte matches the value passed into the function (PREAMBLE) +int check_abyte();//after preamble RXed checks the next byte for address. Returns 1 if address RXed matches ADDRESS, BROADCAST, or multicast; 0 if not. +int read_byte(int size); //reads RXed data and returns it a byte at a time. +int get_crc(int temp_crc);//Get CRC value on data temp_crc +void Ms_Delay(int msec); +void lcdClear(void); +void print(char str[]); +void send_ACK(int ACK); int main() { - - //turn off leds - myled = 1; - - //initialize variables - i = 0; - d_flag = 0; - done = 0; - crc_passed=0; + clock_pin.mode(PullUp);//Enable PullUp Resistors + serial_in.mode(PullUp);//Enable PullUp Resistors + myled=0;//Turn LED On //clear lcd screen, print current build message - lcd.cls(); - lcd.locate(0,3); - lcd.printf("Serial Communication Device Started"); - - while(!d_flag) { - //read input clock pulse and data checking for preamble. - //preamble while loop - check_byte(PREAMBLE); - - //clear lcd screen, print current build message - lcd.cls(); - lcd.locate(0,3); - lcd.printf("Preamble Received"); - - //preamble received check address (next byte), returns to preamble check if not addressed to station - if(check_abyte()) - d_flag = 1; - else { - //pause after incorrect address - so message is visible, then display waiting for preamble - t.start(); - //wait until the timer has reached the set time. - while(t.read_ms() < 500) { - - } - //stop and reset the timer - t.stop(); - t.reset(); - //clear lcd screen, print current build message - lcd.cls(); - lcd.locate(0,3); - lcd.printf("Waiting for preamble"); - } - } + lcdClear(); + print("Comm Started"); + Ms_Delay(500);//Wait 500mS + while(true) { + //initialize variables + i = 0; + done_flag1 = 0; + done_flag2 = 0; + crc_passed=0; - while(!done) { - //store data into character array if crc checks. - temp_data = read_byte(BYTE); //store successfully transmitted data + while(!done_flag1) { + //read input clock pulse and data checking for preamble. + //preamble while loop + check_byte(PREAMBLE); + myled=0;//Turn LED On + //clear lcd screen, print current build message + lcdClear(); + print("Pre RXed"); - //check for postamble - if(temp_data == POSTAMBLE) { - //break out of while loop - data finished sending - done = 1; - //clear lcd screen, print postamble message - lcd.cls(); - lcd.locate(0,3); - lcd.printf("Postamble Received"); - } - //store data in character array if not postamble - check crc when appropriate - else {//recieves hh instead of h 0x06 - //byte1, crc1, byte2, crc2 - data[i]=0; - data[i] = temp_data; - temp_crc=temp_data<<4; - //////////////////////////////////////////////////////////doesnt seem to be reading in fast enough - FCS = read_byte(BYTE); //store successfully transmitted data - c[0]=(FCS & 0x0F)+0x30; - lcd.cls(); - lcd.locate(0,3); - lcd.printf(c); -// t.start(); -////wait until the timer has reached the set time. -// while(t.read_ms() < 1000) { -// } -////stop and reset the timer -// t.stop(); -// t.reset(); - temp_crc=temp_crc+(FCS & 0x0F);//grab the last 4 bits from crc value byte - if(get_crc(temp_crc)==0) { - crc_passed=1; - lcd.cls(); - lcd.locate(0,3); - lcd.printf("Data passes CRC verification."); - } else { - crc_passed=crc_passed & 0; - lcd.cls(); - lcd.locate(0,3); - lcd.printf("Data fails CRC verification, Int Data: %x, CRC Byte %x.", temp_crc, crc_calc); + //preamble RXed check address (next byte), returns to preamble check if not addressed to station + if(check_abyte()) + done_flag1 = 1; + else { + //clear lcd screen, print current build message + lcdClear(); + print("Wait for pre"); } } - i++; //increment array position - //zero out crc temp variables - temp_crc = 0; - } -//pause after displaying postamble received and then display data. - t.start(); -//wait until the timer has reached the set time. - while(t.read_ms() < 1000) { + + while(!done_flag2) { + //store data into character array if crc checks. + temp_data = read_byte(BYTE); //store successfully transmitted data + + //check for postamble + if(temp_data == POSTAMBLE) { + //break out of while loop - data finished sending + done_flag2 = 1; + //clear lcd screen, print current build message + lcdClear(); + print("Post RXed"); + } + //store data in character array if not postamble - check crc when appropriate + else { + //byte1, crc1, byte2, crc2 + data[i]=0; + data[i] = temp_data; + temp_crc=temp_data<<4; + FCS = read_byte(BYTE); //store successfully transmitted data + temp_crc=temp_crc+(FCS & 0x0F);//grab the last 4 bits from crc value byte + if(get_crc(temp_crc)==0) { + crc_passed=1; + lcdClear(); + print("pass CRC"); + } else { + crc_passed=crc_passed & 0;//needs to be fixed + lcdClear(); + print("fail CRC"); + } + } + i++; //increment array position + temp_crc = 0;//zero out crc temp variables + } + //pause after displaying postamble RXed and then display data. + //Ms_Delay(1000);//Display Postamble Message + if(crc_passed) { //if crc passes display data, send ACK + //clear debugging messages - and reset lcd to original position before printing data. + //send ACK + lcdClear(); + print("RXed: "); + print(data); + send_ACK(0x1); +// for(k=0; k<=i; k++) +// print("%c", data[k]); + } else { //if crc fails, send NOACK + send_ACK(0x0); + } + myled=1; + //Ms_Delay(1000); } -//stop and reset the timer - t.stop(); - t.reset(); -//if(crc_passed){//if crc passes display data, send ACK -//clear debugging messages - and reset lcd to original position before printing data. -//send ACK - lcd.cls(); - lcd.locate(0,3); - lcd.printf("Received: "); - for(k=0; k<=i; k++) - lcd.printf("%c", data[k]); - } void check_byte(int value) @@ -200,21 +180,25 @@ } //clear lcd screen, print current build message - lcd.cls(); - lcd.locate(0,3); + lcdClear(); + if(temp == ADDRESS) { rflag = 1; - lcd.printf("Address Received"); + print("Addrss RXed"); } else if(temp == BROADCAST) { rflag = 1; - lcd.printf("Broadcast received"); - } else if(((temp & 0xF0) == NETWORK) && ((temp & 0x0F) == 0)) { + print("Brdcst RXed"); + } else if(((temp & 0xF0) == (ADDRESS & 0xF0)) && ((temp & 0x0F) == 0)) { + rflag = 1; + print("Multicst1 RXed"); + } else if(((temp & 0xF0) == 0) && ((temp & 0x0F) == (ADDRESS & 0x0F))) { rflag = 1; - lcd.printf("Multicast received"); - }//can add if Network==0 and address==x send to x in every network - else - lcd.printf("Wrong address received"); - + print("Multicst2 RXed"); + //can add if Network==0 and address==x send to x in every network + } else { + print("Wrng addrss RXed"); + } + Ms_Delay(1000); return rflag; } @@ -231,7 +215,7 @@ //each new data bit is moved into the least significant bit afater the rest of the bits are shifted to the left //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. temp = (temp << 1) + serial_in; - //increment j (tracks bits received) - turn off data_flag until clock changes. + //increment j (tracks bits RXed) - turn off data_flag until clock changes. j++; data_flag = 0; } @@ -242,6 +226,7 @@ return temp; } + int get_crc(int temp_crc) { int j = 11, b = 0, z = 0, Y = 0, C0 = 0, C1 = 0, C2 = 0, C3 = 0; @@ -259,4 +244,38 @@ z=z+C1<<1; z=z+C0; return z; +} + +void Ms_Delay(int msec)//mS Delay +{ + t.start(); + //wait until the timer has reached the set time. + while(t.read_ms() < msec) { + } + //stop and reset the timer + t.stop(); + t.reset(); +} + +void lcdClear() //Clear LCD, for LPC, lcd.cls();, lcd.locate(0,3); +{ + lcd.cls(); + lcd.locate(0,3); +} + +void print(char str[])// print String to LCD Display, for LPC, printf(str); +{ + lcd.printf(str); +} + +void send_ACK(int ACK) +{ + serial_in = ACK; + Ms_Delay(1500);///////////////can reduce time when Arduino Postamble delay is reduced +// if(clock_pin) { +// //if(!clock_pin && skew_flag && t.read_ms() > sksecs) {//output data before clock high +// serial_in = (byte / j) % 2; +// j /= 2; //decrement j to get to next bit location +// } + //reset skew flag } \ No newline at end of file