Kyle McKeehan / Mbed 2 deprecated DataCommFRDM1

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
n02655194
Date:
Sun May 03 02:04:15 2015 +0000
Parent:
3:bea5cdec5709
Commit message:
5/2/15

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat May 02 16:38:40 2015 +0000
+++ b/main.cpp	Sun May 03 02:04:15 2015 +0000
@@ -1,7 +1,9 @@
 #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
@@ -9,20 +11,21 @@
 #define POSTAMBLE 0x81 //postamble of 10000001
 #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
-Timer t; //timer for pausing after data RXed before displaying data
-DigitalOut myled(LED1); // red led on board
-DigitalIn clock_pin(PTD7), serial_in(PTD6); //clock pulse input and data input pins
+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 data[MAX];//Moved from unsigned char****************************************************
+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, d_flag, done, temp_data; //data flags
+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.
 
@@ -36,109 +39,103 @@
 void IC(void) ;
 void lcdClear(void);
 void print(char str[]);
+void send_byte(unsigned char byte, int position);
 
 //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
 
 int main()
 {
-    LCDInit();
-    lcdClear();
-    print("Hello!");
-    Ms_Delay(500);
-    //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
+    LCDInit();//Initialize LCD
     //clear lcd screen, print current build message
     lcdClear();
     print("Comm Started");
-
-    while(!d_flag) {
-        //read input clock pulse and data checking for preamble.
-        //preamble while loop
-        check_byte(PREAMBLE);
+    Ms_Delay(500);//Wait 500mS
+    while(true) {
+        //initialize variables
+        i = 0;
+        done_flag1 = 0;
+        done_flag2 = 0;
+        crc_passed=0;
 
-        //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())
-            d_flag = 1;
-        else {
-            //pause after incorrect address - so message is visible, then display waiting for preamble
-            Ms_Delay(1000);
-            //clear lcd screen, print current build message
-            lcdClear();
-            print("Wait for pre");
-            d_flag=1;//////////////////////////////////////////////////////////////////////////////////REMOVE(recieves incorrect Address)
-        }
-    }
-
-    while(!done) {
-        //store data into character array if crc checks.
-        data[i] = 0; //initialize current array position to zero
-        lcdClear();
-        print("here");     
-        temp_data = read_byte(BYTE); //store successfully transmitted data//////////////////////////Doesnt finish reading byte
-        done=1;
-        lcdClear();
-        print("here2");     
-
-        //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
             lcdClear();
-            print("Postamble RXed");
+            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 {
-//            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("Data passes CRC verification");
-//                } else {
-//                    lcdClear();
-//                    print("Data fails CRC verification");
-//                }
-//                //zero out crc temp variable
-//                temp_crc = 0;
-//            }
-//        }
-//    }
-    //pause after displaying postamble RXed and then display data.
-//    Ms_Delay(1000);
-//    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 NoACK
-//    }
+            //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)
@@ -196,15 +193,15 @@
         print("Brdcst RXed");
     } else if(((temp & 0xF0) == (ADDRESS & 0xF0)) && ((temp & 0x0F) == 0)) {
         rflag = 1;
-        print("Multicst RXed");
+        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 {
-        temp = (char) temp;
         print("Wrng addrss RXed");
     }
+    Ms_Delay(1000);
     return rflag;
 }
 
@@ -317,4 +314,10 @@
         IC();
         m++;
     }
-}
\ No newline at end of file
+}
+
+void send_byte(unsigned char byte, int position)
+{
+    
+    }
+    
\ No newline at end of file