Key points of this lesson: Register
Part I. Table of Contents
1.10.1. Timer Introduction
1.10.2. Key to software control hardware - registers
1.10.3.51 Introduction to MCU Timer
1.10.4. Introduction to the main registers of the timer
1.10.5. Timer Programming Practice 1
1.10.6. Correction of timing time setting error
Part II: Chapter Introduction
1.10.1. Timer Introduction
This section introduces the timer from a general perspective, mainly the working principle, function and significance of the timer.
1.10.2. Key to software control hardware - registers
This section introduces the concept of registers. By studying registers, you can understand the principle of using software to control hardware.
1.10.3.51 Introduction to MCU Timer
This section will guide you through reading the chapter on timers in the 51 MCU data sheet. By reading the data sheet, you will learn about timers and how to read the data sheet yourself.
1.10.4. Introduction to the main registers of the timer 1
This section begins to explain the timer-related registers of the 51 MCU. This section mainly focuses on the TF and TR bits in TCON and the GATE bit in TMOD.
1.10.5. Introduction to the main registers of the timer 2
This section begins to explain the timer-related registers of the 51 MCU. This section mainly focuses on the TF and TR bits in TCON and the GATE bit in TMOD.
1.10.6. Introduction to the main registers of the timer 3
This section explains the remaining register bits.
1.10.7. Timer Programming Practice 1
This section practices timer programming. Refer to the sample code in the data sheet to rewrite your own code and download it for testing and verification.
1.10.8. Timer Programming Practice 2
This section practices timer programming. Refer to the sample code in the data sheet to rewrite your own code and download it for testing and verification.
1.10.9. Correction of timing time setting error
This section provides additional explanation of the 51 MCU timer timing setting issue. This detail was explained incorrectly in the previous course.
Part III: Class Notes
1.10.1. Timer Introduction
1.10.1.1 What is a timer?
(1) An internal peripheral of a SoC (System on Chip, which can be understood as a single-chip microcomputer)
(2) The timer is the CPU’s “alarm clock”
The timer is to SoC what the alarm clock is to people. A single-core CPU is single-threaded and can only do one thing. After finishing this task, it needs a timer to remind it to do another task.
1.10.1.2 What is a counter?
Counters and timers are actually the same thing
(1) The counter is also an internal peripheral in the SoC
(2) As the name implies, a counter is used to count.
For example, a stopwatch (pointer watch) is actually a counter. It moves one grid (that is, counts a number) every unit. Because the counting time period of the stopwatch is fixed, when a certain time comes, we only need to use the count value * the counting time period to get a time period. This time period is the time we set (this is the timer).
(3) The counter can count the number of external pulses (the pulse is actually the level signal flowing into the microcontroller through the pin)
How long will a pulse last? By using a counter to get the number of pulses, you can know the time!
That is to say: counting internal pulses is called a timer; counting external pulses is called a counter.
The internal pulse is stable, and the time interval between each pulse is fixed, so it can be timed;
External pulses are very unstable. We cannot know the exact time between two pulses and can only count them!
(4) Is it a timer or a counter, and is it controlled by a register?
1.10.1.3 How does the timer work?
(1) Step 1: Set the timer clock source (if necessary)
Clock source: Quartz crystal oscillator (crystal oscillator)
(2) Step 2: Initialize clock-related registers
(3) Step 3: Set the timing time (number of counted pulses + time of each pulse)
(4) Step 4: Set up the interrupt handler (external input has higher priority than timing)
For example: the main task is to watch TV, and the timer interrupt program is to boil water!
(5) Step 5: Turn on the timer
(6) During operation: When the timer counts up, an interrupt is generated, and then the interrupt isr is executed
You will understand it better after looking at the code!
1.10.2. Key to software control hardware - registers
How are some hardware controlled by software? It depends on registers!
1.10.2.1 What is a register?
(1)register [reg]
(2) Register
Registered, variable content, generally defined bit by bit
It is equivalent to a variable, except that this variable is at a fixed address and has a special name.
Registers are divided into general registers (P0...) and special function registers (IE...)
(3) Registers are accessed using addresses and are programmed like memory
As defined in reg51.h!
1.10.2.2 Working principle of registers
(1) There is a bidirectional influence between registers and hardware
To put it simply, registers also have IO, and are related to hardware being input/output devices!
(2) Software can read and write registers
The program can write values into registers and read the values of registers. For example, when we read the value of a GPIO pin, we are actually reading the register inside the pin.
(3) Summary: Registers are the key to software controlling hardware
1.10.2.3、The key to learning MCU is various registers
Registers are to microcontrollers like words are to English learning!
(1) The study of MCU mainly includes two parts: CPU and various internal peripherals
(2) The programming interface of various internal peripherals is the register
(3) Being familiar with a microcontroller actually means being familiar with its registers
(4) Registers become more complex as the MCU becomes more complex (although there are differences between STM32 and MCU, when you learn, you will find that they are very simple!)
(5) Learn the skills of operating registers using C language - key points
You need to learn the skills of using C language to operate the microcontroller!
1.10.3.51 Introduction to MCU Timer
Refer to data sheet P136 Timer/Counter
If the microcontroller works in 12T mode!
For an external 12MHz crystal oscillator, the internal clock frequency is 1MHz, and the clock pulse width is 1us (1/1MHz = 1us).
If the microcontroller works in 6T mode
For an external 12MHz crystal oscillator, the internal clock frequency is 2MHz, and the clock pulse width is 0.5us (1/2MHz = 0.5us).
The modes and working methods of 51 are complicated, so we don’t need to remember them. Just go through them once. When we learn embedded STM32, it will be much easier!
In the next few sections, we will focus on the main registers of the timer through the manual.
1.10.4. Introduction to the main registers of the timer
MSB big endian, LSB little endian P136
1.10.4.1、TCON(Timer Control) Timer/Counter Control Register
0b11000011 is from B7-B0
(1) 8 bits, but with 4 names: TF, TR, IE, IT. Each name has 2 symbols, followed by 0 and 1, corresponding to T0 and T1 respectively.
(2)TF: timer flag, timer (overflow) flag
It is read-only (the software only knows the status of the hardware by reading TF1, and does not need to write this bit to set the status of the hardware).
When the timer reaches the set time, it will do two things: the first is to change the TF flag to 1, and the second is to generate an interrupt for the CPU to handle the interrupt; when the CPU responds, TF is cleared by hardware (the change from 1 to 0 is automatic, and no software intervention is required.)
Some CPU designs need to be cleared by software (mainstream microcontrollers). At this time, the user's program must remember to clear the flag bit, otherwise it will repeatedly enter the interrupt.
=1 overflow, =0 overflow clear
(3)TR: timer run, which is the switch that starts the timer counting.
After we initialize the entire timer, we can start counting by writing 1 to the TR bit.
TR bit and GATE bit have some correlation. GATE is the register bit in TMOD register, let's take a look first!
(4)IE: interrupt enable, external interrupt (INTx) request source flag
Its function is to show the status change of hardware.
When INT1 interrupt occurs, the hardware automatically sets IE1=1
When the CPU processes the INT1 interrupt, the hardware will automatically set IE1=0 (automatically cleared by the hardware).
(5)IT: interrput trigger, external interrupt trigger mode control bit
It is used to set the interrupt trigger mode of external interrupt.
The so-called interrupt triggering method means that the hardware will be judged to generate an interrupt only under certain conditions, so it is actually the condition for the interrupt to occur.
There are generally two types of interrupt triggering: edge triggering and level triggering. Edge triggering is divided into: rising edge triggering, falling edge triggering, and double edge triggering; level triggering is divided into: high level triggering and low level triggering.
=0 External interrupt 1 is low level trigger mode, when INTx input is low level, IE=1
=1 When the external interrupt (INTx) port changes from "1" to "0" on a falling edge, IE=1
1.10.4.2、TMOD(Timer Mode) Timer/Counter Operation Mode Register
(1) GATE: Chinese name is gate control bit
It is in the TMOD register, and there are also 2 corresponding to T0 and T1 respectively.
Way of working:
When GATE=0, the timer is in timer working mode. At this time, the timer switch is only affected by the TR bit. Specifically, TR=1 starts counting, and TR=0 stops counting.
When GATE=1, the timer is in counter mode. When the timer is used to count, the key is under what conditions it counts and under what conditions it does not count. So when GATE=1, whether it counts depends not only on TR1 but also on the INT1 pin (P3.3). The actual rule is: it will count when TR1=1 and the INT1 pin is also high.
(2) C/T position
Set T0/T1 to work in timer mode or counter mode.
=1 means counter mode
=0 indicates timer mode.
(3) M1 + M0
The two bits together indicate which working mode T0/T1 is in. There are generally four types: 13-bit, 16-bit, 8-bit auto-reload, and double 8-bit.
Previous article:Application of STC15F2KA60S2 chip dust sensor
Next article:[STC MCU Learning] Lesson 17: LCD1602 Display
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University ---- Power Semiconductor Devices
- Check the internal working status and problems of the power supply
- DSP AD current sampling data fitting curve
- microPython + esp8266 + tm1650 = Network clock
- FPGA introductory series experimental tutorial - key debounce control LED on and off.pdf
- ADC12 module of MSP430F149
- New Horizons to Pluto: Qorvo Helps Send Images from Pluto to Earth
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board]——RTC routine
- Summary of the trial of creative application development of RVB2601
- After the ADC acquisition pin of LPC1769 is disconnected and then connected again, the microcontroller needs to be restarted to continue acquisition. What is the problem?