Receive data and clock for data communications example

Dependencies:   C12832 mbed

Committer:
askksa12543
Date:
Thu Apr 30 14:05:44 2015 +0000
Revision:
21:874a80ebcab5
Parent:
20:38caf615a9e1
crc fixed

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 13:14002c51e465 6 #define BYTE 8
askksa12543 20:38caf615a9e1 7 #define NIBBLE 4 //used to set size of data read
askksa12543 5:27d091ff2a99 8 #define PREAMBLE 0x7E //preamble of 01111110
askksa12543 13:14002c51e465 9 #define POSTAMBLE 0x81 //postamble of 10000001
n02655194 14:5a55a41dc118 10 #define ADDRESS 0x11 //address of 00010010 - network of 1, id of 2.
askksa12543 13:14002c51e465 11 #define NETWORK 0x10 //network portion of the address
askksa12543 13:14002c51e465 12 #define ID 0x02 //id portion of the address
askksa12543 13:14002c51e465 13 #define BROADCAST 0x00 //address of 00000000
askksa12543 13:14002c51e465 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.
askksa12543 13:14002c51e465 15 #define CRC 0x13 //crc of 10011 or x^4+x+1 or crc-5
askksa12543 13:14002c51e465 16
askksa12543 0:538c5d1e0d1d 17
askksa12543 2:886800d05776 18 C12832 lcd(p5, p7, p6, p8, p11); //LCD structure
askksa12543 4:2c92fc5e1563 19 DigitalOut myled(LED1), myled2(LED2), myled3(LED3), myled4(LED4); //variables to access the four blue leds
askksa12543 4:2c92fc5e1563 20 DigitalIn clock_pin(p21), serial_in(p22); //clock pulse input and data input pins
askksa12543 13:14002c51e465 21 Timer t; //timer for pausing after postamble received before displaying data
askksa12543 19:cbb3a8d9d007 22 unsigned char temp, data[MAX], crc_calc=0; //temp byte storage, storage array, transmitted crc value
askksa12543 13:14002c51e465 23 unsigned char preamble, address, i, j, k; //increment variables
askksa12543 13:14002c51e465 24 unsigned char data_flag, rflag, d_flag, done, temp_data; //data flags
n02655194 15:0f8871dd3324 25 int crc_passed = 0; //crc flag
askksa12543 13:14002c51e465 26 int temp_crc = 0; //stores values for crc check.
askksa12543 5:27d091ff2a99 27
askksa12543 13:14002c51e465 28 //funtion prototypes
askksa12543 13:14002c51e465 29 void check_byte(int value); //stays inside the function until the received byte matches the value passed into the function (PREAMBLE)
askksa12543 13:14002c51e465 30 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.
askksa12543 13:14002c51e465 31 int read_byte(int size); //reads received data and returns it a byte at a time.
askksa12543 13:14002c51e465 32 int check_crc(int temp_crc, unsigned char crc_calc); //double checks the sent crc value - data sent without error.
n02655194 8:1fbfeda91f7f 33
n02655194 16:ca9f769ff024 34 //Improvements
n02655194 16:ca9f769ff024 35 //if crc correct display correct crc
n02655194 16:ca9f769ff024 36 //if crc correct send ACK(1) to Master
n02655194 16:ca9f769ff024 37 //if crc wrong, display incorrect crc
n02655194 16:ca9f769ff024 38 //if crc wrong, send NoACK(0) to Master
n02655194 16:ca9f769ff024 39 //if crc wrong adjust for retransmit
n02655194 16:ca9f769ff024 40
n02655194 8:1fbfeda91f7f 41
n02655194 8:1fbfeda91f7f 42 int main()
n02655194 8:1fbfeda91f7f 43 {
n02655194 8:1fbfeda91f7f 44
n02655194 8:1fbfeda91f7f 45 //turn off leds
n02655194 8:1fbfeda91f7f 46 myled = 0;
n02655194 8:1fbfeda91f7f 47 myled2 = 0;
n02655194 8:1fbfeda91f7f 48 myled3 = 0;
n02655194 8:1fbfeda91f7f 49 myled4 = 0;
n02655194 8:1fbfeda91f7f 50
n02655194 8:1fbfeda91f7f 51 //initialize variables
n02655194 8:1fbfeda91f7f 52 i = 0;
n02655194 10:971ed3df27a7 53 d_flag = 0;
askksa12543 13:14002c51e465 54 done = 0;
n02655194 15:0f8871dd3324 55 crc_passed=0;
askksa12543 18:15d54b8d7ccc 56 //clear lcd screen, print current build message
askksa12543 18:15d54b8d7ccc 57 lcd.cls();
askksa12543 18:15d54b8d7ccc 58 lcd.locate(0,3);
askksa12543 18:15d54b8d7ccc 59 lcd.printf("Serial Communication Device Started");
n02655194 8:1fbfeda91f7f 60
askksa12543 13:14002c51e465 61 while(!d_flag)
askksa12543 13:14002c51e465 62 {
askksa12543 13:14002c51e465 63 //read input clock pulse and data checking for preamble.
askksa12543 13:14002c51e465 64 //preamble while loop
askksa12543 13:14002c51e465 65 check_byte(PREAMBLE);
n02655194 8:1fbfeda91f7f 66
n02655194 8:1fbfeda91f7f 67 //clear lcd screen, print current build message
n02655194 8:1fbfeda91f7f 68 lcd.cls();
n02655194 8:1fbfeda91f7f 69 lcd.locate(0,3);
askksa12543 13:14002c51e465 70 lcd.printf("Preamble Received");
askksa12543 13:14002c51e465 71
askksa12543 13:14002c51e465 72 //preamble received check address (next byte), returns to preamble check if not addressed to station
askksa12543 13:14002c51e465 73 if(check_abyte())
askksa12543 13:14002c51e465 74 d_flag = 1;
askksa12543 18:15d54b8d7ccc 75 else
askksa12543 18:15d54b8d7ccc 76 {
askksa12543 18:15d54b8d7ccc 77 //pause after incorrect address - so message is visible, then display waiting for preamble
askksa12543 18:15d54b8d7ccc 78 t.start();
askksa12543 18:15d54b8d7ccc 79 //wait until the timer has reached the set time.
askksa12543 19:cbb3a8d9d007 80 while(t.read_ms() < 500)
askksa12543 18:15d54b8d7ccc 81 {
askksa12543 18:15d54b8d7ccc 82
askksa12543 18:15d54b8d7ccc 83 }
askksa12543 18:15d54b8d7ccc 84 //stop and reset the timer
askksa12543 18:15d54b8d7ccc 85 t.stop();
askksa12543 18:15d54b8d7ccc 86 t.reset();
askksa12543 18:15d54b8d7ccc 87 //clear lcd screen, print current build message
askksa12543 18:15d54b8d7ccc 88 lcd.cls();
askksa12543 18:15d54b8d7ccc 89 lcd.locate(0,3);
askksa12543 18:15d54b8d7ccc 90 lcd.printf("Waiting for preamble");
askksa12543 18:15d54b8d7ccc 91 }
askksa12543 9:471f39ee2a68 92 }
askksa12543 9:471f39ee2a68 93
askksa12543 13:14002c51e465 94 while(!done)
askksa12543 13:14002c51e465 95 {
askksa12543 13:14002c51e465 96 //store data into character array if crc checks.
askksa12543 13:14002c51e465 97 data[i] = 0; //initialize current array position to zero
askksa12543 13:14002c51e465 98 temp_data = read_byte(BYTE); //store successfully transmitted data
askksa12543 13:14002c51e465 99
askksa12543 13:14002c51e465 100 //check for postamble
askksa12543 13:14002c51e465 101 if(temp_data == POSTAMBLE)
askksa12543 13:14002c51e465 102 {
askksa12543 13:14002c51e465 103 //break out of while loop - data finished sending
askksa12543 13:14002c51e465 104 done = 1;
askksa12543 19:cbb3a8d9d007 105 //clear lcd screen, print postamble message
askksa12543 13:14002c51e465 106 lcd.cls();
askksa12543 13:14002c51e465 107 lcd.locate(0,3);
askksa12543 13:14002c51e465 108 lcd.printf("Postamble Received");
askksa12543 13:14002c51e465 109 }
askksa12543 13:14002c51e465 110
askksa12543 13:14002c51e465 111 //store data in character array if not postamble - check crc when appropriate
askksa12543 13:14002c51e465 112 else
askksa12543 13:14002c51e465 113 {
askksa12543 13:14002c51e465 114 data[i] = temp_data;
n02655194 8:1fbfeda91f7f 115 i++; //increment array position
askksa12543 13:14002c51e465 116 //store the sent data into temp variable for crc calculation
askksa12543 19:cbb3a8d9d007 117 temp_crc <<= 8;
askksa12543 13:14002c51e465 118 temp_crc += temp_data;
askksa12543 13:14002c51e465 119 //wait until i increments and then check to see if 3 bytes have been received
askksa12543 13:14002c51e465 120 if( (i % 3) == 0)
askksa12543 13:14002c51e465 121 {
askksa12543 13:14002c51e465 122 //check crc
askksa12543 13:14002c51e465 123 crc_calc = read_byte(NIBBLE);
askksa12543 13:14002c51e465 124 if(check_crc(temp_crc, crc_calc))
askksa12543 13:14002c51e465 125 {
askksa12543 19:cbb3a8d9d007 126 //crc_passed=1;
askksa12543 19:cbb3a8d9d007 127 lcd.cls();
askksa12543 19:cbb3a8d9d007 128 lcd.locate(0,3);
askksa12543 19:cbb3a8d9d007 129 lcd.printf("Data passes CRC verification.");
askksa12543 19:cbb3a8d9d007 130 }
askksa12543 19:cbb3a8d9d007 131 else
askksa12543 19:cbb3a8d9d007 132 {
askksa12543 13:14002c51e465 133 lcd.cls();
askksa12543 13:14002c51e465 134 lcd.locate(0,3);
askksa12543 19:cbb3a8d9d007 135 lcd.printf("Data fails CRC verification, Int Data: %x, CRC Byte %x.", temp_crc, crc_calc);
askksa12543 19:cbb3a8d9d007 136 //pause after displaying postamble received and then display data.
askksa12543 19:cbb3a8d9d007 137 t.start();
askksa12543 19:cbb3a8d9d007 138 //wait until the timer has reached the set time.
askksa12543 19:cbb3a8d9d007 139 while(t.read_ms() < 10000)
askksa12543 19:cbb3a8d9d007 140 {
askksa12543 19:cbb3a8d9d007 141
n02655194 16:ca9f769ff024 142 }
askksa12543 19:cbb3a8d9d007 143 //stop and reset the timer
askksa12543 19:cbb3a8d9d007 144 t.stop();
askksa12543 19:cbb3a8d9d007 145 t.reset();
askksa12543 19:cbb3a8d9d007 146 }
askksa12543 19:cbb3a8d9d007 147 //zero out crc temp variables
askksa12543 13:14002c51e465 148 temp_crc = 0;
askksa12543 19:cbb3a8d9d007 149 crc_calc = 0;
askksa12543 13:14002c51e465 150 }
askksa12543 13:14002c51e465 151 }
askksa12543 13:14002c51e465 152 }
askksa12543 13:14002c51e465 153 //pause after displaying postamble received and then display data.
askksa12543 13:14002c51e465 154 t.start();
askksa12543 13:14002c51e465 155 //wait until the timer has reached the set time.
askksa12543 13:14002c51e465 156 while(t.read_ms() < 1000)
askksa12543 13:14002c51e465 157 {
askksa12543 13:14002c51e465 158
askksa12543 13:14002c51e465 159 }
askksa12543 13:14002c51e465 160 //stop and reset the timer
askksa12543 13:14002c51e465 161 t.stop();
askksa12543 13:14002c51e465 162 t.reset();
askksa12543 19:cbb3a8d9d007 163 //if(crc_passed){//if crc passes display data, send ACK
askksa12543 13:14002c51e465 164 //clear debugging messages - and reset lcd to original position before printing data.
n02655194 15:0f8871dd3324 165 //send ACK
askksa12543 13:14002c51e465 166 lcd.cls();
askksa12543 13:14002c51e465 167 lcd.locate(0,3);
askksa12543 13:14002c51e465 168 lcd.printf("Received: ");
askksa12543 13:14002c51e465 169 for(k=0; k<=i; k++)
askksa12543 13:14002c51e465 170 lcd.printf("%c", data[k]);
askksa12543 19:cbb3a8d9d007 171
askksa12543 13:14002c51e465 172 }
askksa12543 13:14002c51e465 173
askksa12543 13:14002c51e465 174 void check_byte(int value)
askksa12543 13:14002c51e465 175 {
askksa12543 13:14002c51e465 176 data_flag = 1;
askksa12543 13:14002c51e465 177 temp = 0;
askksa12543 13:14002c51e465 178 rflag=0;
askksa12543 13:14002c51e465 179 //while loop
askksa12543 13:14002c51e465 180 while(!rflag)
askksa12543 13:14002c51e465 181 {
askksa12543 13:14002c51e465 182 //read in data if clock is 1 and data flag is 1
askksa12543 13:14002c51e465 183 if(clock_pin && data_flag)
askksa12543 13:14002c51e465 184 {
askksa12543 13:14002c51e465 185 //data is left shifted into our temporary variable.
askksa12543 13:14002c51e465 186 //each new data bit is moved into the least significant bit after the rest of the bits are shifted to the left
askksa12543 13:14002c51e465 187 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
askksa12543 13:14002c51e465 188 temp = (temp << 1) + serial_in;
askksa12543 13:14002c51e465 189 data_flag = 0;
askksa12543 13:14002c51e465 190 if(temp == value)
askksa12543 13:14002c51e465 191 rflag = 1;
n02655194 8:1fbfeda91f7f 192 }
askksa12543 13:14002c51e465 193 //when clock returns to low - reset data flag to accept the next bit.
askksa12543 13:14002c51e465 194 if(!clock_pin && !data_flag)
askksa12543 13:14002c51e465 195 data_flag = 1;
askksa12543 13:14002c51e465 196 }
n02655194 8:1fbfeda91f7f 197 }
askksa12543 13:14002c51e465 198
askksa12543 13:14002c51e465 199 int check_abyte()
askksa12543 13:14002c51e465 200 {
askksa12543 13:14002c51e465 201 j = 0;
askksa12543 13:14002c51e465 202 temp = 0;
askksa12543 13:14002c51e465 203 rflag=0;
askksa12543 13:14002c51e465 204 //while loop
askksa12543 13:14002c51e465 205 while(j<8)
askksa12543 13:14002c51e465 206 {
askksa12543 13:14002c51e465 207 //read in data if clock is 1 and data flag is 1
askksa12543 13:14002c51e465 208 if(clock_pin && data_flag)
askksa12543 13:14002c51e465 209 {
askksa12543 13:14002c51e465 210 //data is left shifted into our temporary variable.
askksa12543 13:14002c51e465 211 //each new data bit is moved into the least significant bit after the rest of the bits are shifted to the left
askksa12543 13:14002c51e465 212 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
askksa12543 13:14002c51e465 213 temp = (temp << 1) + serial_in;
askksa12543 13:14002c51e465 214 j++;
askksa12543 13:14002c51e465 215 data_flag = 0;
askksa12543 13:14002c51e465 216 }
askksa12543 13:14002c51e465 217 //when clock returns to low - reset data flag to accept the next bit.
askksa12543 13:14002c51e465 218 if(!clock_pin && !data_flag)
askksa12543 13:14002c51e465 219 data_flag = 1;
askksa12543 13:14002c51e465 220 }
askksa12543 13:14002c51e465 221
askksa12543 13:14002c51e465 222 //clear lcd screen, print current build message
askksa12543 13:14002c51e465 223 lcd.cls();
askksa12543 13:14002c51e465 224 lcd.locate(0,3);
askksa12543 13:14002c51e465 225 if(temp == ADDRESS)
askksa12543 13:14002c51e465 226 {
askksa12543 13:14002c51e465 227 rflag = 1;
askksa12543 13:14002c51e465 228 lcd.printf("Address Received");
askksa12543 13:14002c51e465 229 }
askksa12543 13:14002c51e465 230 else if(temp == BROADCAST)
askksa12543 13:14002c51e465 231 {
askksa12543 13:14002c51e465 232 rflag = 1;
askksa12543 13:14002c51e465 233 lcd.printf("Broadcast received");
askksa12543 13:14002c51e465 234 }
askksa12543 13:14002c51e465 235 else if(((temp & 0xF0) == NETWORK) && ((temp & 0x0F) == 0))
askksa12543 13:14002c51e465 236 {
askksa12543 13:14002c51e465 237 rflag = 1;
askksa12543 13:14002c51e465 238 lcd.printf("Multicast received");
n02655194 15:0f8871dd3324 239 }//can add if Network==0 and address==x send to x in every network
askksa12543 13:14002c51e465 240 else
askksa12543 18:15d54b8d7ccc 241 lcd.printf("Wrong address received");
askksa12543 13:14002c51e465 242
askksa12543 13:14002c51e465 243 return rflag;
askksa12543 13:14002c51e465 244 }
askksa12543 13:14002c51e465 245
askksa12543 13:14002c51e465 246 int read_byte(int size)
askksa12543 13:14002c51e465 247 {
askksa12543 13:14002c51e465 248 j = 0;
askksa12543 13:14002c51e465 249 temp = 0;
askksa12543 13:14002c51e465 250
askksa12543 13:14002c51e465 251 //read a byte/nibble at a time and return it to main
askksa12543 13:14002c51e465 252 while(j<size)
askksa12543 13:14002c51e465 253 {
askksa12543 13:14002c51e465 254 //read in data if clock is 1 and data flag is 1
askksa12543 13:14002c51e465 255 if(clock_pin && data_flag) {
askksa12543 13:14002c51e465 256 //data is left shifted into our temporary variable.
askksa12543 13:14002c51e465 257 //each new data bit is moved into the least significant bit afater the rest of the bits are shifted to the left
askksa12543 13:14002c51e465 258 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
askksa12543 13:14002c51e465 259 temp = (temp << 1) + serial_in;
askksa12543 13:14002c51e465 260 //increment j (tracks bits received) - turn off data_flag until clock changes.
askksa12543 13:14002c51e465 261 j++;
askksa12543 13:14002c51e465 262 data_flag = 0;
askksa12543 13:14002c51e465 263 }
askksa12543 13:14002c51e465 264 //when clock returns to low - reset data flag to accept the next bit.
askksa12543 13:14002c51e465 265 if(!clock_pin && !data_flag)
askksa12543 13:14002c51e465 266 data_flag = 1;
askksa12543 13:14002c51e465 267 }
askksa12543 13:14002c51e465 268 return temp;
askksa12543 13:14002c51e465 269 }
askksa12543 13:14002c51e465 270
askksa12543 13:14002c51e465 271 int check_crc(int temp_crc, unsigned char crc_calc)
askksa12543 13:14002c51e465 272 {
askksa12543 20:38caf615a9e1 273 //temp variable
askksa12543 20:38caf615a9e1 274 unsigned char temp;
askksa12543 20:38caf615a9e1 275 //start at 2^25 / used to decrement through the bit places of the crc integer
askksa12543 20:38caf615a9e1 276 int j = 16777216;
askksa12543 13:14002c51e465 277 //assume data sent incorrectly, check crc to see if data sent correctly
askksa12543 13:14002c51e465 278 int data_correct = 0;
askksa12543 13:14002c51e465 279
askksa12543 13:14002c51e465 280 //shift temp 3 bytes by 4 and add transmitted crc
askksa12543 20:38caf615a9e1 281 temp_crc <<= 4;
askksa12543 13:14002c51e465 282 temp_crc += crc_calc;
askksa12543 20:38caf615a9e1 283
askksa12543 20:38caf615a9e1 284 //initialize temp variable to the 8 msb from temp_crc, then XOR with CRC constant
askksa12543 20:38caf615a9e1 285 temp = temp_crc / j;
askksa12543 20:38caf615a9e1 286 temp ^= CRC;
askksa12543 20:38caf615a9e1 287 //decrement j
askksa12543 21:874a80ebcab5 288 j /= 2;
askksa12543 20:38caf615a9e1 289
askksa12543 20:38caf615a9e1 290 //crc loop - shift in each bit position and xor with CRC constant
askksa12543 20:38caf615a9e1 291 while(j>0)
askksa12543 20:38caf615a9e1 292 {
askksa12543 20:38caf615a9e1 293 //left shift current temp value
askksa12543 20:38caf615a9e1 294 temp <<= 1;
askksa12543 20:38caf615a9e1 295 //shift in new bit from temp_crc
askksa12543 20:38caf615a9e1 296 temp += (temp_crc / j) % 2;
askksa12543 20:38caf615a9e1 297 //xor with crc constant
askksa12543 20:38caf615a9e1 298 temp ^= CRC;
askksa12543 20:38caf615a9e1 299 //decrement j
askksa12543 21:874a80ebcab5 300 j /= 2;
askksa12543 20:38caf615a9e1 301 }
askksa12543 20:38caf615a9e1 302
askksa12543 13:14002c51e465 303 //check for crc correctness
askksa12543 20:38caf615a9e1 304 if(temp == 0)
askksa12543 13:14002c51e465 305 data_correct = 1;
askksa12543 13:14002c51e465 306
askksa12543 13:14002c51e465 307 return data_correct;
askksa12543 13:14002c51e465 308 }