Exploring STC 8051 Microcontrollers – Coding
Timing-Related Hardware Overview
When it comes to timer-counters and time-related hardware, STC microcontrollers pack lot of punch. There are five 16-bit timer-counters, a Programmable Counter Array (PCA) and an enhanced PWM hardware peripheral. The timers are similar to the timers of standard 8051 microcontrollers. The rest of the hardware peripherals are bonus features that are not typically available in traditional 8051s.
Timer-Counters T0 – T4 are the five 16-bit timer-counters. As with the timers of any MCU, timers of STC microcontrollers can be employed for a number of tasks including time-base, timing events and outputs, measuring intervals, etc. Timers of 8051 architecture-based microcontrollers are usually also responsible for internal serial port (UART) hardware baud-rate generations and so is the case with STC microcontrollers. Thus, these timers are very versatile.
The typical structure of timers of traditional 8051s looks like the block diagram shown below but it is not completely the same for STC microcontrollers.
THx and TLx are cascaded counters that can be clocked using internal peripheral clock or externally via specific timer input GPIO pins. The clock that would drive the counters can be optionally scaled but unlike other microcontrollers the scaling is restricted to 1T or 12T, i.e., the driving source clock is either divided by one or divided by a factor of 12. When a timer is internally clocked, it is said to be acting like a timer and when it is clocked externally, it is said to be operating as a counter. All timers have interrupt capabilities and always count to top value from either 0 or some predefined value. Timers T0 and T1 are special timers while the rest are general purpose timers.
The timers can be operated in the following modes of operations:
These modes of operations have similarities with standard 8051s but there are also some minor differences.
- 16-bit Auto Reload Mode (Mode 0)
In auto-reload mode, a timer’s internal counter (TLx and THx) is loaded with some user-defined value at the beginning and the timer is started. It will count from that set value to the top value of 65535 (0xFFFF) and then overflow. After the overflow event, the user-defined value is automatically reloaded to the timer’s counter and the process repeats. The overflow event can generate an interrupt.
- 16-bit Non-auto Reload Mode (Mode 1)
Mode 1 is same as Mode 0 but the only difference between them is reloading feature. In Mode 1, a timer’s internal counter (TLx and THx) needs to be reloaded manually through coding.
- 8-bit Auto Reload Mode (Mode 2)
Mode 2 is similar to Mode 0. The differences are resolution and how the timer is reloaded. In this mode, TLx is used as counter and THx is used as the buffer that with reload TLx when overflow occurs.
- 16-bit Auto Reload Mode with Interrupt (Mode 3)
This mode is same as Mode 0 but in this mode, interrupt cannot be disabled. In Mode 0, we have the option to either use timer interrupt or have it disabled.
- Stop Mode
Stop mode is only valid for Timer 1. Actually, all timers can be stopped somehow but entering this mode will disable Timer 1.
Enhanced PWM Module
The enhanced PWM module of STC8A8K64S4A12 provides 8 channel 15-bit PWM outputs. This module allows lot of advanced PWM features like complementary PWMs with dead-times, PWMs with initial level settings, etc. There are options to use this module with internal ADC and there are options to detect faults. This module is best suited for power control applications like inverters, battery chargers, switch-mode power supplies (SMPS), etc. Lastly, this module can operate independently without the need of help from other internal timer modules.
Programmable Counter Array (PCA)
STC8A8K64S4A12’s programmable array counter is an interesting and rare hardware peripheral. PCA is not seen in most microcontrollers. PCA basically extends PWM capabilities of STC8A8K64S4A12 while including input capture peripherals, special outputs and robust interrupt capabilities. PCA-based PWMs are of either 6-bit, 7-bit, 8-bit and 10-bit resolution and can be considered as general-purpose PWMs. The PCA module can be considered as an additional built-in timer peripheral as it can operate independently just like the PWM module.
In STC8A8K64S4A12, there are four PCA groups – CCP0, CCP1, CCP2 and CCP3 that are dependent on a central 16-bit counter and configuration assets. CCP0, CCP1, CCP2 and CCP3 can be inputs or outputs depending on the mode of operation of PCA module itself. The counter can be clocked with a number of clock sources as shown in the block diagram.
Apart from all aforementioned peripherals, STC8A8K64S4A12 packs a wakeup timer and a watchdog timer. Wakeup timer can bring out a STC8A8K64S4A12 micro from low power sleep mode after fixed time intervals. Watchdog timer in STC8A8K64S4A12 is just like the watchdog timer of any other microcontroller and is mainly intended for resetting should there be an unforeseen software loop/error. These timers do not have alternative usage.