51 MCU timer/counter
Timing (timing control, measurement, delay, etc.) and counting (counting the number of external events).
Software Timing | Digital Circuit Timing | Programmable Timer/Counter
How are they achieved?
Uh! Guess
. . .
Software Timing
The machine executes a program that has no other use but to delay time! In order to achieve the purpose of timing.
Digital Circuit Hardware Timing
Devices such as 555 complete the timing
Programmable timer/counter
Hardware timing, but it is programmable and can be initialized by software to set the timing requirements.
1. Structure
The timer/counter is essentially an add-one counter that can work in two modes. In fact, both are counting pulses, but the source of the pulses is different!
1. Timing
1. Add one to the count.
2. The pulse comes from the oscillator's 12-divided pulse (Fosc/12), which simply means counting the machine cycles.
3. The initial timing value (TH, TL) needs to be preset.
4. The initial value setting needs to be a complement code. If the count is increased by one (the complement code is added, the original value is subtracted) and the overflow is added (the original value is reduced to 0), an overflow interrupt occurs.
2. Count
1. Add one to the count.
2. The initial counting value (TH, TL) needs to be preset.
2. The pulse comes from the T0 or T1 pin.
2. When the control switch in the figure above is closed (control = 1), the counter starts counting the falling edge generated by the external pulse.
3. When the counter is full, the initial value becomes 0 and an overflow interrupt is generated.
4. The maximum frequency of the counting pulse cannot exceed Fosc/24
2. Timing counter related registers
We need to consider several aspects of interruption
Is interrupt turned on?
How to trigger an interrupt
Interrupted start and stop
How to respond after multiple interrupts are triggered
(1) TMOD timing counting mode register (address 89h)
GATE:
1. Gate signal, GATE=0, TRx=1 self-start mode
2. GATE=1, TRx=1, INTx=1 to start the timer/counter (a method that uses INTx as an external start condition)
C/-T:
Timing, counting selection—1 timing 0 counting
M1M0:
Four working modes of the timing counter
(2) TCON interrupt request and external interrupt selection (address 88h)
3. Timing counter working mode
Method 0:
When timer/counter T0 operates in mode 0, only 13 bits of the 16-bit counter are used, namely the upper 8 bits of TH0 and the lower 5 bits of TL0, forming a 13-bit timer/counter (maximum count value 8192).
Method 1:
Timer T0 working mode 1 is similar to working mode 0, the difference lies in the number of bits of the counter. Working mode 0 uses a 13-bit counter to participate in counting, while working mode 1 uses a 16-bit counter to participate in counting (maximum count value 65536).
Working mode 1 is a 16-bit counter. This is the only difference between working mode 1 and working mode 0 in the counting method.
Method 2:
The working mode of automatically reloading the preset number. At this time, M1M0=10.
Automatically reload THx to store the initial value (preset number), TLx is used for counting, and the value of THx is automatically assigned to TLx after counting is completed. (Except for reloading the initial value and the maximum count value, the rest is the same as mode 0)
Method 3: (only applicable to T0)
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 flags are required when the count is full. T0 is divided into two, so two sets of control and overflow flags are needed. Where do they come from? TL0 still uses the original T0 flag, while TH0 borrows the T1 flag.
Normally, T0 is allowed to operate in mode 3 only when T1 is operating in mode 2 (when used as a baud rate generator).
3. Timing counter count initial value and initialization
Calculation of the initial value of the count:
Counting method: initial value C = modulus - X (number of pulses to be counted)
Timing method: initial value C = [t /MC] complement = modulus - [t /MC] (t timing time, MC machine cycle, MC = 12/fosc)
Count initial value example:
Mode 0: Initial value C = (64H) complement = 2000H - 64H = 1F9CH
Method 1: Initial value C = (64H) complement = 10000H - 64H = FF9CH
Method 2: Initial value C = (64H) complement = 100H - 64H = 9CH
initialization:
Calculate initial value
TMOD Settings
Write the initial count value THx, TLx
Start the timer, TRx = 1
EA = 1 (turn on the interrupt master switch)
ETx = 1 Enable interrupt
Writing an interrupt subroutine
#include unsigned char i=0; int0() interrupt 1 //Timer 0 interrupt function { i++; //Calculate the number of interrupts } main()//Main function { //Calculate initial value TMOD = 0x01; //TMOD setting TH0 = 0x3c; TL0 = 0xb0; //initial value TR0 = 1 //Start the timer, TRx = 1 EA=1; //Interrupt main switch ET0=1//Timer 0 enables interrupt } ORG 0000H AJMP MAIN ORG 000bH ;Interrupt subroutine entry AJMP COUNTING ;Interrupt function name ORG 0030H MAIN: MOV TMOD,#01H ;TMOD settings MOV TH0 ,#3cH; TL0 = #0b0H; ; Initial value SETB TR0 ; Start timer, TRx = 1 SETB EA ;Interrupt main switch SETB ET0 ;Timer 0 enables interrupt HERE:AJMP HERE COUNTING: INC A ;a++ RETI END
Previous article:C51 MCU interrupt experiment
Next article:C51 MCU————Interrupt system
Recommended posts
- What do the lines outside of pads in KiCAD mean?
- ThereisaredcircleoutsidethepadinKiCAD,whichlooksuncomfortable.Whatisitfor?Canitberemoved? Thislinecanbehidden. Theredcircleindicatesthattheareawillnotbecoveredbythesoldermask. Inthe3Dimage,
- littleshrimp PCB Design
- CC2640R2F: A must-read for beginners
- Letmeexplainafewbasicpointsfirst: 1.Theideaoflowpowerconsumption.Manypeopledon'tunderstandthisconcept.Letmeexplainthisfirst.Ifyouunderstandthis,manythingsaboutBLEwillbemuchclearer. Lowpowerconsumptionforpeop
- Jacktang Wireless Connectivity
- Playing with Zynq Serial 14 - Using GIT for Project Backup and Version Management 6
- 2.6ApplicationofGITbranches IntheShowlog,wecanseethatthelatestversionofthecurrentmasterisver2.Ifwewanttogobacktover1tochangethefile,wecannotreset"master"tothis...rollbacktover1undermaster
- ove学习使我快乐 FPGA/CPLD
- Understanding the CAN bus from the bottom up
- 1.Introduction CANbuswasdevelopedbyBOSCHofGermany,withamaximumspeedof1Mbps.CANhasastrongfaulttolerance,andtheCANcontrollerhasabuilt-inpowerfulerrordetectionandprocessingmechanism. Inaddition,unliketraditio
- Jacktang Embedded System
- What are the main contents of intelligent sensing based on LoRa and WiFi communication technology?
- ThemaincontentofsmartsensingbasedonLoRaandWiFicommunicationtechnologyistorealizeinformationexchangebetweensmartdevicesandthecloud.LoRaisalow-power,long-distancewirelesscommunicationtechnologysuitableforIoTapplicati
- 成都亿佰特 RF/Wirelessly
- [Homemade DA14531 bracelet (similar to Xiaomi bracelet 4/5) development kit] Supporting tutorial 1: Development environment + Getting started..
- Otherlinksforthisseriesoftutorialsareasfollows: Supportingtutorial3:ProjectgitdescriptionandmyBSPhttps://en.eeworld.com/bbs/thread-1270515-1-1.html Pastethecontentdirectoryofthefollowingfloor: 1-0.
- 辛昕 DIY/Open Source Hardware
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- HPM6700 Series Hardware Design Guide (Part 1)
- Share a product specification about non-contact liquid level sensor
- My kid's toy broke, so I revived it by blowing on it
- [Evaluation and experience of Zhongke Yihaiwei EQ6HL45 development platform] +05. Expansion interface-LCD test (zmj)
- ST official MEMS sensor group technical discussion is also included in its original form (updated date 20221110)
- Problems with using immediate values in functions
- N32G45x series MCU development and application records using JLINK RTT
- Award-winning live broadcast|Webinar-High-speed interface testing: PCIe5.0 test solution
- Which textbook should I use when learning C language?
- HPM6000 Series ADC Related Hardware Design Tutorial