Learn About Timer/Counter Type E Single Slope Mode
The Timer Counter Type E (TCE) peripheral is a versatile hardware component commonly found in microcontrollers and embedded systems. It is designed to handle a variety of timing and counting tasks with high precision and flexibility. One of the operational modes supported by the TCE is the Single Slope Mode, which is particularly useful for generating waveforms, measuring time intervals, and controlling event timing.
Use Cases for Single Slope Mode
- Pulse Width Modulation (PWM) Control for Motors: single Slope Mode is often used to generate PWM signals for controlling the speed of DC motors. By adjusting the duty cycle, the average voltage applied to the motor changes, thereby controlling its speed.
- LED Dimming: in lighting applications, Single Slope Mode can be used to dim LEDs by varying the duty cycle of the PWM signal. This allows for precise control over the brightness of the LED without changing the voltage.
- Tone Generation: audio applications can utilize Single Slope Mode to generate tones of specific frequencies. By setting the counter's maximum value, the frequency of the output waveform can be controlled, which in turn can be used to produce different tones.
- Servo Motor Control: the precise timing control offered by Single Slope Mode is ideal for controlling the position of servo motors. The width of the PWM pulse dictates the angle to which the servo arm moves.
- Time Measurement: Single Slope Mode can be used in applications that require time measurement, such as in ultrasonic distance sensors. The time taken for a pulse to return can be measured by the counter, which can then be used to calculate distance.
- Frequency Generation: for applications that require a stable frequency output, such as clock generation or signal processing, Single Slope Mode can be configured to produce a consistent frequency signal.
Description
In Single Slope Mode, the TCE peripheral period is defined by counting clock pulses in the Counter Register value (CNT) register from 0 up to a maximum value, TOP, at which point the counter resets to the starting value, and the cycle repeats. The duty cycle is defined by a match in the counter value and one of the four comparator register values (CMPn). This mode is called single slope because the CNT increases or decreases in a single direction (depending on DIR bit setting) before resetting, resembling a ramp in its waveform representation. PWM signals are essential in controlling the power delivered to devices such as motors and LEDs, where the duty cycle of the waveform (the proportion of time the signal is high versus low) determines the power level.
Waveforms are selected using the WGMODE[2:0] bit. Each mode has its own unique set of definitions for MATCH, TOP, UPDATE and OVF. Single Slope Mode is selected by setting WGMODE[2:0] to 0b011. Single Slope Mode uses the PER register for TOP, CMPn for MATCH and the BOTTOM register for UPDATE and OVF. Refer to the "Terminology" section of the "Learn About Timer/Counter Type E (TCE)" page for definitions.
WGMODE[2:0] | Group Configuration | Mode of Operation | MATCH | TOP | UPDATE | OVF |
000 | NORMAL | Normal (Interrupt only) | - | PER | TOP/BOTTOM* | TOP/BOTTOM* |
001 | FREQUENCY | Frequency PWM | CMPn | CMP0 | TOP/BOTTOM* | TOP/BOTTOM* |
010 | - | Reserved | - | - | - | - |
011 | SINGLESLOPE | Single-Slope PWM | CMPn | PER | BOTTOM | BOTTOM |
100 | - | Reserved | - | - | - | - |
101 | DSTOP | Dual-Slope PWM | CMPn | PER | BOTTOM | BOTTOM |
110 | DSBOTH | Dual-Slope PWM | CMPn | PER | BOTTOM | TOP & BOTTOM |
111 | DSBOTTOM | Dual-Slope PWM | CMPn | PER | BOTTOM | BOTTOM |
Setting the PWM Period
In Single Slope Mode, the Period Register (PER) and associated Period Buffer (PERBUF) in the TCE peripheral are used to control the period time of the Waveform Output. As the Counter Register, CNT, increments, an UPDATE event will occur when its value matches TOP as defined by the PER register. At this time, the associated Waveform Output (WOn) is driven high if the CMPn value is greater than BOTTOM (0x0000).
Here’s how PER, CMPn, CNT, UPDATE, MATCH, TOP, and BOTTOM tie together to control the Waveform Output (WOn) period:
Configuring the PWM Duty Cycle
The Compare (CMP) and Compare Buffer (CMPBUF) registers define the duty cycle of a Waveform Output. When the Counter Register, CNT, matches a Comparator Register (CMPn) value, the associated Waveform Output (WOn) is driven low.
Here’s how PER, CMP, CMPBUF, CNT, UPDATE, MATCH, TOP and BOTTOM work together to control the Waveform Output (WOn) duty cycle:
By adjusting the value in the CMP register, and queuing changes in the CMPBUF, you can precisely control the duty cycle of the PWM signal. This is essential for applications where the amount of power delivered to a load needs to be controlled, such as in motor speed control, where the duty cycle determines the speed of the motor, or in LED dimming, where it determines the brightness of the light.
Initialization Sequence
The following initialization sequence for the TCE in Single Slope Mode. It is highly recommended to use MPLAB® Melody Code Configurator to simplify TCE setup. To see an example, refer to the "How To Generate 4 PWM Channels Simultaneously Using AVR® Timer Counter Type E" page.
- Enable TCE Module by setting the ENABLE bit.
- Select a system clock source divider using the CLKSEL[2:0] bits to divide by 1, 2, 4, 8, 16, 64, 256 or 1024.
- Select a count direction with the DIR Counter Direction bit.
- Use the WGMODE[2:0] Waveform Generation Mode bits to select SINGLESLOPE.
- Request a period using the 16-bit Period Register, PER[15:0].
- Request duty cycles by writing the associated 16-bit CMPn[15:0] registers.
- Verify actual period and duty cycles are acceptable*.
- Enable waveform outputs by setting the associated CMPnEN Compare Enable bit(s).
- Select whether waveform(s) will be inverted via the CMPnPOL Compare n Polarity bits.
- Choose resolution and scaling options with the HREN[1:0] High Resolution Enable bits and SCALEMODE[1:0] Scale Mode bits.
- If interrupts are needed, set the Comparator Interrupt using CMPn and/or the Timer Overflow/Underflow Interrupt Enable bit, OVF, bit in the Interrupt Control Register.
In summary, the Single Slope Mode of the TCE peripheral is a fundamental tool for developers working on embedded systems. Its ability to generate precise timing signals and PWM outputs makes it invaluable in a wide range of applications, from motor control to signal processing.
Learn More
- Learn About Timer/Counter Type E (TCE)
- How To Generate 4 PWM Channels Simultaneously Using AVR Timer Counter Type E
- Demo 1 - High Resolution
- Demo 2 – Scaling
- Demo 3 - WEX Direct
- Demo 4 - HW Fault
- Demo 5 - PWM Signal Modulation Example
- Generate PWM Signals Using TCE
- AVR16EB14/20/28/32 Data Sheet
Topics