Same as LPC, but with correct Address

Dependencies:   C12832 mbed DataCommLPC2

Dependents:   DataCommLPC2

Committer:
n02655194
Date:
Mon May 04 04:11:09 2015 +0000
Revision:
4:9adf7acadda2
Parent:
3:36792648739e
Child:
5:7632d56db35e
CRC 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 4:9adf7acadda2 23 char c[1];
n02655194 0:03b21cb48863 24 unsigned char preamble, address, i, j, k; //increment variables
n02655194 0:03b21cb48863 25 unsigned char data_flag, rflag, d_flag, done, temp_data; //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 4:9adf7acadda2 107 else {
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 4:9adf7acadda2 111 temp_crc=temp_data<<=4;
n02655194 4:9adf7acadda2 112 } else { //crc value
n02655194 4:9adf7acadda2 113 temp_crc=temp_crc+(temp_data && 0x0F);//grab the last 4 bits from crc value byte
n02655194 4:9adf7acadda2 114 if(get_crc(temp_crc)==0) {////////////////////////////Fix CRC_passed logic(crc_passed starts 1, &s)
n02655194 1:ab56f995aa13 115 crc_passed=1;
n02655194 0:03b21cb48863 116 lcd.cls();
n02655194 0:03b21cb48863 117 lcd.locate(0,3);
n02655194 4:9adf7acadda2 118 lcd.printf("Data passes CRC verification.");
n02655194 4:9adf7acadda2 119 t.start();
n02655194 4:9adf7acadda2 120 //wait until the timer has reached the set time.
n02655194 4:9adf7acadda2 121 while(t.read_ms() < 1000) {
n02655194 4:9adf7acadda2 122 }
n02655194 4:9adf7acadda2 123 //stop and reset the timer
n02655194 4:9adf7acadda2 124 t.stop();
n02655194 4:9adf7acadda2 125 t.reset();
n02655194 4:9adf7acadda2 126 } else {
n02655194 4:9adf7acadda2 127 crc_passed=crc_passed & 0;
n02655194 2:25380de9c996 128 lcd.cls();
n02655194 2:25380de9c996 129 lcd.locate(0,3);
n02655194 4:9adf7acadda2 130 lcd.printf("Data fails CRC verification, Int Data: %x, CRC Byte %x.", temp_crc, crc_calc);
n02655194 4:9adf7acadda2 131 }
n02655194 0:03b21cb48863 132 }
n02655194 4:9adf7acadda2 133 i++; //increment array position
n02655194 4:9adf7acadda2 134
n02655194 0:03b21cb48863 135 }
n02655194 4:9adf7acadda2 136 //zero out crc temp variables
n02655194 4:9adf7acadda2 137 temp_crc = 0;
n02655194 0:03b21cb48863 138 }
n02655194 4:9adf7acadda2 139 //pause after displaying postamble received and then display data.
n02655194 0:03b21cb48863 140 t.start();
n02655194 4:9adf7acadda2 141 //wait until the timer has reached the set time.
n02655194 4:9adf7acadda2 142 while(t.read_ms() < 1000) {
n02655194 4:9adf7acadda2 143
n02655194 0:03b21cb48863 144 }
n02655194 4:9adf7acadda2 145 //stop and reset the timer
n02655194 0:03b21cb48863 146 t.stop();
n02655194 4:9adf7acadda2 147 t.reset();
n02655194 4:9adf7acadda2 148 //if(crc_passed){//if crc passes display data, send ACK
n02655194 4:9adf7acadda2 149 //clear debugging messages - and reset lcd to original position before printing data.
n02655194 4:9adf7acadda2 150 //send ACK
n02655194 0:03b21cb48863 151 lcd.cls();
n02655194 0:03b21cb48863 152 lcd.locate(0,3);
n02655194 0:03b21cb48863 153 lcd.printf("Received: ");
n02655194 0:03b21cb48863 154 for(k=0; k<=i; k++)
n02655194 0:03b21cb48863 155 lcd.printf("%c", data[k]);
n02655194 4:9adf7acadda2 156
n02655194 0:03b21cb48863 157 }
n02655194 0:03b21cb48863 158
n02655194 0:03b21cb48863 159 void check_byte(int value)
n02655194 0:03b21cb48863 160 {
n02655194 0:03b21cb48863 161 data_flag = 1;
n02655194 0:03b21cb48863 162 temp = 0;
n02655194 0:03b21cb48863 163 rflag=0;
n02655194 0:03b21cb48863 164 //while loop
n02655194 4:9adf7acadda2 165 while(!rflag) {
n02655194 0:03b21cb48863 166 //read in data if clock is 1 and data flag is 1
n02655194 4:9adf7acadda2 167 if(clock_pin && data_flag) {
n02655194 0:03b21cb48863 168 //data is left shifted into our temporary variable.
n02655194 0:03b21cb48863 169 //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 170 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
n02655194 0:03b21cb48863 171 temp = (temp << 1) + serial_in;
n02655194 0:03b21cb48863 172 data_flag = 0;
n02655194 0:03b21cb48863 173 if(temp == value)
n02655194 0:03b21cb48863 174 rflag = 1;
n02655194 0:03b21cb48863 175 }
n02655194 0:03b21cb48863 176 //when clock returns to low - reset data flag to accept the next bit.
n02655194 0:03b21cb48863 177 if(!clock_pin && !data_flag)
n02655194 0:03b21cb48863 178 data_flag = 1;
n02655194 0:03b21cb48863 179 }
n02655194 0:03b21cb48863 180 }
n02655194 0:03b21cb48863 181
n02655194 0:03b21cb48863 182 int check_abyte()
n02655194 0:03b21cb48863 183 {
n02655194 0:03b21cb48863 184 j = 0;
n02655194 0:03b21cb48863 185 temp = 0;
n02655194 0:03b21cb48863 186 rflag=0;
n02655194 0:03b21cb48863 187 //while loop
n02655194 4:9adf7acadda2 188 while(j<8) {
n02655194 0:03b21cb48863 189 //read in data if clock is 1 and data flag is 1
n02655194 4:9adf7acadda2 190 if(clock_pin && data_flag) {
n02655194 0:03b21cb48863 191 //data is left shifted into our temporary variable.
n02655194 0:03b21cb48863 192 //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 193 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
n02655194 0:03b21cb48863 194 temp = (temp << 1) + serial_in;
n02655194 0:03b21cb48863 195 j++;
n02655194 0:03b21cb48863 196 data_flag = 0;
n02655194 0:03b21cb48863 197 }
n02655194 0:03b21cb48863 198 //when clock returns to low - reset data flag to accept the next bit.
n02655194 0:03b21cb48863 199 if(!clock_pin && !data_flag)
n02655194 0:03b21cb48863 200 data_flag = 1;
n02655194 0:03b21cb48863 201 }
n02655194 4:9adf7acadda2 202
n02655194 0:03b21cb48863 203 //clear lcd screen, print current build message
n02655194 0:03b21cb48863 204 lcd.cls();
n02655194 0:03b21cb48863 205 lcd.locate(0,3);
n02655194 4:9adf7acadda2 206 if(temp == ADDRESS) {
n02655194 0:03b21cb48863 207 rflag = 1;
n02655194 0:03b21cb48863 208 lcd.printf("Address Received");
n02655194 4:9adf7acadda2 209 } else if(temp == BROADCAST) {
n02655194 0:03b21cb48863 210 rflag = 1;
n02655194 0:03b21cb48863 211 lcd.printf("Broadcast received");
n02655194 4:9adf7acadda2 212 } else if(((temp & 0xF0) == NETWORK) && ((temp & 0x0F) == 0)) {
n02655194 0:03b21cb48863 213 rflag = 1;
n02655194 0:03b21cb48863 214 lcd.printf("Multicast received");
n02655194 1:ab56f995aa13 215 }//can add if Network==0 and address==x send to x in every network
n02655194 0:03b21cb48863 216 else
n02655194 4:9adf7acadda2 217 lcd.printf("Wrong address received");
n02655194 4:9adf7acadda2 218
n02655194 0:03b21cb48863 219 return rflag;
n02655194 0:03b21cb48863 220 }
n02655194 0:03b21cb48863 221
n02655194 0:03b21cb48863 222 int read_byte(int size)
n02655194 0:03b21cb48863 223 {
n02655194 0:03b21cb48863 224 j = 0;
n02655194 0:03b21cb48863 225 temp = 0;
n02655194 0:03b21cb48863 226
n02655194 0:03b21cb48863 227 //read a byte/nibble at a time and return it to main
n02655194 4:9adf7acadda2 228 while(j<size) {
n02655194 0:03b21cb48863 229 //read in data if clock is 1 and data flag is 1
n02655194 0:03b21cb48863 230 if(clock_pin && data_flag) {
n02655194 0:03b21cb48863 231 //data is left shifted into our temporary variable.
n02655194 0:03b21cb48863 232 //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 233 //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit.
n02655194 0:03b21cb48863 234 temp = (temp << 1) + serial_in;
n02655194 0:03b21cb48863 235 //increment j (tracks bits received) - turn off data_flag until clock changes.
n02655194 0:03b21cb48863 236 j++;
n02655194 0:03b21cb48863 237 data_flag = 0;
n02655194 0:03b21cb48863 238 }
n02655194 0:03b21cb48863 239 //when clock returns to low - reset data flag to accept the next bit.
n02655194 0:03b21cb48863 240 if(!clock_pin && !data_flag)
n02655194 0:03b21cb48863 241 data_flag = 1;
n02655194 0:03b21cb48863 242 }
n02655194 0:03b21cb48863 243 return temp;
n02655194 0:03b21cb48863 244 }
n02655194 0:03b21cb48863 245
n02655194 4:9adf7acadda2 246 int get_crc(int temp_crc)
n02655194 4:9adf7acadda2 247 {
n02655194 4:9adf7acadda2 248 while((temp_crc ^ CRC)>0) {//gets stuck in here
n02655194 4:9adf7acadda2 249 myled=1;
n02655194 4:9adf7acadda2 250 temp_crc=temp_crc ^ CRC;
n02655194 4:9adf7acadda2 251 myled=0;
n02655194 4:9adf7acadda2 252 }
n02655194 4:9adf7acadda2 253 return temp_crc;
n02655194 0:03b21cb48863 254 }