Xavier Bélanger / Mbed 2 deprecated APP4_Manchester

Dependencies:   mbed

Committer:
IC_Blood
Date:
Tue Feb 23 02:06:15 2016 +0000
Revision:
0:c7df736f11c4
Commit_1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IC_Blood 0:c7df736f11c4 1 #include "main.h"
IC_Blood 0:c7df736f11c4 2
IC_Blood 0:c7df736f11c4 3 int main()
IC_Blood 0:c7df736f11c4 4 {
IC_Blood 0:c7df736f11c4 5 tclock = 1;
IC_Blood 0:c7df736f11c4 6 pos = 0;
IC_Blood 0:c7df736f11c4 7
IC_Blood 0:c7df736f11c4 8 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 9 m_crc = new CRC16();
IC_Blood 0:c7df736f11c4 10 messageReady = false;
IC_Blood 0:c7df736f11c4 11
IC_Blood 0:c7df736f11c4 12
IC_Blood 0:c7df736f11c4 13 pc.printf("\n");
IC_Blood 0:c7df736f11c4 14 pc.printf("\n");
IC_Blood 0:c7df736f11c4 15 pc.printf("\n");
IC_Blood 0:c7df736f11c4 16 pc.printf("\n");
IC_Blood 0:c7df736f11c4 17 pc.printf("\n");
IC_Blood 0:c7df736f11c4 18 pc.printf("\n");
IC_Blood 0:c7df736f11c4 19 pc.printf("\n");
IC_Blood 0:c7df736f11c4 20 pc.printf("\n");
IC_Blood 0:c7df736f11c4 21
IC_Blood 0:c7df736f11c4 22 bitset<200> testMEF (string("0101010101111110000000000010000001000001010011000100110001001111001001001101011001111110"));
IC_Blood 0:c7df736f11c4 23
IC_Blood 0:c7df736f11c4 24 for (int i = 87; i >= 0; i--)
IC_Blood 0:c7df736f11c4 25 {
IC_Blood 0:c7df736f11c4 26 analyze_state(testMEF[i]);
IC_Blood 0:c7df736f11c4 27 }
IC_Blood 0:c7df736f11c4 28
IC_Blood 0:c7df736f11c4 29 if(messageReady)
IC_Blood 0:c7df736f11c4 30 {
IC_Blood 0:c7df736f11c4 31 for (int i = 0; i < LENGTH_CHARGE_UTILE_BYTE; i++)
IC_Blood 0:c7df736f11c4 32 pc.printf("%c",c_chargeUtile[i]);
IC_Blood 0:c7df736f11c4 33 }
IC_Blood 0:c7df736f11c4 34 //toEncodeTest = bitset<696>(string("00011101010011"));
IC_Blood 0:c7df736f11c4 35
IC_Blood 0:c7df736f11c4 36 //pc.printf("Test");
IC_Blood 0:c7df736f11c4 37
IC_Blood 0:c7df736f11c4 38 //timer2_init();
IC_Blood 0:c7df736f11c4 39
IC_Blood 0:c7df736f11c4 40 while(1)
IC_Blood 0:c7df736f11c4 41 {
IC_Blood 0:c7df736f11c4 42 }
IC_Blood 0:c7df736f11c4 43 }
IC_Blood 0:c7df736f11c4 44
IC_Blood 0:c7df736f11c4 45 void timer2_init(void)
IC_Blood 0:c7df736f11c4 46 {
IC_Blood 0:c7df736f11c4 47 LPC_SC->PCLKSEL1 |=1<<12; //pclk = cclk timer2
IC_Blood 0:c7df736f11c4 48 LPC_SC->PCONP |=1<<22; //timer2 power on
IC_Blood 0:c7df736f11c4 49 LPC_TIM2->MR0 = 9600000; //100 msec
IC_Blood 0:c7df736f11c4 50 LPC_TIM2->MCR = 3; //interrupt and reset control
IC_Blood 0:c7df736f11c4 51 //3 = Interrupt & reset timer2 on match
IC_Blood 0:c7df736f11c4 52 //1 = Interrupt only, no reset of timer0
IC_Blood 0:c7df736f11c4 53 LPC_TIM2->EMR =2<<4; //EMC0 = 10 (Toogle)
IC_Blood 0:c7df736f11c4 54 NVIC_EnableIRQ(TIMER2_IRQn); //enable timer2 interrupt
IC_Blood 0:c7df736f11c4 55 LPC_TIM2->TCR = 1; //enable Timer2
IC_Blood 0:c7df736f11c4 56 }
IC_Blood 0:c7df736f11c4 57
IC_Blood 0:c7df736f11c4 58 void timer3_init(void)
IC_Blood 0:c7df736f11c4 59 {
IC_Blood 0:c7df736f11c4 60 LPC_SC->PCLKSEL1 |=1<<12; //pclk = cclk timer2
IC_Blood 0:c7df736f11c4 61 LPC_SC->PCONP |=1<<22; //timer2 power on
IC_Blood 0:c7df736f11c4 62 LPC_TIM2->MR0 = 9600000; //100 msec
IC_Blood 0:c7df736f11c4 63 LPC_TIM2->MCR = 3; //interrupt and reset control
IC_Blood 0:c7df736f11c4 64 //3 = Interrupt & reset timer2 on match
IC_Blood 0:c7df736f11c4 65 //1 = Interrupt only, no reset of timer0
IC_Blood 0:c7df736f11c4 66 LPC_TIM2->EMR =2<<4; //EMC0 = 10 (Toogle)
IC_Blood 0:c7df736f11c4 67 NVIC_EnableIRQ(TIMER2_IRQn); //enable timer2 interrupt
IC_Blood 0:c7df736f11c4 68 LPC_TIM2->TCR = 1; //enable Timer2
IC_Blood 0:c7df736f11c4 69 }
IC_Blood 0:c7df736f11c4 70
IC_Blood 0:c7df736f11c4 71
IC_Blood 0:c7df736f11c4 72 bool analyze_state(bool bit)
IC_Blood 0:c7df736f11c4 73 {
IC_Blood 0:c7df736f11c4 74 switch(state)
IC_Blood 0:c7df736f11c4 75 {
IC_Blood 0:c7df736f11c4 76 case INIT_STATE:
IC_Blood 0:c7df736f11c4 77
IC_Blood 0:c7df736f11c4 78 pc.printf("INIT_STATE\t");
IC_Blood 0:c7df736f11c4 79
IC_Blood 0:c7df736f11c4 80 if (bit == 0)
IC_Blood 0:c7df736f11c4 81 {
IC_Blood 0:c7df736f11c4 82 state = PREAMBULE;
IC_Blood 0:c7df736f11c4 83 nextBit = 1;
IC_Blood 0:c7df736f11c4 84 cntState = 0;
IC_Blood 0:c7df736f11c4 85 }
IC_Blood 0:c7df736f11c4 86
IC_Blood 0:c7df736f11c4 87 break;
IC_Blood 0:c7df736f11c4 88
IC_Blood 0:c7df736f11c4 89 case PREAMBULE:
IC_Blood 0:c7df736f11c4 90
IC_Blood 0:c7df736f11c4 91 pc.printf("PREAMBULE\t");
IC_Blood 0:c7df736f11c4 92 if (bit == nextBit && cntState < 6)
IC_Blood 0:c7df736f11c4 93 {
IC_Blood 0:c7df736f11c4 94 nextBit = !nextBit;
IC_Blood 0:c7df736f11c4 95 cntState++;
IC_Blood 0:c7df736f11c4 96 }
IC_Blood 0:c7df736f11c4 97 else if (bit == nextBit && cntState == 6)
IC_Blood 0:c7df736f11c4 98 {
IC_Blood 0:c7df736f11c4 99 state = START;
IC_Blood 0:c7df736f11c4 100 cntState = 0;
IC_Blood 0:c7df736f11c4 101 nextBit = 0;
IC_Blood 0:c7df736f11c4 102 }
IC_Blood 0:c7df736f11c4 103 else
IC_Blood 0:c7df736f11c4 104 {
IC_Blood 0:c7df736f11c4 105 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 106 }
IC_Blood 0:c7df736f11c4 107
IC_Blood 0:c7df736f11c4 108 break;
IC_Blood 0:c7df736f11c4 109
IC_Blood 0:c7df736f11c4 110 case START:
IC_Blood 0:c7df736f11c4 111
IC_Blood 0:c7df736f11c4 112 pc.printf("START\t");
IC_Blood 0:c7df736f11c4 113
IC_Blood 0:c7df736f11c4 114 if(cntState == 0 && bit == nextBit)
IC_Blood 0:c7df736f11c4 115 {
IC_Blood 0:c7df736f11c4 116 nextBit = 1;
IC_Blood 0:c7df736f11c4 117 cntState++;
IC_Blood 0:c7df736f11c4 118 }
IC_Blood 0:c7df736f11c4 119 else if (cntState < 6 && bit == nextBit)
IC_Blood 0:c7df736f11c4 120 {
IC_Blood 0:c7df736f11c4 121 cntState++;
IC_Blood 0:c7df736f11c4 122 }
IC_Blood 0:c7df736f11c4 123 else if (cntState == 6 && bit == nextBit)
IC_Blood 0:c7df736f11c4 124 {
IC_Blood 0:c7df736f11c4 125 nextBit = 0;
IC_Blood 0:c7df736f11c4 126 cntState++;
IC_Blood 0:c7df736f11c4 127 }
IC_Blood 0:c7df736f11c4 128 else if (cntState == 7 && bit == nextBit)
IC_Blood 0:c7df736f11c4 129 {
IC_Blood 0:c7df736f11c4 130 state = ENTETE;
IC_Blood 0:c7df736f11c4 131 cntState = 0;
IC_Blood 0:c7df736f11c4 132 nextBit = 0;
IC_Blood 0:c7df736f11c4 133 }
IC_Blood 0:c7df736f11c4 134 else
IC_Blood 0:c7df736f11c4 135 {
IC_Blood 0:c7df736f11c4 136 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 137 }
IC_Blood 0:c7df736f11c4 138
IC_Blood 0:c7df736f11c4 139 break;
IC_Blood 0:c7df736f11c4 140
IC_Blood 0:c7df736f11c4 141 case ENTETE:
IC_Blood 0:c7df736f11c4 142
IC_Blood 0:c7df736f11c4 143 if(cntState < 7 && nextBit == 0)
IC_Blood 0:c7df736f11c4 144 {
IC_Blood 0:c7df736f11c4 145 pc.printf("TEST1: %i \t", cntState);
IC_Blood 0:c7df736f11c4 146 cntState++;
IC_Blood 0:c7df736f11c4 147
IC_Blood 0:c7df736f11c4 148 }
IC_Blood 0:c7df736f11c4 149 else if (cntState == 7 && nextBit == 0)
IC_Blood 0:c7df736f11c4 150 {
IC_Blood 0:c7df736f11c4 151 pc.printf("TEST2: %i \t", cntState);
IC_Blood 0:c7df736f11c4 152 nextBit = 1;
IC_Blood 0:c7df736f11c4 153 }
IC_Blood 0:c7df736f11c4 154 else if (nextBit == 1 && cntState > 0)
IC_Blood 0:c7df736f11c4 155 {
IC_Blood 0:c7df736f11c4 156 pc.printf("TEST3: %i \t", cntState);
IC_Blood 0:c7df736f11c4 157 buffer.set(cntState, bit);
IC_Blood 0:c7df736f11c4 158 cntState--;
IC_Blood 0:c7df736f11c4 159 }
IC_Blood 0:c7df736f11c4 160 else if (nextBit == 1 && cntState == 0)
IC_Blood 0:c7df736f11c4 161 {
IC_Blood 0:c7df736f11c4 162 buffer.set(cntState, bit);
IC_Blood 0:c7df736f11c4 163 state = CHARGE_UTILE;
IC_Blood 0:c7df736f11c4 164 LENGTH_CHARGE_UTILE_BIT = (int)(buffer.to_ulong());
IC_Blood 0:c7df736f11c4 165
IC_Blood 0:c7df736f11c4 166 cntState = LENGTH_CHARGE_UTILE_BIT - 1;
IC_Blood 0:c7df736f11c4 167 }
IC_Blood 0:c7df736f11c4 168 else
IC_Blood 0:c7df736f11c4 169 {
IC_Blood 0:c7df736f11c4 170 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 171 }
IC_Blood 0:c7df736f11c4 172
IC_Blood 0:c7df736f11c4 173 break;
IC_Blood 0:c7df736f11c4 174
IC_Blood 0:c7df736f11c4 175 case CHARGE_UTILE:
IC_Blood 0:c7df736f11c4 176
IC_Blood 0:c7df736f11c4 177 pc.printf("CHARGE UTILE\t");
IC_Blood 0:c7df736f11c4 178
IC_Blood 0:c7df736f11c4 179 if (cntState > 0)
IC_Blood 0:c7df736f11c4 180 {
IC_Blood 0:c7df736f11c4 181 chargeUtile.set(cntState, bit);
IC_Blood 0:c7df736f11c4 182 cntState--;
IC_Blood 0:c7df736f11c4 183 }
IC_Blood 0:c7df736f11c4 184 else if (cntState == 0)
IC_Blood 0:c7df736f11c4 185 {
IC_Blood 0:c7df736f11c4 186 chargeUtile.set(cntState, bit);
IC_Blood 0:c7df736f11c4 187 cntState = 0;
IC_Blood 0:c7df736f11c4 188 state = CONTROLE;
IC_Blood 0:c7df736f11c4 189 nextBit = 0;
IC_Blood 0:c7df736f11c4 190 }
IC_Blood 0:c7df736f11c4 191 else
IC_Blood 0:c7df736f11c4 192 {
IC_Blood 0:c7df736f11c4 193 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 194 }
IC_Blood 0:c7df736f11c4 195
IC_Blood 0:c7df736f11c4 196 break;
IC_Blood 0:c7df736f11c4 197
IC_Blood 0:c7df736f11c4 198 case CONTROLE:
IC_Blood 0:c7df736f11c4 199
IC_Blood 0:c7df736f11c4 200 if(nextBit == 0)
IC_Blood 0:c7df736f11c4 201 {
IC_Blood 0:c7df736f11c4 202 LENGTH_CHARGE_UTILE_BYTE = LENGTH_CHARGE_UTILE_BIT / 8;
IC_Blood 0:c7df736f11c4 203
IC_Blood 0:c7df736f11c4 204 for (int i = 0; i < LENGTH_CHARGE_UTILE_BIT; i++)
IC_Blood 0:c7df736f11c4 205 {
IC_Blood 0:c7df736f11c4 206 pc.printf("%i", (chargeUtile[i] & 0x1));
IC_Blood 0:c7df736f11c4 207 if (i % 8 == 0)
IC_Blood 0:c7df736f11c4 208 c_chargeUtile[i / 8] = (chargeUtile[i] << 7) | (chargeUtile[i + 1] << 6) | (chargeUtile[i + 2] << 5) | (chargeUtile[i + 3] << 4) | (chargeUtile[i + 4] << 3) | (chargeUtile[i + 5] << 2) | (chargeUtile[i + 6] << 1) | (chargeUtile[i + 7] << 0);
IC_Blood 0:c7df736f11c4 209 }
IC_Blood 0:c7df736f11c4 210
IC_Blood 0:c7df736f11c4 211 CRC_Calculated = m_crc->calculateCRC16(c_chargeUtile, LENGTH_CHARGE_UTILE_BYTE);
IC_Blood 0:c7df736f11c4 212
IC_Blood 0:c7df736f11c4 213 pc.printf("CRC Calculated: %hu\t", CRC_Calculated);
IC_Blood 0:c7df736f11c4 214
IC_Blood 0:c7df736f11c4 215 nextBit = 1;
IC_Blood 0:c7df736f11c4 216 cntState = 14;
IC_Blood 0:c7df736f11c4 217 buffer.reset();
IC_Blood 0:c7df736f11c4 218 buffer.set(15, bit);
IC_Blood 0:c7df736f11c4 219 }
IC_Blood 0:c7df736f11c4 220 else if (nextBit == 1 && cntState > 0)
IC_Blood 0:c7df736f11c4 221 {
IC_Blood 0:c7df736f11c4 222 buffer.set(cntState, bit);
IC_Blood 0:c7df736f11c4 223 cntState--;
IC_Blood 0:c7df736f11c4 224 }
IC_Blood 0:c7df736f11c4 225 else if (nextBit == 1 && cntState == 0)
IC_Blood 0:c7df736f11c4 226 {
IC_Blood 0:c7df736f11c4 227 buffer.set(cntState, bit);
IC_Blood 0:c7df736f11c4 228 CRC_Received = (unsigned short)buffer.to_ulong();
IC_Blood 0:c7df736f11c4 229
IC_Blood 0:c7df736f11c4 230 pc.printf("CRC Received: %hu\t", CRC_Received);
IC_Blood 0:c7df736f11c4 231
IC_Blood 0:c7df736f11c4 232 if (CRC_Received != CRC_Calculated)
IC_Blood 0:c7df736f11c4 233 {
IC_Blood 0:c7df736f11c4 234 state = END;
IC_Blood 0:c7df736f11c4 235 nextBit = 0;
IC_Blood 0:c7df736f11c4 236 cntState = 0;
IC_Blood 0:c7df736f11c4 237 }
IC_Blood 0:c7df736f11c4 238 else
IC_Blood 0:c7df736f11c4 239 {
IC_Blood 0:c7df736f11c4 240 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 241 }
IC_Blood 0:c7df736f11c4 242 }
IC_Blood 0:c7df736f11c4 243 else
IC_Blood 0:c7df736f11c4 244 {
IC_Blood 0:c7df736f11c4 245 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 246 }
IC_Blood 0:c7df736f11c4 247
IC_Blood 0:c7df736f11c4 248 break;
IC_Blood 0:c7df736f11c4 249
IC_Blood 0:c7df736f11c4 250 case END:
IC_Blood 0:c7df736f11c4 251
IC_Blood 0:c7df736f11c4 252 if (nextBit == 0 && nextBit == bit && cntState == 0)
IC_Blood 0:c7df736f11c4 253 {
IC_Blood 0:c7df736f11c4 254 nextBit = 1;
IC_Blood 0:c7df736f11c4 255 cntState++;
IC_Blood 0:c7df736f11c4 256 }
IC_Blood 0:c7df736f11c4 257 else if (nextBit == 1 && nextBit == bit && cntState < 6)
IC_Blood 0:c7df736f11c4 258 {
IC_Blood 0:c7df736f11c4 259 cntState++;
IC_Blood 0:c7df736f11c4 260 }
IC_Blood 0:c7df736f11c4 261 else if (nextBit == 1 && nextBit == bit && cntState == 6)
IC_Blood 0:c7df736f11c4 262 {
IC_Blood 0:c7df736f11c4 263 nextBit = 0;
IC_Blood 0:c7df736f11c4 264 cntState++;
IC_Blood 0:c7df736f11c4 265 }
IC_Blood 0:c7df736f11c4 266 else if (nextBit == 0 && nextBit == bit && cntState == 7)
IC_Blood 0:c7df736f11c4 267 {
IC_Blood 0:c7df736f11c4 268 messageReady = true;
IC_Blood 0:c7df736f11c4 269 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 270 }
IC_Blood 0:c7df736f11c4 271 else
IC_Blood 0:c7df736f11c4 272 {
IC_Blood 0:c7df736f11c4 273 state = INIT_STATE;
IC_Blood 0:c7df736f11c4 274 messageReady = false;
IC_Blood 0:c7df736f11c4 275 }
IC_Blood 0:c7df736f11c4 276
IC_Blood 0:c7df736f11c4 277 break;
IC_Blood 0:c7df736f11c4 278 }
IC_Blood 0:c7df736f11c4 279
IC_Blood 0:c7df736f11c4 280 return 1;
IC_Blood 0:c7df736f11c4 281 }
IC_Blood 0:c7df736f11c4 282
IC_Blood 0:c7df736f11c4 283 extern "C" void TIMER2_IRQHandler (void)
IC_Blood 0:c7df736f11c4 284 {
IC_Blood 0:c7df736f11c4 285 if ((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
IC_Blood 0:c7df736f11c4 286 {
IC_Blood 0:c7df736f11c4 287 LPC_TIM2->IR |= 1 << 0; // Clear MR0 interrupt flag
IC_Blood 0:c7df736f11c4 288
IC_Blood 0:c7df736f11c4 289 tclock = !tclock;
IC_Blood 0:c7df736f11c4 290
IC_Blood 0:c7df736f11c4 291 if (pos < 14)
IC_Blood 0:c7df736f11c4 292 {
IC_Blood 0:c7df736f11c4 293 LPC_TIM2->EMR = (toEncodeTest[pos] == tclock);
IC_Blood 0:c7df736f11c4 294 pc.printf("%i", (toEncodeTest[pos] == tclock));
IC_Blood 0:c7df736f11c4 295 if (tclock)
IC_Blood 0:c7df736f11c4 296 pos++;
IC_Blood 0:c7df736f11c4 297 }
IC_Blood 0:c7df736f11c4 298 }
IC_Blood 0:c7df736f11c4 299 }
IC_Blood 0:c7df736f11c4 300
IC_Blood 0:c7df736f11c4 301 extern "C" void TIMER3_IRQHandler (void)
IC_Blood 0:c7df736f11c4 302 {
IC_Blood 0:c7df736f11c4 303 if ((LPC_TIM3->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
IC_Blood 0:c7df736f11c4 304 {
IC_Blood 0:c7df736f11c4 305 LPC_TIM3->IR |= 1 << 0; // Clear MR0 interrupt flag
IC_Blood 0:c7df736f11c4 306
IC_Blood 0:c7df736f11c4 307 tclock = !tclock;
IC_Blood 0:c7df736f11c4 308
IC_Blood 0:c7df736f11c4 309 if (pos < 14)
IC_Blood 0:c7df736f11c4 310 {
IC_Blood 0:c7df736f11c4 311 LPC_TIM2->EMR = (toEncodeTest[pos] == tclock);
IC_Blood 0:c7df736f11c4 312 pc.printf("%i", (toEncodeTest[pos] == tclock));
IC_Blood 0:c7df736f11c4 313 if (tclock)
IC_Blood 0:c7df736f11c4 314 pos++;
IC_Blood 0:c7df736f11c4 315 }
IC_Blood 0:c7df736f11c4 316 }
IC_Blood 0:c7df736f11c4 317 }