LPCTimer and PWM Operations
LPCTimer and PWM Operations
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.
Applications
Timer Registers
• A timer has a Timer Counter (TC) and Prescale Register(PR) associated
with it.
• When Timer is Reset and Enabled TC is set to 0 and incremented by 1
every ‘PR+1′ clock cycles.
• When it reaches its maximum value it gets reset to 0 and hence restarts
counting.
• Prescale Register is used to define the resolution of the timer.
• If PR=0 then TC is incremented every 1 clock cycle of the peripheral
clock. If PR=1 then TC is incremented every 2 clock cycles of peripheral
clock and so on.
• By setting an appropriate value in PR we can make timer increment or
count : every peripheral clock cycle or 1 microsecond or 1 millisecond
or 1 second and so on.
• Match Register (MR0, MR1, MR2, MR3)
A Match Register is a Register which contains a specific value set by the
user. When the Timer starts – every time after TC is incremented the
value in TC is compared with match register. If it matches then it can
Reset the Timer or can generate an interrupt as defined by the user.
Match Registers can be used to:
• Stop Timer on Match(i.e when the value in count register is same as
that in Match register) and trigger an optional interrupt.
• Reset Timer on Match and trigger an optional interrupt.
• To count continuously and trigger an interrupt on match.
• MAT0.0 (2 pins) : P0.3 and P0.22
• MAT0.1 (1 pin) : P0.5
• MAT0.2 (2 pin) : P0.16 and P0.28
• MAT0.3 (1 pin) : P0.29
• MAT1.0 (1 pin) : P0.12
• MAT1.1 (1 pin) : P0.13
• MAT1.2 (2 pins) : P0.17 and P0.19
• MAT1.3 (2 pins) : P0.18 and P0.20
• Capture Register (CR0, CR1, CR2, CR3)
As the name suggests it is used to Capture Input signal. When a transition
event occurs on a Capture pin , it can be used to copy the value of TC into
any of the 4 Capture Register or to generate an Interrupt.
• CAP0.0 (3 pins) : P0.2, P0.22 and P0.30
• CAP0.1 (1 pin) : P0.4
• CAP0.2 (3 pin) : P0.6 (pin30), P0.16(pin46) and P0.28
• CAP0.3 (1 pin) : P0.29
• CAP1.0 (1 pin) : P0.10
• CAP1.1 (1 pin) : P0.11
• CAP1.2 (2 pins) : P0.17 and P0.19
• CAP1.3 (2 pins) : P0.18 and P0.21
1) PR : Prescale Register (32 bit) – Stores the maximum value of Prescale counter after
which it is reset.
2) PC : Prescale Counter Register (32 bit) – This register increments on every
PCLK(Peripheral clock). This register controls the resolution of the timer. When PC
reaches the value in PR , PC is reset back to 0 and Timer Counter is incremented by 1.
Hence if PR=0 then Timer Counter Increments on every 1 PCLK. If PR=9 then Timer
Counter Increments on every 10th cycle of PCLK. Hence by selecting an appropriate
prescale value we can control the resolution of the timer.
3) TC : Timer Counter Register (32 bit) – This is the main counting register. Timer Counter
increments when PC reaches its maximum value as specified by PR. If timer is not reset
explicitly(directly) or by using an interrupt then it will act as a free running counter which
resets back to zero when it reaches its maximum value which is 0xFFFFFFFF.
4) TCR : Timer Control Register (8 bit) – This register is used to enable , disable and reset
TC. When bit0 is 1 timer is enabled and when 0 it is disabled. When bit1 is set to 1 TC and
PC are set to zero together in sync on the next positive edge of PCLK. Rest of the bits of
TCR are reserved.
5) CTCR : Count Control register (8 bit)– Used to select Timer/Counter Mode. When the
value of the CTCR is set to 0×0 Timer Mode is selected.
6) MCR : Match Control register (16 bit) – This register is used to control which all
operations can be done when the value in MR matches the value in TC. Bits 0,1,2 are
for MR0 , Bits 3,4,5 for MR1 and so on.. Heres a quick table which shows the usage:
For MR0:
• Bit 0 : Interrupt on MR0 i.e trigger an interrupt when MR0 matches TC. Interrupts
are enabled when set to 1 and disabled when set to 0.
• Bit 1 : Reset on MR0. When set to 1 , TC will be reset when it matched MR0.
Disabled when set to 0.
• Bit 2 : Stop on MR0. When set to 1 , TC & PC will stop when MR0 matches TC.
• Similarly bits 3-5 , 6-8 , 9-11 are for MR1 , MR2 , MR3 respectively.
7) IR : Interrupt Register (8 bit) – It contains the interrupt flags for 4 match and 4
capture interrupts. Bit0 to bit3 are for MR0 to MR3 interrupts respectively. And
similarly the next 4 for CR0-3 interrupts. when an interrupt is raised the
corresponding bit in IR will be set to 1 and 0 otherwise. Writing a 1 to the
corresponding bit location will reset the interrupt – which is used to acknowledge
the completion of the corresponding ISR execution.
Setting up & configuring 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 0×01 to Enable the Timer when required
• Reset TxTCR to 0×00 to Disable the Timer when required
Void initTimer0(void)
• void initTimer0(void)
{
/*Assuming that PLL0 has been setup with CCLK = 60Mhz and PCLK
also = 60Mhz.*/
T0CTCR = 0x0;
T0PR = PRESCALE-1; //(Value in Decimal!) - Increment T0TC at every
PRESCALE clock cycles
T0TCR = 0x02; //Reset Timer
}
Calculations
• The delay or time required for 1 clock cycle at ‘X’ MHz is given by :
(1 / X * (10^6)) 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:
((0+1) / 60 * (10^6)) Seconds
• Similarly when we set PR = 59999 the delay in this case will be:
((59999+1) / 60 * (10^6)) = (60000 / 60 * (10^6) = 1 / (10^3) Seconds
• 1/1000 = 0.001 Seconds which is nothing but 1 Milli-Second Hence
the delay required for TC to increment by 1 will be 1mS.
delayMS(unsigned int milliseconds);
• void delayMS(unsigned int milliseconds) //Using Timer0
{
T0TCR = 0x02; //Reset Timer
T0TCR = 0x01; //Enable timer
while(T0TC < milliseconds); //wait until timer counter reaches the
desired delay
T0TCR = 0x00; //Disable timer
}
• #include <lpc214x.h>
void delayMS(unsigned int milliseconds);
void initTimer0(void);
int main(void)
{
initTimer0(); //Initialize Timer0
IO0DIR = 0xFFFFFFFF; //Configure all pins on Port 0 as Output
while(1)
{
IO0SET = 0xFFFFFFFF; //Turn on LEDs
delayMS(500); //0.5 Second(s) Delay
IO0CLR = 0xFFFFFFFF; //Turn them off
delayMS(500);
}
}
• void initTimer0(void)
{
/*Assuming that PLL0 has been setup with CCLK = 60Mhz and PCLK also = 60Mhz.*/
T0CTCR = 0x0;
T0PR = PRESCALE-1; //(Value in Decimal!) - Increment T0TC at every 60000 clock cycles
//Count begins from zero hence subtracting 1
//60000 clock cycles @60Mhz = 1 mS
T0TCR = 0x02; //Reset Timer
}
void delayMS(unsigned int milliseconds) //Using Timer0
{
T0TCR = 0x02; //Reset Timer
T0TCR = 0x01; //Enable timer
while(T0TC < milliseconds); //wait until timer counter reaches the desired delay
T0TCR = 0x00; //Disable timer
}
PWM
• Pulse Width Modulation or PWM is a way to encode data such that it
corresponds to the width of the pulse given a fixed frequency.
• Its also a way to control motors , power circuits , etc.. using the
‘width’ of the pulse.
• PWM has numerous uses like Motion Control , Dimming , Encoding
Analog Signal into its Digital form , in Power Regulation , etc.
• Pulse period and width can be any number of timer counts. This
allows complete flexibility in the trade-off between resolution and
repetition rate. All PWM outputs will occur at the same repetition
rate.
• LPC2148 supports 2 types of PWM :
2. Each PWM output will go low when its match value is reached. If no
match occurs (i.e. the match value is greater than the PWM rate), the
PWM output remains continuously high.
Configuring and Initializing PWM
//PWM Generation goes active now - LED must be 25% Bright after Reset!!
//Now you can get the PWM output at Pin P0.0!
}