Index/Blueprint
- What are TIMER peripheral in MCU?
- Concepts involved in Timer peripheral?
- Types of Features in TIMER peripheral in MCU?
What is TIMER Peripheral in Microcontroller?
- A timer peripheral in a microcontroller is a peripheral that is used to count the clock cycles in the form of ticks to generate/ capture different forms of digital signals and to generate time delay. ( For now we will not focus on time delays)
Here by digital signals, we mean the PWM signals. PWM signals are raw digital signals having the rapid switching from High to Low state. So, using TIMER peripheral we can generate and capture these PWM signals.
(In a Microcontroller, clock signals are a type of signal that oscillates btw a high and low state and in that each pulse is a tick)
When used for generation of PWM signals, its objective is to control the voltage/current across a device by manipulating its frequency and duty cycle parameters.
- In which frequency determines how fast the signal switches from High to Low state and Low to high state. Higher the frequency, higher is the switching rate. Means faster the device will get On and Off. On at High Time and Off at Low time. This frequency is so fast that, device doesnt gets off, but it gets some mean/average volatge across itself because of multiple high and low signals within very short time. This short time is basically frequency. Say if we say frequency of 100 hertz, meaning in 1 sec signal will be switching state ,100 times. If we say frequency of 10,000 hertz, meaning in 1 sec signal would be switching at 10,000 times.
- And duty cycle determines, how long the signal would be in high state and how long the signal would be low state. Higher is the duty cycle, means higher is the High time of PWM signal. So, more voltage would be provided to end device. Lower the duty cycle, means lower is the High time of PWM signal. So, less voltage would be provided to end device.
When used for capture of PWM signals, its objective to decode the external digital data by calculating its Frequency and Duty cycle values.
- Based on these values, of external PWM signal, MCU can get to know what the readings of number of external sensors. Several sensor circuits generate PWM (Pulse Width Modulation) signals directly, as PWM is a convenient way to encode sensor outputs into a digital format.
- Sensor outputs like: Temperature sensor, light sensor, current sensors, ultrasonic distance sensors, pressure sensors and list is very large.
Functional Concept in Timer Peripheral
- TIMER Counters:
- Types of Counters
- Running Counter
- MAX Counter
- Compare/Capture counter
- Types of modes in which counter runs
- Upcounting
- Downcounting
- Updown counting
- Types of Counters
- Timer clock/prescaler:
TIMER Counter
Now in TIMER peripheral, so as to generate and capture these PWM signals there are concepts of Counters in it. Counters as it names says, counts the mathematics numeric numbers. There are 3 types of Counters:
- Running counter: This counter is free running counter that keeps on running either in incremental order or decremental order.
- MAX counter: This counter has the value up to which value, running counter runs.
- Compare/Capture counter: This counter stores the value, according to which state of PWM signal changes from High to Low or Low to High.( It should be of less value then MAX counter value)
So how things work is, as we start the Timer peripheral, running counter starts counting from 0 value to value stored in MAX counter. And now depending upon whether we want to generate or capture, compare/capture register role will come in.
Briefing about Upcounting, Downcountinga and updown counting of Counter registers
TIMER Clock and Prescaler
Types of features in TIMER Peripheral in MCU
- Time Delays
- Output capture mode: PWM signals
- Input Capture mode: capturing external PWM Signals
Time Delays
Time delay feature is feature in which we do not output or input any PWM signal. We just use counters for generating delays. Till the time running counter reaches MAX counter value delays are generated.
Output Capture feature: PWM Signals
As it name says, Output Capture feature, it outputs the digital signal. In output capture register, the PWM signals are outputted via the Microcontroller pin. In this their is concept of PWM signal state and edges. That is which PWM signal state should be outputted first: High signal or Low signal. And which PWM signal state should be outputted after Compare match. Based on this value polarity of PWM signals are determined. IF we have configured to output High signal at first, then after compare LOW signal should be configured. If we configure High signal only, then no PWM signal change we will see.
Working of PWM signals
If we want to generate, then value would be stored in compare register by MCU and as running counter starts incrementing from 0 value, the digital signal generated from MCU pin would be of High state or Low state( that we can configure in programming which state of signal should be generated at first) and
- as Running counter reaches to Compare register value, the PWM signal state toggles from previous state, (that is state of pin changes from High signal (if that is previous state) to low signal or low signal (if that is previous state) to high signal).
- And for time till running counter reaches max counter value, that new state signal is generated and as Running counter reaches to MAX counter value, the PWM signal state toggles again to original state.
- And then this process keeps on happening to generate the PWM signal.
So you will see that compare register value determines the duty cycle, for how long the signal should be kept high or how long the signal should be kept low. And the value of MAX register determines the frequency, as according to MAX register value same pulse would repeat, so less the MAX register value more pulses would be their more frequency would be there.
Based upon nature of PWM signals there are 3 types of PWM signals in an MCU
Types of PWM Signals:
- Center Aligned
- Edge Aligned
- Complementary
Input Capture Modes: External PWM signals
In the case of ICU signal, we collect the external digital signals. For that we need to configure which state of external PWM signal should be detected first and what all states of external signal should be captured. Based on that signal measurement happens.
Working of ICU signals:
If we want to capture, then we at first configure which state of external signal we want to capture: High state (Rising edge) or Low state (failing edge). Based on state’s captured on Pin of MCU, the value of running counter is shifted to Capture register. And then using these values in capture register PWM parameters are calculated.
- For better understanding of it, running counter would start incrementing from 0.
- Now let’s say we have configured both state’s (High State and Low state) of PWM signal to be captured. So, when at first High signal comes in, the running counter has value of 1000, so Capture registers get 1000 value,
- after that low state comes in and at that point running counter has value of 2000, so capture register gets 2000 value.
- And subtraction of this 2000-1000 value would be Duty cycle. As this is the amount of time signal is High.
- After that again High signal would come and in capture register, we get 3000 value. So, 3000-1000 would be period of PWM signal, from which we can calculate frequency of PWM signal.
And so an and so for. We just have to taken care that MAX counter value is not capped. That is if external PWM signal is of very low frequency, then its period would be high and it should be not that high that MAX counter value capes out.
ICU Signal Parameters briefing
TIMER Peripheral in Microcontroller
- Timer Instances and Timer Pins
- TIMER Counter value: 8 bit, 16 bit or 32 bit
Now in a Microcontroller to use TIMER peripheral, there are instances of TIMER peripheral. And each instance has some Microcontroller pins asscoitaed with them. Which can be used in TIMER peripheral mode to generate and capture such digital signals. In Timer peripheral we refer these pins as Channels. So Each Instance of TIMER peripheral has channels associated with it through which PWM signals can be captured and generated.
And the counters in TIMER peripheral can vary from 8 bit size to 32 size in MCU to MCU. Low end MCU’s like AVR Atmega 328 has 8 bit and 16 bit Timer counters. High end MCU’s like S32K144 has 32 bit Timer counters.
More the Timer counter value, means higher amount of PWM frequencies we can capture and generate.
Author: Kunal Gupta
Author