Timer set-up problem LPC 1769

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Timer set-up problem LPC 1769

1,540 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Fabienvg on Wed Oct 09 08:06:14 MST 2013
Hello,

I'm really new to microcontrollers. I tried to set up a timer for the LPC 1769 with interrupt, but I think I'm doing something wrong. For example the prescaler. I set it to 1000-1 AND it looks like, when I test my program,  it is set to 1 ms, but I can't imagine it works on only 1 Mhz. I think I'm doing things wrong in the basic configuration. Anyone who can help me out setting up a basic configuration for the timer and interrupt would help me a ton.

User manual: http://www.nxp.com/documents/user_manual/UM10360.pdf

Basic timer configuration starts at page 490.

#define TMR16B0TC  0x40004008
#define PCLKSEL0                  0x400FC1A8
#define TMR16B0IR 0x40004000
#define TMR16B0TCR 0x40004004
#define TMR16B0PR 0x4000400C
#define TMR16B0MR0 0x40004018
#define TMR16B0MCR          0x40004014
#define TMR16B0CTCR         0x40004070
#define PCONP 0x400FC0C4
#define ISER0 0xE000E100

int* pCLKSEL0        = (int*) PCLKSEL0;
int* tMR16B0TCR = (int*) TMR16B0TCR;
int* tMR16B0PR = (int*) TMR16B0PR;
int* tMR16B0MR0 = (int*) TMR16B0MR0;
int* tMR16B0MCR = (int*) TMR16B0MCR;
int* tMR16B0CTCR = (int*) TMR16B0CTCR;
int* tMR16B0IR = (int*) TMR16B0IR;
int* iSER0 = (int*) ISER1;
int* tMR16B0TC = (int*) TMR16B0TC;
int* pCONP = (int*) PCONP;

void (*timer_tmr_callback)();

void init()
{
*pCONP = 0x2; 
*pCLKSEL0    = 0xC;
*tMR16B0TC   = 0x0;
*tMR16B0PR     = 1000 - 1;
*tMR16B0CTCR    = 0;
*tMR16B0MR0 = 1000;
*tMR16B0MCR     = 0x05;
}

void timer_async_usec( unsigned int msec, void (*callback)())
{
timer_tmr_callback     = callback;
*tMR16B0MR0    = msec;
*tMR16B0MCR           = 0x07;
*iSER0    = (1 << 1);
*tMR16B0TCR    = 0x2;
*tMR16B0TCR    = 0x1;
}

void (TIMER0_IRQHandler(void))
{
*tMR16B0IR = 0x1F;
timer_tmr_callback();
}

Labels (1)
0 Kudos
Reply
3 Replies

1,388 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Pacman on Mon Oct 28 16:03:28 MST 2013
Now, I changed your code, so it can be used with the header files from NXP (and ARM):

#include "lpc175x_6x.h"
#include "lpc17xx_clkpwr.h"

void (*timer_tmr_callback)();

#define CLKPWR_PCLKSEL_CCLK_DIV_8((uint32_t)(3))/* Ahem.. This definition seems to be missing from the include files. */

void init()
{
LPC_SC->PCONP = CLKPWR_PCONP_PCTIM0;/* NOTE: Turn off EVERYTHING but timer0! */
LPC_SC->PCLKSEL0 = CLKPWR_PCLKSEL_SET(CLKPWR_PCLKSEL_TIMER0, CLKPWR_PCLKSEL_CCLK_DIV_8);
LPC_TIM0->TC = 0x0;
LPC_TIM0->PR = 1000 - 1;
LPC_TIM0->CTCR = 0;
LPC_TIM0->MR0 = 1000;
LPC_TIM0->MCR = TIM_INT_ON_MATCH(0) | TIM_STOP_ON_MATCH(0);
}

void timer_async_usec( unsigned int msec, void (*callback)())
{
timer_tmr_callback = callback;
LPC_TIM0->MR0 = msec;
LPC_TIM0->MCR = TIM_INT_ON_MATCH(0) | TIM_RESET_ON_MATCH(0) | TIM_STOP_ON_MATCH(0);
NVIC->ISER[1] = (1 << TIMER0_IRQn);/* using NVIC_EnableIRQ(TIMER0_IRQn); won't hurt */
LPC_TIM0->TCR = TIM_RESET;
LPC_TIM0->TCR = TIM_ENABLE;
}

void (TIMER0_IRQHandler(void))
{
LPC_TIM0->IR = 0x1F;/* note: NXP clears all 32 bits; I suggest using LPC_TIM0->IR = ~0; */
timer_tmr_callback();
}



It's not tested. But as I wrote earlier, I think you want to change your peripheral clock divider for the timer.
0 Kudos
Reply

1,388 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rocketdawg on Mon Oct 28 08:12:31 MST 2013

Quote: Pacman
First off all, download the libraries  - or git clone http://git.lpcware.com/(libname).

The libraries are distributed as source-code, and you can easily find out how to do things by looking in these sources. (Believe me, you will want to use the libraries!)



Yes.  And the libraries will have useful defined names for all the magic numbers that are in the code.
Try modifying that code a few months from now!   Who knows what
*iSER0 = (1 << 1);
means?  Have to look up the hardware reference.
0 Kudos
Reply

1,388 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Pacman on Sun Oct 27 21:18:50 MST 2013
First off all, download the libraries  - or git clone http://git.lpcware.com/(libname).

The libraries are distributed as source-code, and you can easily find out how to do things by looking in these sources. (Believe me, you will want to use the libraries!)

Second: It's not always a good idea to declare something 'int'.
An 'int' can be any size. 8, 16, 32 or 64 bit; the compiler may choose whatever it wants.

You may want to check your CPU clock frequency.
Is it running on the internal clock frequency ?
Is it using a PCLK/4 or PCLK/8 ?
-That could make your timer run at something that feels like 1MHz.

Note: If you used the libraries, your code would be much more readable for people in this forum, which means you would probably get a reply faster.
0 Kudos
Reply