Summary of 51 MCU timer counter interrupt

Publisher:楼高峰Latest update time:2015-12-25 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. TMOD: Timer Counter Mode Register

M1M0: There are four working modes for the timer/counter, which are controlled by M1M0. 2 bits are exactly four combinations.

1.     Working mode 1

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

Working mode 2
Before introducing this mode, let us 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 has to count to 65536 before it is full (if the initial value is 0, it will become 0001H after counting 1, and 0FFFFH after counting 65535. It will become 65536 after counting again. It overflows, and all bits are 0, and then an interrupt is requested. So it can count 65536, although it overflowed the last time.), what should we 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 it? Yes, but what should we do after the counter is full? You know, counting is always repeated. When the pipeline is full, the next count will start immediately. Will the next count still be 1000? When the counter is full and overflows, the value in the counter becomes 0 (why, you can refer to the explanation in the previous course), so the next time it will overflow after the counter reaches 65536, which does not meet the requirements. What should we do? Of course, the solution is very simple, that is, execute a program every time it overflows (this is usually necessary, otherwise why overflow?). In this program, you can do something to send the preset number 64536 into the counter. Therefore, using working mode 0 or 1, you must reset the preset number after overflow. Of course, it takes time to do this work. Generally speaking, this time is nothing, but there are some occasions where 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 reloaded, the preset number must be placed in a place, otherwise what should it be placed in? So where should the preset number be placed? It is placed in the upper 8 bits of T (0/1), so won't the upper 8 bits not be able to 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, so the counting range is much smaller. Of course, there is always a price to pay for doing anything, and 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 enter the lower 8 bits. This is done automatically by hardware and does not require manual intervention.

Usually this mode of operation is used for baud rate generators (we will explain it in the serial interface). When used for this purpose, the timer is to provide a time base. There is no need to do anything after the count overflows. There is only one thing to do, which is to reload the preset number and start counting again, and there should be no delay in the middle. It can be seen that this task is best completed by working mode 2.

Working mode 3
Under this mode of operation, timer/counter 0 is split into two independent timers/counters. Among them, TL0 can constitute an 8-bit timer or counter working mode, 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 an overflow mark. T0 is divided into two, so two sets of control and overflow marks are needed. 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.

In general, only when T1 runs in working mode 2 (when used as a baud rate generator), T0 is allowed to work in mode 3.

Timing/counting range of timer/counter

Working mode 0: 13-bit timing/counting mode, so it can count up to 2 to the 13th power, that is, 8192 times.

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

Working mode 2 and working mode 3 are both 8-bit timing/counting modes, so the maximum count can be 2 to the 8th power, or 256 times.

Preset value calculation: subtract the required count times from the maximum count amount.

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 at the same time. This is very common sense. Almost no textbooks will mention this, but many beginners will be confused by this.

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

1.     GATE = 0. Analyze the logic. GATE is 1 after negation. It 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 depends only on TR1. As long as TR1 is 1, the switch is closed and the counting pulse can pass through unimpeded. If TR1 is equal to 0, the switch is open and the counting pulse cannot pass through. 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 through. This feature can be used to measure the width of the high level of a signal. Think about it, how to measure it?


II. TCON: Timer counter control register

1.     Interrupt request source:

(1) External interrupt request source: that is, external interrupts 0 and 1, which are introduced through external pins. There are two pins on the microcontroller, named INT0 and INT1, that is, P3.2 and P3.3. There are four bits in the internal TCON that are related to external interrupts.

IT0: INT0 trigger mode control bit, which can be set and reset by software. IT0=0, INT0 is low-level trigger mode, IT0=1, INT0 is negative jump trigger mode. The difference between these two modes will be discussed later.

IE0: INT0 interrupt request flag. When there is an external interrupt request, this bit will be set to 1 (this is done by hardware). After the CPU responds to the interrupt, IE0 is cleared to 0 by hardware

. The purpose of IT1 and IE1 is the same as IT0 and IE0.

(2) Internal interrupt request source

TF0: Timer T0 overflow interrupt flag. When T0 counts overflow, TF0 is set by hardware. When the CPU responds to the interrupt, TF0 is cleared to 0 by hardware.

TF1: Similar to TF0.

TI, RI: Serial port send and receive interrupts

III. Interrupt enable register IE

In the MCS-51 interrupt system, the enable or disable of interrupts is controlled by the 8-bit interrupt enable register IE that can be bit-addressed on the chip. See the table below

EA

master switch
X
X
ES

serial port interrupt enable
ET1

timer 1 interrupt enable


EX1

