LPC1768 Timer Programming: Timer0, Timer1 & Registers

LPC1768 Timer Programming_ Timer0, Timer1 & Registers
 The LPC1768 Timer is a hardware peripheral used for accurate delays, periodic events, time measurement, and timer interrupts. This guide explains LPC1768 Timer0 and Timer1, including PR, PC, TC, MR0, MCR, and TCR registers. It also covers timer-delay calculations and a practical register-level C programming example

The LPC1768 Timer is an important peripheral for generating accurate delays, measuring time intervals, creating periodic events, generating interrupts, and controlling time-dependent operations in embedded applications. The LPC1768 is an ARM Cortex-M3-based microcontroller widely used for learning and developing embedded systems. For students learning LPC1768 Timer Programming, understanding registers such as PR, TC, PC, MR0, MCR, and TCR is essential. This guide explains LPC1768 Timer0 and Timer1 programming from the basics, including timer clock configuration, timer calculations, important registers, polling, interrupts, and a simple register-level C programming example.

What is a Timer in LPC1768?

A timer is a hardware peripheral that counts clock pulses. Instead of using software loops to create delays, the microcontroller’s hardware timer can count clock cycles accurately.

The LPC1768 provides four timer/counter peripherals:

  • Timer0
  • Timer1
  • Timer2
  • Timer3

Among these, LPC1768 Timer0 and Timer1 are commonly used when learning timer programming and developing embedded applications.

An LPC1768 timer can be used for applications such as:

  • Generating accurate delays
  • Creating periodic events
  • Generating timer interrupts
  • Measuring the duration of an event
  • Controlling LEDs and buzzers
  • Generating time bases for communication protocols
  • Measuring pulse width and frequency

Because timer operation is handled by dedicated hardware, it is generally more predictable than creating long delays using software loops.

LPC1768 Timer Clock

Before programming an LPC1768 Timer, it is important to understand the clock supplied to the timer peripheral.

The CPU clock and peripheral clock are related, but they do not necessarily operate at the same frequency. The timer uses its configured peripheral clock, commonly referred to as PCLK.

For example, suppose the LPC1768 is configured with CCLK = 96 MHz and the peripheral clock is configured as PCLK = 24 MHz.

The timer receives the peripheral clock according to the appropriate PCLK configuration.

This clock becomes the starting point for calculating the timer frequency and delay.

The basic relationship is:

Timer Frequency = PCLK / (PR + 1)

where PR is the Prescale Register value.

registor_now_P

Important LPC1768 Timer Registers

Several registers are used when programming LPC1768 Timer0 and Timer1. Understanding these registers is important for register-level LPC1768 embedded programming.

1. PR – Prescale Register

The Prescale Register (PR) determines how many peripheral-clock cycles are required before the Timer Counter is incremented.

For example:

LPC_TIM0->PR = 23;

If PCLK = 24 MHz, then:

Timer Counter Frequency = 24 MHz / (23 + 1) = 1 MHz

This means the Timer Counter increments once every 1 / 1 MHz = 1 µs.

So, a PR value of 23 converts a 24 MHz peripheral clock into a 1 MHz timer counting rate.

The prescaler is therefore useful when a convenient timer tick, such as 1 µs, is required.

2. TC – Timer Counter

The Timer Counter (TC) contains the current timer count.

Once the prescaler reaches the value specified by PR, the TC is incremented.

For example, if the timer is operating at 1 MHz: TC = 0, TC = 1, TC = 2, TC = 3, and so on.

Each increment represents approximately 1 microsecond.

The TC register is therefore useful for determining how much time has elapsed.

3. PC – Prescale Counter

The Prescale Counter (PC) counts peripheral clock cycles.

When PC reaches the value specified by PR, PC is reset and TC is incremented.

The basic relationship can be represented as:

PCLK → PC → TC

If PCLK = 24 MHz and PR = 23, the PC counts the required clock cycles, and after completing one prescale period, the TC increases by one.

Therefore, PC and TC work together. PC handles the smaller clock divisions, while TC represents the actual timer count.

4. MR0 – Match Register 0

