Lesson 17: MCU timer/counter mode control word

Publisher:悠然自在Latest update time:2020-07-02 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

From the previous section, we have learned that the timer/counter in the microcontroller can have multiple uses, so how can I make them work for the purpose I need? This requires setting the timer/counter control word.


There are two special function registers related to timing/counting in the microcontroller, which are TMOD and TCON. By the way, TMOD and TCON are names. We can directly use these names to specify them when writing programs. Of course, we can also directly use their addresses 89H and 88H to specify them (in fact, using names means directly using addresses, and the assembly software will help you translate).


As we can see from Figure 1, TMOD is divided into two parts, each with 4 bits, which are used to control T1 and T0 respectively. As for what this means, we will introduce it below.


As we can see from Figure 2, TCON is also divided into two parts. The upper 4 bits are used for the timer/counter, and the lower 4 bits are used for interrupts (we will not discuss this for now). We mentioned TF1(0) in the last class. When the count overflows, TF1(0) changes from 0 to 1. So TF1(0) is here! So what are TR0 and TR1? Look at the picture in the last class.


It is not difficult to get the counting pulse into the counter. There are many levels to pass. At the very least, TR0 (1) must be 1, the switch can be closed, and the pulse can pass. Therefore, TR0 (1) is called the operation control bit. You can use the instruction SETB to set it to start the counter/timer operation, and use the instruction CLR to stop the timer/counter operation. Everything is under your control.


Four working modes of timer/counter

Working mode 0

The working mode 0 of the timer/counter is called the 13-bit timer/counter mode. It is composed of the lower 5 bits of TL (1/0) and the 8 bits of TH (0/1) to form a 13-bit counter. At this time, the upper 3 bits of TL (1/0) are not used.


We use this diagram to discuss several issues:

M1M0: The timer/counter has four working modes, which are controlled by M1M0. Two bits have exactly four combinations.

C/T: As we said before, the timer/counter can be used for timing or counting. It is up to us to decide what to use it for, or we, the programmer, can decide. If C/T is 0, it is used as a timer (the switch is turned up), and if C/T is 1, it is used as a counter (the switch is turned down). By the way, a timer/counter can be used for either timing or counting at the same time, but not both. This is common sense, and almost no textbooks will mention this, but many beginners will be confused by this.


GATE: Look at the picture. When we select the timing or counting working mode, the timing/counting pulse may not reach the counter end. There is a switch in the middle. Obviously, if the switch is not closed, the counting pulse cannot pass. So when does the switch pass? There are two situations.

GATE=0. Analyze the logic. GATE is 1 after being negated and enters the OR gate. The OR gate always outputs 1, which has nothing to do with the other input terminal INT1 of the OR gate. In this case, the opening and closing of the switch only depends on TR1. As long as TR1 is 1, the switch is closed and the counting pulse can pass smoothly. If TR1 is equal to 0, the switch is open and the counting pulse cannot pass. Therefore, whether the timing/counting works depends only on TR1.


GATE=1, in this case, the switch on the counting pulse path is not only controlled by TR1, but also by the INT1 pin. Only when TR1 is 1 and the INT1 pin is also high, the switch is closed and the counting pulse can pass. This feature can be used to measure the width of the high level of a signal. Think about it, how to measure it?


Why only use 13 bits in this mode? Why not use 16 bits? This is a working mode designed for compatibility with the 48 series, the predecessor of the 51 machine. If you feel uncomfortable using it, just use the second working mode.


Working method 1

Working mode 1 is a 16-bit timing/counting mode. Just set M1M0 to 01. Other characteristics are the same as working mode 0.


Working method 2

Before introducing this method, let's think about a question: In the last class, we mentioned the problem of arbitrary counting and arbitrary timing. For example, I want to count 1000 numbers, but the 16-bit counter will not be full until it reaches 65536. What should I do? After discussion, we came up with a solution to use a preset number. First, put 64536 in the counter, and then 1000 pulses. Isn't that enough? Yes, but what should we do after the counter is full? You know, counting is always repeated. After the count is full on the pipeline, the next count will start immediately. Will the next count still be 1000? When the count is full and overflows, the value in the counter becomes 0 (why, you can refer to the explanation of the previous course), so the next time it will overflow after the count is full of 65536, which does not meet the requirements. What should I do? Of course, the solution is very simple, that is, execute a program every time there is an overflow (this is often necessary, otherwise why overflow?) In this program, you can do the thing of sending the preset number 64536 into the counter. Therefore, whether using working mode 0 or 1, a preset number must be reset after the overflow. Of course, this work takes time. Generally speaking, this amount of time is nothing, but in some situations we still have to care about it, so there is a third working mode - the working mode of automatically reloading the preset number.


Since the preset number needs to be automatically loaded, the preset number must be placed in a place, otherwise what should be loaded? So where is the preset number placed? It is placed in the upper 8 bits of T (0/1), so the upper 8 bits cannot participate in the counting? Yes, in working mode 2, only the lower 8 bits participate in the counting, while the upper 8 bits do not participate in the counting and are used to store the preset number. In this way, the counting range is much smaller. Of course, there is always a price to pay for doing anything. The key is to see whether it is worth it. If I don’t need to count so many numbers at all, then I can use this method. Look at Figure 4. Whenever the count overflows, the switch between the upper and lower 8 bits of T (0/1) will be turned on, and the preset number will be counted into the lower 8 bits. This is done automatically by hardware and does not require manual intervention.


