LPC11xx and Timer16 - example problems

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by robert.palmer on Thu Jun 14 14:27:30 MST 2012
I am trying to use the 16bit timer to generate a delay.  I started out using the driver code provided by NXP (timer16.c/.h) but since I was having problems, I reduced to the minimum code that will generate the issue. 

I'm using the timer to generate pulses on I/O pins.  I have a scope on the pins and can see them toggling, but my delay is not working, the pulses are always about 5uS long (they SHOULD be 5msec long).

It seems that no matter what value I write into the LPC_TMR16B0->PR and ->MR0 register, the loop seems to exit immediately.

Also, when I step through the register setup and use the peripheral display, the values in the register always show zero.  They never seem to update.  I've done the same thing with the SSP and I2C peripherals in other projects and had not problems.

I thought it might be optimization issues, so I ensured that I have optimization off.

Here is my code:  I used the project wizard to create a CMSIS 2.0 empty project and added the delay function based on what was originally in the timer16.c file.  I have read through the LPC11xx UserManual and cannot see anything obvious that I am doing wrong in setting up the timer.

void delay(uint32_t time)
    LPC_TMR16B0->TCR = 0x02;        /* reset timer */
    LPC_TMR16B0->PR  = 48; /* set prescaler to get 1 M counts/sec */
    LPC_TMR16B0->MR0 = time * 1000;
    LPC_TMR16B0->IR  = 0xff;        /* reset all interrrupts */
    LPC_TMR16B0->MCR = 0x04;        /* stop timer on match */
    LPC_TMR16B0->TCR = 0x01;        /* start timer */
    /* wait until delay time has elapsed */
    while (LPC_TMR16B0->TCR & 0x01);

int main(void) {

    /* Initialize GPIO (sets up clock) */

    volatile static int i = 0 ;
    GPIOSetDir( 3, 1, 1 );
    for(i=10; i>0; i--)
        GPIOSetValue( 3, 1, 0 );
        GPIOSetValue( 3, 1, 1 );
// Enter an infinite loop, just incrementing a counter

while(1) {
i++ ;
return 0 ;