Timer IRQ

18 Nov 2010 . Edited: 18 Nov 2010

Hello

 

#include "mbed.h"

DigitalOut LiveLED(LED1);

void Timer1_init(void);
void Timer1_IRQHandler(void);


int main() 
{
Timer1_init();
LiveLED=0;

    while(1)
    {

    }
}

void Timer1_init(void)
{
   LPC_SC->PCONP |= 1 << 2;     // Power on Timer
   LPC_TIM1->TCR = 0x2;         // Reset and set to timer mode
   LPC_TIM1->CTCR = 0x0;
   LPC_TIM1->PR = 0;            // No prescale
   LPC_TIM1->MR0 = 2398;        // Match count for 100mS
   LPC_TIM1->MCR = 3;           // Interrupt and Reset on Match
   LPC_TIM1->TCR = 1;           // Enable Timer
   
   // Enable the ISR vector
   NVIC_SetVector (TIMER1_IRQn, (uint32_t)&Timer1_IRQHandler);
   NVIC_EnableIRQ(TIMER1_IRQn);
}


void Timer1_IRQHandler(void)
{
    LPC_TIM1->MR0 = 2398;
    LiveLED!=1;
}

 

I found some of this code in an example. I am simply attempting to toggle an LED via a timer based Interrupt. I have modified it slightly for my purposes, but it is not functioning correctly. I understand what each statement does. My questions are:

1) why use

   LPC_SC->PCONP |= 1 << 2;     // Power on Timer

instead of addressing the register regarding power for the timer directly? or is it not bit adressable?

 

2) is this necessary at all?

LPC_TIM1->CTCR = 0x0;

as this is its reset value is it necessary considering that i only call this function during initialisation?

 

3) Shouldn't this be set to 00000001

LPC_TIM1->MR0 = 2398;    

That would enable the IRQ for match channel 0?

 

Apologies if there is a good tutorial on this somewhere else.

Regards, Joel

18 Nov 2010 . Edited: 18 Nov 2010

First up, you will probably find using the Mbed library Ticker a much easier solution.

However, assuming you want to bash the registers as a learning exercise, then you may find LPC17xx.h enlightening which is #include'd by mbed.h

Basically, it abstracts the pointers to the LPC register set meaning your code is portable amoungst the processor family.

As for questions 2 & 3, the manual speaks for itself. Sometimes people make sure registers are set how they want, not relying on a reset condition. For example, when using the Mbed libraries they assume "they are in control". So if you had defined an Mbed library that _may_ have touched your peripheral in some way, you should always assume your registers are dirty at init and set them as required. It's not just the Mbed libs, any lib may touch your peripheral, who knows? However, again, if you are messing with the registeres and using other libs then you need to pay attention as you may well break a library'e expected functioning.

As for why your led doesn't toggle:-

LiveLED!=1;

should this not be:-

LiveLED = !LiveLED;

??

regards,

--Andy

18 Nov 2010 . Edited: 18 Nov 2010

Ah dear. That would be a step in the right direction (this command has worked in previous IDE's. There are quite a few other errors I have found since sitting down and studying the manual in a lot more detail. Thanks for the input!

 

Also, why would you suggest using a ticker? using the .attach to attach a software based interrupt?

18 Nov 2010

Joel, the example of the Ticker shows it calling a function to toggle an LED just like what you are doing. Yes, you attach a "callback" function (prototype: void myCallback(void); ) function to call when the ticker times out. Note, internally, Ticker uses Timer3 (iirc). But the Mbed libs generally make things like this simple.

18 Nov 2010

Cheers Andy, that makes things a lot easier

18 Nov 2010 . Edited: 18 Nov 2010

Here is what I used. Im not sure I believe that it changes state 5 times a second though - although I'm quite prepared to be told that it definitely does. Does the function it call's use NOP's to time the delay ?

 

 

#include "mbed.h"


DigitalOut LiveLED(LED1);
Ticker Live;
void Live_isr(void);

int main() 
{
Live.attach(Live_isr, 0.2);
LiveLED=1;

    while(1)
    {

    }
}

void Live_isr()
{
LiveLED = !LiveLED;
}
18 Nov 2010

No, the Mbed libraries use Timer3 for measure waits etc.  Otherwise your program would never get anything done after you attach a callback.