FRDM2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
n02655194
Date:
Fri May 08 14:10:09 2015 +0000
Parent:
0:906202f8e2f2
Commit message:
final

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Apr 16 14:13:01 2015 +0000
+++ b/main.cpp	Fri May 08 14:10:09 2015 +0000
@@ -1,147 +1,137 @@
 #include "mbed.h"
 #include "stdio.h"
-#include "C12832.h"
+#include "math.h"
+#include "PortOut.h"
 
+//To Do:
+//Fix crc_passed logic
+
+//Improvements:
+//Increase bandwidth by reducing wasted nibbles from 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 ADDRESS 0x12 //address of 00010010 - network of 1, id of 2.
 #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
+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
 
-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; //temp byte storage, storage array, transmitted crc value
-unsigned char preamble, address, i, j, k; //increment variables
-unsigned char data_flag, rflag, d_flag, done, temp_data; //data flags
-int crc_passed = 0; //crc flag
-int temp_crc = 0; //stores values for crc check.
+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, 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 check_crc(int temp_crc, unsigned char crc_calc); //double checks the sent crc value - data sent without error.
-
-//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 lcdInit(void) ;
+void IC(void) ;
+void lcdClear(void);
+void print(char str[]);
+void send_ACK(int ACK);
 
 int main()
 {
-
-    //turn off leds
-    myled = 0;
-    myled2 = 0;
-    myled3 = 0;
-    myled4 = 0;
-
-    //initialize variables
-    i = 0;
-    d_flag = 0;
-    done = 0;
-    crc_passed=0;
-
-
-    while(!d_flag)
-    {
-        //clear lcd screen, print current build message
-        lcd.cls();
-        lcd.locate(0,3);
-        lcd.printf("Waiting for Preamble");
-        //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");
+    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;
 
-        //preamble received check address (next byte), returns to preamble check if not addressed to station
-        if(check_abyte())
-            d_flag = 1;
-    }
-        
-    while(!done)
-    {     
-        //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 = 1;
+        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
-            lcd.cls();
-            lcd.locate(0,3);
-            lcd.printf("Postamble Received");
-        }
-        
-        //store data in character array if not postamble - check crc when appropriate
-        else 
-        {
-            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 received
-            if( (i % 3) == 0)
-            {
-                //check crc
-                crc_calc = read_byte(NIBBLE);
-                if(check_crc(temp_crc, crc_calc))
-                {
-                    crc_passed=1;
-                    lcd.cls();
-                    lcd.locate(0,3);
-                    lcd.printf("Data passes CRC verification");    
-                }else{
-                    lcd.cls();
-                    lcd.locate(0,3);
-                    lcd.printf("Data fails CRC verification");
-                    }
-                //zero out crc temp variable
-                temp_crc = 0;
+            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");
             }
         }
-    }
-    //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]);
-    }else{//if crc fails, send NOACK
-        //send NoACK
-        }
 }
 
 void check_byte(int value)
@@ -150,11 +140,9 @@
     temp = 0;
     rflag=0;
     //while loop
-    while(!rflag)
-    {
+    while(!rflag) {
         //read in data if clock is 1 and data flag is 1
-        if(clock_pin && data_flag)
-        {
+        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.
@@ -175,11 +163,9 @@
     temp = 0;
     rflag=0;
     //while loop
-    while(j<8)
-    {
+    while(j<8) {
         //read in data if clock is 1 and data flag is 1
-        if(clock_pin && data_flag)
-        {
+        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.
@@ -191,28 +177,27 @@
         if(!clock_pin && !data_flag)
             data_flag = 1;
     }
-    
+
     //clear lcd screen, print current build message
-    lcd.cls();
-    lcd.locate(0,3);
-    if(temp == ADDRESS)
-    {
+    lcdClear();
+
+    if(temp == ADDRESS) {
+        rflag = 1;
+        print("Addrss RXed");
+    } else if(temp == BROADCAST) {
         rflag = 1;
-        lcd.printf("Address Received");
+        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");
     }
-    else if(temp == BROADCAST)
-    {
-        rflag = 1;
-        lcd.printf("Broadcast received");
-    }
-    else if(((temp & 0xF0) == NETWORK) && ((temp & 0x0F) == 0))
-    {
-        rflag = 1;
-        lcd.printf("Multicast received");
-    }//can add if Network==0 and address==x send to x in every network
-    else
-        printf("Wrong address received");
-    
+    Ms_Delay(1000);
     return rflag;
 }
 
@@ -222,15 +207,14 @@
     temp = 0;
 
     //read a byte/nibble at a time and return it to main
-    while(j<size)
-    {
+    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 received) - turn off data_flag until clock changes.
+            //increment j (tracks bits RXed) - turn off data_flag until clock changes.
             j++;
             data_flag = 0;
         }
@@ -241,17 +225,105 @@
     return temp;
 }
 
-int check_crc(int temp_crc, unsigned char crc_calc)
+
+int get_crc(int temp_crc)
+{
+    int j = 11, b = 0, z = 0, Y = 0, C0 = 0, C1 = 0, C2 = 0, C3 = 0;
+    while(j > -1) {
+        b = (temp_crc >> j) % 2;//bit
+        Y = C3 ^ b;
+        C3 = C2;
+        C2 = C1;
+        C1 = Y ^ C0;
+        C0 = Y;
+        j--;
+    }
+    z=z+C3<<1;
+    z=z+C2<<1;
+    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 lcdInit()//Initialize LCD
 {
-    //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;
+    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 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 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 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_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
--- a/mbed.bld	Thu Apr 16 14:13:01 2015 +0000
+++ b/mbed.bld	Fri May 08 14:10:09 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/8ab26030e058
\ No newline at end of file