The Match Register 0 (MR0) stores a value that the Timer Counter is compared against.

For example:

LPC_TIM0->MR0 = 5000000;

When TC == MR0, a match event occurs.

Depending on the configuration of the Match Control Register (MCR), the timer can generate an interrupt, reset the timer, or stop the timer.

The match register is particularly useful for creating accurate periodic timer events.

LPC1768 Timer Delay Calculation

A common LPC1768 Timer calculation uses the peripheral clock and prescaler.

Suppose PCLK = 24 MHz and PR = 23. The timer frequency becomes:

Timer Frequency = PCLK / (PR + 1) = 24,000,000 / 24 = 1,000,000 Hz

Therefore, 1 Timer Count = 1 µs.

For a 5-second delay, 5 seconds = 5,000,000 µs. Therefore:

MR0 = 5,000,000

This gives the relationship:

Delay = MR0 × Timer Count Period

For a 1 MHz timer:

Delay = 5,000,000 × 1 µs = 5 seconds

This type of calculation is fundamental when creating an LPC1768 Timer delay.

5. TCR – Timer Control Register

The Timer Control Register (TCR) controls the operation of the timer.

Important bits include:

  • Bit 0 – Timer Enable
  • Bit 1 – Timer Reset

For example, the following starts Timer0:

LPC_TIM0->TCR = 1;

To reset the timer:

LPC_TIM0->TCR = 2;

After resetting, the timer can be enabled again:

LPC_TIM0->TCR = 1;

The TCR is commonly used when starting, stopping, or resetting an LPC1768 Timer.

6. MCR – Match Control Register

The Match Control Register (MCR) determines what happens when a match occurs.

For MR0, commonly used options include:

  • Generate an interrupt
  • Reset the timer
  • Stop the timer

For example, a configuration can be selected so that when TC == MR0, the timer automatically resets and starts counting again.

This is useful for generating periodic events.

The MCR therefore allows the timer to operate automatically without requiring the software to constantly monitor the TC register.

Explore Courses - Learn More

Simple LPC1768 Timer0 Example

The following register-level C program demonstrates a basic LPC1768 Timer0 delay. The example assumes that the required clock configuration has already been performed.

#include <LPC17xx.h>

void Timer0_Init(void)
{
    LPC_SC->PCONP |= (1 << 1);

    LPC_TIM0->PR = 23;
    LPC_TIM0->MR0 = 5000000;

    LPC_TIM0->MCR = (1 << 1);

    LPC_TIM0->TCR = 2;
    LPC_TIM0->TCR = 1;
}

void delay_5sec(void)
{
    LPC_TIM0->TCR = 2;
    LPC_TIM0->TCR = 1;

    while(LPC_TIM0->TC < 5000000);

    LPC_TIM0->TCR = 0;
}

int main(void)
{
    Timer0_Init();

    while(1)
    {
        delay_5sec();
    }
}

This example demonstrates the basic concept of configuring the timer peripheral, setting the prescaler, starting the timer, monitoring the Timer Counter, and stopping the timer after the required delay.

The exact register configuration can be modified depending on whether the application requires polling, interrupts, periodic operation, or one-shot timing.

LPC1768 Timer0 vs Timer1

LPC1768 Timer0 and Timer1 provide similar timer functionality, but they are separate hardware peripherals with their own registers.

For example, Timer0 uses:

LPC_TIM0->PR
LPC_TIM0->TC
LPC_TIM0->PC
LPC_TIM0->MR0

For Timer1, the corresponding registers are:

LPC_TIM1->PR
LPC_TIM1->TC
LPC_TIM1->PC
LPC_TIM1->MR0

This allows Timer0 and Timer1 to be used for different timing requirements within the same application.

For example, one timer could be used to generate a periodic sensor-sampling event while another timer handles a communication timeout or other timing requirement.

Polling vs Timer Interrupt

There are two common ways to detect a timer event: polling and timer interrupts.

Polling

With polling, the program continuously checks the timer value:

while(LPC_TIM0->TC < 5000000);

