Basic Timers in LPC2148
Today we are going to discuss about timers. LPC2148 have two 32bit timers. By 32 bit I mean it’s registers are 32bit wide. In other controllers it could be 8 or 16 bit also.
Timing is a very important part of any microcontroller programming. We certainly can not depend on delay functions because no matter how precise they are, they are not accurate. For eg- DHT11, a temperature sensor, works only if we give it specific pulses for specific amount of time(in microseconds and milliseconds).We will learn about it later in the upcoming tutorials, But for now we will use timer for controlling the blinking rate of LED, with different time interval.
Before going to the program, let’s go through some important registers, that we are going to use in timer.
- TCR- Timer Control Register. The TCR is used to control the Timer Counter functions. The Timer Counter can be disabled or reset through the TCR.
- TC- Timer Counter. The 32-bit TC is incremented every PR+1 cycles of PCLK(Peripheral Clock). The TC is controlled through the TCR. We will set the delay required here.
- PR- Prescale Register. The Prescale Counter (below) is equal to this value, the next clock increments the TC and clears the PC.
- 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 and the PC is cleared. The PC is observable and controllable through the bus interface.
- CTCR- Count Control Register.The CTCR selects between Timer and Counter mode, and in Counter mode selects the signal and edge(s) for counting. We are mostly going to use timer mode.
One important thing before we go to main programming section, as I mentioned that I have 12 MHz oscillator providing clock input but LPC2148 have capability to reach upto 60MHz. So through some PLLs (Phase Locked Loops) setting, I am first setting my PCLK (Peripheral clock) to 60 MHz, and than divide it according to the need.
Some Insight into the code
Below is the Timer initialisation function. In order to initialise the Timer, we need to do the following
- select the timer mode
- Enter the value for the Prescalar Register
- reset the timer
The Prescalar Register divides the APB clock to get the required Timer Clock Frequency. The Timer Clock frequency depends on the working range for the delay. For example, here I want to blink the LED every 1 second, so even millisecond Delays are okay for me.
Considering the above scenario, I will input the Timer clock of 1000 Hz, which is equivalent to 1 millisecond delay.
After initializing the Timer, we need to write a function to create delay. Below are the steps to do so
- Set TC counter to 0
- Reset the timer
- Enable timer
- Wait until TC counter reaches our input value
- Disable the timer
Let’s see How to use this function
You can see above that I am initializing the Timer so that the Timer clock frequency would be 1KHz. This means the the Timer Counter (TC) is going to take 1 millisecond for each count.
Now, I have entered the delay (1000), so TC will wait until it reaches the input value. Which will take 1 second for it to do so. When it does, the timer will be disabled, and we will get the required delay.
- FreeRTOS Tutorial #5 ->Using Queue April 2, 2020
- Low Power Modes in STM32 March 30, 2020
- FreeRTOS TUTORIAL #4 -> Using Counting Semaphore February 18, 2020
- Interface LCD 16×2 with STM32 without I2C January 21, 2020
- Interface DS3231 RTC module with STM32 January 13, 2020
- How to use Binary Semaphore in STM32 January 11, 2020