![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
app4
Dependencies: mbed-rtos mbed CRC16
Fork of S5info_APP2 by
Diff: main.cpp
- Revision:
- 24:2034362db66f
- Parent:
- 23:9c4e4898b741
- Child:
- 25:d0c348c3c85d
--- a/main.cpp Tue Mar 07 06:09:06 2017 +0000 +++ b/main.cpp Tue Mar 07 08:52:38 2017 +0000 @@ -5,11 +5,14 @@ using std::vector; DigitalIn in(p30); Thread ThreadLecture; +Thread ThreadWrite; const int PREAMBULE = 0b01010101; const int START = 0b01111110; const int END = 0b01111110; Serial pc(USBTX, USBRX); -DigitalOut out(p8); +DigitalOut out(p13); +DigitalOut myled1(LED1); +DigitalOut myled2(LED2); bool transmit = false; //faux pour le debut de demi-periode d'envoie d'un bit, vrai pour la 2ème demi-periode char trame_sent[80]; //tableau de la trame à envoyer @@ -19,31 +22,6 @@ signed char bit_sent; //position du bit de l'octet en cours d'envoi (du MSB au LSB) 7..0 bool bTimer1 = false; -void initTimers() -{ - //Timer 1 (match) - LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1 - LPC_SC->PCONP |= (1 << 2); // timer1 power on - LPC_TIM1->MR0 = 960000 / 2; // 10/2 ms - LPC_TIM1->MCR = 3; // interrupt and reset control - LPC_TIM1->EMR = (3 << 4); // Interrupt & reset timer on match - NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt - LPC_TIM1->TCR = 0; // disable Timer - - //Timer 2 (cap) - LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2 - LPC_SC->PCONP |= (1 << 22); // timer2 power on - LPC_TIM2->TC = 0; // clear timer counter - LPC_TIM2->PC = 0; // clear prescale counter - LPC_TIM2->PR = 0; // clear prescale register - LPC_TIM2->TCR |= (1 << 1); // reset timer - LPC_TIM2->TCR &= ~(1 << 1); // release reset - LPC_TIM2->IR = 0xFFFFFFFF; // clear interrupt register - LPC_TIM2->CCR |= 0x0000007; // enable rising-edge and falling-edge capture - NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt - LPC_TIM2->TCR = 1; // start Timer -} - //encodage manchester bool codeManchester(bool bit, bool clock) { @@ -74,45 +52,45 @@ extern "C" void TIMER1_IRQHandler() { + LPC_TIM1->TC = 0; + + myled1 = !myled1; if ((LPC_TIM1->IR & 0x01) == 0x01) { send_data(); - LPC_TIM1->IR |= 1 << 0; // clear } } -int sumClocks = 0; int PeriodLength = 0; bool bReceivedFirstBit = false; +bool bInvert = false; extern "C" void TIMER2_IRQHandler() { + LPC_TIM2->TC = 0; + LPC_TIM2->IR |= 0xFFFFFFFF; // clear + + myled2 = !myled2; if (PeriodLength > 0) { - if (LPC_TIM2->CR0 >= PeriodLength*1.5 || (sumClocks + LPC_TIM2->CR0) >= PeriodLength*1.5) + if (LPC_TIM2->CR0 >= PeriodLength) { - sumClocks = 0; - - ThreadLecture.signal_set(1); + bInvert = false; } else { - sumClocks += LPC_TIM2->CR0; + bInvert = true; } } else if (bReceivedFirstBit) { - PeriodLength = LPC_TIM2->CR0 / 2; - ThreadLecture.signal_set(1); + PeriodLength = (LPC_TIM2->CR0 / 2) * 1.5; } else { bReceivedFirstBit = true; - ThreadLecture.signal_set(1); } - - LPC_TIM2->TC = 0; - LPC_TIM2->IR |= 0xFFFFFFFF; // clear + ThreadLecture.signal_set(1); } //création de la trame @@ -140,21 +118,23 @@ trame_length = taille + 7; //Longueur de la trame } -void write() -{ - byte_sent_pos = 0; - bit_sent = 7; - bTimer1 = false; - LPC_TIM1->TCR = 3; // enable and reset Timer - //TODO start interrupt timer 1 - //LPC_RIT->MCR = 1; //Permettre les interruption du MAT - //LPC_RIT->MR0 = LPC_RIT->TC + HALF_PERIOD; //Faire une interruption a la prochaine demi-periode -} - //obtention du texte void get_text() { - pc.printf("\n\rYour text : "); + while (true) + { + char text[4] = {'a','l','l','o'}; + unsigned char count = 4; + + create_trame(text,count); + byte_sent_pos = 0; + bit_sent = 7; + bTimer1 = false; + LPC_TIM1->TCR = 1; // enable and reset Timer + + wait_ms(1000); + } + /*pc.printf("\n\rYour text : "); unsigned char count = 0; char c = 0x00; char text[73]; @@ -164,9 +144,7 @@ pc.putc(c); count++; } - - create_trame(text,count); - write(); + pc.printf("get_text();\n");*/ } void read() @@ -180,7 +158,14 @@ { ThreadLecture.signal_wait(1); - byte = (byte << 1) + !in; // inversion car 2e période + if (bInvert) + { + byte = (byte << 1) + !in; + } + else + { + byte = (byte << 1) + in; + } shift++; if (shift == 8) @@ -194,8 +179,6 @@ if ((bytes.size() == 1 && bytes[0] != PREAMBULE) || (bytes.size() == 2 && bytes[1] != START)) { - bytes.clear(); - if (bytes.size() == 1) { pc.printf("[DEBUG] - Invalid PREAMBULE : %x\n", bytes[0]); @@ -204,7 +187,13 @@ { pc.printf("[DEBUG] - Invalid START : %x\n", bytes[1]); } + bytes.clear(); } + if (bytes.size() == 3) + { + pc.printf("[DEBUG] - YESSSSSSSSSSSSSSSSSSSS\n"); + } + if (bytes.size() == 4) { totalsize = 7 + bytes[3]; @@ -233,13 +222,41 @@ } } +void initTimers() +{ + //Timer 1 (match) + LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1 + LPC_SC->PCONP |= (1 << 2); // timer1 power on + LPC_TIM1->MCR = 3; // interrupt and reset control + LPC_TIM1->TC = 0; // clear timer counter + LPC_TIM1->PC = 0; // clear prescale counter + LPC_TIM1->PR = 0; // clear prescale register + LPC_TIM1->MR0 = (960000 / 2) * 10; // 10/2 ms + LPC_TIM1->EMR = (3 << 4); // Interrupt & reset timer on match + NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt + LPC_TIM1->TCR = 0; // disable Timer + + //Timer 2 (cap) + LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2 + LPC_SC->PCONP |= (1 << 22); // timer2 power on + LPC_TIM2->TC = 0; // clear timer counter + LPC_TIM2->PC = 0; // clear prescale counter + LPC_TIM2->PR = 0; // clear prescale register + LPC_TIM2->TCR |= (1 << 1); // reset timer + LPC_TIM2->TCR &= ~(1 << 1); // release reset + LPC_TIM2->CCR |= 0x0000007; // enable rising-edge and falling-edge capture + NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt + LPC_TIM2->TCR = 1; // start Timer +} + int main() { LPC_PINCON->PINSEL0 |= (3 << 8); // pin30 + LPC_PINCON->PINMODE1 |= 3; initTimers(); ThreadLecture.start(read); + ThreadWrite.start(get_text); while(true) { - get_text(); } }; \ No newline at end of file