APP 4

Dependencies:   mbed CRC16 mbed-rtos

APP.cpp

Committer:
manl2003
Date:
2016-02-22
Revision:
12:715af3660c73
Parent:
11:097ae746d8ac
Child:
14:9505b98c6623

File content as of revision 12:715af3660c73:

#include "APP.h"
#include "Manchester.h"
#include "Frame.h"

Serial pc(USBTX, USBRX);
DigitalIn in(p9);

bool clockTick = false;
bitset<FRAMESIZE> message;
bitset<MAX_DATA> decodedMessage;
bool dataReady;
bool frameDropped; 
int counter = 0;

int benchmark(void (*function) (void))
{
    int count = LPC_TIM2->TC;
    function();
    return LPC_TIM2->TC - count;
}

extern "C" void TIMER2_IRQHandler()
{
    if ((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
    {
        LPC_TIM2->IR |= 1 << 0;        // Clear MR0 interrupt flag
        clockTick = !clockTick;
        LPC_TIM2->EMR = encode(message[counter] & 0x1, clockTick);

        if (clockTick)
        {
            counter++;
        }

        if (counter >= FRAMESIZE)
        {
            counter = 0;
        }
    }
}
/*
extern "C" void TIMER1_IRQHandler()
{
    if ((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
    {
        LPC_TIM2->IR |= 1 << 0;        // Clear MR0 interrupt flag
        clockTick = !clockTick;
        LPC_TIM2->EMR = encode(message[counter], clockTick);

        if (clockTick)
        {
            counter++;
        }

        if (counter >= FRAMESIZE)
        {
            counter = 0;
        }
    }
}*/

 
void initTimers()
{
    //Timer 2 (match)
    LPC_SC->PCLKSEL1 |= (1 << 12);          // pclk = cclk timer2
    LPC_SC->PCONP |= (1 << 22);             // timer2 power on
    LPC_TIM2->MR0 = CLOCKS_TO_SECOND / 10;  // 100 ms
    LPC_TIM2->MCR = 3;                      // interrupt and reset control
                                            // Interrupt & reset timer2 on match
    LPC_TIM2->EMR = (2 << 4);               // toggle
    NVIC_EnableIRQ(TIMER2_IRQn);            // enable timer2 interrupt
    LPC_TIM2->TCR = 1;                      // enable Timer2
    /*
    //Timer 1 (cap)
    LPC_SC->PCLKSEL0 |= (1 << 4);           // pclk = cclk timer2
    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 timer1 on match
    NVIC_EnableIRQ(TIMER1_IRQn);            // enable timer1 interrupt        
    LPC_TIM1->TCR = 1;                      // enable Timer1
    */
}

int main()
{
    message = buildFrame(convertToBits("ASDF", 4, &pc), 4, &pc);

    LPC_PINCON->PINSEL0 |= (3 << 12);  // P0.6 = MAT2.0
    initTimers();

    while (true)
    {
        pc.printf("%i ", decode(in.read(), clockTick));
        wait(0.2);
    }
}

static void _decodeCallback(bitset<MESSAGE_SIZE> decMessage)
{
   decodedMessage = decMessage;
   dataReady = true;
}

static void _decodeError()
{
    frameDropped = true;
}