User manual
LPCXpresso Experiment Kit - User’s Guide
Page 71
Copyright 2013 © Embedded Artists AB
7.10 Work with a Timer
In this experiment you will learn how to work with a hardware timer.
7.10.1 Lab 9a: Create Exact Delay Function
In earlier experiments a delay loop has been used to create delays. This is not a good solution for two
reasons. First, the processor will be fully occupied looping and cannot do any other useful work and it
results in unnecessary power consumption. Secondly, if something interrupts the processor the delay
length will no longer be correct.
In this experiment a more accurate delay function shall be created. The processor will still be idling
waiting for the delay to elapse. Later on, when we explore interrupts, we will create a timer functionality
that is more universal and power efficient.
Have a look in chapter 20 -32-bit counter/timer CT32B0/1 in the LPC111x user’s manual for a
description of the how the 32-bit timers works. Note that chapter 21 describes the same timer for the
LPC1115 (the XL device family) but for our purposes the timers are identical in the LPC111x family.
A timer can be quite complicated since it can be used for creating PWM signals and capturing external
events (with time stamps) and other, more or less advanced functions.
The principle to use a timer as delay function is as follows:
1. Setup and start the timer to count up from zero to a set value. The set value is calculated as:
“time to delay” / “count clock period”. The timer counts up/increments.
2. Wait until timer reaches the match value (a bit is then typically set in a status register).
The subroutine below implements the principles outlined above. Study the code and read in the user’s
manual to understand how the code works and in what way the timer is used.
/*****************************************************************************
** Function name: delayMS
**
** Descriptions: Start the timer delay in milliseconds until elapsed
** 32-bit timer #0 is used
**
** Parameters: Delay value in millisecond
**
** Returned value: None
**
*****************************************************************************/
void delayMS(uint32_t delayInMs)
{
//setup timer #0 for delay
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); /* Enable 32-bit timer #0 clock */
LPC_TMR32B0->TCR = 0x02; /* reset timer */
LPC_TMR32B0->PR = 0x00; /* set prescaler to zero */
//(SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV) = 48000000 => Timer clock is 48MHz
LPC_TMR32B0->MR0 = delayInMs * ((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/ 1000);
LPC_TMR32B0->IR = 0xff; /* reset all interrupts (not needed) */
LPC_TMR32B0->MCR = 0x04; /* stop timer on match */
LPC_TMR32B0->TCR = 0x01; /* start timer */
/* wait until delay time has elapsed */
while (LPC_TMR32B0->TCR & 0x01);
}
For how long can the function above delay? _______________________________
Create a function for microsecond delays, i.e., delayUS(). Let the function check so that there is no
overflow in time resolution. The MR0 register has 32 bit resolution. Verify that the functions work
correctly with a previous experiment, for example Lab 7b.
Place the timer related functions in file delay.c. This file already has delay functions.