Receive data and clock for data communications example

Dependencies:   C12832 mbed

Committer:
askksa12543
Date:
Thu Apr 02 14:21:52 2015 +0000
Revision:
9:471f39ee2a68
Parent:
8:1fbfeda91f7f
Child:
10:971ed3df27a7
testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
askksa12543 0:538c5d1e0d1d 1 #include "mbed.h"
askksa12543 0:538c5d1e0d1d 2 #include "stdio.h"
askksa12543 0:538c5d1e0d1d 3 #include "C12832.h"
askksa12543 0:538c5d1e0d1d 4
askksa12543 4:2c92fc5e1563 5 #define MAX 100 //set the size of the character data storage array
askksa12543 5:27d091ff2a99 6 #define PREAMBLE 0x7E //preamble of 01111110
askksa12543 7:a4a28f144b07 7 #define ADDRESS 0x02 //address of 00000010
askksa12543 0:538c5d1e0d1d 8
askksa12543 2:886800d05776 9 C12832 lcd(p5, p7, p6, p8, p11); //LCD structure
askksa12543 4:2c92fc5e1563 10 DigitalOut myled(LED1), myled2(LED2), myled3(LED3), myled4(LED4); //variables to access the four blue leds
askksa12543 4:2c92fc5e1563 11 DigitalIn clock_pin(p21), serial_in(p22); //clock pulse input and data input pins
askksa12543 4:2c92fc5e1563 12 unsigned char temp, data[MAX]; //temp byte storage and storage array
askksa12543 7:a4a28f144b07 13 int preamble, address, i, j; //increment variables
askksa12543 9:471f39ee2a68 14 int data_flag, rflag, d_flag; //data flag
n02655194 8:1fbfeda91f7f 15 void check_byte(int value);
n02655194 8:1fbfeda91f7f 16 int check_tbyte(int value);//Timed byte like 8 clock cycles
n02655194 8:1fbfeda91f7f 17 int read_byte(void);
askksa12543 4:2c92fc5e1563 18
n02655194 8:1fbfeda91f7f 19 void check_byte(int value)
n02655194 8:1fbfeda91f7f 20 {
askksa12543 0:538c5d1e0d1d 21 j = 0;
askksa12543 0:538c5d1e0d1d 22 data_flag = 1;
askksa12543 9:471f39ee2a68 23 d_flag = 0;
askksa12543 2:886800d05776 24 temp = 0;
n02655194 8:1fbfeda91f7f 25 rflag=0;
n02655194 8:1fbfeda91f7f 26 //while loop
n02655194 8:1fbfeda91f7f 27 while(!rflag) {
askksa12543 5:27d091ff2a99 28 //read in data if clock is 1 and data flag is 1
n02655194 8:1fbfeda91f7f 29 if(clock_pin && data_flag) {
askksa12543 5:27d091ff2a99 30 //data is left shifted into our temporary variable.
askksa12543 6:46fe2170bb5c 31 //each new data bit is moved into the least significant bit after the rest of the bits are shifted to the left
askksa12543 5:27d091ff2a99 32 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
askksa12543 5:27d091ff2a99 33 temp = (temp << 1) + serial_in;
askksa12543 5:27d091ff2a99 34 data_flag = 0;
n02655194 8:1fbfeda91f7f 35 if(temp == value) {
n02655194 8:1fbfeda91f7f 36 rflag = 1;
askksa12543 5:27d091ff2a99 37 }
askksa12543 5:27d091ff2a99 38 }
askksa12543 5:27d091ff2a99 39 //when clock returns to low - reset data flag to accept the next bit.
n02655194 8:1fbfeda91f7f 40 if(!clock_pin && !data_flag) {
askksa12543 5:27d091ff2a99 41 data_flag = 1;
askksa12543 5:27d091ff2a99 42 }
n02655194 8:1fbfeda91f7f 43 }
n02655194 8:1fbfeda91f7f 44 }
askksa12543 5:27d091ff2a99 45
n02655194 8:1fbfeda91f7f 46 int check_tbyte(int value)
n02655194 8:1fbfeda91f7f 47 {
n02655194 8:1fbfeda91f7f 48 j = 0;
n02655194 8:1fbfeda91f7f 49 data_flag = 1;
n02655194 8:1fbfeda91f7f 50 temp = 0;
n02655194 8:1fbfeda91f7f 51 rflag=0;
n02655194 8:1fbfeda91f7f 52 //while loop
n02655194 8:1fbfeda91f7f 53 while(j<8) {
askksa12543 7:a4a28f144b07 54 //read in data if clock is 1 and data flag is 1
n02655194 8:1fbfeda91f7f 55 if(clock_pin && data_flag) {
askksa12543 7:a4a28f144b07 56 //data is left shifted into our temporary variable.
askksa12543 7:a4a28f144b07 57 //each new data bit is moved into the least significant bit after the rest of the bits are shifted to the left
askksa12543 7:a4a28f144b07 58 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
askksa12543 7:a4a28f144b07 59 temp = (temp << 1) + serial_in;
n02655194 8:1fbfeda91f7f 60 j++;
askksa12543 7:a4a28f144b07 61 data_flag = 0;
n02655194 8:1fbfeda91f7f 62 if(temp == value) {
n02655194 8:1fbfeda91f7f 63 rflag = 1;
askksa12543 7:a4a28f144b07 64 }
askksa12543 7:a4a28f144b07 65 }
askksa12543 7:a4a28f144b07 66 //when clock returns to low - reset data flag to accept the next bit.
n02655194 8:1fbfeda91f7f 67 if(!clock_pin && !data_flag) {
askksa12543 7:a4a28f144b07 68 data_flag = 1;
askksa12543 7:a4a28f144b07 69 }
n02655194 8:1fbfeda91f7f 70 }
n02655194 8:1fbfeda91f7f 71 return rflag;
n02655194 8:1fbfeda91f7f 72 }
n02655194 8:1fbfeda91f7f 73
n02655194 8:1fbfeda91f7f 74 int read_byte(void)
n02655194 8:1fbfeda91f7f 75 {
n02655194 8:1fbfeda91f7f 76 j = 0;
n02655194 8:1fbfeda91f7f 77 data_flag = 1;
n02655194 8:1fbfeda91f7f 78 temp = 0;
n02655194 8:1fbfeda91f7f 79
n02655194 8:1fbfeda91f7f 80 //second loop / variable to increment through character array. (post amble also if implemented)
n02655194 8:1fbfeda91f7f 81 while(j<8) {
n02655194 8:1fbfeda91f7f 82 //read in data if clock is 1 and data flag is 1
n02655194 8:1fbfeda91f7f 83 if(clock_pin && data_flag) {
n02655194 8:1fbfeda91f7f 84 //data is left shifted into our temporary variable.
n02655194 8:1fbfeda91f7f 85 //each new data bit is moved into the least significant bit afater the rest of the bits are shifted to the left
n02655194 8:1fbfeda91f7f 86 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
n02655194 8:1fbfeda91f7f 87 temp = (temp << 1) + serial_in;
n02655194 8:1fbfeda91f7f 88 //increment j (tracks 8 bits received) - turn off data_flag until clock changes.
n02655194 8:1fbfeda91f7f 89 j++;
n02655194 8:1fbfeda91f7f 90 data_flag = 0;
n02655194 8:1fbfeda91f7f 91 }
n02655194 8:1fbfeda91f7f 92 //when clock returns to low - reset data flag to accept the next bit.
n02655194 8:1fbfeda91f7f 93 if(!clock_pin && !data_flag) {
n02655194 8:1fbfeda91f7f 94 data_flag = 1;
n02655194 8:1fbfeda91f7f 95 }
n02655194 8:1fbfeda91f7f 96 }
n02655194 8:1fbfeda91f7f 97 return temp;
n02655194 8:1fbfeda91f7f 98 }
n02655194 8:1fbfeda91f7f 99
n02655194 8:1fbfeda91f7f 100 int main()
n02655194 8:1fbfeda91f7f 101 {
n02655194 8:1fbfeda91f7f 102
n02655194 8:1fbfeda91f7f 103 //turn off leds
n02655194 8:1fbfeda91f7f 104 myled = 0;
n02655194 8:1fbfeda91f7f 105 myled2 = 0;
n02655194 8:1fbfeda91f7f 106 myled3 = 0;
n02655194 8:1fbfeda91f7f 107 myled4 = 0;
n02655194 8:1fbfeda91f7f 108
n02655194 8:1fbfeda91f7f 109 //initialize variables
n02655194 8:1fbfeda91f7f 110 i = 0;
askksa12543 7:a4a28f144b07 111
askksa12543 7:a4a28f144b07 112 //clear lcd screen, print current build message
askksa12543 7:a4a28f144b07 113 lcd.cls();
askksa12543 7:a4a28f144b07 114 lcd.locate(0,3);
n02655194 8:1fbfeda91f7f 115 lcd.printf("Testing receive data");
n02655194 8:1fbfeda91f7f 116
askksa12543 9:471f39ee2a68 117 while(!d_flag){
n02655194 8:1fbfeda91f7f 118 //read input clock pulse and data checking for preamble.
n02655194 8:1fbfeda91f7f 119 //preamble while loop
n02655194 8:1fbfeda91f7f 120 check_byte(PREAMBLE);
n02655194 8:1fbfeda91f7f 121
n02655194 8:1fbfeda91f7f 122 //clear lcd screen, print current build message
askksa12543 4:2c92fc5e1563 123 lcd.cls();
askksa12543 4:2c92fc5e1563 124 lcd.locate(0,3);
askksa12543 9:471f39ee2a68 125 lcd.printf("Preamble Received");
n02655194 8:1fbfeda91f7f 126
n02655194 8:1fbfeda91f7f 127 //address while loop
n02655194 8:1fbfeda91f7f 128 if(check_tbyte(ADDRESS)) {
n02655194 8:1fbfeda91f7f 129 //clear lcd screen, print current build message
n02655194 8:1fbfeda91f7f 130 lcd.cls();
n02655194 8:1fbfeda91f7f 131 lcd.locate(0,3);
askksa12543 9:471f39ee2a68 132 lcd.printf("Address Received: %c", temp);
askksa12543 9:471f39ee2a68 133 d_flag = 1;
askksa12543 9:471f39ee2a68 134 }
askksa12543 9:471f39ee2a68 135 }
askksa12543 9:471f39ee2a68 136
askksa12543 9:471f39ee2a68 137 while(i<3) {
n02655194 8:1fbfeda91f7f 138 //crc while loop / store data into character array if crc checks.
n02655194 8:1fbfeda91f7f 139 data[i] = 0; //initialize current array position to zero
n02655194 8:1fbfeda91f7f 140 data[i] = read_byte(); //store successfully transmitted data
n02655194 8:1fbfeda91f7f 141 j = 0; //reset data read loop
n02655194 8:1fbfeda91f7f 142 i++; //increment array position
n02655194 8:1fbfeda91f7f 143 }
n02655194 8:1fbfeda91f7f 144 //clear debugging message - and reset lcd to original position before printing data.
n02655194 8:1fbfeda91f7f 145 lcd.cls();
n02655194 8:1fbfeda91f7f 146 lcd.locate(0,3);
n02655194 8:1fbfeda91f7f 147 lcd.printf("Received: ");
n02655194 8:1fbfeda91f7f 148 for(i=0; i<3; i++)
n02655194 8:1fbfeda91f7f 149 lcd.printf("%c", data[i]);
n02655194 8:1fbfeda91f7f 150 }