External interrupt 1 interrupt enable
ET0

Timer 0 interrupt enable
EX0

External interrupt 0 interrupt enable


EA is the master switch. If it is equal to 0, all interrupts are not allowed.

ES-Serial port interrupt enable

ET1-Timer 1 interrupt enable

EX1-External interrupt 1 interrupt enable.

ET0-Timer 0 interrupt enable

EX0-External interrupt 0 interrupt enable

IV. Interrupt priority register IP

The interrupt priority is set high by the interrupt priority register IP. If a bit in IP is set to 1, the corresponding interrupt is high priority, otherwise it is low priority.



X
X
X
PS

Serial port
PT1

Timer 1
PX1

External interrupt 1
PT0

Timer 0
PX0

External interrupt 0


Request address of five interrupt sources

External interrupt 0: 0003H

Timer 0: 000BH

External interrupt 1: 0013H

Timer 1: 001BH

Serial port: 0023H

Reference address:Summary of 51 MCU timer counter interrupt

Previous article:Analysis of pull-up resistors for 51 MCU drive capability
Next article:Proteus and Keil Cx51 microcontroller simulation (flowing light)

Recommended ReadingLatest update time:2024-11-16 19:55

A preliminary study on 51 single chip microcomputer PWM subdivision control of stepper motor
Why do we need PWM subdivision? Because it can make the stepper motor run smoothly, reduce noise, increase speed (MAX), increase torque... Why do we emphasize that it is a 51 single-chip microcomputer? Because the 51 single-chip microcomputer does not have a hardware PWM module, it can only be simulated by software...
[Microcontroller]
A preliminary study on 51 single chip microcomputer PWM subdivision control of stepper motor
Microcontroller Learning Part 4: MCS-51 Microcontroller Pin Description
51 series microcontroller 89C51/89S51 both adopt 40-pin package dual-row direct DIP structure. In their 40-pin configuration, there are two positive power supply and ground lines, two clock lines of external quartz oscillator, 4 groups of 8 bits, a total of 32 I/O ports, and the P3 port line is multiplexed with the sec
[Microcontroller]
Microcontroller Learning Part 4: MCS-51 Microcontroller Pin Description
How to use 51 single-chip microcomputer return? How to use single-chip microcomputer return
Function writing 1: u8 is_timeout(tick start_time, tick interval) { return (get_diff_tick(get_tick(), start_time) interval); } Function writing 2: u8 is_timeout(tick start_time, tick interval) { u8 status; status = get_diff_tick(get_tick(), start_time) interval); return status; } Function calling
[Microcontroller]
[51 MCU] (Teach you step by step) Serial communication-Basics
way of communication parallel Suitable for short-distance communication, parallel communication control is simple and the transmission speed is relatively fast (8 bits are transmitted together). Serial It can only be transmitted one by one. Synchronize (Understand) The sender's clock directly controls the receiv
[Microcontroller]
[51 MCU] (Teach you step by step) Serial communication-Basics
Design of aircraft engine intelligent measurement and display system using AT89S51 microcontroller
introduction During the flight of an aircraft, the rotational speed is an important parameter that needs to be measured. By measuring the rotational speed of the jet engine, the power and thrust of the engine can be understood, and the motion load and energy load endured by the engine can be determined. When measuring
[Microcontroller]
Design of aircraft engine intelligent measurement and display system using AT89S51 microcontroller
51 single chip microcomputer realizes the dynamic scanning program of driving digital tube in the main function while loop
1. Use proteus to draw a simple circuit diagram for subsequent simulation 2. Programming /******************************************************************************************************************** ---- @Project: LED-74HC595 ---- @File: main.c ---- @Edit: ZHQ ---- @Version: V1.0 ---- @CreationTim
[Microcontroller]
51 single chip microcomputer realizes the dynamic scanning program of driving digital tube in the main function while loop
Microcontroller Basics (IV): C51 Extensions to C Language
C51's extension of C language C51 differs from standard C language in terms of data structure, I/O processing, functions, etc. data structure sfr, sfr16 Special function register type variables Special Function Register (SFR) plays an important role in microcontrollers. Common SFR is 8-bit, so sfr16 is used to r
[Microcontroller]
Microcontroller Basics (IV): C51 Extensions to C Language
Low power consumption design based on 51 single chip microcomputer
introduction In the design of control terminal system, when the system requires low overall power consumption, C8051F series microcontrollers are the best choice. They have flexible clock hardware, which enables the system to easily switch between high-efficiency operation mode and low-power mode. The intellige
[Microcontroller]
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号