How to set the working mode of AT89C52 microcontroller timer

Publisher:JoyfulHarmonyLatest update time:2023-04-03 Source: elecfansKeywords:AT89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  AT89C52 is a low-voltage, high-performance CMOS 8-bit microcontroller. It contains 8k bytes of rewritable Flash read-only program memory and 256 bytes of random access data memory (RAM). The device adopts ATMEL's high-density, Produced with non-volatile storage technology, compatible with the standard MCS-51 instruction system, with a built-in general-purpose 8-bit central processor and Flash memory unit, the AT89C52 microcontroller is widely used in the electronics industry.


  AT89C52 microcontroller hardware structure

  As shown in Figure 3-1, it is the hardware structure diagram of AT89C52. The internal structure of the AT89C52 microcontroller is basically the same as that of the MCS-51 series microcontroller. The CPU is composed of arithmetic units and controllers. The arithmetic unit is mainly used to perform arithmetic, logical operations and bit operations on the operands. The controller is the command and control component of the microcontroller. It identifies instructions for its main tasks and controls the functional components of the microcontroller according to the nature of the instructions, thereby ensuring that all parts of the microcontroller can work automatically and harmoniously. Its program memory is 8K bytes of rewritable Flash memory. The flash memory allows online +5V electrical erasure, electrical writing, or repeated programming using a programmer. The data memory is much larger than the 51 series microcontroller, with 256 bytes of RAM. The instruction system and pin functions of the AT89C52 microcontroller are fully compatible with those of the MCS-51.


  Main performance parameters

  • 8K bytes of rewritable Flash memory

  • 1000 erasable and writable cycles

  • Fully static operation: 0Hz-24MHz

  • Level 3 encrypted program memory

  • 256×8 bytes internal RAM

  • 32 programmable I/O lines

  • 3 16-bit timer/counters

  • 8 interrupt sources

  • Programmable serial UART channel

  • Low-power idle and power-down modes

  AT89C52 external pin diagram

How to set the working mode of AT89C52 microcontroller timer

  AT89C52 pin description

  VCC: power supply GND: ground

  Port P0: Port P0 is an 8-bit open-drain bidirectional I/O port. As an output port, each bit can drive 8 TTL logic levels. When "1" is written to the P0 port, the pin acts as a high-impedance input. When accessing external program and data memory, Port 0 is also used as the lower 8-bit address/data multiplexing. In this mode, P0 has an internal pull-up resistor.

  During flash programming, the P0 port is also used to receive instruction bytes: during program verification, the instruction bytes are output. When the program is validated, an external pull-up resistor is required.

  Port P1: Port P1 is an 8-bit bidirectional I/O port with an internal pull-up resistor. The output buffer stage of P1 can drive (sink or output current) 4 TTL logic levels. When "1" is written to P1, the current ILL will be output due to the internal pull-up resistor.

  AT89C52 crystal oscillator characteristics

  The AT89C52 microcontroller has an inverting amplifier used to form an internal oscillator. XTAL1 and XTAL2 are the input and output terminals of the amplifier respectively. Both quartz crystals and ceramic resonators can be used together to form a self-excited oscillator. If the device is driven from an external clock, XTAL2 can be left unconnected and connected through XTAL1. Since the external clock signal is input to the external clock circuit after being triggered by dividing by two, there are no other requirements for the duty cycle of the external clock signal. The maximum low-level duration and the minimum high-level duration must still meet the requirements. .

  Quartz crystal oscillator C1, C2=30PF+-10PF Ceramic resonator C1, C2=40PF+-10PF

  AT89C52 microcontroller minimum system interface circuit design

