Today we will learn about LPC2148 Timer/Counter.
In LED Interfacing program, we were used random delay, which is not precise. Now its time to improvise and induce precise delay using timers. Timer and counter is very important feature which allows us to provide time variable to our microcontroller based project. Most microcontrollers comes with built-in timer peripheral.
The LPC2148 has two functionally identical general purpose timers: Timer0 and Timer1. These both timers are 32-bit along with 32-bit prescaler. Timer allows us to generate precise time delay. Each Timer module can act as a Counter or Timer.
- 1 Suggest to Read
- 2 LPC2148 Timer Features
- 3 LPC2148 Timer Working Operation
- 4 Registers Used in LPC2148 Timer
- 4.1 Interrupt Register (IR)
- 4.2 Timer Control Register (TCR)
- 4.3 Count Control Register (CTCR)
- 4.4 Timer Counter Register (TC)
- 4.5 Prescale Register (PR)
- 4.6 Prescale Counter Register (PC)
- 4.7 Match Control Register (MCR)
- 4.8 Match Registers (MR0 – MR3)
- 4.9 Capture Control Registers (CCR)
- 4.10 Capture Registers (CR0 -CR3)
- 5 Configuring Timer
- 6 Prescale Register Value Calculation
- 7 Code
Suggest to Read
LPC2148 Timer Features
- A 32-bit Timer/Counter with a programmable 32-bit Prescaler.
- Counter or Timer operation
- Up to four 32-bit capture channels per timer, that can take a snapshot of the timer value when an input signal transitions. A capture event may also optionally generate an interrupt.
- Four 32-bit match registers that allow:
- Continuous operation with optional interrupt generation on match.
- Stop timer on match with optional interrupt generation.
- Reset timer on match with optional interrupt generation.
- Up to four external outputs corresponding to match registers, with the following capabilities:
- Set low on match.
- Set high on match.
- Toggle on match.
- Do nothing on match.
There are some registers used for Configuring and Running the Timer. Before getting into Registers, first we will see working of Timer in LPC2148.
LPC2148 Timer Working Operation
The Timer/Counter is designed to count cycles of the peripheral clock (PCLK) or an externally-supplied clock, and can optionally generate interrupts or perform other actions at specified timer values, based on four match registers.
The heart of timers of the LPC2148 Microcontroller is a 32-bit free running counter, which is designed to count cycles of the Peripheral Clock (PCLK) or an external clock, this counter is programmable with 32-bit prescaler.
The tick rate of the Timer Counter (TC) is controlled by the 32-bit number written in the Prescaler Register (PR) in the following way. There is a Prescale Counter (PC) which increments on each tick of the PCLK. When it reaches the value in the prescaler register, the timer count is incremented and the Prescaler Counter (PC) is reset, on the next PCLK. This cause the timer counters to increment on every PCLK when PR=0, every 2 PCLKs when PR=1, etc.
Now we will look into the Registers used in Timers/Counters.
Registers Used in LPC2148 Timer
The below table shows the registers associated with LPC2148 Timer module. In this tutorial we will concentrate only on Timer module. We will post the Capture register soon.
|IR||Interrupt Register: The IR can be read to identify which of 6(4-match, 2-Capture) possible interrupt sources are pending. Writing Logic-1 will clear the corresponding interrupt.|
|TCR||Timer Control Register: The TCR is used to control the Timer Counter functions(enable/disable/reset).|
|TC||Timer Counter: The 32-bit TC is incremented every PR+1 cycles of PCLK. The TC is controlled through the TCR.|
|PR||Prescaler Register: This is used to specify the Prescaler value for incrementing the TC.|
|PC||Prescale Counter: The 32-bit PC is a counter which is incremented to the value stored in PR. When the value in PR is reached, the TC is incremented.|
|CTCR||Count Control Register. The CTCR selects between Timer and Counter mode, and in Counter mode selects the signal and edge(s) for counting.|
|MR0-MR3||Match Registers: The Match register values are continuously compared to the Timer Counter value. When the two values are equal, actions can be triggered automatically. The action possibilities are to generate an interrupt, reset the Timer Counter, or stop the timer. Actions are controlled by the settings in the MCR register.|
|MCR||Match Control Register: The MCR is used to control the resetting of TC and generating of interrupt whenever a Match occurs.|
|CR0 - CR4||Capture Registers. TC value is loaded to this Capture Register when there is an event on the CAPn.0 - CAPn.4|
|CCR||Capture Control Register. The CCR controls which edges of the capture inputs are used to load the Capture Registers and whether or not an interrupt is generated when a capture takes place.|
Interrupt Register (IR)
Interrupt Register consists of flag bits for Match Interrupts and Capture Interrupts. It contains four bits each for match and capture interrupts. Bits 0 to 3 in the IR register are for Match Register Interrupts i.e. Bit 0 for MR0 interrupt, Bit 1 for MR1 interrupt etc. Bits 4 to 7 are for Capture Register Interrupts Bit 4 for CR0 interrupt, Bit 5 for CR1 interrupts etc. If an interrupt is triggered, then the corresponding bit in the IR register is set to 1. Manually writing a logic 1 on the bit of the IR register will reset the interrupt. Writing a zero has no effect.
Timer Control Register (TCR)
Timer Control Register is used to control the functions of timer / counter. It is used to enable / disable or reset the Timer Counter. Timer Control Register is used to control the functions of timer / counter. It is used to enable / disable or reset the Timer Counter. When the first bit (TSR ) is 0, Timer Counter and Prescale Counter are disabled. When the first bit is 1, the counters are enabled. When the second bit (TSR ) is 1, both the counters (Timer Counter and Prescale Counter) are reset on the next positive edge of the peripheral clock. They remain reset until the second bit is set to 0.
Count Control Register (CTCR)
Count Control Register is used to set either Timer mode or Counter Mode. If Counter Mode is selected, the counter pin and the edges (rising, falling or both) can be selected using CTCR. In this tutorial, we are going to use only the Timer Mode and hence CTCR is set to “0x0”.
Timer Counter Register (TC)
The value in the Timer Counter register in incremented when the value PC reaches its terminal value as set by the PR register. If the PC is reset before reaching its maximum value, then the TC will count to the maximum possible value i.e. 0xFFFF FFFF (since it is a 32 – bit register) and resets back to 0x0000 0000. This event does not cause an interrupt, but a Match register can be used to detect an overflow if needed.
Prescale Register (PR)
Prescale Register specifies the maximum value for the Prescale Counter. When the Prescale Counter (PC) is equal to PR, the TC in incremented on the next clock and also PC is cleared.
Prescale Counter Register (PC)
Prescale Counter increments on every peripheral clock to the value stored in the PR. When the value in the PC is equal to the value in the PR, PC is reset and TC is incremented by 1 on the next clock cycle. For example, if PR = 2, then TC is incremented on every third cycle of the peripheral clock. Hence, PC defines the resolution of the Timer.
Match Control Register (MCR)
The Match Control Register is used control the actions to be performed when the value in the Match Register (MR) matches with the value in the Timer Counter (TC). Bits 0 to 2 in MCR (i.e. MCR , MCR  and MCR ) are used to control the actions for MR0 register. Similarly, bits 3 to 5 for MR1, bits 6 to 8 for MR2 and bits 9 to 11 for MR3 respectively. We will see the function associated with MR0.
Bit 0: When this bit is 1, an interrupt is triggered when MR0 is equal to TC. When this bit is 0, the interrupts is disabled.
Bit 1: When this bit is 1, TC is reset when MR0 is equal to TC. When this bit is 0, this feature is disabled.
Bit 2: When this bit is 1, the Timer Counter (TC) and Prescale Counter (PC) are stopped when the value in MR0 is equal to TC. Also, the TC is reset to 0.
Similar actions can be applicable for other Match Register.
Match Registers (MR0 – MR3)
The Match register values are continuously compared to the Timer Counter value. When the two values are equal, actions can be triggered automatically. The action possibilities are to generate an interrupt, reset the Timer Counter, or stop the timer. Actions are controlled by the settings in the MCR register.
Capture Control Registers (CCR)
The Capture Control Register is used to control whether one of the four Capture Registers is loaded with the value in the Timer Counter when the capture event occurs, and whether an interrupt is generated by the capture event.
Capture Registers (CR0 -CR3)
Each Capture register is associated with a device pin and may be loaded with the Timer Counter value when a specified event occurs on that pin. The settings in the Capture Control Register determine whether the capture function is enabled, and whether a capture event happens on the rising edge of the associated pin, the falling edge, or on both edges.
To use timers we need to first configure them. I would like to encourage the readers to use the following sequence for Setting up Timers:
Set appropriate value in TxCTCR
Define the Prescale value in TxPR
Set Value(s) in Match Register(s) if required
Set appropriate value in TxMCR if using Match registers / Interrupts
Reset Timer – Which resets PR and TC
Set TxTCR to 0x01 to Enable the Timer when required
Reset TxTCR to 0x00 to Disable the Timer when required
Prescale Register Value Calculation
The delay or time required for 1 clock cycle at ‘X’ MHz is given by :
Delay = 1/(X*1000000) Seconds
Hence in our case when PR=0 i.e TC increments at every PCLK the delay required for TC to increment by 1 is:
Delay = (0+1)/(60*1000000) Seconds
Similarly when we set PR = 59999 the delay in this case will be:
Delay = (599999+1)/(60*1000000) Seconds
Delay = 1mS
… which boils down to 1/1000 = 0.001 Seconds which is nothing but 1 Milli-Second i.e mS. Hence the delay required for TC to increment by 1 will be 1mS.
Without Match Register
In this code LED will Blink with 1 second delay. I’m using Timer 0 in this code. LEDs are connected to Port 0. Here My PCLK is 60MHz. You can download entire project here.
void delay(unsigned int z);
delay(1000); //1sec delay
delay(1000); //1sec delay
void pll() //Fosc=12Mhz,CCLK=60Mhz,PCLK=60MHz
void delay(unsigned int z)
T0CTCR=0x0; //Select Timer Mode
T0TCR=0x00; //Timer off
T0PR=59999; //Prescaler value for 1ms
T0TCR=0x02; //Timer reset
T0TCR=0x01; //Timer ON
T0TCR=0x00; //Timer OFF
T0TC=0; //Clear the TC value. This is Optional.
With Match Register
In this code LED will Blink with 1 second delay. I’m using Timer 1 in this code. LEDs are connected to Port 0. Here My PCLK is 60MHz. You can download entire project here.
void pll() //Fosc=12Mhz,CCLK=60Mhz,PCLK=60MHz
T1CTCR=0X0000; //Timer Mode
T1PR=59999; //Prescaler Value
T1MR0=1000; //Match Register Value
T1MCR=0x00000004; //TC and PC will be stopped and TCR will be set to 0 if MR0 matches the TC.
T1TCR=0X02; //Reset Timer
T1TCR=0X01; //Timer ON
T1TC=0; //Timer OFF
Hope it might helped you. If you have any doubts feel free to ask us.