Kyle McKeehan
/
DataCommFRDM1
FRDM1
main.cpp
- Committer:
- n02655194
- Date:
- 2015-05-03
- Revision:
- 4:eb53ab6541a2
- Parent:
- 3:bea5cdec5709
File content as of revision 4:eb53ab6541a2:
#include "mbed.h" #include "stdio.h" #include "math.h" #include "PortOut.h" //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 0x12 //address of 00010010 - network of 1, id of 2. #define BROADCAST 0x00 //address of 00000000 #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 BusOut db(PTC0,PTC1,PTC2,PTC3,PTC4,PTC5,PTC6,PTC7);//db0,db1,db2,db3,db4,db5,db6,db7 BusOut lcdc(PTB8,PTB9,PTB10);//rs,rw,e int msecs, sksecs; //clock time needed for data transfer and skew time DigitalIn clock_pin(PTD7); //clock pulse input and data input pins DigitalInOut serial_in(PTD6);//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],temp_data;//Moved from unsigned char**************************************************** unsigned char preamble, address, i, j, k; //increment variables unsigned char data_flag, rflag, done_flag1, done_flag2; //data flags int crc_passed = 0; //crc flag int temp_crc = 0; //stores values for crc check. //funtion prototypes 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 check_crc(int temp_crc, unsigned char crc_calc); //double checks the sent crc value - data sent without error. void Ms_Delay(int msec); void LCDInit(void) ; void IC(void) ; void lcdClear(void); void print(char str[]); void send_byte(unsigned char byte, int position); //Improvements //if crc correct send ACK(1) to Master //if crc wrong, send NoACK(0) to Master int main() { clock_pin.mode(PullUp);//Enable PullUp Resistors serial_in.mode(PullUp);//Enable PullUp Resistors myled=0;//Turn LED On LCDInit();//Initialize LCD //clear lcd screen, print current build message 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_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"); //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"); } } while(!done_flag2) { //store data into character array if crc checks. data[i] = 0; //initialize current array position to zero 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 {///////////////////////////////////////////////////////////////////////Never Makes it in here, always recieves Postamble first data[i] = temp_data; i++; //increment array position //store the sent data into temp variable for crc calculation temp_crc <<= 8; temp_crc += temp_data; //wait until i increments and then check to see if 3 bytes have been RXed if((i % 3) == 0) { //check crc crc_calc = read_byte(NIBBLE); if(check_crc(temp_crc, crc_calc)) { crc_passed=1; lcdClear(); print("pass CRC"); } else { lcdClear(); print("fail CRC"); } Ms_Delay(1000); //zero out crc temp variable temp_crc = 0; } } } //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); // for(k=0; k<=i; k++) // print("%c", data[k]); } else { //if crc fails, send NOACK send_byte(0x00,BYTE); } myled=1; Ms_Delay(1000); } } void check_byte(int value) { data_flag = 1; temp = 0; rflag=0; //while loop while(!rflag) { //read in data if clock is 1 and data flag is 1 if(clock_pin && data_flag) { //data is left shifted into our temporary variable. //each new data bit is moved into the least significant bit after 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; data_flag = 0; if(temp == value) rflag = 1; } //when clock returns to low - reset data flag to accept the next bit. if(!clock_pin && !data_flag) data_flag = 1; } } int check_abyte() { j = 0; temp = 0; rflag=0; //while loop while(j<8) { //read in data if clock is 1 and data flag is 1 if(clock_pin && data_flag) { //data is left shifted into our temporary variable. //each new data bit is moved into the least significant bit after 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; j++; data_flag = 0; } //when clock returns to low - reset data flag to accept the next bit. if(!clock_pin && !data_flag) data_flag = 1; } //clear lcd screen, print current build message lcdClear(); if(temp == ADDRESS) { rflag = 1; print("Addrss RXed"); } else if(temp == BROADCAST) { rflag = 1; 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; 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; } int read_byte(int size) { j = 0; temp = 0; //read a byte/nibble at a time and return it to main while(j<size) { //read in data if clock is 1 and data flag is 1 if(clock_pin && data_flag) { //data is left shifted into our temporary variable. //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 RXed) - turn off data_flag until clock changes. j++; data_flag = 0; } //when clock returns to low - reset data flag to accept the next bit. if(!clock_pin && !data_flag) data_flag = 1; } return temp; } int check_crc(int temp_crc, unsigned char crc_calc) { //assume data sent incorrectly, check crc to see if data sent correctly int data_correct = 0; //shift temp 3 bytes by 4 and add transmitted crc temp_crc <<= 4; temp_crc += crc_calc; //check for crc correctness if( (temp_crc % CRC) == 0) data_correct = 1; return data_correct; } 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 LCDInit()//Initialize LCD { Ms_Delay(100); db = 0x30; Ms_Delay(2); lcdc = 0x4; //1 Enable Ms_Delay(2); lcdc = 0x0; //Disable Ms_Delay(2); db = 0xF; Ms_Delay(2); lcdc = 0x4; //1 Enable Ms_Delay(2); lcdc = 0x0; //Disable Ms_Delay(2); db = 0x1; Ms_Delay(2); lcdc = 0x4; //1 Enable Ms_Delay(2); lcdc = 0x0; //Disable Ms_Delay(2); db = 0x6; Ms_Delay(2); lcdc = 0x4; //1 Enable Ms_Delay(2); lcdc = 0x0; //Disable Ms_Delay(2); } void lcdClear() //Clear LCD, for LPC, lcd.cls();, lcd.locate(0,3); { lcdc = 0x0; //Disable Ms_Delay(2); db = 0x1; Ms_Delay(2); lcdc = 0x4; //1 Enable Ms_Delay(2); lcdc = 0x0; //Disable Ms_Delay(2); } void IC() //Write to LCD { Ms_Delay(2); lcdc = 0x5; //Enable and register select high Ms_Delay(2); lcdc = 0x1; //Disable and register select low Ms_Delay(2); } void print(char str[])// print String to LCD Display, for LPC, printf(str); { int m=0, stl=strlen(str); while(m<stl) { db = str[m]; IC(); m++; } } void send_byte(unsigned char byte, int position) { }