APP 4

Dependencies:   mbed CRC16 mbed-rtos

APP_match.cpp

Committer:
vinbel93
Date:
2016-02-20
Revision:
0:ac5e42371639

File content as of revision 0:ac5e42371639:

#include "mbed.h"
#include "LPC17xx.h"

#define CLOCKS_TO_SECOND 96000000

Serial pc(USBTX, USBRX);

int state = 0;
int temps = -1;
float tempsHaut = 0.4;
float tempsBas = 0.1;

void asdf()
{
    for (int i = 0; i < 100000000; i++)
    {
        
    }
}

int benchSwag(void (*function) (void))
{
    int counter = LPC_TIM2->TC;
    function();
    return LPC_TIM2->TC - counter;
    
}

void swagger()
{
    if ((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
    {
        if (state == 0)
        {
            state = 1;
            LPC_TIM2->MR0 = (int) CLOCKS_TO_SECOND * tempsHaut;
        }
        else if (state == 1)
        {
            state = 0;
            LPC_TIM2->MR0 = (int) CLOCKS_TO_SECOND * tempsBas;
        }

        LPC_TIM2->IR |= 1 << 0;        // Clear MR0 interrupt flag
    }
}

extern "C" void TIMER2_IRQHandler()
{
    temps = benchSwag(swagger);
}
 
void initTimer2()
{
    LPC_SC->PCLKSEL1 |= (1 << 12);     // pclk = cclk timer2
    LPC_SC->PCONP |= (1 << 22);        // timer2 power on
    LPC_TIM2->MR0 = 9600000;           // 100 msec
    LPC_TIM2->MCR = 1;                 // interrupt and reset control
                                       // 3 = Interrupt & reset timer2 on match
                                       // 1 = Interrupt only, no reset of timer0
    LPC_TIM2->EMR = (3 << 4);          // EMC0 = 11 (Toogle)
    NVIC_EnableIRQ(TIMER2_IRQn);       // enable timer2 interrupt
    LPC_TIM2->TCR = 1;                 // enable Timer2
}

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

    initTimer2();

    pc.printf("Time = %i\r\n", temps);
    
    while(1)
    {
        wait(1.0);
    }
}