APP 4
Dependencies: mbed CRC16 mbed-rtos
APP.cpp
- Committer:
- vinbel93
- Date:
- 2016-02-22
- Revision:
- 19:41e914ab0d19
- Parent:
- 18:493a5aa7e4ec
- Child:
- 20:f0932bfe09ed
File content as of revision 19:41e914ab0d19:
#include "APP.h" #include "Manchester.h" #include "Frame.h" #include "MEF.h" Serial pc(USBTX, USBRX); DigitalOut out(p8); DigitalIn in(p30); bool clockTick = false; bitset<FRAMESIZE> message;// (string("1010101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")); bitset<MAX_DATA> decodedMessage; bool dataReady; bool frameDropped; int counter = 0; unsigned int period = 0; bool readBuffer[2] = {false, false}; bool readBufferReady = false; bool value = false; MEF mef; STATES mefSTATE; int benchmark(void (*function) (void)) { int count = LPC_TIM2->TC; function(); return LPC_TIM2->TC - count; } extern "C" void TIMER1_IRQHandler() { if ((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt, proceed { LPC_TIM1->IR |= 1 << 0; // Clear MR0 interrupt flag clockTick = !clockTick; out = encode(message[counter] & 0x1, clockTick); if (clockTick) { counter++; } if (counter >= FRAMESIZE) { counter = 0; } } } extern "C" void TIMER2_IRQHandler() { unsigned int clocks = LPC_TIM2->CR0; period = clocks; // preambule 01010101 if (!readBufferReady) { readBuffer[0] = in.read(); } else { readBuffer[1] = in.read(); value = decode(readBuffer[0], readBuffer[1]); mef.ReceiveBit(value); } readBufferReady = !readBufferReady; LPC_TIM2->TC = 0; LPC_TIM2->IR |= 0xFFFFFFFF; // clear Timer interrupt register } void initTimers() { //Timer 1 (match) LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1 LPC_SC->PCONP |= (1 << 2); // timer1 power on LPC_TIM1->MR0 = CLOCKS_TO_SECOND / 10; // 100 ms LPC_TIM1->MCR = 3; // interrupt and reset control // Interrupt & reset timer on match LPC_TIM1->EMR = (3 << 4); NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt LPC_TIM1->TCR = 1; // enable 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 on 2.0 NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt LPC_TIM2->TCR = 1; // start Timer } int main() { message = buildFrame(convertToBits("ASDF", 4, &pc), 4, &pc); LPC_PINCON->PINSEL0 |= (3 << 8); // P0.4 = CAP2.0 initTimers(); STATES tempState = BLU; while (true) { if (dataReady) { pc.printf("ASDF"); } if (tempState != mefSTATE) { pc.printf("BluBlu new state: %i \r\n", mefSTATE); tempState = mefSTATE; } pc.printf("%i ", period); pc.printf("%i ", value); pc.printf("%i \r\n", in.read()); //pc.printf("%i ", decode(in.read(), clockTick)); wait(0.1); } } void _decodeCallback(bitset<MAX_DATA> decMessage) { decodedMessage = decMessage; dataReady = true; } void _decodeError() { frameDropped = true; } void _updateState(STATES state) { mefSTATE = state; } void _readDephased() { readBufferReady = !readBufferReady; }