LPC1768 PWM is a hardware-based Pulse Width Modulation feature used to generate precise PWM signals for embedded applications. This guide explains LPC1768 PWM registers such as PR, MR0, MR1, MCR, PCR, TCR, and LER, along with PWM frequency and duty-cycle calculations. It also includes a practical Embedded C example and explains PWM applications such as LED brightness, motor speed, and servo control.Pulse Width Modulation (PWM) is an important technique used in embedded systems to control power, motor speed, LED brightness, servo position, and other electrical parameters. The LPC1768 PWM peripheral allows developers to generate accurate PWM signals using dedicated hardware instead of manually toggling GPIO pins through software. The LPC1768 is an ARM Cortex-M3 based microcontroller with hardware PWM capabilities. Understanding LPC1768 PWM programming, PWM registers, frequency calculations, and duty-cycle calculations is useful for embedded systems students and developers working with motor control, LED applications, and other real-time control systems. This guide explains the LPC1768 PWM peripheral, important PWM registers, frequency and duty-cycle calculations, and a practical Embedded C example.
PWM stands for Pulse Width Modulation.
A PWM signal is a digital signal that repeatedly switches between HIGH and LOW states. Instead of continuously changing the voltage level, PWM controls the amount of time the signal remains HIGH during each cycle.
Two important parameters define a PWM signal:
These parameters determine how the PWM output behaves in an embedded system.
The PWM duty cycle represents the percentage of one complete PWM period during which the signal remains HIGH.
The formula is:
For example, if a PWM signal remains HIGH for 25% of its total period, Duty Cycle = 25%.
Common duty-cycle values include:
In LED applications, increasing the duty cycle generally increases the average power delivered to the LED and therefore its apparent brightness.
In motor-control applications, changing the PWM duty cycle can control the average voltage or power delivered to the motor, depending on the motor driver and overall circuit design.
Understanding LPC1768 PWM duty cycle is therefore important when configuring PWM output for practical embedded applications.
PWM frequency represents the number of complete PWM cycles generated per second. It is measured in Hertz (Hz).
For example:
The relationship between frequency and period is:
The required PWM frequency depends on the application. LED dimming, motor control, servo control, and other applications can require different frequency ranges.
The LPC1768 includes dedicated hardware for PWM signal generation. This allows the microcontroller to generate PWM outputs without requiring the CPU to manually toggle a GPIO pin for every PWM pulse.
This hardware-based approach provides more accurate and consistent timing.
The LPC1768 PWM peripheral includes important components such as:
The PWM peripheral can generate multiple PWM outputs depending on the selected PWM channel and the corresponding pin configuration.
Understanding the LPC1768 PWM registers is essential for register-level PWM programming.
The following registers are commonly involved when configuring PWM.
The Prescale Register (PR) determines how the peripheral clock is divided before the PWM Timer Counter increments.
For example:
LPC_PWM1->PR = 23;Assume the PWM peripheral clock is PCLK = 24 MHz. The PWM counter frequency becomes:
Therefore, one PWM counter increment represents approximately 1 µs.
This makes timing calculations easier when configuring the PWM period.
The Timer Counter (TC) keeps track of the current PWM timer count.
For example: TC = 0, TC = 1, TC = 2, TC = 3, and so on.
If the PWM counter frequency is 1 MHz, each timer-counter increment represents approximately 1 microsecond.
The Timer Counter works together with the match registers to determine when PWM events occur.
The Prescale Counter (PC) counts peripheral clock cycles according to the value programmed into the Prescale Register.
When the required prescale period is completed, the Timer Counter is incremented.
The basic timing relationship can be represented as:
This relationship is important when understanding LPC1768 PWM frequency calculation.
Match Register 0 (MR0) is one of the most important registers in PWM programming because it establishes the PWM period in the selected PWM mode.
For example:
LPC_PWM1->MR0 = 10000;If one PWM counter increment equals 1 µs:
So, under these clock and prescaler assumptions, an MR0 value of 10,000 produces a 100 Hz PWM period.
The LPC1768 PWM peripheral provides multiple match registers.
Depending on the PWM channel being used, registers such as MR1, MR2, MR3, MR4, MR5, and MR6 can be used to determine individual PWM match positions.
For example:
LPC_PWM1->MR1 = 2500;If MR0 = 10000 and MR1 = 2500, then the corresponding duty cycle can be calculated as:
This relationship is central to LPC1768 PWM duty-cycle calculation.
The PWM Match Control Register (MCR) determines what happens when the PWM timer reaches a configured match value.
Depending on the selected bits, a match event can:
For PWM period generation, the timer can be configured to reset when the Timer Counter reaches MR0.
For example:
LPC_PWM1->MCR = (1 << 1);This configures the corresponding MR0 match action for timer reset.
The PWM Control Register (PCR) is used to enable PWM outputs and configure the supported PWM operating mode.
For example:
LPC_PWM1->PCR = (1 << 9);The appropriate PCR bit depends on the PWM channel being used.
The PCR therefore plays an important role in enabling the required LPC1768 PWM output.
The Timer Control Register (TCR) controls the PWM timer.
Important functions include:
For example:
LPC_PWM1->TCR = (1 << 0) | (1 << 3);This enables the timer and PWM operation after the required configuration has been completed.
The Latch Enable Register (LER) is used to transfer updated match-register values into the PWM logic at the appropriate period boundary.
For example:
LPC_PWM1->LER = (1 << 0) | (1 << 1);This enables the updated MR0 and MR1 values to be latched.
The LER is particularly useful when changing PWM parameters during runtime because it helps apply updated match values synchronously with the PWM cycle.
Let’s consider a practical LPC1768 PWM frequency calculation example.
Assume PCLK = 24 MHz and PR = 23.
The PWM counter frequency is:
Therefore, 1 counter increment = 1 µs.
Suppose MR0 = 10000. Then:
So, under these clock assumptions, MR0 = 10000 gives a PWM Frequency of 100 Hz.
This demonstrates how the prescaler and MR0 value determine the PWM period.
Suppose MR0 = 10000 and MR1 = 5000. The duty cycle is:
Therefore, the PWM output has a 50% duty cycle.
For a 25% duty cycle, MR1 = 2500 and MR0 = 10000:
Similarly, for a 75% duty cycle, MR1 = 7500 and MR0 = 10000, giving a resulting duty cycle of 75%.
Therefore, changing the match value while keeping MR0 constant allows the PWM duty cycle to be adjusted.
The following is a simplified LPC1768 PWM C program demonstrating register-level configuration:
#include
void PWM1_Init(void)
{
/* Power to PWM1 */
LPC_SC->PCONP |= (1 << 6); /* PWM peripheral clock configuration */ LPC_SC->PCLKSEL0 &= ~(3 << 12); LPC_SC->PCLKSEL0 |= (1 << 12); /* Prescaler */ LPC_PWM1->PR = 23;
/* PWM period */
LPC_PWM1->MR0 = 10000;
/* PWM duty cycle */
LPC_PWM1->MR1 = 5000;
/* Reset PWM counter on MR0 */
LPC_PWM1->MCR = (1 << 1); /* Enable PWM1 output */ LPC_PWM1->PCR = (1 << 9); /* Latch MR0 and MR1 */ LPC_PWM1->LER = (1 << 0) | (1 << 1); /* Enable counter and PWM */ LPC_PWM1->TCR = (1 << 0) | (1 << 3);
}
int main(void)
{
PWM1_Init();
while(1)
{
}
}This example configures the PWM peripheral for a period and duty cycle based on the assumed clock configuration.
However, the exact output frequency depends on the actual LPC1768 peripheral clock (PCLK) and system clock configuration used by the application.
Register configuration alone is not sufficient to obtain a physical PWM signal from the LPC1768.
The selected microcontroller pin must also be configured for the appropriate PWM alternate function using the corresponding pin function registers.
For example, if a particular PWM channel is mapped to a specific LPC1768 pin, that pin must be configured for the PWM function rather than normal GPIO operation.
Therefore, a complete LPC1768 PWM programming workflow generally involves:
The LPC1768 PWM peripheral can be used in several embedded-system applications.
PWM can rapidly switch an LED ON and OFF. Changing the duty cycle changes the average power delivered to the LED, which allows brightness control.
PWM can be used with an appropriate motor driver to control the average voltage or power supplied to a DC motor.
PWM-based timing signals can be used for servo-control applications when the required pulse timing and frequency are configured according to the servo’s specifications.
PWM is also widely used in power electronics and embedded control systems to regulate the average power delivered to a load.
One of the major advantages of LPC1768 PWM is that PWM generation is handled by dedicated hardware.
With software-based GPIO toggling, the CPU has to continuously manage the timing of HIGH and LOW transitions. This can consume processor time and introduce timing variations.
Hardware PWM allows the peripheral to manage the waveform after the required registers have been configured.
This provides:
Two values are particularly important when configuring PWM:
For example:
This makes MR0 and the selected PWM match register the key parameters for controlling the PWM waveform.
LPC1768 PWM provides a hardware-based method for generating accurate Pulse Width Modulation signals for embedded applications such as LED brightness control, motor control, servo applications, and power control.
To program PWM at the register level, it is important to understand the Prescale Register, Timer Counter, Prescale Counter, Match Registers, Match Control Register, PWM Control Register, Timer Control Register, and Latch Enable Register.
The basic relationship is:
Once these concepts are understood, developers can calculate the required LPC1768 PWM frequency and duty cycle and configure the peripheral using Embedded C.
For practical embedded development, always verify the actual system clock, peripheral clock (PCLK), PWM channel, and pin alternate-function configuration before calculating the final PWM frequency or connecting the output to external hardware.
LPC1768 PWM is a hardware-based Pulse Width Modulation feature of the LPC1768 microcontroller used to generate PWM signals for applications such as LED brightness control, motor speed control, servo control, and power control.
PWM frequency depends on the peripheral clock, prescaler, and MR0 value. First, calculate the PWM counter frequency using the PCLK and prescaler, then use the MR0 value to determine the PWM period and resulting frequency.
The PWM duty cycle can generally be calculated using the match value relative to the period value: Duty Cycle (%) = (Match Value / MR0) × 100. For example, if MR0 is 10,000 and the match value is 5,000, the duty cycle is 50%.
Indian Institute of Embedded Systems – IIES