This method is simple and useful for beginners learning LPC1768 Timer Programming, but the CPU remains occupied while waiting.

Polling can be suitable for simple applications where the processor does not need to perform other tasks during the delay.

Timer Interrupt

With a timer interrupt, the CPU can perform other tasks and respond automatically when the timer reaches the configured match value.

Timer interrupts are particularly useful in real-time embedded applications where multiple tasks must execute at specific intervals.

For example, a timer interrupt can periodically trigger a sensor-reading routine while the main program continues to handle other operations.

Practical Applications of LPC1768 Timers

LPC1768 Timer peripherals can be used in many embedded projects, including:

  • LED blinking
  • Buzzer control
  • Digital clocks
  • Periodic sensor sampling
  • Motor control timing
  • Communication timeouts
  • Pulse measurement
  • Event scheduling
  • Real-time monitoring systems
  • Automotive embedded applications

For example, a timer can trigger an ADC reading every 100 milliseconds. The collected sensor data can then be processed without relying on inaccurate software delay loops.

This makes hardware timers useful for applications where consistent timing is important.

Why Learn LPC1768 Timer Programming?

Learning LPC1768 Timer Programming helps embedded systems students understand how a microcontroller manages time using hardware peripherals rather than relying entirely on software delays.

By working with registers such as PR, PC, TC, MR0, MCR, and TCR, students can understand:

  • How peripheral clocks are converted into timer ticks
  • How prescalers control timer resolution
  • How match registers generate timing events
  • How timers create periodic operations
  • How timer interrupts work
  • How polling differs from interrupt-based timing
  • How hardware timers are used in real embedded applications

These concepts are also useful when moving from basic microcontroller programming to more advanced ARM Cortex-M development.

Frequently Asked Questions About LPC1768 Timer

1. What is an LPC1768 Timer?

An LPC1768 Timer is a hardware peripheral in the LPC1768 ARM Cortex-M3 microcontroller used for generating delays, measuring time intervals, creating periodic events, generating interrupts, and other time-dependent operations.

2. What is the difference between LPC1768 Timer0 and Timer1?

Timer0 and Timer1 are separate timer/counter peripherals in the LPC1768. They provide similar functionality but have independent registers, allowing them to be configured for different timing requirements within the same application.

3. How is LPC1768 Timer delay calculated?

The timer frequency can be calculated using:

Timer Frequency = PCLK / (PR + 1)

Once the timer tick period is known, the required match value can be calculated using:

Delay = MR0 × Timer Count Period

For example, with a 1 MHz timer, one timer count is approximately 1 µs.

Conclusion

The LPC1768 Timer is an important peripheral for embedded systems programming. Timer0, Timer1, Timer2, and Timer3 provide hardware-based timing capabilities that can be used for delays, interrupts, periodic events, pulse measurement, and real-time applications.

Understanding the PR, PC, TC, MR0, MCR, and TCR registers is essential for anyone learning LPC1768 timer programming. Once the relationship between PCLK, prescaler, timer frequency, and match values is understood, creating accurate timer delays and periodic events becomes much easier.

For embedded systems students, practical knowledge of LPC1768 Timer0, Timer1, timer calculations, and register-level C programming provides a strong foundation for working with ARM-based microcontrollers and real-time embedded applications.

Talk to Academic Advisor

Frequently Asked Questions

An LPC1768 Timer is a hardware peripheral in the LPC1768 ARM Cortex-M3 microcontroller used for generating delays, measuring time intervals, creating periodic events, generating interrupts, and other time-dependent operations.

Timer0 and Timer1 are separate timer/counter peripherals in the LPC1768. They provide similar functionality but have independent registers, allowing them to be configured for different timing requirements within the same application.

The timer frequency can be calculated using:

Timer Frequency = PCLK / (PR + 1)

Once the timer tick period is known, the required match value can be calculated using:

Delay = MR0 × Timer Count Period

For example, with a 1 MHz timer, one timer count is approximately 1 µs.

Author

Embedded Systems Trainer– IIES

Updated On: 07-10-26


10+ years of hands-on experience delivering practical training in Embedded Systems and it's design