Same as LPC, but with correct Address

Dependencies:   C12832 mbed DataCommLPC2

Dependents:   DataCommLPC2

Committer:
n02655194
Date:
Tue May 05 04:12:23 2015 +0000
Revision:
5:7632d56db35e
Parent:
4:9adf7acadda2
Child:
6:7c921924cdd3
CRC logic and receive issue

Who changed what in which revision?

UserRevisionLine numberNew 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 5:7632d56db35e 23 char c[1], temp_data;//temp_data moved from unsigned char
n02655194 0:03b21cb48863 24 unsigned char preamble, address, i, j, k; //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 data[i] = 0; //initialize current array position to zero
n02655194 0:03b21cb48863 92 temp_data = read_byte(BYTE); //store successfully transmitted data
n02655194 4:9adf7acadda2 93 // lcd.cls();
n02655194 4:9adf7acadda2 94 // lcd.locate(0,3);
n02655194 4:9adf7acadda2 95 // c[1]=temp_data;
n02655194 4:9adf7acadda2 96 // lcd.printf(c); //check for postamble
n02655194 4:9adf7acadda2 97 if(temp_data == POSTAMBLE) {
n02655194 0:03b21cb48863 98 //break out of while loop - data finished sending
n02655194 0:03b21cb48863 99 done = 1;
n02655194 4:9adf7acadda2 100 //clear lcd screen, print postamble message
n02655194 0:03b21cb48863 101 lcd.cls();
n02655194 0:03b21cb48863 102 lcd.locate(0,3);
n02655194 0:03b21cb48863 103 lcd.printf("Postamble Received");
n02655194 0:03b21cb48863 104 }
n02655194 4:9adf7acadda2 105
n02655194 0:03b21cb48863 106 //store data in character array if not postamble - check crc when appropriate
n02655194 5:7632d56db35e 107 else {//recieves hh instead of h 0x06
n02655194 4:9adf7acadda2 108 //byte1, crc1, byte2, crc2
n02655194 4:9adf7acadda2 109 if((i % 2)==0) { //byte
n02655194 4:9adf7acadda2 110 data[i] = temp_data;
n02655194 5:7632d56db35e 111 temp_crc=temp_data<<4;
n02655194 5:7632d56db35e 112 t.start();
n02655194 5:7632d56db35e 113 //wait until the timer has reached the set time.
n02655194 5:7632d56db35e 114 while(t.read_ms() < 750) {
n02655194 5:7632d56db35e 115 }
n02655194 5:7632d56db35e 116 //stop and reset the timer
n02655194 5:7632d56db35e 117 t.stop();
n02655194 5:7632d56db35e 118 t.reset();
n02655194 4:9adf7acadda2 119 } else { //crc value
n02655194 4:9adf7acadda2 120 temp_crc=temp_crc+(temp_data && 0x0F);//grab the last 4 bits from crc value byte
n02655194 5:7632d56db35e 121 lcd.cls();
n02655194 5:7632d56db35e 122 lcd.locate(0,3);
n02655194 5:7632d56db35e 123 c[0]=temp_data;
n02655194 5:7632d56db35e 124 lcd.printf(c);
n02655194 5:7632d56db35e 125 t.start();
n02655194 5:7632d56db35e 126 //wait until the timer has reached the set time.
n02655194 5:7632d56db35e 127 while(t.read_ms() < 1000) {
n02655194 5:7632d56db35e 128 }
n02655194 5:7632d56db35e 129 //stop and reset the timer
n02655194 5:7632d56db35e 130 t.stop();
n02655194 5:7632d56db35e 131 t.reset();
n02655194 4:9adf7acadda2 132 if(get_crc(temp_crc)==0) {////////////////////////////Fix CRC_passed logic(crc_passed starts 1, &s)
n02655194 1:ab56f995aa13 133 crc_passed=1;
n02655194 0:03b21cb48863 134 lcd.cls();
n02655194 0:03b21cb48863 135 lcd.locate(0,3);
n02655194 4:9adf7acadda2 136 lcd.printf("Data passes CRC verification.");
n02655194 4:9adf7acadda2 137 t.start();
n02655194 4:9adf7acadda2 138 //wait until the timer has reached the set time.
n02655194 4:9adf7acadda2 139 while(t.read_ms() < 1000) {
n02655194 4:9adf7acadda2 140 }
n02655194 4:9adf7acadda2 141 //stop and reset the timer
n02655194 4:9adf7acadda2 142 t.stop();
n02655194 4:9adf7acadda2 143 t.reset();
n02655194 4:9adf7acadda2 144 } else {
n02655194 4:9adf7acadda2 145 crc_passed=crc_passed & 0;
n02655194 2:25380de9c996 146 lcd.cls();
n02655194 2:25380de9c996 147 lcd.locate(0,3);
n02655194 4:9adf7acadda2 148 lcd.printf("Data fails CRC verification, Int Data: %x, CRC Byte %x.", temp_crc, crc_calc);
n02655194 4:9adf7acadda2 149 }
n02655194 0:03b21cb48863 150 }
n02655194 4:9adf7acadda2 151 i++; //increment array position
n02655194 4:9adf7acadda2 152
n02655194 0:03b21cb48863 153 }
n02655194 4:9adf7acadda2 154 //zero out crc temp variables
n02655194 4:9adf7acadda2 155 temp_crc = 0;
n02655194 0:03b21cb48863 156 }
n02655194 4:9adf7acadda2 157 //pause after displaying postamble received and then display data.
n02655194 0:03b21cb48863 158 t.start();
n02655194 4:9adf7acadda2 159 //wait until the timer has reached the set time.
n02655194 4:9adf7acadda2 160 while(t.read_ms() < 1000) {
n02655194 4:9adf7acadda2 161
n02655194 0:03b21cb48863 162 }
n02655194 4:9adf7acadda2 163 //stop and reset the timer
n02655194 0:03b21cb48863 164 t.stop();
n02655194 4:9adf7acadda2 165 t.reset();
n02655194 4:9adf7acadda2 166 //if(crc_passed){//if crc passes display data, send ACK
n02655194 4:9adf7acadda2 167 //clear debugging messages - and reset lcd to original position before printing data.
n02655194 4:9adf7acadda2 168 //send ACK
n02655194 0:03b21cb48863 169 lcd.cls();
n02655194 0:03b21cb48863 170 lcd.locate(0,3);
n02655194 0:03b21cb48863 171 lcd.printf("Received: ");
n02655194 0:03b21cb48863 172 for(k=0; k<=i; k++)
n02655194 0:03b21cb48863 173 lcd.printf("%c", data[k]);
n02655194 4:9adf7acadda2 174
n02655194 0:03b21cb48863 175 }
n02655194 0:03b21cb48863 176
n02655194 0:03b21cb48863 177 void check_byte(int value)
n02655194 0:03b21cb48863 178 {
n02655194 0:03b21cb48863 179 data_flag = 1;
n02655194 0:03b21cb48863 180 temp = 0;
n02655194 0:03b21cb48863 181 rflag=0;
n02655194 0:03b21cb48863 182 //while loop
n02655194 4:9adf7acadda2 183 while(!rflag) {
n02655194 0:03b21cb48863 184 //read in data if clock is 1 and data flag is 1
n02655194 4:9adf7acadda2 185 if(clock_pin && data_flag) {
n02655194 0:03b21cb48863 186 //data is left shifted into our temporary variable.
n02655194 0:03b21cb48863 187 //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 188 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
n02655194 0:03b21cb48863 189 temp = (temp << 1) + serial_in;
n02655194 0:03b21cb48863 190 data_flag = 0;
n02655194 0:03b21cb48863 191 if(temp == value)
n02655194 0:03b21cb48863 192 rflag = 1;
n02655194 0:03b21cb48863 193 }
n02655194 0:03b21cb48863 194 //when clock returns to low - reset data flag to accept the next bit.
n02655194 0:03b21cb48863 195 if(!clock_pin && !data_flag)
n02655194 0:03b21cb48863 196 data_flag = 1;
n02655194 0:03b21cb48863 197 }
n02655194 0:03b21cb48863 198 }
n02655194 0:03b21cb48863 199
n02655194 0:03b21cb48863 200 int check_abyte()
n02655194 0:03b21cb48863 201 {
n02655194 0:03b21cb48863 202 j = 0;
n02655194 0:03b21cb48863 203 temp = 0;
n02655194 0:03b21cb48863 204 rflag=0;
n02655194 0:03b21cb48863 205 //while loop
n02655194 4:9adf7acadda2 206 while(j<8) {
n02655194 0:03b21cb48863 207 //read in data if clock is 1 and data flag is 1
n02655194 4:9adf7acadda2 208 if(clock_pin && data_flag) {
n02655194 0:03b21cb48863 209 //data is left shifted into our temporary variable.
n02655194 0:03b21cb48863 210 //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 211 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
n02655194 0:03b21cb48863 212 temp = (temp << 1) + serial_in;
n02655194 0:03b21cb48863 213 j++;
n02655194 0:03b21cb48863 214 data_flag = 0;
n02655194 0:03b21cb48863 215 }
n02655194 0:03b21cb48863 216 //when clock returns to low - reset data flag to accept the next bit.
n02655194 0:03b21cb48863 217 if(!clock_pin && !data_flag)
n02655194 0:03b21cb48863 218 data_flag = 1;
n02655194 0:03b21cb48863 219 }
n02655194 4:9adf7acadda2 220
n02655194 0:03b21cb48863 221 //clear lcd screen, print current build message
n02655194 0:03b21cb48863 222 lcd.cls();
n02655194 0:03b21cb48863 223 lcd.locate(0,3);
n02655194 4:9adf7acadda2 224 if(temp == ADDRESS) {
n02655194 0:03b21cb48863 225 rflag = 1;
n02655194 0:03b21cb48863 226 lcd.printf("Address Received");
n02655194 4:9adf7acadda2 227 } else if(temp == BROADCAST) {
n02655194 0:03b21cb48863 228 rflag = 1;
n02655194 0:03b21cb48863 229 lcd.printf("Broadcast received");
n02655194 4:9adf7acadda2 230 } else if(((temp & 0xF0) == NETWORK) && ((temp & 0x0F) == 0)) {
n02655194 0:03b21cb48863 231 rflag = 1;
n02655194 0:03b21cb48863 232 lcd.printf("Multicast received");
n02655194 1:ab56f995aa13 233 }//can add if Network==0 and address==x send to x in every network
n02655194 0:03b21cb48863 234 else
n02655194 4:9adf7acadda2 235 lcd.printf("Wrong address received");
n02655194 4:9adf7acadda2 236
n02655194 0:03b21cb48863 237 return rflag;
n02655194 0:03b21cb48863 238 }
n02655194 0:03b21cb48863 239
n02655194 0:03b21cb48863 240 int read_byte(int size)
n02655194 0:03b21cb48863 241 {
n02655194 0:03b21cb48863 242 j = 0;
n02655194 0:03b21cb48863 243 temp = 0;
n02655194 0:03b21cb48863 244
n02655194 0:03b21cb48863 245 //read a byte/nibble at a time and return it to main
n02655194 4:9adf7acadda2 246 while(j<size) {
n02655194 0:03b21cb48863 247 //read in data if clock is 1 and data flag is 1
n02655194 0:03b21cb48863 248 if(clock_pin && data_flag) {
n02655194 0:03b21cb48863 249 //data is left shifted into our temporary variable.
n02655194 0:03b21cb48863 250 //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 251 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
n02655194 0:03b21cb48863 252 temp = (temp << 1) + serial_in;
n02655194 0:03b21cb48863 253 //increment j (tracks bits received) - turn off data_flag until clock changes.
n02655194 0:03b21cb48863 254 j++;
n02655194 0:03b21cb48863 255 data_flag = 0;
n02655194 0:03b21cb48863 256 }
n02655194 0:03b21cb48863 257 //when clock returns to low - reset data flag to accept the next bit.
n02655194 0:03b21cb48863 258 if(!clock_pin && !data_flag)
n02655194 0:03b21cb48863 259 data_flag = 1;
n02655194 0:03b21cb48863 260 }
n02655194 0:03b21cb48863 261 return temp;
n02655194 0:03b21cb48863 262 }
n02655194 0:03b21cb48863 263
n02655194 4:9adf7acadda2 264 int get_crc(int temp_crc)
n02655194 5:7632d56db35e 265 {
n02655194 5:7632d56db35e 266 int j = 7, b = 0, z = 0, Y = 0, C0 = 0, C1 = 0, C2 = 0, C3 = 0;
n02655194 5:7632d56db35e 267 while(j > -1) {
n02655194 5:7632d56db35e 268 b = (temp_crc >> j) % 2;//bit
n02655194 5:7632d56db35e 269 Y = C3 ^ b;
n02655194 5:7632d56db35e 270 C3 = C2;
n02655194 5:7632d56db35e 271 C2 = C1;
n02655194 5:7632d56db35e 272 C1 = Y ^ C0;
n02655194 5:7632d56db35e 273 C0 = Y;
n02655194 5:7632d56db35e 274 j--;
n02655194 4:9adf7acadda2 275 }
n02655194 5:7632d56db35e 276 z=z+C3<<1;
n02655194 5:7632d56db35e 277 z=z+C2<<1;
n02655194 5:7632d56db35e 278 z=z+C1<<1;
n02655194 5:7632d56db35e 279 z=z+C0;
n02655194 5:7632d56db35e 280 return z;
n02655194 0:03b21cb48863 281 }