How to set the working mode of AT89C52 microcontroller timer

  AT89C52, clock circuit (including crystal oscillator, capacitor C19, C20), and power-on reset circuit (including R42, C5, S3, VD1, C3, R9) constitute the minimum system of the microcontroller. Among them, the crystal oscillator uses a 12MHz highly stable passive crystal oscillator, which forms an oscillator with the reverse amplifier in the AT89C52 to provide a highly stable clock signal to the CPU. Capacitors C19 and C20 can play the role of frequency fine-tuning. The capacitance value can be selected between 5pF and 30pF. For this circuit, 20pF is selected. Capacitor C5 and resistor R42 constitute a power-on reset circuit. When the power is turned on, the power charges the capacitor C5 and generates a high pulse at the reset terminal of the CPU. As long as the high level is maintained for longer than two machine cycles (24 oscillation cycles). The CPU can be reset. The function of the diode VD1 is to quickly release the charge stored in the capacitor C5 when the power is turned off, so that it can be reliably reset the next time the power is turned on. Capacitor C5 can filter out high-frequency interference and prevent the microcontroller from erroneously resetting. Button S3 and resistor R9 constitute a button reset circuit.

  AT89C52 timer 2 working mode:

  Timer 2 is a 16-bit timer/counter. It can be used as a timer or an external event counter, and its working mode is selected by the C/T2 bit of the special function register T2CON (see Table 3). Timer 2 has three working modes: capture mode, automatic reload (up or down counting) mode and baud rate generator mode. The working mode is selected by the control bit of T2CON. Timer 2 consists of two 8-bit registers TH2 and TL2. In the timer working mode, the value of the TL2 register is incremented by 1 for each machine cycle. Since one machine cycle consists of 12 oscillation clocks, the counting rate is the oscillation frequency. 1/12. In the counting mode, when the external input signal on the T2 pin generates a falling edge from 1 to 0, the value of the register is incremented by 1. In this mode of operation, the external input is sampled during 5SP2 of each machine cycle. . If the value taken in the first machine cycle is 1 and the value taken in the next machine cycle is 0, then the register is incremented by 1 during S3P1 of the following cycle. Since it takes 2 machine cycles (24 oscillator periods) to recognize a 1 to 0 transition, the maximum counting rate is 1/24 of the oscillator frequency. In order to ensure the accuracy of sampling, the input level is required to be maintained for at least a complete cycle before changing to ensure that the input signal is sampled at least once. 


  Capture method:

  In capture mode, two modes are selected through the T2CON control bit EXEN2. If EXEN2=0, Timer 2 is a 16-bit timer or counter. When the count overflows, the overflow flag TF2 of T2CON is set and the interrupt is activated at the same time. If EXEN2=1, timer 2 completes the same operation, and when the T2EX pin external input signal undergoes a negative transition from 1 to 0, the values ​​in TH2 and TL2 are captured into RCAP2H and RCAP2L respectively. In addition, the transition of the T2EX pin signal causes EXF2 in T2CON to be set. Similar to TF2, EXF2 will also activate the interrupt. The capture method is shown in Figure 4. ·Auto-reload (up or down counter) mode:

  When Timer 2 works in 16-bit auto-reload mode, it can be programmed to count up or down. This function can be programmed through the special function register T2CON.

  Selected by the DCEN bit (allowing down counting) (see Table 5). On reset, the DCEN bit is "0" and Timer 2 is set to count up by default. When DCEN is set, Timer 2 can count up or down, depending on the value of the T2EX pin, see Figure 5. When DCEN=0, Timer 2 is automatically set to count up. In this case In mode, the EXEN2 control bit in T2CON has two options. If EXEN2=0, timer 2 counts up to 0FFFFH and overflows, sets TF2 to activate the interrupt, and at the same time reloads the 16-bit counting registers RCAP2H and RCAP2L, RCAP2H and RCAP2L The value can be preset by software. If EXEN2=1, the 16-bit reload of Timer 2 is triggered by an overflow or a falling edge from 1 to 0 at the external input T2EX. This pulse sets EXF2, which also generates an interrupt if interrupts are enabled.

