Same as LPC, but with correct Address
Dependencies: C12832 mbed DataCommLPC2
main.cpp@10:f26deacef98d, 2015-05-08 (annotated)
- Committer:
- n02655194
- Date:
- Fri May 08 14:31:44 2015 +0000
- Revision:
- 10:f26deacef98d
- Parent:
- 9:c907be24e49e
final
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 | 10:f26deacef98d | 3 | #include "math.h" |
n02655194 | 0:03b21cb48863 | 4 | #include "C12832.h" |
n02655194 | 0:03b21cb48863 | 5 | |
n02655194 | 10:f26deacef98d | 6 | //To Do: |
n02655194 | 10:f26deacef98d | 7 | //Fix crc_passed logic |
n02655194 | 10:f26deacef98d | 8 | |
n02655194 | 10:f26deacef98d | 9 | //Improvements: |
n02655194 | 10:f26deacef98d | 10 | //Increase bandwidth by reducing wasted nibbles from CRC |
n02655194 | 10:f26deacef98d | 11 | |
n02655194 | 10:f26deacef98d | 12 | //Changes: |
n02655194 | 10:f26deacef98d | 13 | //unsigned chars moved |
n02655194 | 10:f26deacef98d | 14 | //removed nibble |
n02655194 | 10:f26deacef98d | 15 | //added CRC |
n02655194 | 10:f26deacef98d | 16 | |
n02655194 | 10:f26deacef98d | 17 | //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 | 18 | #define MAX 100 //set the size of the character data storage array |
n02655194 | 0:03b21cb48863 | 19 | #define BYTE 8 |
n02655194 | 0:03b21cb48863 | 20 | #define PREAMBLE 0x7E //preamble of 01111110 |
n02655194 | 0:03b21cb48863 | 21 | #define POSTAMBLE 0x81 //postamble of 10000001 |
n02655194 | 4:9adf7acadda2 | 22 | #define ADDRESS 0x11 //address of 00010010 - network of 1, id of 2. |
n02655194 | 0:03b21cb48863 | 23 | #define BROADCAST 0x00 //address of 00000000 |
n02655194 | 0:03b21cb48863 | 24 | #define CRC 0x13 //crc of 10011 or x^4+x+1 or crc-5 |
n02655194 | 0:03b21cb48863 | 25 | |
n02655194 | 10:f26deacef98d | 26 | Timer t; //timer for pausing after data RXed before displaying data |
n02655194 | 10:f26deacef98d | 27 | DigitalOut myled(LED1); // red led on board |
n02655194 | 0:03b21cb48863 | 28 | C12832 lcd(p5, p7, p6, p8, p11); //LCD structure |
n02655194 | 10:f26deacef98d | 29 | int msecs, sksecs; //clock time needed for data transfer and skew time |
n02655194 | 10:f26deacef98d | 30 | DigitalIn clock_pin(p21); //clock pulse input and data input pins |
n02655194 | 10:f26deacef98d | 31 | DigitalInOut serial_in(p22);//Recieve, send for ACK |
n02655194 | 10:f26deacef98d | 32 | |
n02655194 | 10:f26deacef98d | 33 | unsigned char temp, crc_calc; //temp byte storage, storage array, transmitted crc value |
n02655194 | 10:f26deacef98d | 34 | char c[1];//Used for troubleshooting to send a char to LCD |
n02655194 | 10:f26deacef98d | 35 | char data[MAX];//data array |
n02655194 | 9:c907be24e49e | 36 | unsigned char preamble, address, i, j, k, temp_data,FCS; //increment variables |
n02655194 | 10:f26deacef98d | 37 | unsigned char data_flag, rflag, done_flag1, done_flag2; //data flags |
n02655194 | 10:f26deacef98d | 38 | int crc_passed = 0, temp_crc = 0; //CRC Flag, stores values for crc check. |
n02655194 | 0:03b21cb48863 | 39 | |
n02655194 | 0:03b21cb48863 | 40 | //funtion prototypes |
n02655194 | 10:f26deacef98d | 41 | void check_byte(int value); //stays inside the function until the RXed byte matches the value passed into the function (PREAMBLE) |
n02655194 | 10:f26deacef98d | 42 | int check_abyte();//after preamble RXed checks the next byte for address. Returns 1 if address RXed matches ADDRESS, BROADCAST, or multicast; 0 if not. |
n02655194 | 10:f26deacef98d | 43 | int read_byte(int size); //reads RXed data and returns it a byte at a time. |
n02655194 | 10:f26deacef98d | 44 | int get_crc(int temp_crc);//Get CRC value on data temp_crc |
n02655194 | 10:f26deacef98d | 45 | void Ms_Delay(int msec); |
n02655194 | 10:f26deacef98d | 46 | void lcdClear(void); |
n02655194 | 10:f26deacef98d | 47 | void print(char str[]); |
n02655194 | 10:f26deacef98d | 48 | void send_ACK(int ACK); |
n02655194 | 0:03b21cb48863 | 49 | |
n02655194 | 0:03b21cb48863 | 50 | int main() |
n02655194 | 0:03b21cb48863 | 51 | { |
n02655194 | 10:f26deacef98d | 52 | clock_pin.mode(PullUp);//Enable PullUp Resistors |
n02655194 | 10:f26deacef98d | 53 | serial_in.mode(PullUp);//Enable PullUp Resistors |
n02655194 | 10:f26deacef98d | 54 | myled=0;//Turn LED On |
n02655194 | 4:9adf7acadda2 | 55 | //clear lcd screen, print current build message |
n02655194 | 10:f26deacef98d | 56 | lcdClear(); |
n02655194 | 10:f26deacef98d | 57 | print("Comm Started"); |
n02655194 | 10:f26deacef98d | 58 | Ms_Delay(500);//Wait 500mS |
n02655194 | 10:f26deacef98d | 59 | while(true) { |
n02655194 | 10:f26deacef98d | 60 | //initialize variables |
n02655194 | 10:f26deacef98d | 61 | i = 0; |
n02655194 | 10:f26deacef98d | 62 | done_flag1 = 0; |
n02655194 | 10:f26deacef98d | 63 | done_flag2 = 0; |
n02655194 | 10:f26deacef98d | 64 | crc_passed=0; |
n02655194 | 4:9adf7acadda2 | 65 | |
n02655194 | 10:f26deacef98d | 66 | while(!done_flag1) { |
n02655194 | 10:f26deacef98d | 67 | //read input clock pulse and data checking for preamble. |
n02655194 | 10:f26deacef98d | 68 | //preamble while loop |
n02655194 | 10:f26deacef98d | 69 | check_byte(PREAMBLE); |
n02655194 | 10:f26deacef98d | 70 | myled=0;//Turn LED On |
n02655194 | 10:f26deacef98d | 71 | //clear lcd screen, print current build message |
n02655194 | 10:f26deacef98d | 72 | lcdClear(); |
n02655194 | 10:f26deacef98d | 73 | print("Pre RXed"); |
n02655194 | 7:dbec2e8ce07f | 74 | |
n02655194 | 10:f26deacef98d | 75 | //preamble RXed check address (next byte), returns to preamble check if not addressed to station |
n02655194 | 10:f26deacef98d | 76 | if(check_abyte()) |
n02655194 | 10:f26deacef98d | 77 | done_flag1 = 1; |
n02655194 | 10:f26deacef98d | 78 | else { |
n02655194 | 10:f26deacef98d | 79 | //clear lcd screen, print current build message |
n02655194 | 10:f26deacef98d | 80 | lcdClear(); |
n02655194 | 10:f26deacef98d | 81 | print("Wait for pre"); |
n02655194 | 7:dbec2e8ce07f | 82 | } |
n02655194 | 0:03b21cb48863 | 83 | } |
n02655194 | 10:f26deacef98d | 84 | |
n02655194 | 10:f26deacef98d | 85 | while(!done_flag2) { |
n02655194 | 10:f26deacef98d | 86 | //store data into character array if crc checks. |
n02655194 | 10:f26deacef98d | 87 | temp_data = read_byte(BYTE); //store successfully transmitted data |
n02655194 | 10:f26deacef98d | 88 | |
n02655194 | 10:f26deacef98d | 89 | //check for postamble |
n02655194 | 10:f26deacef98d | 90 | if(temp_data == POSTAMBLE) { |
n02655194 | 10:f26deacef98d | 91 | //break out of while loop - data finished sending |
n02655194 | 10:f26deacef98d | 92 | done_flag2 = 1; |
n02655194 | 10:f26deacef98d | 93 | //clear lcd screen, print current build message |
n02655194 | 10:f26deacef98d | 94 | lcdClear(); |
n02655194 | 10:f26deacef98d | 95 | print("Post RXed"); |
n02655194 | 10:f26deacef98d | 96 | } |
n02655194 | 4:9adf7acadda2 | 97 | |
n02655194 | 10:f26deacef98d | 98 | //store data in character array if not postamble - check crc when appropriate |
n02655194 | 10:f26deacef98d | 99 | else { |
n02655194 | 10:f26deacef98d | 100 | //byte1, crc1, byte2, crc2 |
n02655194 | 10:f26deacef98d | 101 | data[i]=0; |
n02655194 | 10:f26deacef98d | 102 | data[i] = temp_data; |
n02655194 | 10:f26deacef98d | 103 | temp_crc=temp_data<<4; |
n02655194 | 10:f26deacef98d | 104 | FCS = read_byte(BYTE); //store successfully transmitted data |
n02655194 | 10:f26deacef98d | 105 | temp_crc=temp_crc+(FCS & 0x0F);//grab the last 4 bits from crc value byte |
n02655194 | 10:f26deacef98d | 106 | if(get_crc(temp_crc)==0) { |
n02655194 | 10:f26deacef98d | 107 | crc_passed=1; |
n02655194 | 10:f26deacef98d | 108 | lcdClear(); |
n02655194 | 10:f26deacef98d | 109 | print("pass CRC"); |
n02655194 | 10:f26deacef98d | 110 | } else { |
n02655194 | 10:f26deacef98d | 111 | crc_passed=crc_passed & 0;//needs to be fixed |
n02655194 | 10:f26deacef98d | 112 | lcdClear(); |
n02655194 | 10:f26deacef98d | 113 | print("fail CRC"); |
n02655194 | 10:f26deacef98d | 114 | } |
n02655194 | 10:f26deacef98d | 115 | } |
n02655194 | 10:f26deacef98d | 116 | i++; //increment array position |
n02655194 | 10:f26deacef98d | 117 | temp_crc = 0;//zero out crc temp variables |
n02655194 | 10:f26deacef98d | 118 | } |
n02655194 | 10:f26deacef98d | 119 | //pause after displaying postamble RXed and then display data. |
n02655194 | 10:f26deacef98d | 120 | //Ms_Delay(1000);//Display Postamble Message |
n02655194 | 10:f26deacef98d | 121 | if(crc_passed) { //if crc passes display data, send ACK |
n02655194 | 10:f26deacef98d | 122 | //clear debugging messages - and reset lcd to original position before printing data. |
n02655194 | 10:f26deacef98d | 123 | //send ACK |
n02655194 | 10:f26deacef98d | 124 | lcdClear(); |
n02655194 | 10:f26deacef98d | 125 | print("RXed: "); |
n02655194 | 10:f26deacef98d | 126 | print(data); |
n02655194 | 10:f26deacef98d | 127 | send_ACK(0x1); |
n02655194 | 10:f26deacef98d | 128 | // for(k=0; k<=i; k++) |
n02655194 | 10:f26deacef98d | 129 | // print("%c", data[k]); |
n02655194 | 10:f26deacef98d | 130 | } else { //if crc fails, send NOACK |
n02655194 | 10:f26deacef98d | 131 | send_ACK(0x0); |
n02655194 | 10:f26deacef98d | 132 | } |
n02655194 | 10:f26deacef98d | 133 | myled=1; |
n02655194 | 10:f26deacef98d | 134 | //Ms_Delay(1000); |
n02655194 | 0:03b21cb48863 | 135 | } |
n02655194 | 0:03b21cb48863 | 136 | } |
n02655194 | 0:03b21cb48863 | 137 | |
n02655194 | 0:03b21cb48863 | 138 | void check_byte(int value) |
n02655194 | 0:03b21cb48863 | 139 | { |
n02655194 | 0:03b21cb48863 | 140 | data_flag = 1; |
n02655194 | 0:03b21cb48863 | 141 | temp = 0; |
n02655194 | 0:03b21cb48863 | 142 | rflag=0; |
n02655194 | 0:03b21cb48863 | 143 | //while loop |
n02655194 | 4:9adf7acadda2 | 144 | while(!rflag) { |
n02655194 | 0:03b21cb48863 | 145 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 4:9adf7acadda2 | 146 | if(clock_pin && data_flag) { |
n02655194 | 0:03b21cb48863 | 147 | //data is left shifted into our temporary variable. |
n02655194 | 0:03b21cb48863 | 148 | //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 | 149 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
n02655194 | 0:03b21cb48863 | 150 | temp = (temp << 1) + serial_in; |
n02655194 | 0:03b21cb48863 | 151 | data_flag = 0; |
n02655194 | 0:03b21cb48863 | 152 | if(temp == value) |
n02655194 | 0:03b21cb48863 | 153 | rflag = 1; |
n02655194 | 0:03b21cb48863 | 154 | } |
n02655194 | 0:03b21cb48863 | 155 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 0:03b21cb48863 | 156 | if(!clock_pin && !data_flag) |
n02655194 | 0:03b21cb48863 | 157 | data_flag = 1; |
n02655194 | 0:03b21cb48863 | 158 | } |
n02655194 | 0:03b21cb48863 | 159 | } |
n02655194 | 0:03b21cb48863 | 160 | |
n02655194 | 0:03b21cb48863 | 161 | int check_abyte() |
n02655194 | 0:03b21cb48863 | 162 | { |
n02655194 | 0:03b21cb48863 | 163 | j = 0; |
n02655194 | 0:03b21cb48863 | 164 | temp = 0; |
n02655194 | 0:03b21cb48863 | 165 | rflag=0; |
n02655194 | 0:03b21cb48863 | 166 | //while loop |
n02655194 | 4:9adf7acadda2 | 167 | while(j<8) { |
n02655194 | 0:03b21cb48863 | 168 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 4:9adf7acadda2 | 169 | if(clock_pin && data_flag) { |
n02655194 | 0:03b21cb48863 | 170 | //data is left shifted into our temporary variable. |
n02655194 | 0:03b21cb48863 | 171 | //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 | 172 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
n02655194 | 0:03b21cb48863 | 173 | temp = (temp << 1) + serial_in; |
n02655194 | 0:03b21cb48863 | 174 | j++; |
n02655194 | 0:03b21cb48863 | 175 | data_flag = 0; |
n02655194 | 0:03b21cb48863 | 176 | } |
n02655194 | 0:03b21cb48863 | 177 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 0:03b21cb48863 | 178 | if(!clock_pin && !data_flag) |
n02655194 | 0:03b21cb48863 | 179 | data_flag = 1; |
n02655194 | 0:03b21cb48863 | 180 | } |
n02655194 | 4:9adf7acadda2 | 181 | |
n02655194 | 0:03b21cb48863 | 182 | //clear lcd screen, print current build message |
n02655194 | 10:f26deacef98d | 183 | lcdClear(); |
n02655194 | 10:f26deacef98d | 184 | |
n02655194 | 4:9adf7acadda2 | 185 | if(temp == ADDRESS) { |
n02655194 | 0:03b21cb48863 | 186 | rflag = 1; |
n02655194 | 10:f26deacef98d | 187 | print("Addrss RXed"); |
n02655194 | 4:9adf7acadda2 | 188 | } else if(temp == BROADCAST) { |
n02655194 | 0:03b21cb48863 | 189 | rflag = 1; |
n02655194 | 10:f26deacef98d | 190 | print("Brdcst RXed"); |
n02655194 | 10:f26deacef98d | 191 | } else if(((temp & 0xF0) == (ADDRESS & 0xF0)) && ((temp & 0x0F) == 0)) { |
n02655194 | 10:f26deacef98d | 192 | rflag = 1; |
n02655194 | 10:f26deacef98d | 193 | print("Multicst1 RXed"); |
n02655194 | 10:f26deacef98d | 194 | } else if(((temp & 0xF0) == 0) && ((temp & 0x0F) == (ADDRESS & 0x0F))) { |
n02655194 | 0:03b21cb48863 | 195 | rflag = 1; |
n02655194 | 10:f26deacef98d | 196 | print("Multicst2 RXed"); |
n02655194 | 10:f26deacef98d | 197 | //can add if Network==0 and address==x send to x in every network |
n02655194 | 10:f26deacef98d | 198 | } else { |
n02655194 | 10:f26deacef98d | 199 | print("Wrng addrss RXed"); |
n02655194 | 10:f26deacef98d | 200 | } |
n02655194 | 10:f26deacef98d | 201 | Ms_Delay(1000); |
n02655194 | 0:03b21cb48863 | 202 | return rflag; |
n02655194 | 0:03b21cb48863 | 203 | } |
n02655194 | 0:03b21cb48863 | 204 | |
n02655194 | 0:03b21cb48863 | 205 | int read_byte(int size) |
n02655194 | 0:03b21cb48863 | 206 | { |
n02655194 | 0:03b21cb48863 | 207 | j = 0; |
n02655194 | 0:03b21cb48863 | 208 | temp = 0; |
n02655194 | 0:03b21cb48863 | 209 | |
n02655194 | 0:03b21cb48863 | 210 | //read a byte/nibble at a time and return it to main |
n02655194 | 4:9adf7acadda2 | 211 | while(j<size) { |
n02655194 | 0:03b21cb48863 | 212 | //read in data if clock is 1 and data flag is 1 |
n02655194 | 0:03b21cb48863 | 213 | if(clock_pin && data_flag) { |
n02655194 | 0:03b21cb48863 | 214 | //data is left shifted into our temporary variable. |
n02655194 | 0:03b21cb48863 | 215 | //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 | 216 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
n02655194 | 0:03b21cb48863 | 217 | temp = (temp << 1) + serial_in; |
n02655194 | 10:f26deacef98d | 218 | //increment j (tracks bits RXed) - turn off data_flag until clock changes. |
n02655194 | 0:03b21cb48863 | 219 | j++; |
n02655194 | 0:03b21cb48863 | 220 | data_flag = 0; |
n02655194 | 0:03b21cb48863 | 221 | } |
n02655194 | 0:03b21cb48863 | 222 | //when clock returns to low - reset data flag to accept the next bit. |
n02655194 | 0:03b21cb48863 | 223 | if(!clock_pin && !data_flag) |
n02655194 | 0:03b21cb48863 | 224 | data_flag = 1; |
n02655194 | 0:03b21cb48863 | 225 | } |
n02655194 | 0:03b21cb48863 | 226 | return temp; |
n02655194 | 0:03b21cb48863 | 227 | } |
n02655194 | 0:03b21cb48863 | 228 | |
n02655194 | 10:f26deacef98d | 229 | |
n02655194 | 4:9adf7acadda2 | 230 | int get_crc(int temp_crc) |
n02655194 | 5:7632d56db35e | 231 | { |
n02655194 | 6:7c921924cdd3 | 232 | int j = 11, b = 0, z = 0, Y = 0, C0 = 0, C1 = 0, C2 = 0, C3 = 0; |
n02655194 | 5:7632d56db35e | 233 | while(j > -1) { |
n02655194 | 5:7632d56db35e | 234 | b = (temp_crc >> j) % 2;//bit |
n02655194 | 5:7632d56db35e | 235 | Y = C3 ^ b; |
n02655194 | 5:7632d56db35e | 236 | C3 = C2; |
n02655194 | 5:7632d56db35e | 237 | C2 = C1; |
n02655194 | 5:7632d56db35e | 238 | C1 = Y ^ C0; |
n02655194 | 5:7632d56db35e | 239 | C0 = Y; |
n02655194 | 5:7632d56db35e | 240 | j--; |
n02655194 | 4:9adf7acadda2 | 241 | } |
n02655194 | 5:7632d56db35e | 242 | z=z+C3<<1; |
n02655194 | 5:7632d56db35e | 243 | z=z+C2<<1; |
n02655194 | 5:7632d56db35e | 244 | z=z+C1<<1; |
n02655194 | 5:7632d56db35e | 245 | z=z+C0; |
n02655194 | 5:7632d56db35e | 246 | return z; |
n02655194 | 10:f26deacef98d | 247 | } |
n02655194 | 10:f26deacef98d | 248 | |
n02655194 | 10:f26deacef98d | 249 | void Ms_Delay(int msec)//mS Delay |
n02655194 | 10:f26deacef98d | 250 | { |
n02655194 | 10:f26deacef98d | 251 | t.start(); |
n02655194 | 10:f26deacef98d | 252 | //wait until the timer has reached the set time. |
n02655194 | 10:f26deacef98d | 253 | while(t.read_ms() < msec) { |
n02655194 | 10:f26deacef98d | 254 | } |
n02655194 | 10:f26deacef98d | 255 | //stop and reset the timer |
n02655194 | 10:f26deacef98d | 256 | t.stop(); |
n02655194 | 10:f26deacef98d | 257 | t.reset(); |
n02655194 | 10:f26deacef98d | 258 | } |
n02655194 | 10:f26deacef98d | 259 | |
n02655194 | 10:f26deacef98d | 260 | void lcdClear() //Clear LCD, for LPC, lcd.cls();, lcd.locate(0,3); |
n02655194 | 10:f26deacef98d | 261 | { |
n02655194 | 10:f26deacef98d | 262 | lcd.cls(); |
n02655194 | 10:f26deacef98d | 263 | lcd.locate(0,3); |
n02655194 | 10:f26deacef98d | 264 | } |
n02655194 | 10:f26deacef98d | 265 | |
n02655194 | 10:f26deacef98d | 266 | void print(char str[])// print String to LCD Display, for LPC, printf(str); |
n02655194 | 10:f26deacef98d | 267 | { |
n02655194 | 10:f26deacef98d | 268 | lcd.printf(str); |
n02655194 | 10:f26deacef98d | 269 | } |
n02655194 | 10:f26deacef98d | 270 | |
n02655194 | 10:f26deacef98d | 271 | void send_ACK(int ACK) |
n02655194 | 10:f26deacef98d | 272 | { |
n02655194 | 10:f26deacef98d | 273 | serial_in = ACK; |
n02655194 | 10:f26deacef98d | 274 | Ms_Delay(1500);///////////////can reduce time when Arduino Postamble delay is reduced |
n02655194 | 10:f26deacef98d | 275 | // if(clock_pin) { |
n02655194 | 10:f26deacef98d | 276 | // //if(!clock_pin && skew_flag && t.read_ms() > sksecs) {//output data before clock high |
n02655194 | 10:f26deacef98d | 277 | // serial_in = (byte / j) % 2; |
n02655194 | 10:f26deacef98d | 278 | // j /= 2; //decrement j to get to next bit location |
n02655194 | 10:f26deacef98d | 279 | // } |
n02655194 | 10:f26deacef98d | 280 | //reset skew flag |
n02655194 | 0:03b21cb48863 | 281 | } |