Master of Time Management in Single Chip Computers - Timer/Counter

Nov 08, 2023

Basic concepts of timing and counting

Ancient Chinese sages invented a vessel called the "drip glass" that measured time through flowing water. It drips water into the container through a fixed hole, with 3 drops per second, 180 drops per minute, and 10800 drops per minute, forming the earliest timer.

SIEMENS | 6FC5370-8AA20-0AA0 | Panel Processing Unit

Counting refers to the counting of the number of events or things, often accumulated in units of 1, such as counting the number of water droplets. When there is a temporal pattern in the occurrence of the event being counted, multiplying the result of the count by the time interval between the events becomes timing.

In the microcontroller, the corresponding "water droplet" is provided by the clock source. Assuming a clock source of 12MHz and a clock cycle of 1/12 μ s. You can program the microcontroller to add 1 at a speed of 1 clock cycle or at a speed of 12 clock cycles.

2. Timer/counter of STC15W4K32S4 microcontroller

The STC15W4K32S4 series microcontroller is internally equipped with five 16 bit timers/counters: T0, T1, T2, T3, and T1.

These 5 16 bit timers/counters all have two working modes: counting and timing. For timers/counters 10 and T1, use their corresponding control bits C/T in the special function register TMOD to select whether 10 or 1 is a timer or a counter.

For timer/counter T2, use the control bit (T2C) in the special function register AUXR/ ī) Choose whether T2 is a timer or a counter.

The core component of a timer/counter is an additive counting oath, which essentially counts pulses. The difference between counting and timing lies only in the source of the counting pulses: if the counting comes from the system clock, it is a timing method. At this time, the timer/counter receives counting pulses every 12 clocks or every 1 clock, and the counting value is added by 1; If the counting pulse comes from the external pin of the microcontroller (TO is P3.4, T1 is F5.5.12, F3.1, T3 is Po.5, T4 is P0.7), then it is in counting mode, and one pulse is processed every time it comes.

Overflow and overflow flags

When water droplets continue to drip in, due to the limited size of the container, they will overflow when filled, and the container needs to be emptied. For microcontrollers, it is to remind the microcontroller through the timer/counter overflow flag (in query mode)/interrupt request flag (in interrupt mode), notifying the microcontroller that the timed time has arrived or that the count has reached.

Setting the initial value of the timer/counter

The core of the timer/counter of the STC15W4K32S4 microcontroller is a 16 bit addition counter, which essentially counts pulses with a value range of 0-65535. Once the running process begins, it no longer requires the attention of the CPU and runs independently until the state of the corresponding overflow flag bit changes, which can be processed through software queries or triggered by interrupts to cause the CPU to execute interrupt service subroutines.

Therefore, in practical applications, users need to use a preset initial value to allow the timer/counter to overflow when reaching a reasonable time or count value.

Now assuming the clock source is 12MHz, there are the following requirements:

(1) Counting to 10800 overflow

The maximum count value of the timer/counter is 65 535, which means overflow will occur when the count reaches 65536. If overflow occurs when counting to 10800, the initial value of the counter should be set:

X=65 536-10 800=54 736=D5D0H

(2) Timing 10000 μ S overflow

The maximum timing value of the timer/counter is 65 536 times × one μ S/time=65 536 μ S. If a timing of 10 000 is required μ If overflow occurs after s, it is necessary to count the count pulses 10000 times, and set the initial value of the timer at this time:

X=65 536-10 000=55 536=D8F0H

The timer in a microcontroller usually requires constant repetition of timing, and after overflow, it needs to be reset again

The initial value is fed into the timer to ensure that the timing time for the next round is still 10000 μ S.

Special function register for timer/counter TO, T1

The working mode and control of the internal timer/counter TO and T1 of the STC15W4K32S4 series microcontroller are mainly managed by three special function registers: TCON, TMOD, and AUXR.

Using timer/counter T as method 0) to control the flow light

Due to the software query method used, it is necessary to reset the overflow flag with software every 1s after the overflow flag is triggered. The specific settings for the timer are as follows:


AUXR=0x00// Setting T1 as a timer to work in mode 0 can save time

//Set T1 to 12T mode, which can save time

TH1=(65536-50000)/256// Set the initial value of the timer, with a single timer of 50ms

TI1=(65536-50000) 256;

//Start T1


While (! TF1)// The software queries the status of TF1 and waits for the 50ms scheduled time to arrive

TF1=0// Clear overflow flag to prepare for loop timing

Repeatedly query the status of overflow flag TF1 through software programming. When the scheduled time arrives, TF1 will be set

Therefore, it is only necessary to query whether TF1 is equal to 1 to determine whether the timing time has arrived, so the program uses' while '

The loop statement "(! TF1)" is used to query and judge, and wait for the timed time of 50 ms.

Using timer/counter T1 interrupt mode (working mode 0) to control the flow light

Due to the timer working in interrupt mode, the interrupt flag TF1 will be triggered every 50 ms timed time. After the microcontroller responds to the interrupt, the internal hardware circuit will automatically reset TF1. Therefore, there is no need to use software to reset TF1 when programming. The specific settings for the timer are as follows: Image

TMOD=0x00// Setting T1 as a timer to work in mode 0 can save time

TH1=(65536-50000)/256// Set the initial timer value for 50ms

TL1=(65536-50000) number 256;

EA=1// Enable interrupt to allow total control bit

ET1=1// Enable T1 interrupt to allow control bit

TR1=1// Start T1

When T1 works in interrupt mode, when the timing time reaches 50 ms, it will automatically trigger the interrupt to execute the interrupt service subroutine.

  • Brand Name: :

  • Product Origin :

    Made in Germany
  • Model Number: :

  • Payment Terms: :

  • Minimum Order Quantity: :

  • Delivery Time: :

    spot goods
  • Net Weight (Kg) :

  • Package Size :

  • Packaging Quantity :

  • Shipping Port: :

  • Sales Manager:  Liao

    Mobile: +86 15259245292

    WeChat/WhatsApp: +86 15259245292

    1.10 years sales experience in control system components field.
    2.High efficiency application.
    4.Reduce the intermediate links between each other.
    5.Professional sales team and quality customer service team.
    6.Wide products range, nearly 70 million inventory parts for customers worldwide.

    We will supply:

    PLC : Allen Bradley ,ABB,GE FANUC,Schneider

    DCS : ABB,Siemens MOORE,Foxboro,Westinghouse,YOKOGAWA

    ESD : Triconex ,HIMA , Bently Nevada ,ICS

    Excitation cards : ABB,GE Marke

Need Help? Chat with us

leave a message
For any request of information or technical support, fill in the form. All fields marked with an asterisk* are required.
Looking for FAQs?
Contact us #
+86 -15259245292

Our hours

Mon 11/21 - Wed 11/23: 9 AM - 8 PM
Thu 11/24: closed - Happy Thanksgiving!
Fri 11/25: 8 AM - 10 PM
Sat 11/26 - Sun 11/27: 10 AM - 9 PM
(all hours are Eastern Time)




Contacts Us