MCU internal timer/counter and interrupt system

Publisher:森绿企鹅Latest update time:2012-08-08 Source: 21ic Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the previous lecture, we introduced the working principle and development process of single-chip microcomputers by describing an experiment of using a single-chip microcomputer to control an external LED flashing. This lecture will introduce two very important resources inside the single-chip microcomputer - the timer/counter and the interrupt system. Through this lecture, readers can understand the working principle of the timer and the interrupt system of the single-chip microcomputer.

Therefore, the timer counting program and interrupt service program are designed.

1. Principle Introduction

First, let's take an alarm clock as an example. If we set it to go off in one minute, the second hand needs to move one circle (60 times). That is, one minute is converted into the number of times the second hand moves, which is the number of counts. When it reaches 60 times, the alarm goes off, and each count lasts for 1 second.

The timer/counter inside the microcontroller is similar to an alarm clock. You can set the time to be timed through programming, and perform corresponding operations when the timed time is reached. So how long does it take to count once inside the microcontroller? The clock pulse input by the 51 microcontroller is obtained by dividing the output of the crystal oscillator by 12, so the timer can also be regarded as a counter for the computer machine cycle. Because each machine cycle contains 12 oscillation cycles, the timer adds 1 for each machine cycle, and the input clock pulse can be regarded as a machine cycle signal. Therefore, its frequency is 1/12 of the crystal frequency. If the crystal frequency is 12MHz, the time for the timer to receive an input pulse is exactly 1μs. In this experimental kit, a 11.0592M crystal oscillator is used, so the time for each input pulse to be received is about 1.085μs. Achieving precise timing is very important in actual project applications, because it is often necessary to use precise timing for a period of time, and then perform the corresponding task when the timing time is reached.

So how do you program to achieve timing? First, let's briefly introduce the timer resources in the microcontroller (STC89C52) on this experimental board. STC89C52 has three timers/counters, namely T0, T1 and T2. T0 and T1 work in the same way, so they are introduced together.

The working mode of T2 is slightly different, so I won't introduce it here. There is a practical application in the experiment kit CD. At the same time, the timer and counter in the microcontroller are multiplexed. The counter records the number of external pulses, while the timer is a very stable counting source provided by the internal clock of the microcontroller. In this lecture, T0 and T1 are used as timers for example.

After understanding the timer resources in the microcontroller, let's introduce the timer registers in detail. TMOD (see Table 1), TCON (see Table 3) are connected to timer T0 and timer T1 through internal buses and logic circuits. TMOD is used to set the working mode of the timer, and TCON is used to control the start and stop of the timer, and mark the overflow and interruption of the timer. After the working mode of the timer is set and the timer is started, the timer will work independently according to the set working mode and no longer occupy the CPU's operating time. The current operation of the CPU can only be interrupted when the counter is full and overflows.

Table 1 TMOD register

The meaning of each bit in the table (from left to right, from high to low) is as follows.

The lower 4 bits of TMOD are the mode field of timer 0, and the upper 4 bits are the mode field of timer 1. Their meanings are exactly the same.

M1 and M0: working mode control bits, their definitions are shown in Table 2 (where i=0, 1).

Table 2 Timer working mode control bits

: Function selection bit.

15.jpg [page]

It is worth noting that the TMOD register cannot be bit-addressed, and can only be set with byte instructions to define the working mode of timer 1 in the upper 4 bits or the working mode of timer 0 in the lower 4 bits. Moreover, all bits of TMOD are set to 0 when reset.

Table 3 TCON register

The meaning of each bit in the table (from left to right, from high to low) is as follows.

(1) TF1: Timer 1 overflow flag. When Timer 1 overflows, the hardware automatically sets TF1=1 and sends a Timer 1 interrupt request to the CPU. The CPU responds when the interrupt is enabled. After entering the interrupt service routine, the hardware automatically clears it to 0. When the interrupt is masked, TF1 can be used for query testing. At this time, it can only be cleared to 0 by software.

(2) TR1: Timer 1 operation control bit. Set to 1 or clear to 0 by software to start or stop Timer 1.

When GATE=1 and is high, TRI is set to 1 to start timer 1; when GATE=0, TR1 is set to 1 to start timer 1.

(3) TF0: Timer 0 overflow flag. Its function and operation are the same as TF1.

(4) TR0: Timer 0 operation control bit. Its function and operation are the same as TR1.

(5) IE1: External interrupt 1 ( ) request flag.

(6) IT1: External interrupt 1 trigger mode selection bit.

(7) IE0: External interrupt 0 ( ) request flag.

(8) IT0: External interrupt 0 trigger mode selection bit.

It is worth noting that the lower 4 bits in TCON are used to control external interrupts and have nothing to do with the timer/counter, which will be mentioned in later lectures. When the system is reset, all bits in TCON are cleared to 0.

