Same as LPC, but with correct Address
Dependencies: C12832 mbed DataCommLPC2
main.cpp@7:dbec2e8ce07f, 2015-05-05 (annotated)
- Committer:
- n02655194
- Date:
- Tue May 05 05:42:14 2015 +0000
- Revision:
- 7:dbec2e8ce07f
- Parent:
- 6:7c921924cdd3
- Child:
- 8:ed25ec8935d0
Still Having Sync Issues
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
n02655194 | 0:03b21cb48863 | 1 | #include "mbed.h" |
n02655194 | 0:03b21cb48863 | 2 | #include "stdio.h" |
n02655194 | 0:03b21cb48863 | 3 | #include "C12832.h" |
n02655194 | 0:03b21cb48863 | 4 | |
n02655194 | 0:03b21cb48863 | 5 | #define MAX 100 //set the size of the character data storage array |
n02655194 | 0:03b21cb48863 | 6 | #define BYTE 8 |
n02655194 | 0:03b21cb48863 | 7 | #define NIBBLE 4 //used to set size of data read |
n02655194 | 0:03b21cb48863 | 8 | #define PREAMBLE 0x7E //preamble of 01111110 |
n02655194 | 0:03b21cb48863 | 9 | #define POSTAMBLE 0x81 //postamble of 10000001 |
n02655194 | 4:9adf7acadda2 | 10 | #define ADDRESS 0x11 //address of 00010010 - network of 1, id of 2. |
n02655194 | 0:03b21cb48863 | 11 | #define NETWORK 0x10 //network portion of the address |
n02655194 | 0:03b21cb48863 | 12 | #define ID 0x02 //id portion of the address |
n02655194 | 0:03b21cb48863 | 13 | #define BROADCAST 0x00 //address of 00000000 |
n02655194 | 0:03b21cb48863 | 14 | //multicast using 4bit address / 4 bit network. network = 4 msb, address = 4 lsb. broadcast = all 0's. multicast = network id & address of 0's. |
n02655194 | 0:03b21cb48863 | 15 | #define CRC 0x13 //crc of 10011 or x^4+x+1 or crc-5 |
n02655194 | 0:03b21cb48863 | 16 | |
n02655194 | 0:03b21cb48863 | 17 | |
n02655194 | 0:03b21cb48863 | 18 | C12832 lcd(p5, p7, p6, p8, p11); //LCD structure |
n02655194 | 0:03b21cb48863 | 19 | DigitalOut myled(LED1), myled2(LED2), myled3(LED3), myled4(LED4); //variables to access the four blue leds |
n02655194 | 0:03b21cb48863 | 20 | DigitalIn clock_pin(p21), serial_in(p22); //clock pulse input and data input pins |
n02655194 | 0:03b21cb48863 | 21 | Timer t; //timer for pausing after postamble received before displaying data |
n02655194 | 4:9adf7acadda2 | 22 | unsigned char temp, data[MAX], crc_calc=0; //temp byte storage, storage array, transmitted crc value |
n02655194 | 7:dbec2e8ce07f | 23 | char c[1];//temp_data moved from unsigned char |
n02655194 | 7:dbec2e8ce07f | 24 | unsigned char preamble, address, i, j, k, temp_data; //increment variables |
n02655194 | 5:7632d56db35e | 25 | unsigned char data_flag, rflag, d_flag, done; //data flags |
n02655194 | 1:ab56f995aa13 | 26 | int crc_passed = 0; //crc flag |
n02655194 | 0:03b21cb48863 | 27 | int temp_crc = 0; //stores values for crc check. |
n02655194 | 0:03b21cb48863 | 28 | |
n02655194 | 0:03b21cb48863 | 29 | //funtion prototypes |
n02655194 | 0:03b21cb48863 | 30 | void check_byte(int value); //stays inside the function until the received byte matches the value passed into the function (PREAMBLE) |
n02655194 | 0:03b21cb48863 | 31 | int check_abyte();//after preamble received checks the next byte for address. Returns 1 if address received matches ADDRESS, BROADCAST, or multicast; 0 if not. |
n02655194 | 0:03b21cb48863 | 32 | int read_byte(int size); //reads received data and returns it a byte at a time. |
n02655194 | 4:9adf7acadda2 | 33 | int get_crc(int temp_crc); |
n02655194 | 0:03b21cb48863 | 34 | |
n02655194 | 2:25380de9c996 | 35 | //Improvements |
n02655194 | 2:25380de9c996 | 36 | //if crc correct display correct crc |
n02655194 | 2:25380de9c996 | 37 | //if crc correct send ACK(1) to Master |
n02655194 | 2:25380de9c996 | 38 | //if crc wrong, display incorrect crc |
n02655194 | 2:25380de9c996 | 39 | //if crc wrong, send NoACK(0) to Master |
n02655194 | 2:25380de9c996 | 40 | //if crc wrong adjust for retransmit |
n02655194 | 2:25380de9c996 | 41 | |
n02655194 | 0:03b21cb48863 | 42 | |
n02655194 | 0:03b21cb48863 | 43 | int main() |
n02655194 | 0:03b21cb48863 | 44 | { |
n02655194 | 0:03b21cb48863 | 45 | |
n02655194 | 0:03b21cb48863 | 46 | //turn off leds |
n02655194 | 4:9adf7acadda2 | 47 | myled = 1; |
n02655194 | 0:03b21cb48863 | 48 | |
n02655194 | 0:03b21cb48863 | 49 | //initialize variables |
n02655194 | 0:03b21cb48863 | 50 | i = 0; |
n02655194 | 0:03b21cb48863 | 51 | d_flag = 0; |
n02655194 | 0:03b21cb48863 | 52 | done = 0; |
n02655194 | 1:ab56f995aa13 | 53 | crc_passed=0; |
n02655194 | 4:9adf7acadda2 | 54 | //clear lcd screen, print current build message |
n02655194 | 4:9adf7acadda2 | 55 | lcd.cls(); |
n02655194 | 4:9adf7acadda2 | 56 | lcd.locate(0,3); |
n02655194 | 4:9adf7acadda2 | 57 | lcd.printf("Serial Communication Device Started"); |
n02655194 | 0:03b21cb48863 | 58 | |
n02655194 | 4:9adf7acadda2 | 59 | while(!d_flag) { |
n02655194 | 0:03b21cb48863 | 60 | //read input clock pulse and data checking for preamble. |
n02655194 | 0:03b21cb48863 | 61 | //preamble while loop |
n02655194 | 0:03b21cb48863 | 62 | check_byte(PREAMBLE); |
n02655194 | 0:03b21cb48863 | 63 | |
n02655194 | 0:03b21cb48863 | 64 | //clear lcd screen, print current build message |
n02655194 | 0:03b21cb48863 | 65 | lcd.cls(); |
n02655194 | 0:03b21cb48863 | 66 | lcd.locate(0,3); |
n02655194 | 0:03b21cb48863 | 67 | lcd.printf("Preamble Received"); |
n02655194 | 0:03b21cb48863 | 68 | |
n02655194 | 0:03b21cb48863 | 69 | //preamble received check address (next byte), returns to preamble check if not addressed to station |
n02655194 | 0:03b21cb48863 | 70 | if(check_abyte()) |
n02655194 | 0:03b21cb48863 | 71 | d_flag = 1; |
n02655194 | 4:9adf7acadda2 | 72 | else { |
n02655194 | 4:9adf7acadda2 | 73 | //pause after incorrect address - so message is visible, then display waiting for preamble |
n02655194 | 4:9adf7acadda2 | 74 | t.start(); |
n02655194 | 4:9adf7acadda2 | 75 | //wait until the timer has reached the set time. |
n02655194 | 4:9adf7acadda2 | 76 | while(t.read_ms() < 500) { |
n02655194 | 4:9adf7acadda2 | 77 | |
n02655194 | 4:9adf7acadda2 | 78 | } |
n02655194 | 4:9adf7acadda2 | 79 | //stop and reset the timer |
n02655194 | 4:9adf7acadda2 | 80 | t.stop(); |
n02655194 | 4:9adf7acadda2 | 81 | t.reset(); |
n02655194 | 4:9adf7acadda2 | 82 | //clear lcd screen, print current build message |
n02655194 | 4:9adf7acadda2 | 83 | lcd.cls(); |
n02655194 | 4:9adf7acadda2 | 84 | lcd.locate(0,3); |
n02655194 | 4:9adf7acadda2 | 85 | lcd.printf("Waiting for preamble"); |
n02655194 | 4:9adf7acadda2 | 86 | } |
n02655194 | 0:03b21cb48863 | 87 | } |
n02655194 | 4:9adf7acadda2 | 88 | |
n02655194 | 4:9adf7acadda2 | 89 | while(!done) { |
n02655194 | 0:03b21cb48863 | 90 | //store data into character array if crc checks. |
n02655194 | 0:03b21cb48863 | 91 | temp_data = read_byte(BYTE); //store successfully transmitted data |
n02655194 | 7:dbec2e8ce07f | 92 | |
n02655194 | 6:7c921924cdd3 | 93 | //check for postamble |
n02655194 | 4:9adf7acadda2 | 94 | if(temp_data == POSTAMBLE) { |
n02655194 | 0:03b21cb48863 | 95 | //break out of while loop - data finished sending |
n02655194 | 0:03b21cb48863 | 96 | done = 1; |
n02655194 | 4:9adf7acadda2 | 97 | //clear lcd screen, print postamble message |
n02655194 | 0:03b21cb48863 | 98 | lcd.cls(); |
n02655194 | 0:03b21cb48863 | 99 | lcd.locate(0,3); |
n02655194 | 0:03b21cb48863 | 100 | lcd.printf("Postamble Received"); |
n02655194 | 0:03b21cb48863 | 101 | } |
n02655194 | 0:03b21cb48863 | 102 | //store data in character array if not postamble - check crc when appropriate |
n02655194 | 5:7632d56db35e | 103 | else {//recieves hh instead of h 0x06 |
n02655194 | 4:9adf7acadda2 | 104 | //byte1, crc1, byte2, crc2 |
n02655194 | 7:dbec2e8ce07f | 105 | data[i]=0; |
n02655194 | 7:dbec2e8ce07f | 106 | data[i] = temp_data; |
n02655194 | 7:dbec2e8ce07f | 107 | temp_crc=temp_data<<4; |
n02655194 | 7:dbec2e8ce07f | 108 | //////////////////////////////////////////////////////////doesnt seem to be reading in fast enough |
n02655194 | 7:dbec2e8ce07f | 109 | temp_data = read_byte(BYTE); //store successfully transmitted data |
n02655194 | 7:dbec2e8ce07f | 110 | temp_crc=temp_crc+(temp_data);//grab the last 4 bits from crc value byte |
n02655194 | 7:dbec2e8ce07f | 111 | if(get_crc(temp_crc)==0) { |
n02655194 | 7:dbec2e8ce07f | 112 | crc_passed=1; |
n02655194 | 7:dbec2e8ce07f | 113 | lcd.cls(); |
n02655194 | 7:dbec2e8ce07f | 114 | lcd.locate(0,3); |
n02655194 | 7:dbec2e8ce07f | 115 | lcd.printf("Data passes CRC verification."); |
n02655194 | 7:dbec2e8ce07f | 116 | } else { |
n02655194 | 7:dbec2e8ce07f | 117 | crc_passed=crc_passed & 0; |
n02655194 | 7:dbec2e8ce07f | 118 | lcd.cls(); |
n02655194 | 7:dbec2e8ce07f | 119 | lcd.locate(0,3); |
n02655194 | 7:dbec2e8ce07f | 120 | lcd.printf("Data fails CRC verification, Int Data: %x, CRC Byte %x.", temp_crc, crc_calc); |
n02655194 | 7:dbec2e8ce07f | 121 | } |
n02655194 | 0:03b21cb48863 | 122 | } |
n02655194 | 6:7c921924cdd3 | 123 | i++; //increment array position |
n02655194 | 4:9adf7acadda2 | 124 | //zero out crc temp variables |
n02655194 | 4:9adf7acadda2 | 125 | temp_crc = 0; |
n02655194 | 0:03b21cb48863 | 126 | } |
n02655194 | 4:9adf7acadda2 | 127 | //pause after displaying postamble received and then display data. |
n02655194 | 0:03b21cb48863 | 128 | t.start(); |
n02655194 | 4:9adf7acadda2 | 129 | //wait until the timer has reached the set time. |
n02655194 | 4:9adf7acadda2 | 130 | while(t.read_ms() < 1000) { |
n02655194 | 4:9adf7acadda2 | 131 | |
n02655194 | 0:03b21cb48863 | 132 | } |
n02655194 | 4:9adf7acadda2 | 133 | //stop and reset the timer |
n02655194 | 0:03b21cb48863 | 134 | t.stop(); |
n02655194 | 4:9adf7acadda2 | 135 | t.reset(); |
n02655194 | 4:9adf7acadda2 | 136 | //if(crc_passed){//if crc passes display data, send ACK |
n02655194 | 4:9adf7acadda2 | 137 | //clear debugging messages - and reset lcd to original position before printing data. |
n02655194 | 4:9adf7acadda2 | 138 | //send ACK |
n02655194 | 0:03b21cb48863 | 139 | lcd.cls(); |
n02655194 | 0:03b21cb48863 | 140 | lcd.locate(0,3); |
n02655194 | 0:03b21cb48863 | 141 | lcd.printf("Received: "); |
n02655194 | 0:03b21cb48863 | 142 | for(k=0; k<=i; k++) |
n02655194 | 0:03b21cb48863 | 143 | lcd.printf("%c", data[k]); |
n02655194 | 4:9adf7acadda2 | 144 | |
n02655194 | 0:03b21cb48863 | 145 | } |
n02655194 | 0:03b21cb48863 | 146 | |
n02655194 | 0:03b21cb48863 | 147 | void check_byte(int value) |
n02655194 | 0:03b21cb48863 | 148 | { |
n02655194 | 0:03b21cb48863 | 149 | data_flag = 1; |
n02655194 | 0:03b21cb48863 | 150 | temp = 0; |
n02655194 | 0:03b21cb48863 | 151 | rflag=0; |
n02655194 | 0:03b21cb48863 | 152 | //while loop |
n02655194 | 4:9adf7acadda2 | 153 | while(!rflag) { |
n02655194 | 0:03b21cb48863 | 154 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 4:9adf7acadda2 | 155 | if(clock_pin && data_flag) { |
n02655194 | 0:03b21cb48863 | 156 | //data is left shifted into our temporary variable. |
n02655194 | 0:03b21cb48863 | 157 | //each new data bit is moved into the least significant bit after the rest of the bits are shifted to the left |
n02655194 | 0:03b21cb48863 | 158 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
n02655194 | 0:03b21cb48863 | 159 | temp = (temp << 1) + serial_in; |
n02655194 | 0:03b21cb48863 | 160 | data_flag = 0; |
n02655194 | 0:03b21cb48863 | 161 | if(temp == value) |
n02655194 | 0:03b21cb48863 | 162 | rflag = 1; |
n02655194 | 0:03b21cb48863 | 163 | } |
n02655194 | 0:03b21cb48863 | 164 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 0:03b21cb48863 | 165 | if(!clock_pin && !data_flag) |
n02655194 | 0:03b21cb48863 | 166 | data_flag = 1; |
n02655194 | 0:03b21cb48863 | 167 | } |
n02655194 | 0:03b21cb48863 | 168 | } |
n02655194 | 0:03b21cb48863 | 169 | |
n02655194 | 0:03b21cb48863 | 170 | int check_abyte() |
n02655194 | 0:03b21cb48863 | 171 | { |
n02655194 | 0:03b21cb48863 | 172 | j = 0; |
n02655194 | 0:03b21cb48863 | 173 | temp = 0; |
n02655194 | 0:03b21cb48863 | 174 | rflag=0; |
n02655194 | 0:03b21cb48863 | 175 | //while loop |
n02655194 | 4:9adf7acadda2 | 176 | while(j<8) { |
n02655194 | 0:03b21cb48863 | 177 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 4:9adf7acadda2 | 178 | if(clock_pin && data_flag) { |
n02655194 | 0:03b21cb48863 | 179 | //data is left shifted into our temporary variable. |
n02655194 | 0:03b21cb48863 | 180 | //each new data bit is moved into the least significant bit after the rest of the bits are shifted to the left |
n02655194 | 0:03b21cb48863 | 181 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
n02655194 | 0:03b21cb48863 | 182 | temp = (temp << 1) + serial_in; |
n02655194 | 0:03b21cb48863 | 183 | j++; |
n02655194 | 0:03b21cb48863 | 184 | data_flag = 0; |
n02655194 | 0:03b21cb48863 | 185 | } |
n02655194 | 0:03b21cb48863 | 186 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 0:03b21cb48863 | 187 | if(!clock_pin && !data_flag) |
n02655194 | 0:03b21cb48863 | 188 | data_flag = 1; |
n02655194 | 0:03b21cb48863 | 189 | } |
n02655194 | 4:9adf7acadda2 | 190 | |
n02655194 | 0:03b21cb48863 | 191 | //clear lcd screen, print current build message |
n02655194 | 0:03b21cb48863 | 192 | lcd.cls(); |
n02655194 | 0:03b21cb48863 | 193 | lcd.locate(0,3); |
n02655194 | 4:9adf7acadda2 | 194 | if(temp == ADDRESS) { |
n02655194 | 0:03b21cb48863 | 195 | rflag = 1; |
n02655194 | 0:03b21cb48863 | 196 | lcd.printf("Address Received"); |
n02655194 | 4:9adf7acadda2 | 197 | } else if(temp == BROADCAST) { |
n02655194 | 0:03b21cb48863 | 198 | rflag = 1; |
n02655194 | 0:03b21cb48863 | 199 | lcd.printf("Broadcast received"); |
n02655194 | 4:9adf7acadda2 | 200 | } else if(((temp & 0xF0) == NETWORK) && ((temp & 0x0F) == 0)) { |
n02655194 | 0:03b21cb48863 | 201 | rflag = 1; |
n02655194 | 0:03b21cb48863 | 202 | lcd.printf("Multicast received"); |
n02655194 | 1:ab56f995aa13 | 203 | }//can add if Network==0 and address==x send to x in every network |
n02655194 | 0:03b21cb48863 | 204 | else |
n02655194 | 4:9adf7acadda2 | 205 | lcd.printf("Wrong address received"); |
n02655194 | 4:9adf7acadda2 | 206 | |
n02655194 | 0:03b21cb48863 | 207 | return rflag; |
n02655194 | 0:03b21cb48863 | 208 | } |
n02655194 | 0:03b21cb48863 | 209 | |
n02655194 | 0:03b21cb48863 | 210 | int read_byte(int size) |
n02655194 | 0:03b21cb48863 | 211 | { |
n02655194 | 0:03b21cb48863 | 212 | j = 0; |
n02655194 | 0:03b21cb48863 | 213 | temp = 0; |
n02655194 | 0:03b21cb48863 | 214 | |
n02655194 | 0:03b21cb48863 | 215 | //read a byte/nibble at a time and return it to main |
n02655194 | 4:9adf7acadda2 | 216 | while(j<size) { |
n02655194 | 0:03b21cb48863 | 217 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 0:03b21cb48863 | 218 | if(clock_pin && data_flag) { |
n02655194 | 0:03b21cb48863 | 219 | //data is left shifted into our temporary variable. |
n02655194 | 0:03b21cb48863 | 220 | //each new data bit is moved into the least significant bit afater the rest of the bits are shifted to the left |
n02655194 | 0:03b21cb48863 | 221 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
n02655194 | 0:03b21cb48863 | 222 | temp = (temp << 1) + serial_in; |
n02655194 | 0:03b21cb48863 | 223 | //increment j (tracks bits received) - turn off data_flag until clock changes. |
n02655194 | 0:03b21cb48863 | 224 | j++; |
n02655194 | 0:03b21cb48863 | 225 | data_flag = 0; |
n02655194 | 0:03b21cb48863 | 226 | } |
n02655194 | 0:03b21cb48863 | 227 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 0:03b21cb48863 | 228 | if(!clock_pin && !data_flag) |
n02655194 | 0:03b21cb48863 | 229 | data_flag = 1; |
n02655194 | 0:03b21cb48863 | 230 | } |
n02655194 | 0:03b21cb48863 | 231 | return temp; |
n02655194 | 0:03b21cb48863 | 232 | } |
n02655194 | 0:03b21cb48863 | 233 | |
n02655194 | 4:9adf7acadda2 | 234 | int get_crc(int temp_crc) |
n02655194 | 5:7632d56db35e | 235 | { |
n02655194 | 6:7c921924cdd3 | 236 | int j = 11, b = 0, z = 0, Y = 0, C0 = 0, C1 = 0, C2 = 0, C3 = 0; |
n02655194 | 5:7632d56db35e | 237 | while(j > -1) { |
n02655194 | 5:7632d56db35e | 238 | b = (temp_crc >> j) % 2;//bit |
n02655194 | 5:7632d56db35e | 239 | Y = C3 ^ b; |
n02655194 | 5:7632d56db35e | 240 | C3 = C2; |
n02655194 | 5:7632d56db35e | 241 | C2 = C1; |
n02655194 | 5:7632d56db35e | 242 | C1 = Y ^ C0; |
n02655194 | 5:7632d56db35e | 243 | C0 = Y; |
n02655194 | 5:7632d56db35e | 244 | j--; |
n02655194 | 4:9adf7acadda2 | 245 | } |
n02655194 | 5:7632d56db35e | 246 | z=z+C3<<1; |
n02655194 | 5:7632d56db35e | 247 | z=z+C2<<1; |
n02655194 | 5:7632d56db35e | 248 | z=z+C1<<1; |
n02655194 | 5:7632d56db35e | 249 | z=z+C0; |
n02655194 | 5:7632d56db35e | 250 | return z; |
n02655194 | 0:03b21cb48863 | 251 | } |