Kevin Anderson
/
DataCommLPC
Receive data and clock for data communications example
main.cpp
- Committer:
- askksa12543
- Date:
- 2015-04-02
- Revision:
- 12:a7244ff9a00e
- Parent:
- 11:b37d0454f623
- Child:
- 13:14002c51e465
File content as of revision 12:a7244ff9a00e:
#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, rflag, d_flag; //data flag void check_byte(int value); int check_tbyte(int value);//Timed byte like 8 clock cycles int read_byte(void); 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_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) { //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 == value) { rflag = 1; } } //when clock returns to low - reset data flag to accept the next bit. if(!clock_pin && !data_flag) { data_flag = 1; } } lcd.cls(); lcd.locate(0,3); lcd.printf("Address Received: %h", temp); 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; d_flag = 0; //clear lcd screen, print current build message lcd.cls(); lcd.locate(0,3); lcd.printf("Testing receive data"); while(!d_flag){ //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"); //address while loop if(check_tbyte(ADDRESS)) { //clear lcd screen, print current build message lcd.cls(); lcd.locate(0,3); lcd.printf("Address Received: %c", temp); d_flag = 1; } } 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]); }