![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Receive data and clock for data communications example
main.cpp@10:971ed3df27a7, 2015-04-02 (annotated)
- Committer:
- n02655194
- Date:
- Thu Apr 02 14:25:46 2015 +0000
- Revision:
- 10:971ed3df27a7
- Parent:
- 9:471f39ee2a68
- Child:
- 11:b37d0454f623
d_flag;
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:886800d05776 | 23 | temp = 0; |
n02655194 | 8:1fbfeda91f7f | 24 | rflag=0; |
n02655194 | 8:1fbfeda91f7f | 25 | //while loop |
n02655194 | 8:1fbfeda91f7f | 26 | while(!rflag) { |
askksa12543 | 5:27d091ff2a99 | 27 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 8:1fbfeda91f7f | 28 | if(clock_pin && data_flag) { |
askksa12543 | 5:27d091ff2a99 | 29 | //data is left shifted into our temporary variable. |
askksa12543 | 6:46fe2170bb5c | 30 | //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 | 31 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
askksa12543 | 5:27d091ff2a99 | 32 | temp = (temp << 1) + serial_in; |
askksa12543 | 5:27d091ff2a99 | 33 | data_flag = 0; |
n02655194 | 8:1fbfeda91f7f | 34 | if(temp == value) { |
n02655194 | 8:1fbfeda91f7f | 35 | rflag = 1; |
askksa12543 | 5:27d091ff2a99 | 36 | } |
askksa12543 | 5:27d091ff2a99 | 37 | } |
askksa12543 | 5:27d091ff2a99 | 38 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 8:1fbfeda91f7f | 39 | if(!clock_pin && !data_flag) { |
askksa12543 | 5:27d091ff2a99 | 40 | data_flag = 1; |
askksa12543 | 5:27d091ff2a99 | 41 | } |
n02655194 | 8:1fbfeda91f7f | 42 | } |
n02655194 | 8:1fbfeda91f7f | 43 | } |
askksa12543 | 5:27d091ff2a99 | 44 | |
n02655194 | 8:1fbfeda91f7f | 45 | int check_tbyte(int value) |
n02655194 | 8:1fbfeda91f7f | 46 | { |
n02655194 | 8:1fbfeda91f7f | 47 | j = 0; |
n02655194 | 8:1fbfeda91f7f | 48 | data_flag = 1; |
n02655194 | 8:1fbfeda91f7f | 49 | temp = 0; |
n02655194 | 8:1fbfeda91f7f | 50 | rflag=0; |
n02655194 | 8:1fbfeda91f7f | 51 | //while loop |
n02655194 | 8:1fbfeda91f7f | 52 | while(j<8) { |
askksa12543 | 7:a4a28f144b07 | 53 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 8:1fbfeda91f7f | 54 | if(clock_pin && data_flag) { |
askksa12543 | 7:a4a28f144b07 | 55 | //data is left shifted into our temporary variable. |
askksa12543 | 7:a4a28f144b07 | 56 | //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 | 57 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
askksa12543 | 7:a4a28f144b07 | 58 | temp = (temp << 1) + serial_in; |
n02655194 | 8:1fbfeda91f7f | 59 | j++; |
askksa12543 | 7:a4a28f144b07 | 60 | data_flag = 0; |
n02655194 | 8:1fbfeda91f7f | 61 | if(temp == value) { |
n02655194 | 8:1fbfeda91f7f | 62 | rflag = 1; |
askksa12543 | 7:a4a28f144b07 | 63 | } |
askksa12543 | 7:a4a28f144b07 | 64 | } |
askksa12543 | 7:a4a28f144b07 | 65 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 8:1fbfeda91f7f | 66 | if(!clock_pin && !data_flag) { |
askksa12543 | 7:a4a28f144b07 | 67 | data_flag = 1; |
askksa12543 | 7:a4a28f144b07 | 68 | } |
n02655194 | 8:1fbfeda91f7f | 69 | } |
n02655194 | 8:1fbfeda91f7f | 70 | return rflag; |
n02655194 | 8:1fbfeda91f7f | 71 | } |
n02655194 | 8:1fbfeda91f7f | 72 | |
n02655194 | 8:1fbfeda91f7f | 73 | int read_byte(void) |
n02655194 | 8:1fbfeda91f7f | 74 | { |
n02655194 | 8:1fbfeda91f7f | 75 | j = 0; |
n02655194 | 8:1fbfeda91f7f | 76 | data_flag = 1; |
n02655194 | 8:1fbfeda91f7f | 77 | temp = 0; |
n02655194 | 8:1fbfeda91f7f | 78 | |
n02655194 | 8:1fbfeda91f7f | 79 | //second loop / variable to increment through character array. (post amble also if implemented) |
n02655194 | 8:1fbfeda91f7f | 80 | while(j<8) { |
n02655194 | 8:1fbfeda91f7f | 81 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 8:1fbfeda91f7f | 82 | if(clock_pin && data_flag) { |
n02655194 | 8:1fbfeda91f7f | 83 | //data is left shifted into our temporary variable. |
n02655194 | 8:1fbfeda91f7f | 84 | //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 | 85 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
n02655194 | 8:1fbfeda91f7f | 86 | temp = (temp << 1) + serial_in; |
n02655194 | 8:1fbfeda91f7f | 87 | //increment j (tracks 8 bits received) - turn off data_flag until clock changes. |
n02655194 | 8:1fbfeda91f7f | 88 | j++; |
n02655194 | 8:1fbfeda91f7f | 89 | data_flag = 0; |
n02655194 | 8:1fbfeda91f7f | 90 | } |
n02655194 | 8:1fbfeda91f7f | 91 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 8:1fbfeda91f7f | 92 | if(!clock_pin && !data_flag) { |
n02655194 | 8:1fbfeda91f7f | 93 | data_flag = 1; |
n02655194 | 8:1fbfeda91f7f | 94 | } |
n02655194 | 8:1fbfeda91f7f | 95 | } |
n02655194 | 8:1fbfeda91f7f | 96 | return temp; |
n02655194 | 8:1fbfeda91f7f | 97 | } |
n02655194 | 8:1fbfeda91f7f | 98 | |
n02655194 | 8:1fbfeda91f7f | 99 | int main() |
n02655194 | 8:1fbfeda91f7f | 100 | { |
n02655194 | 8:1fbfeda91f7f | 101 | |
n02655194 | 8:1fbfeda91f7f | 102 | //turn off leds |
n02655194 | 8:1fbfeda91f7f | 103 | myled = 0; |
n02655194 | 8:1fbfeda91f7f | 104 | myled2 = 0; |
n02655194 | 8:1fbfeda91f7f | 105 | myled3 = 0; |
n02655194 | 8:1fbfeda91f7f | 106 | myled4 = 0; |
n02655194 | 8:1fbfeda91f7f | 107 | |
n02655194 | 8:1fbfeda91f7f | 108 | //initialize variables |
n02655194 | 8:1fbfeda91f7f | 109 | i = 0; |
n02655194 | 10:971ed3df27a7 | 110 | d_flag = 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 | } |