Detailed explanation of 80C51 microcontroller timer/counter (including working mode)

Publisher:心灵律动Latest update time:2023-06-19 Source: elecfansKeywords:80C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

To realize the timing function of the 80C51 microcontroller, a more convenient way is to use the timer/counter inside the microcontroller. Below we will explain the 80C51 microcontroller timer/counter in detail and analyze the working mode of the timer/counter.

Detailed explanation of 80C51 microcontroller timer/counter
Detailed explanation of 80C51 microcontroller timer/counter

Detailed explanation of 80C51 microcontroller timer/counter


  -------, invalid bit.

  PS---Serial port interrupt priority control bit.

  PS=1, the serial port interrupt is defined as a high priority interrupt.

  PS=0, the serial port interrupt is defined as a low priority interrupt.

  PT1---Timer/Counter 1 interrupt priority control bit.

  PT1=1, the timer/counter 1 interrupt is defined as a high priority interrupt.

  PT1=0, the timer/counter 1 interrupt is defined as a low priority interrupt.

  PX1---External interrupt 1 interrupt priority control bit.

  PX1=1, the external interrupt 1 interrupt is defined as a high-priority interrupt.

  PX1=0, external interrupt 1 interrupt is defined as a low priority interrupt.

  PT0---Timer/Counter 0 interrupt priority control bit.

  PT0=1, the timer/counter 0 interrupt is defined as a high priority interrupt.

  PT0=0, the timer/counter 0 interrupt is defined as a low priority interrupt.

  PX0---External interrupt 0 interrupt priority control bit.

  PX0=1, external interrupt 0 interrupt is defined as a high priority interrupt.

  PX0=0, external interrupt 0 interrupt is defined as a low priority interrupt.

  Timer/counter operating mode register TMOD

Detailed explanation of 80C51 microcontroller timer/counter

  TF1---Timer 1 overflow flag.

  When timer 1 is full and overflows, TF1 is set to 1 by hardware and an interrupt is requested. After entering the interrupt service routine, it is automatically cleared to 0 by the hardware. It should be noted that if a timer interrupt is used, this bit does not need to be manually operated at all. However, if a software query method is used, when the bit is found to be 1, software needs to be cleared to 0.

  TR1---Timer 1 operation control bit.

  Cleared by software to turn off Timer 1. When GATE=1 and INIT is high level, TR1 is set to 1 to start timer 1; when GATE=0, TR1 is set to 1 to start timer 1.

  TF0---Timer 0 overflow flag, its function and operation method are the same as TF1.

  TR0---Timer 0 operation control bit, its function and operation method are the same as TR1.

  IE1---External interrupt 1 request flag.

  When IT1=0, bit level trigger mode, S5P2 samples the INT1 pin in each machine cycle. If the NIT1 pin is a constant level, it is set to 1, otherwise IE1 is cleared to 0.

  When IT1=1, INT1 is an edge trigger mode. When INIT1 is low level during the first machine cycle sampling, IE1 is set to 1. IE1=1 means that external interrupt 1 is requesting an interrupt from the CPU. When the CPU responds to an interrupt and switches to the interrupt service routine, this bit is cleared by hardware.

  IT1 external interrupt 1 trigger mode selection bit.

  IT1=0, which is the level trigger mode, and the low level on the pin INT1 is active.

  IT1=1, which is the edge trigger mode. The negative transition of the level on pin INT1 from high to low is effective.

  IE0---External interrupt 0 request flag, its function and operation method are the same as IE1.

  IT0---external interrupt 0 trigger mode selection bit, its function and operation method are the same as IT1.

  From the above knowledge points, we can know that each timer has 4 working modes, and the working mode can be selected by setting the M1M0 bit in the TMOD register.

  The counting number of mode 1 is 16 bits. For T0, the TL0 register serves as the low 8 bits and the TH0 register serves as the high 8 bits, forming a 16-bit plus 1 counter.

  Calculation of initial value of timer T0

  Once the timer is started, it starts counting by 1 on the original value. If we do not set TH0 and TL0 at the beginning of the program, their default values ​​are both 0. Assume that the clock frequency is 12MHz and 12 clock cycles are One machine cycle, then the machine cycle at this time is 1us. It takes 216 -1 numbers to fully record TH0 and TL0. Then a pulse counter overflows, and then an interrupt is applied to the CPU. Therefore, it takes a total of 65536us to overflow once, which is approximately equal to 65.6ms. If we want to time 50ms, then we need to install an initial value for TH0 and TL0 first. After recording 50,000 numbers based on this initial value, the timer overflows. It happens to be an interrupt every 50ms. When we need to time it for 1s, when we write the program, after 20 50ms timer interrupts are generated, it is considered to be 1s, so that we can accurately control the timing time. When counting 50,000, the total number that should be loaded into TH0 and TL0 is 65536-50000=15536. Find the modulo of 15536 ​​to 256: 15536/256=60 and load it into TH0. Find the remainder of 15536 ​​to 256: 15536/ 256=176 is loaded into TL0.

