app3 prob
Dependencies: mbed
Diff: main.cpp
- Revision:
- 1:cb05875a0960
- Parent:
- 0:6517bada7928
- Child:
- 2:c25ba89e3581
diff -r 6517bada7928 -r cb05875a0960 main.cpp --- a/main.cpp Fri Feb 07 20:16:21 2014 +0000 +++ b/main.cpp Tue Feb 11 15:39:58 2014 +0000 @@ -1,81 +1,177 @@ #include "mbed.h" +#include "rtos.h" +#include <bitset> +#include <string> Serial pc(USBTX, USBRX); -DigitalIn in(p29); + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +unsigned long tc_periods[7] = {0}; +unsigned long period = 0; +bool type_bit = true; +int synchrone = 0; + +string message = ""; +const unsigned long offset = 100; + +bitset<16> bits(string("0101010101111110")); -bool trame[16] = {false, true, false, true, true, true, true, false, false, false, true, false, false, true, true, true}; - -void ThatsTheSpirit() { +void readTrame() +{ + if (synchrone < 7) + { + tc_periods[synchrone] = LPC_TIM2->CR1 / 2; + synchrone++; + + if (synchrone == 7) + { + for (int i = 0; i < synchrone; i++) + { + period += tc_periods[i]; + } + + period = period/7; + } + } + else + { + unsigned int tc_count = LPC_TIM2->CR1; + if (tc_count > (period - offset) && tc_count < (period + offset)) + { + if (type_bit) + { + led2 = 1; + led3 = 0; + } + else + { + led3 = 1; + led2 = 0; + } + } + else if (tc_count > (period*2 - offset) && tc_count < (period*2 + offset)) + { + type_bit = !type_bit; + led4 = 1; + if (type_bit) + { + led2 = 1; + led3 = 0; + } + else + { + led3 = 1; + led2 = 0; + } + } + + /*if (type_bit) + message += "1"; + else + message += "0";*/ + } - pc.printf("Duree impulsion : %d microsecondes.\n\r", LPC_TIM2->CR1/(SystemCoreClock/1000000)); - + if (led1 == 0) + led1 = 1; + else + led1 = 0; + LPC_TIM2->TC = 0; - LPC_TIM2->PC = 0; - LPC_TIM2->PR = 0; LPC_TIM2->IR = 0xFF; } -void SendMessage() { - - LPC_SC->PCONP |= (1 << 6); // Enable power on PWM1 - LPC_SC->PCLKSEL0 |= (3 << 12); // Run PWM1 - - LPC_PINCON->PINSEL4 |= (1 << 2); - LPC_PINCON->PINMODE3 |= (2 << 14) | (2 << 8); - - LPC_PWM1->MCR |= (0 << 8) | ( 0 << 7) | (0 << 6) | (1 << 1); - LPC_PWM1->CTCR |= (0 << 0); - LPC_PWM1->PCR |= (1 << 12) | (1 << 10) | (1 << 4) | (1 << 2); - - LPC_PWM1->MR0 = SystemCoreClock/8; - LPC_PWM1->LER |= (0x6); - LPC_PWM1->TCR |= (0x9); - - for (int a = 0; a < 16; a++) { - if (trame[a]) { - LPC_PWM1->MR1 = 0; - LPC_PWM1->MR2 = SystemCoreClock/16; - } else { - LPC_PWM1->MR1 = SystemCoreClock/16; - LPC_PWM1->MR2 = 0; +void sendTrame(std::bitset<16> bit) +{ + for (int a = 15; a > 0; a--) + { + if (bit.test(a)) + { + LPC_PWM1->MR1 = SystemCoreClock/2; + LPC_PWM1->MR2 = 1; + } + else + { + LPC_PWM1->MR1 = 1; + LPC_PWM1->MR2 = SystemCoreClock/2; } - pc.printf("trame: %d\n\r", trame[a]); + while(LPC_PWM1->IR != 0x01); - wait(1); + LPC_PWM1->IR = 0xFF; + } + + LPC_PWM1->TCR = 0x0; + + pc.printf("%s\n", message.c_str()); + + for ( int i = 0; i < 8; i++) + { + pc.printf("%d : \t", tc_periods[i]); } + + pc.printf("\n"); +} + +void send(void const *args) +{ + sendTrame(bits); +} + +void _send() +{ + if (led1 == 0) + led1 = 1; + else + led1 = 0; } -int main() { - - SendMessage(); - - /*LPC_PINCON->PINSEL0 |= 0xF00; - NVIC_SetVector(TIMER2_IRQn, uint32_t(ThatsTheSpirit)); - - LPC_SC->PCONP |= (1 << 22); - LPC_SC->PCLKSEL1 |= (1 << 12); - +void initialize() +{ + // Set system control + LPC_SC->PCONP |= (1 << 22) | (1 << 6); // Enable Timer2 et PWM + LPC_SC->PCLKSEL0 |= (1 << 12); // PClk PWM = CCLK + LPC_SC->PCLKSEL1 |= (1 << 12); // PClk Timer2 = CCLK + + // Set pin connection + LPC_PINCON->PINSEL0 |= (3 << 10); // Pin 29 Capture + LPC_PINCON->PINSEL4 |= (1 << 2); // Pin 25 PWM + + //Initialize Timer2 for capture + + LPC_TIM2->TC = 0; // Initialize Time Counter + LPC_TIM2->PC = 0; // Initialize Prescale Counter + LPC_TIM2->PR = 0; // Initialize Prescale Register + LPC_TIM2->TCR |= (1 << 1); // Reset Timer Control Register + LPC_TIM2->IR = 0xFF; // Reset Interrupt Register + LPC_TIM2->CCR |= (1 << 5) | (1 << 4) | (1 << 3); // Initialize Capture Control Register + LPC_TIM2->CTCR = 0x00; + + NVIC_SetVector(TIMER2_IRQn, (uint32_t)&readTrame); + NVIC_EnableIRQ(TIMER2_IRQn); - LPC_TIM2->TC = 0; // Initialise Time Counter - LPC_TIM2->PC = 0; // Initialise Prescale Counter - LPC_TIM2->PR = 0; // Initialise Prescale Register - LPC_TIM2->TCR = 0x02; // Reset Timer Control Register - LPC_TIM2->IR = 0xFF; // Initialise Interrupt Register - LPC_TIM2->CCR |= 0x29; // Initialise Capture Control Register + LPC_TIM2->TCR = 0x01; // Start Timer Control Register - LPC_TIM2->TCR = 0x01; // Start Timer Control Register + //Initialize PWM + LPC_PWM1->MCR |= (1 << 1) | (1 << 0); // Initialize Match Control Register Interrupt/Reset + LPC_PWM1->PCR |= (1 << 10) | (1 << 2); // Initialize PWM Control Register Output/Double-edge + + LPC_PWM1->MR0 = SystemCoreClock; // Period + LPC_PWM1->LER |= (1 << 2) | (1 << 1); // Initialize Latch Enable Register + LPC_PWM1->TCR = (1 << 0); // Enable counter - while(1) { - - while (in.read() == 0); - - NVIC_EnableIRQ(TIMER2_IRQn); - NVIC_DisableIRQ(TIMER2_IRQn); - - while (in.read() == 1); - - NVIC_EnableIRQ(TIMER2_IRQn); - NVIC_DisableIRQ(TIMER2_IRQn); - }*/ + LPC_PWM1->IR = 0xFF; // Reset Interrupt Registe } + +int main() +{ + initialize(); + + sendTrame(bits); + + while(true) { + } +}