In the above text, we mentioned timer overflow and interrupt. What is timer overflow? We can understand it this way: drip water into a basin, the water drops continue to fall, the water in the basin continues to fill up, and eventually there will be a drop of water that fills the basin (this is equivalent to counting to the maximum value). At this time, if another drop of water falls, the water will overflow, which is "overflow". Of course, water overflows and flows to the ground, and the timer overflow will make TF0 become "1". Once TF0 changes from 0 to 1, an interrupt will be generated. An interrupt is a process in which the CPU pauses the currently executing program due to the occurrence of a certain event and executes a program to handle the event instead. After the execution of the program is completed, the CPU continues to execute the paused program. This is just like when we are doing something, someone comes to ask for help, we stop what we are doing to help, and come back to do the original thing after the work is done. According to the different causes of interrupts, or the different conditions under which the CPU responds to interrupts, interrupts can also be divided into maskable interrupts (that is, we can refuse to help others and continue to do our own things) and non-maskable interrupts (we are tired of doing things and must rest).

After understanding the principle of interruption, let's look at the interrupt enable control register IE (see Table 4) and the interrupt priority register IP (see Table 5).

[page]

Table 4 IE register

EA: Interrupt enable control bit. EA=0, disable all interrupts; EA=1, enable all interrupts, but whether to allow interrupt requests from each interrupt source depends on the state of the interrupt enable control bit of each interrupt source. Beginners often forget to enable all interrupts, which results in being unable to enter the interrupt source.

ET2: Interrupt enable bit for timer/counter T2.

ES: Serial port interrupt enable bit.

ET1: Interrupt enable bit for timer/counter T1.

EX1: Interrupt enable bit for external interrupt 1 (INT1).

ET0: Interrupt enable bit for timer/counter T0.

EX0: Interrupt enable bit for external interrupt 0 (INT0).

When the above 7 interrupt enable control bits are 0, interrupts are disabled, and when they are 1, interrupts are enabled.

Table 5 IP Register

PT2: Timer/Counter T1 interrupt priority control bit.

PS: Serial port interrupt priority control bit.

PT1: Timer/Counter T1 interrupt priority control bit.

PX1: External interrupt 1 priority control bit.

PT0: Timer/Counter T0 interrupt control bit.

PX0: External interrupt 0 interrupt priority control bit.

The above 6 interrupt priority control bits are low-level interrupts when "0" and high-level interrupts when "1". If several interrupt sources with the same priority level request interrupts from the CPU at the same time, the CPU will query the logic circuits in the internal sequence and determine which interrupt request to respond to according to the natural priority order. The natural priority is formed by hardware, and its priority level from high to low is external interrupt 0, timer/counter T0, external interrupt 1, timer/counter T1, serial port interrupt, timer/counter T2.

So far, the important registers related to timer control have been introduced. Next, we will verify the practice by writing a program.

2. Circuit Detail

This lecture uses the same circuit as the previous lecture, so I will not go into details here (see Figure 1).


Figure 1 Timer application experiment circuit diagram

[page]

3. Programming

Timer 0 application test program (control D1 flashing).

#include (1)

#define led P0_0 (2)

unsigned char count=0; (3)

void main(void) (4)

{

TMOD=0X01; (5)

TH0=(65536-50000)/256; (6)

TL0=(65536-50000)%256; (7)

EA=1; (8)

ET0=1; (9)

TR0=1; (10)

PT0=1; (11)

while(1); (12)

}

void timer0 (void) interrupt 1 (13)

{

TH0=(65536-50000)/256; (14)

TL0=(65536-50000)%256;(15)

count++; (16)

if (count == 10) (17)

{

count = 0; (18)

led = !led; (19)

}

}

1. Detailed description of the procedure:

(1) Header file inclusion. The P0_0 called by the program next is a register address defined in the header file. Before operating the register inside the microcontroller, its source should be stated. Interested readers can look at the contents of the AT89X52.h file.

(2) Macro definition of led makes it easier to understand and modify the program. Name the P0_0 port as led, so that led can be used instead of P0_0 port to perform operations in the program.

(3) Define an 8-bit global variable.

(4) Main function entry. The main function does not pass parameters or return values.

(5) Set timer 0 to operate in mode 1, which is a 16-bit counter.

(6) Assign an initial value to the high 8 bits of the timer and round it to the integer of 256.

(7) Assign an initial value to the lower 8 bits of the timer and take the modulo 256.

(8) Turn on the general interrupt.

(9) Enable timer 0 interrupt.

(10) Timer 0 starts counting.

(11) Set the timer 0 interrupt as the priority interrupt.

(12) Infinite loop, waiting for interruption.

(13) Timer 0 interrupt service function entry.

