Microcontroller interrupt experiment

Publisher:温柔心情Latest update time:2016-01-22 Source: eefocusKeywords:MCU  Interrupt Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I have been confused about interrupts and timing these past two days. I heard that this thing can improve the efficiency of the CPU, and I think it should be a very important programming method, so I found a small program in the book, hoping to discover something from the experiment.

The requirement is to use timer T0 mode 1 to generate a 10ms timing, and output a square wave with a period of 20ms on the P1.0 pin, using the interrupt mode, and set the system clock frequency to 12MHz.

Let's talk about the program first, as follows

              ORG   0000H
             LJMP  MAIN                                     ;Jump to main program
             ORG   000BH                                   ;T0 terminal entry address
              LJMP  DVT0                                     ;Go to interrupt service program
             ORG   0100H
 MAIN:   MOV   TMOD,#01H                        ;Set T0 to work in mode 1
              MOV     TH0,#0D8H          
             MOV      TL0,#0F0H                       ;Load initial count value
             SETB  ET0                                         ;T0 opens interrupt
             SETB    EA                                       ;CPU opens interrupt
            SETB     TR0                                      ;Start T0
             SJMP                                              ;Wait for interrupt   
 DVT0:   CPL     P1.0                                      ;P1.0 inverts output
              MOV      TH0,#0D8H             
             MOV       TL0,#0F0H                         ;Reload initial value
            RETI                                                      ;Interrupt return
             END

 

The above program uses the interrupt method to generate interrupts. It takes a certain amount of time for the counter to add 1 each time. The timer timing is obtained by the accumulation of this time. 89C51 has four register units TH0, TL0, TH1, TL1 to store count values. Each register is 8 bits, and there is also a register TMOD to set the working mode. Each mode has a different counting range and different functions. I will talk about this issue later when I have time.

The timer is controlled by setting the relevant registers, mainly TCON and TMOD

TCON is a control register, which contains many interrupt flags. By setting it, the interrupt of the microcontroller can be controlled. The microcontroller can know whether an interrupt has occurred in the system through it.

TMOD is the working mode register, which can control the working mode of the microcontroller. It can control the trigger mode of the microcontroller and the switching of C/T.

Because the idea of ​​this article is to use interrupt control, it is also necessary to set the interrupt control bit IE

Here we will introduce the joint debugging of KEIL software and PROTEUS software.

Let's take a look at the keil software first


This software is free and very easy to use. It supports C language programming and assembly programming. We chose assembly programming. Before compiling, we need to build a project.

When compiling, please note that this software will eventually generate a hex file to support simulation, so click the button on the left of target, as shown below


After opening, the following interface will appear. Select the output tab and tick the box next to create hex file.


If there are no errors in the program, a hex file will be generated under the project folder. OK, the program work is completed.

Next, we will move on to the design of the microcontroller hardware circuit (in practice, hardware should come first and software later, but this article forgets to consider this point)

This is the circuit diagram in the proteus environment, which is a very good simulation software (note that the oscillation frequency of the crystal oscillator should be set to 12MHz)


My circuit is to output a pulse signal at port P1.0. The following is a simulation


The effect is very good, and I feel a sense of accomplishment.

Through this design, we can find that the interrupt of the MCU is generated after the MCU counts fully. Then the counter is an accurate timing function in this process. When an interrupt occurs, it is necessary to enter the interrupt service program. This program is written by ourselves. In order to ensure continuous output of pulses, it is necessary to assign an initial value to the counter in this program and invert P1.0. OK, let's take a break.


Keywords:MCU  Interrupt Reference address:Microcontroller interrupt experiment

Previous article:Serial port communication problem between MCUs
Next article:The use of single chip computer scattered transfer table program

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号