Receive data and clock for data communications example

Dependencies:   C12832 mbed

Revision:
8:1fbfeda91f7f
Parent:
7:a4a28f144b07
Child:
9:471f39ee2a68
--- a/main.cpp	Mon Mar 23 14:19:52 2015 +0000
+++ b/main.cpp	Sun Mar 29 04:14:25 2015 +0000
@@ -11,121 +11,135 @@
 DigitalIn clock_pin(p21), serial_in(p22); //clock pulse input and data input pins
 unsigned char temp, data[MAX]; //temp byte storage and storage array
 int preamble, address, i, j; //increment variables
-int data_flag; //data flag
-
+int data_flag, rflag; //data flag
+void check_byte(int value);
+int check_tbyte(int value);//Timed byte like 8 clock cycles
+int read_byte(void);
 
-int main() {
-    
-    //turn off leds
-    myled = 0;
-    myled2 = 0;
-    myled3 = 0;
-    myled4 = 0;
-    
-    //initialize variables
-    preamble = 0;
-    address = 0;
-    i = 0;
+void check_byte(int value)
+{
     j = 0;
     data_flag = 1;
     temp = 0;
-    
-    //clear lcd screen, print current build message
-    lcd.cls();
-    lcd.locate(0,3);
-    lcd.printf("Testing receive data"); 
-    
-    //read input clock pulse and data checking for preamble.
-    //preamble while loop
-    while(!preamble)
-    {
+    rflag=0;
+    //while loop
+    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.
             temp = (temp << 1) + serial_in;
             data_flag = 0;
-            if(temp == PREAMBLE)
-            {
-                preamble = 1;    
+            if(temp == value) {
+                rflag = 1;
             }
         }
         //when clock returns to low - reset data flag to accept the next bit.
-        if(!clock_pin && !data_flag)
-        {
+        if(!clock_pin && !data_flag) {
             data_flag = 1;
         }
-    } 
+    }
+}
 
-    //clear lcd screen, print current build message
-    lcd.cls();
-    lcd.locate(0,3);
-    lcd.printf("Preamble Recieved"); 
-           
-    //address while loop
-     while(!address)
-    {
+int check_tbyte(int value)
+{
+    j = 0;
+    data_flag = 1;
+    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)
-        {
+        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;
-            if(temp == ADDRESS)
-            {
-                address= 1;    
+            if(temp == value) {
+                rflag = 1;
             }
         }
         //when clock returns to low - reset data flag to accept the next bit.
-        if(!clock_pin && !data_flag)
-        {
+        if(!clock_pin && !data_flag) {
             data_flag = 1;
         }
-    } 
+    }
+    return rflag;
+}
+
+int read_byte(void)
+{
+    j = 0;
+    data_flag = 1;
+    temp = 0;
+
+    //second loop / variable to increment through character array. (post amble also if implemented)
+    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 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 8 bits received) - 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 main()
+{
+
+    //turn off leds
+    myled = 0;
+    myled2 = 0;
+    myled3 = 0;
+    myled4 = 0;
+
+    //initialize variables
+    i = 0;
 
     //clear lcd screen, print current build message
     lcd.cls();
     lcd.locate(0,3);
-    lcd.printf("Address Recieved");
-    
-       
-    while(i<3)
-    { 
-        //read data while loop store bits to temp chard
-        //second loop / variable to increment through character array. (post amble also if implemented)
-        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 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 8 bits received) - 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;
-            }
-        }
-        //crc while loop / store data into character array if crc checks.
-        data[i] = 0; //initialize current array position to zero
-        data[i] = temp; //store successfully transmitted data
-        j = 0; //reset data read loop
-        i++; //increment array position
-    }
-    //clear debugging message - and reset lcd to original position before printing data.
+    lcd.printf("Testing receive data");
+
+    //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("Received: "); 
-    for(i=0; i<3; i++)
-        lcd.printf("%c", data[i]); 
-}
\ No newline at end of file
+    lcd.printf("Preamble Recieved");
+
+    //address while loop
+    if(check_tbyte(ADDRESS)) {
+        //clear lcd screen, print current build message
+        lcd.cls();
+        lcd.locate(0,3);
+        lcd.printf("Address Recieved");
+        while(i<3) {
+            //crc while loop / store data into character array if crc checks.
+            data[i] = 0; //initialize current array position to zero
+            data[i] = read_byte(); //store successfully transmitted data
+            j = 0; //reset data read loop
+            i++; //increment array position
+        }
+        //clear debugging message - and reset lcd to original position before printing data.
+        lcd.cls();
+        lcd.locate(0,3);
+        lcd.printf("Received: ");
+        for(i=0; i<3; i++)
+            lcd.printf("%c", data[i]);
+    }
+}