This mode of operation is often used in baud rate generators (we will explain this in the serial interface). When used for this purpose, the timer is used to provide a time base. There is no need to do anything after the count overflows. The only thing to do is to reload the preset number and start counting again without any delay in between. It can be seen that this task is best accomplished using mode 2.


Working method 3

In this operation mode, timer/counter 0 is split into two independent timers/counters. Among them, TL0 can form an 8-bit timer or counter, while TH0 can only be used as a timer. We know that when used as a timer or counter, it needs control, and overflow after full count requires overflow mark. T0 is divided into two, so there must be two sets of control and overflow marks. Where do they come from? TL0 still uses the original T0 mark, while TH0 borrows the mark of T1. In this way, T1 is unmarked and control is available, right? Yes.


Normally, T0 is allowed to work in mode 3 only when T1 is running in mode 2 (when the baud rate generator is used).


Timing/Counting Range of Timer/Counter

Working mode 0: 13-bit timing/counting mode, therefore, the maximum that can be counted is 2 to the 13th power, which is 8192 times.

Working mode 1: 16-bit timing/counting mode, therefore, it can count up to 2 to the 16th power, which is 65536 times.

Working mode 2 and working mode 3 are both 8-bit timing/counting modes. Therefore, the maximum value that can be counted is 2 to the power of 8, or 256 times.


Preset value calculation: subtract the required number of counts from the maximum number of counts.


For example, there are 12 boxes on the assembly line. It is required that an action be taken when 12 boxes are reached. If the single-chip microcomputer is used in working mode 0 to control it, what value should be preset? Yes, it is 8192-12=8180.


The above is counting. If you understand this principle, the same is true for timing. This has been mentioned in the previous course, so we will not repeat it here. Please refer to the previous routine.

Keywords:MCU Reference address:Lesson 17: MCU timer/counter mode control word

Previous article:Lesson 16: MCU Timer and Counter
Next article:Lesson 18: MCU interrupt system

Recommended ReadingLatest update time:2024-11-16 15:59

Design of ultrasonic voice ranging cane based on single chip microcomputer
In my country, hearing and speech disabilities rank first among the five major disabilities, including visual disabilities, physical disabilities, and mental disabilities, with 20.57 million people, accounting for 1.67% of the total population of China. In developed countries, most blind people are equipped with guide
[Microcontroller]
Design of ultrasonic voice ranging cane based on single chip microcomputer
PIC microcontroller realizes music playback
Source program: The following program needs to use two timer resources. Any PIC microcontroller with two timers can implement it. The MCU required for this example is MICROCHIP PIC16C62 INCLUDE "D:PICP16XX.EQU" ; This file can be found in the MICROCHIP CD ; ********************************************
[Microcontroller]
Design of stepper motor driver system based on AT89C2051 single chip microcomputer
AT89C2051 outputs the control pulse from P1.4 to P1.7 of P1 port, and enters 9014 after being inverted by 74LS14. After being amplified by 9014, it controls the photoelectric switch. After photoelectric isolation, the power tube TIP122 amplifies the pulse signal in voltage and current to drive each phase winding of th
[Microcontroller]
Design of stepper motor driver system based on AT89C2051 single chip microcomputer
Single chip microcomputer (51) neon lights
#include regx51.h #include intrins.h  void delay(unsigned int i){ while(i--);  }  void move_left(unsigned char p){ unsigned char i; P1=p; for(i=0;i 8;i++){ delay(20000); P1=_crol_(P1,1); }  }  void move_right(unsigned char p){ unsigned char i; P1=p; for(i=0;i 8;i++){ delay(20000); P1=_cror_(P
[Microcontroller]
Single chip microcomputer timer T0 counts seconds C51 program + circuit
Schematic diagram: The c51 microcontroller program is as follows: #include reg51.h #define UCHAR unsigned char #define UINT  unsigned int   UCHAR table = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; FLY counter; FLY timer;   void inittime(void) { timer
[Microcontroller]
Single chip microcomputer timer T0 counts seconds C51 program + circuit
Looking back on the three years of MCU learning. Please teach me how to learn ARM
I grew up in the countryside, and no one told me what college was when I was young. My Chinese teacher in elementary school never taught me in Mandarin. I never thought that I would enter an electrical technical secondary school to study "electronic technology", let alone that I would work in hardware development in th
[Microcontroller]
Are the MCS-51 series and 80C51 series microcontrollers the same?
MCS is a special series symbol for Intel's single-chip microcomputers. Intel has currently launched three series of single-chip microcomputers: MCS-48, MCS-51, and MCS-96. The MCS-51 series includes three basic models: 8031, 8051, and 8751, as well as corresponding low-power models: 80C31, 80C51, and 87C51. In the c
[Microcontroller]
51 MCU-New Key Program
1. Support continuous pressing writing Let's add some code to support double-clicking to deepen our understanding of the knowledge in the previous lecture. The experimental phenomenon is that if you hold down the key, the frequency of "key=1;" will appear more frequently than when the continuous pressing code is n
[Microcontroller]
51 MCU-New Key Program
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号