How to set the working mode of AT89C52 microcontroller timer

  AT89C52 timer

  The interrupt entry address of timer 2 is: 002BH - 0032H.

  When DCEN=1, Timer 2 is allowed to count up or down, as shown in Figure 6. In this mode, the T2EX pin controls the counter direction. When the T2EX pin is logic "1", the timer counts upward. When the count 0FFFFH overflows upward, TF2 is set, and the 16-bit count registers RCAP2H and RCAP2L are reloaded into TH2 and TL2. When the T2EX pin is logic "0", timer 2 counts down. When the values ​​in TH2 and TL2 are equal to the values ​​in RCAP2H and RCAP2L, the count overflows, TF2 is set, and the 0FFFFH value is reloaded into the timing register. .


  The EXF2 bit is set when Timer/Event Counter 2 overflows or underflows. ·Baud rate generator: When TCLK and RCLK in T2CON are set, Timer/Counter 2 is used as a baud rate generator. If Timer/Counter 2 is used as a transmitter or receiver, its transmit and receive baud rates can be different, and Timer 1 is used for other functions, as shown in Figure 7. If RCLK and TCLK are set, Timer 2 operates in baud rate generator mode.


  The mode of the baud rate generator is similar to the auto-reload mode, in which a TH2 rollover causes the Timer 2 register to be reloaded with the 16-bit value in RCAP2H and RCAP2L, which is set by software. In mode 1 and mode 3, the baud rate is determined by the overflow rate of timer 2 according to the following formula: The baud rate of mode 1 and 3 = the overflow rate of the timer/16 The timer can work in both timing mode and For counting mode, in most applications, it works in timing mode (C/T2=0). When Timer 2 is used as a baud rate generator, its operation is different from that of a timer. Usually, when used as a timer, the value of the register is increased by 1 at each machine cycle (1/12 oscillation frequency), and as a baud rate When the generator is used, the register value is incremented by 1 at each state time (1/2 oscillation frequency). The baud rate is calculated as follows:

[1] [2]
Keywords:AT89C52 Reference address:How to set the working mode of AT89C52 microcontroller timer

Previous article:Design of intelligent traffic light using AT89C52 microcontroller
Next article:Working principle and production method of LED dot matrix Chinese character display circuit

Recommended ReadingLatest update time:2024-11-23 15:10

STM8L151C8 MCU learning routine (5)——TIMER1 timing 0.5s
STM8L_5_TIMER: 1.User: Project and main files 2.Hardware:TIMER,CLK,LED,Delay 3.STM8L15x_StdPeriph_Driver: STM8 comes with library files 4.Debug: The hex file is stored in the EXE folder 5.Function: TIMER1 is set to 0.5s and TIMER4 is set to about 0.5s, etc. /** *******************************************************
[Microcontroller]
STM8L151C8 MCU learning routine (5)——TIMER1 timing 0.5s
Design of portable geotechnical thermal property tester
Ground source heat pump systems have outstanding advantages compared to other air conditioning systems. Since the temperature deep in the ground remains unchanged all year round, it is much higher than the outdoor temperature in winter, but significantly lower than the outdoor temperature in summer. Therefore, gro
[Test Measurement]
Design of portable geotechnical thermal property tester
Realization of short-distance wireless communication based on AT89C52 single-chip microcomputer
1 Introduction Short-distance wireless transmission has the advantages of strong anti-interference performance, high reliability, good security, less geographical restrictions, and flexible installation. It has broad application prospects in many fields. Low power consumption and miniaturization are the act
[Microcontroller]
Realization of LCD Display Based on AT89C52
1 Introduction With the continuous expansion of the application field of single-chip microcomputers, using single-chip microcomputers to control Chinese character display has become an inevitable trend in the development of single-chip microcomputers. The previous method of storing Chinese character dot matrix
[Microcontroller]
Realization of LCD Display Based on AT89C52
Design of an Intelligent Power Supply Monitoring System
  0 Preface   UPS power supply system is an essential power supply for power, communication, banking and other industries. It has a history of several decades since its creation. In the process of continuous development and improvement of technology, its protection function is also constantly changing. UPS can be di
[Microcontroller]
Design of an Intelligent Power Supply Monitoring System
Design of robot toy control system using magnetoresistive sensor and AT89C52 microcontroller
The chip only completes the main operations related to communication, computing, etc. When there is no operation, it is maintained by the operating system uClinux running on it. The maintenance and control of all peripheral devices unrelated to the core calculation, control, communication and operation of the EZ328 ar
[Microcontroller]
Design of robot toy control system using magnetoresistive sensor and AT89C52 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号