Detailed explanation of 80C51 microcontroller timer/counter (including working mode)


  The program code is as follows:

  #include《reg52.h》

  #define uchar unsigned char

  #define uint unsigned int

  sbit led1=P1^0;

  uchar num;

  void main()

  {

  TMOD=0x01; //Set timer 0 bit working mode 1 (M1, M0 bit 0, 1)

  TH0=(65536-45872)/256; //The initial value of 11.0592M crystal oscillator timing 50ms is 45872

  TL0=(65536-45872)%256;

  EA=1; //Enable total interrupt

  ET0=1; //Enable timer 0 interrupt

  TR0=1; //Start timer 0

  while(1)

  {

  if (num==20) //If it reaches 20 times, indicate 1 second

  {

  led1=~led1; //Invert the status of the light-emitting tube

  num=0;

  }

  }

  }

  void T0_TIme()interrupt 1

  {

  TH0=(65536-45872)/256; //Reload the initial value

  TL0=(65536-45872)%256;

  num++;

  }

80C51 microcontroller timer working mode

Detailed explanation of 80C51 microcontroller timer/counter (including working mode)

Detailed explanation of 80C51 microcontroller timer/counter (including working mode)

Detailed explanation of 80C51 microcontroller timer/counter (including working mode)

Detailed explanation of 80C51 microcontroller timer/counter (including working mode)

Detailed explanation of 80C51 microcontroller timer/counter (including working mode)

 Timer/counter for external interrupt expansion

  The expansion method is to set the timer/counter to counter mode, set the initial counting value to full range, and connect the external interrupt source to be expanded to the external counting pin of the timer/counter. A falling edge signal is input from this pin, and a timer/counter overflow interrupt is generated after the counter is incremented by 1.

  For example, use T0 to expand an external interrupt source. Set T0 to counter mode and work in mode 2. The initial values ​​of TH0 and TL0 are both 0FFH. T0 allows interrupts and the CPU enables interrupts. Its initialization procedure is as follows:

  MOV TMOD, #06H; Set T0 to counter mode 2

  MOV TL0, #0FFH; Set initial counting value

  MOV TH0, #0FFH

  SETB TR0; start T0 work

  SETB EA; CPU turns on interrupt

  SETB ET0; enable T0 interrupt


Keywords:80C51 Reference address:Detailed explanation of 80C51 microcontroller timer/counter (including working mode)

Previous article:Design of temperature/humidity monitoring system based on SHTl5 smart sensor
Next article:Design of automotive MP3 wireless transmitter based on single chip AT89C52 and MC145152 chips

Recommended ReadingLatest update time:2024-11-23 07:36

C51中断(void timer1(void) interrupt 3 using 3)
interrupt indicates the interrupt priority, and using indicates the working register group used. interrupt x using y        The xx value after interrupt is the interrupt number, that is, which interrupt port this function corresponds to. Generally in 51,        0 is external interrupt 0          1 is timer 0        2
[Microcontroller]
80C51 MCU Program (3)
80C51 MCU program - light up the digital tube (one digit 0-9 is displayed in a cycle) #include reg52.h         //Include reg52.h header file #define uint unsigned int      //Macro definition of uint data type is unsigned int type  #define uchar unsigned char      //Macro definition of uchar data type is unsigned ch
[Microcontroller]
80C51 MCU instruction system (II) addressing mode
4.2.1 Common symbols used in the instruction system: Rn: register in the current register workspace, where n=0--7; direct: internal data memory address, can specify an internal RAM unit (0-- 127) or a dedicated register (128-- 255); @Ri: Indirect addressing through R1 or R0, specify a unit in the data memory RAM (
[Microcontroller]
80C51 MCU instruction system (II) addressing mode
80C51 serial communication
1. Serial port structure 2. Serial port control register The difference is that the timer interrupt control register is TMOD and the serial port is SCON.   fosc is the crystal frequency, generally used mode 1----- SM0SM1 = 01; start bit + 8 bits of data + stop bit = 10 bits of data We usually set SM2 to 0; RE
[Microcontroller]
80C51 serial communication
USB printer driver design for 80C51 and CH375
Introduction:   This topic comes from a project of Beijing Puji General Company. Since the company’s existing stand-alone spectroscopic instrument products (such as 1810, T6, etc.) use parallel port printing technology, and with the gradual popularization of USB printer technology, parallel port printers are becoming
[Microcontroller]
Introduction to 80C51 Microcontroller
The 80C51 microcontroller belongs to the MCS-51 series of microcontrollers, developed by Intel. Its structure is an extension of the 8048 and improves on its shortcomings. Instructions such as multiplication (MUL), division (DIV), subtraction (SUBB), comparison (CMP), 16-bit data pointers, Boolean algebra opera
[Microcontroller]
Latest Microcontroller Articles
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号