CPU Timings:
Oscillation period: The period of the oscillation source that provides the timing signal for the microcontroller (crystal oscillator period or external oscillation period)
State cycle: 2 oscillation cycles are 1 state cycle, represented by S. The oscillation cycle is also called the S cycle or clock cycle.
Machine cycle: 1 machine cycle contains 6 state cycles and 12 oscillation cycles.
Instruction cycle: The total time taken to complete one instruction, measured in machine cycles.
For example: When the external crystal oscillator is 12MHz, the specific values of the 4 time periods of the 89S52 microcontroller are:
Oscillation period = 1/12us;
Status cycle = 1/6us;
Machine cycle = 1us;
Instruction cycle = 1~4us;
Notice:
The timer/counter and the microcontroller's CPU are independent of each other. The timer/counter works automatically without the CPU's involvement.
The 51 single-chip microcomputer has two sets of timer/counters, which are called timer/counters because they can both time and count.
structure:
It consists of two registers, THx and TLx, with high 8 bits and low 8 bits.
TMOD is the working mode register of the timer/counter, which determines the working mode and function
TCON is a control register that controls the start and stop of T0 and T1 and sets the overflow flag.
control:
TMOD is used to set its working mode
TCON is used to control its startup and interrupt application.
GATE is the gate bit: it is used to control whether the start of the timer is affected by the external interrupt source signal.
When GATE=0: not affected by external factors; (Start: TR0 or TR1 in TCON is 1)
When GATA=1: affected by external factors; (start: TR0 or TR1 is 1, and the external interrupt pin INT0/1 is also high)
C/T: Timing/Counting mode selection bit. C/T = 0 is timing mode; C/T = 1 is counting mode.
M1M0: Working mode setting bit. The timer/counter has four working modes. (mainly 01)
TF1 (TCON.7): T1 overflow interrupt request flag. When T1 counts overflow, TF1 is automatically set to 1 by hardware. After the CPU responds to the interrupt, TF1 is automatically cleared to 0 by hardware. When T1 is working, the CPU can query the status of TF1 at any time. Therefore, TF1 can be used as a flag for query testing. TF1 can also be set to 1 or cleared to 0 by software, which has the same effect as setting 1 or clearing 0 by hardware.
TR1 (TCON.6): T1 operation control bit. When TR1 is set to 1, T1 starts to work; when TR1 is set to 0, T1 stops working. TR1 is set to 1 or cleared to 0 by software. Therefore, the start and stop of the timer/counter can be controlled by software. //Manual main operation TR
TF0 (TCON.5): T0 overflow interrupt request flag, its function is similar to TF1.
TR0 (TCON.4): T0 operation control bit, its function is similar to TR1.
Timer initial value calculation formula:
In timer mode: N = t/ Tcy
The formula for calculating the initial count value is: X=2^(number of timer bits)-N.
The initial value of the timer can also be obtained by directly complementing the number of counts.
In counting mode, the counting pulse is an external pulse on the T0 pin.
How to use the timer:
Assign values to TMOD to determine the working mode of T0 and T1.
Calculate the initial value and write it into TH0, TL0 or TH1, TL1.
Assign values to EA and enable timer interrupt.
Set TR0 or TR1 to start the timer/counter timing or counting.
#include #define uchar unsigned char #define uint unsigned int unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; uchar num,kk; void main() { TMOD=0x01; //Set timer 0 to working mode 1 TH0=(65536-50000)/256; //Set the initial value TL0=(65536-50000)%256; EA=1; //Turn on the general interrupt switch ET0=1; //Enable timer interrupt TR0=1; //Start the timer while(1) { if(kk==20) //The digital tube displays after the timer runs 20 times { kk=0; P1=smg_du[num]; num++; if(num==10) //loop digital tube num=0; } } } void timer0 () interrupt 1 { //After entering the function, only method 2 can automatically load //Manually load the initial value TH0=(65536-50000)/256; TL0=(65536-50000)%256; kk++; // kk adds one each time it comes in }
Previous article:[C51 Getting Started Notes] IIC bus + E2PROM chip (24C02)
Next article:【C51 Self-study Notes】Interrupt System
- 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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- LaunchPad Development Kit for SimpleLinktm MCUs
- Automatically generate Dockerfile to build Docker image
- [Help] What happens if PT100 is used at over-temperature?
- Design of frequency sweeper using digital frequency synthesis technology, FPGA and single chip microcomputer
- Bluetooth MESH technology makes up for the shortcomings of networking
- Some Problems with Differential Circuits
- Common base amplifier circuit problems
- Analog input
- [Sipeed LicheeRV 86 Panel Review] 10-Video Playback Test
- [Silicon Labs BG22-EK4108A Bluetooth Development Review] VII. WeChat applet controls LED and receives button status 2