Receive data and clock for data communications example

Dependencies:   C12832 mbed

main.cpp

Committer:
askksa12543
Date:
2015-03-23
Revision:
7:a4a28f144b07
Parent:
6:46fe2170bb5c
Child:
8:1fbfeda91f7f

File content as of revision 7:a4a28f144b07:

#include "mbed.h"
#include "stdio.h"
#include "C12832.h"

#define MAX 100 //set the size of the character data storage array
#define PREAMBLE 0x7E //preamble of 01111110
#define ADDRESS 0x02 //address of 00000010

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
unsigned char temp, data[MAX]; //temp byte storage and storage array
int preamble, address, i, j; //increment variables
int data_flag; //data flag


int main() {
    
    //turn off leds
    myled = 0;
    myled2 = 0;
    myled3 = 0;
    myled4 = 0;
    
    //initialize variables
    preamble = 0;
    address = 0;
    i = 0;
    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)
    {
        //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 == PREAMBLE)
            {
                preamble = 1;    
            }
        }
        //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
    lcd.cls();
    lcd.locate(0,3);
    lcd.printf("Preamble Recieved"); 
           
    //address while loop
     while(!address)
    {
        //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 == ADDRESS)
            {
                address= 1;    
            }
        }
        //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
    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.cls();
    lcd.locate(0,3);
    lcd.printf("Received: "); 
    for(i=0; i<3; i++)
        lcd.printf("%c", data[i]); 
}