(14) Assign an initial value to the high 8 bits of the timer and round it to the integer of 256.

(15) Assign an initial value to the lower 8 bits of the timer and take the modulo 256.

(16) Add 1 to the variable count.

(17) If count increases to 10.

(18) The count variable is cleared to 0.

(19) LED output is inverted.

[page]

2. Program flow chart and experimental phenomena

The program flow is shown in Figure 2. After the program is compiled and downloaded to the microcontroller and run, you can see that the LED light connected to the P0_0 port on the experimental board flashes regularly, as shown in Figure 3. The time of on and off can be calculated as 10×50000×1.085μs=542.5ms.

The actual time is a few milliseconds longer than this because the instruction time for conditional judgment and program call is not included. In situations where extreme precision is required, actual fine-tuning should be performed.


Figure 2 Flowchart of main program and interrupt service function


Figure 3 Experimental effect

IV. Conclusion

This lecture mainly introduces the internal timer and interrupt system of 51 MCU and writes the first simple timer experiment program. Through this lecture, you can find that the timer and interrupt system in the MCU are not difficult to learn. As long as you remember the relevant operation steps of the application and practice several times, you can master the use of these resources very skillfully. Since this lecture contains a lot of theoretical content, I hope readers will take more time to write it down or familiarize themselves with it. In the next lecture, I will introduce the serial port communication of the MCU and give examples. Please stay tuned.

Keywords:MCU Reference address:MCU internal timer/counter and interrupt system

Previous article:Minimum System Composition and I/O Output Control of Single Chip Microcomputer
Next article:Learn 51 MCU timer

Recommended ReadingLatest update time:2024-11-16 19:43

Proteus simulation of perpetual calendar based on 51 MCU + LCD12864
The schematic diagram is as follows, and part of the program is as follows. The microcontroller source program is as follows: #include reg51.h #include "DS18B20.h" #include "DS1302.h" #include "LCD12864.h" char a; sbit KEY1=P3^4; sbit KEY2=P3^5; sbit KEY3=P3^6; bit flag=1; //Enter/Exit setting flag uchar cho
[Microcontroller]
Proteus simulation of perpetual calendar based on 51 MCU + LCD12864
Some issues and opinions that need to be paid attention to in programming PIC microcontrollers
1. Distinguish between uppercase and lowercase letters When writing the source program of the PIC microcontroller, in addition to the strict list instructions at the beginning of the source program, you must also pay attention to the upper and lower case rules of the letters in the source program, otherwise the progra
[Microcontroller]
51 MCU pwm signal simulation
Applicable to all microcontrollers, crystal oscillator is 11.0592M #include "reg52.h"     int pwm1=1; duty cycle of the first pwm int pwm2=60; the second pwm duty cycle sbit led1=P1^0;     sbit led2=P1^3; sbit md1=P1^0; void pwm_init() { TMOD|=0x01; TH0=(65536-100)/256; about 1Kmz       TL0=(65536-100)%256; ET0=1; E
[Microcontroller]
Car music rhythm atmosphere lighting solution based on the JIHAI G32A1445 car general-purpose MCU
With the deep penetration of smart car technology, car lighting, as an important part of car design, is reshaping the way drivers interact with cars, and is being optimized and upgraded from simple lighting tools to intelligent safety devices that carry more functions and different application scenarios. Modern smar
[Automotive Electronics]
Car music rhythm atmosphere lighting solution based on the JIHAI G32A1445 car general-purpose MCU
LabVIEW realizes serial communication between PC and microcontroller
       Use Proteus to simulate the microcontroller and the hardware connection. Use Keil and Labview to write programs for the serial communication part to achieve:          PC sends numbers (00, 01, 02, 03..., FF, hexadecimal) to the microcontroller through the serial port. The microcontroller sends back the number a
[Test Measurement]
Using a single chip microcomputer to control LED dot matrix
introduction Since the advent of the single-chip microcomputer in the 1970s, it has attracted people's attention and concern for its extremely high cost performance, so it is widely used and developed rapidly. The advantages of single-chip microcomputers are small size, light weight, strong anti-interferenc
[Microcontroller]
Is it assembly programming or C language programming to learn microcontroller?
Regarding the question of programming in assembly language and C language. This question bothers many people, probably mainly due to the teaching and guidance of some textbooks. C language programming is introduced in the freshman year, but before students know what the practical use of C language is, other languages ​
[Microcontroller]
Design of intelligent lighting control system using single chip microcomputer
    introduction     Natural resources are various substances and energies in nature that can be used by humans. They are the basis for human production activities and life, the material guarantee and material basis for human survival and development, and the cornerstone of sustainable development of human society. Wi
[Microcontroller]
Design of intelligent lighting control system using single chip microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号