Section 66: The basis of microcontroller external interrupts

Publisher:WhisperingHeartLatest update time:2016-03-16 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Opening remarks:

External interrupt is a very important internal resource of the microcontroller and is widely used. It is the high-speed switch sensor input interface of the microcontroller. It can detect pulse input, receive input signals from infrared remote controls, detect feedback signals from high-speed wheels or circular motion of motors, detect fleeting water drop signals in infusion sets, receive data information from analog serial ports, and so on.

This section will teach you two knowledge points:

The first one: the initialization code of external interrupt and the basic program template of interrupt function.

Second: When there are more than two interrupts in the system, how to set external interrupt 0 as the highest priority to realize the interrupt nesting function.

For details, please see the source code.

(1) Hardware platform:

Based on Zhu Zhaoqi 51 MCU learning board. Use S1 button as the falling edge pulse input of simulated external interrupt 0. Originally, S1 button is directly connected to P0^0 port, so P0^0 port must be connected to P3^2, the dedicated IO port of MCU external interrupt 0, through a jumper. Just remove the two yellow jumpers of P0^0 and P3^2, and connect P0^0 and P3^2 with a line. At this time, every time S1 button is pressed, a falling edge pulse will be generated for P3^2 port, and then the program will automatically jump to the interrupt function and execute once.

(2) Functions to be implemented:

Use the lower 4 digits of the digital tube to record the current falling edge pulse number. Use the S1 button through the jumper to simulate the falling edge input of the external interrupt 0. Each time you press it, the digital tube will display the number of pulses that are accumulated. Because there is jitter when the button is pressed, only a few pulses may be generated when it is pressed once, so you often see three or four data added at a time when you press it once. This experimental phenomenon is normal.

(3) The source code is explained as follows:

#include "REG52.H"

#define const_voice_short 40 //Duration of the buzzer short call

#define const_key_time1 20 //Key debounce delay time

void initial_myself();

void initial_peripheral();

void delay_short(unsigned int uiDelayShort);

void delay_long(unsigned int uiDelaylong);

//74HC595 driving the digital tube

void dig_hc595_drive(unsigned char ucDigStatusTemp16_09, unsigned char ucDigStatusTemp08_01);

void display_drive(); //Drive function for displaying digital tube fonts

void display_service(); //Display window menu service program

//74HC595 driving LED

void hc595_drive(unsigned char ucLedStatusTemp16_09, unsigned char ucLedStatusTemp08_01);

void T0_time(); //Timer interrupt function

void INT0_int(); //External 0 interrupt function

sbit key_gnd_dr=P0^4; //Simulate the ground GND of the independent key, so it must always output a low level

sbit beep_dr=P2^7; //Buzzer driver IO port

sbit dig_hc595_sh_dr=P2^0; //74HC595 program of digital tube

sbit dig_hc595_st_dr=P2^1;

sbit dig_hc595_ds_dr=P2^2;

unsigned char ucDigShow8; //The content to be displayed on the 8th digital tube

unsigned char ucDigShow7; //The content to be displayed on the 7th digital tube

unsigned char ucDigShow6; //The content to be displayed on the 6th digital tube

unsigned char ucDigShow5; //The content to be displayed on the 5th digital tube

unsigned char ucDigShow4; //The content to be displayed on the 4th digital tube

unsigned char ucDigShow3; //The content to be displayed on the third digital tube

unsigned char ucDigShow2; //The content to be displayed on the second digital tube

unsigned char ucDigShow1; //The content to be displayed on the first digital tube

unsigned char ucDigDot8; //Whether the decimal point of digital tube 8 is displayed

unsigned char ucDigDot7; //Whether the decimal point of digital tube 7 is displayed

unsigned char ucDigDot6; //Whether the decimal point of digital tube 6 is displayed

unsigned char ucDigDot5; //Whether the decimal point of digital tube 5 is displayed

unsigned char ucDigDot4; //Whether the decimal point of digital tube 4 is displayed

unsigned char ucDigDot3; //Whether the decimal point of digital tube 3 is displayed

unsigned char ucDigDot2; //Whether the decimal point of digital tube 2 is displayed

unsigned char ucDigDot1; //Whether the decimal point of digital tube 1 is displayed

unsigned char ucDigShowTemp=0; //temporary intermediate variable

unsigned char ucDisplayDriveStep=1; //Dynamic scanning digital tube step variable

unsigned char ucWd1Update=1; //Window 1 update display flag

unsigned char ucWd=1; //Core variable of this program, window display variable. Similar to the variable of the first-level menu. Represents different windows. This program has only one display window

unsigned int uiPluseCnt=0; //Variable for accumulating interrupt pulses in this program

unsigned char ucTemp1=0; //intermediate transition variable

unsigned char ucTemp2=0; //Intermediate transition variable

unsigned char ucTemp3=0; //intermediate transition variable

unsigned char ucTemp4=0; //intermediate transition variable

//Common cathode digital tube character table obtained based on the schematic diagram

code unsigned char dig_table[]=

{

0x3f, //0 Sequence number 0

0x06, //1 Sequence number 1

0x5b, //2 Sequence number 2

0x4f, //3 Sequence number 3

0x66, //4 Sequence number 4

0x6d, //5 Sequence number 5

0x7d, //6 Sequence number 6

0x07, //7 Serial number 7

0x7f, //8 Sequence number 8

0x6f, //9 Sequence number 9

0x00, //No sequence number 10

0x40, //- Sequence number 11

0x73, //P serial number 12

};

void main()

{

initial_myself();

delay_long(100);

initial_peripheral();

while(1)

{

display_service(); //Display window menu service program

}

}

void display_service() //Display window menu service program

{

switch(ucWd) //The core variable of this program, the window display variable. Similar to the variable of the first-level menu. Represents the display of different windows.

{

case 1: //Display the data of the first window. There is only one display window in this system.

if(ucWd1Update==1) //Window 1 needs to be fully updated

{

ucWd1Update=0; //Clear the flag in time to avoid continuous scanning

ucDigShow8=10; //The 8th digital tube displays no

ucDigShow7=10; //The 7th digital tube displays no

ucDigShow6=10; //The 6th digital tube displays no

ucDigShow5=10; //The 5th digital tube displays no

//Decompose the data first

ucTemp4=uiPluseCnt/1000;

ucTemp3=uiPluseCnt%1000/100;

ucTemp2=uiPluseCnt%100/10;

ucTemp1=uiPluseCnt%10;

//Transition the data to be displayed to the buffer variable, making the transition time as short as possible

//The following added if judgment is a slight modification to remove the high bit of 0 in the entire 4-bit data and not display it.

if(uiPluseCnt<1000)

{

ucDigShow4=10; //If less than 1000, thousands will be displayed

}

else

{

ucDigShow4=ucTemp4; //The content to be displayed on the 4th digital tube

}

if(uiPluseCnt<100)

{

ucDigShow3=10; //If it is less than 100, the hundreds place will be displayed

}

else

{

ucDigShow3=ucTemp3; //The content to be displayed on the third digital tube

}

if(uiPluseCnt<10)

{

ucDigShow2=10; //If less than 10, the tens digit will be displayed

}

else

{

ucDigShow2=ucTemp2; //The content to be displayed on the second digital tube

}

ucDigShow1=ucTemp1; //The content to be displayed on the first digital tube

}

break;

}

}

void display_drive()

{

// The following program can also compress the capacity if some arrays and shifted elements are added. However, what Hongge pursues is not capacity, but clear explanation ideas.

switch(ucDisplayDriveStep)

{

case 1: //Display the first position

ucDigShowTemp=dig_table[ucDigShow1];

if(ucDigDot1==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xfe);

break;

case 2: //Display the second digit

ucDigShowTemp=dig_table[ucDigShow2];

if(ucDigDot2==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xfd);

break;

case 3: //Display the third digit

ucDigShowTemp=dig_table[ucDigShow3];

if(ucDigDot3==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xfb);

break;

case 4: //Display the 4th digit

ucDigShowTemp=dig_table[ucDigShow4];

if(ucDigDot4==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xf7);

break;

case 5: //Display the 5th digit

ucDigShowTemp=dig_table[ucDigShow5];

if(ucDigDot5==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xef);

break;

case 6: //Display the 6th digit

ucDigShowTemp=dig_table[ucDigShow6];

if(ucDigDot6==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xdf);

break;

case 7: //Display the 7th position

ucDigShowTemp=dig_table[ucDigShow7];

if(ucDigDot7==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xbf);

break;

case 8: //Display the 8th digit

ucDigShowTemp=dig_table[ucDigShow8];

if(ucDigDot8==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0x7f);

break;

}

ucDisplayDriveStep++;

if(ucDisplayDriveStep>8) //After scanning 8 digital tubes, restart scanning from the first one

{

ucDisplayDriveStep=1;

}

}

//74HC595 driver function of digital tube

void dig_hc595_drive(unsigned char ucDigStatusTemp16_09, unsigned char ucDigStatusTemp08_01)

{

unsigned char i;

unsigned char ucTempData;

dig_hc595_sh_dr=0;

dig_hc595_st_dr=0;

ucTempData=ucDigStatusTemp16_09; //Send the high 8 bits first

for(i=0;i<8;i++)

{

if(ucTempData>=0x80)dig_hc595_ds_dr=1;

else dig_hc595_ds_dr=0;

dig_hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register

delay_short(1);

dig_hc595_sh_dr=1;

delay_short(1);

ucTempData=ucTempData<<1;

}

ucTempData=ucDigStatusTemp08_01; //Send the lower 8 bits first

for(i=0;i<8;i++)

{

if(ucTempData>=0x80)dig_hc595_ds_dr=1;

else dig_hc595_ds_dr=0;

dig_hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register

delay_short(1);

dig_hc595_sh_dr=1;

delay_short(1);

ucTempData=ucTempData<<1;

}

dig_hc595_st_dr=0; //The ST pin updates the data of the two registers and outputs them to the output pin of 74HC595 and latches them

delay_short(1);

dig_hc595_st_dr=1;

delay_short(1);

dig_hc595_sh_dr=0; //Pull low to enhance anti-interference

dig_hc595_st_dr=0;

dig_hc595_ds_dr=0;

}

void T0_time() interrupt 1 //Timer interrupt function

{

TF0=0; //Clear interrupt flag

TR0=0; //Disable interrupt

display_drive(); //Drive function of digital tube font

TH0=0xfe; //Reinstall initial value (65535-500)=65035=0xfe0b

TL0=0x0b;

TR0=1; //Open interrupt

}

/* Note 1:

* Use the S1 button in Zhu Zhaoqi 51 learning board as the falling edge pulse input of simulated external interrupt 0.

* The S1 button is directly connected to the P0^0 port, so the P0^0 port must be connected to the

* On the MCU external interrupt 0 dedicated IO port P3^2, just remove the two yellow jumpers P0^0 and P3^2, and use a

* Connect P0^0 and P3^2 with a wire. At this time, every time you press the S1 button, P3^2 will be

* Generate a falling edge pulse, and then the program will automatically jump to the following interrupt function and execute once.

* Because there is jitter when the button is pressed, only a few pulses may be generated when it is pressed once, so you often see three or four data added at a time.

* This kind of experimental phenomenon is normal.

*/

void INT0_int(void) interrupt 0 //INT0 external interrupt function

{

EX0=0; //Disable external 0 interrupt This is just my personal programming habit, you can also leave it open

uiPluseCnt++; //Accumulate the number of pulses on the falling edge of external interrupt

ucWd1Update=1; //Window 1 updates the display

EX0=1; //Open external 0 interrupt

}

void delay_short(unsigned int uiDelayShort)

{

unsigned int i;

for(i=0;i

{

; //A semicolon is equivalent to executing an empty statement

}

}

void delay_long(unsigned int uiDelayLong)

{

unsigned int i;

unsigned int j;

for(i=0;i

{

for(j=0;j<500;j++) //Number of empty instructions in the embedded loop

{

; //A semicolon is equivalent to executing an empty statement

}

}

}

void initial_myself() //Initialize the MCU

{

/* Note 2:

* The matrix keyboard can also be used as an independent key, provided that one of the common output lines outputs a low level.

* Simulate the triggering ground of an independent key. In this program, key_gnd_dr is outputted at a low level.

* S1 of Zhu Zhaoqi 51 learning board is an independent key used in this program.

* Connected to the external interrupt dedicated interface P3^2 of the microcontroller to simulate external falling edge pulse input.

*/

key_gnd_dr=0; //Simulate the ground GND of the independent key, so it must always output a low level

beep_dr=1; //Use PNP transistor to control the buzzer, it will not sound when the output is high level.

TMOD=0x01; //Set timer 0 to working mode 1

TH0=0xfe; //Reinstall initial value (65535-500)=65035=0xfe0b

TL0=0x0b;

}

void initial_peripheral() //Initialize the periphery

{

ucDigDot8=0; //No decimal points are displayed

ucDigDot7=0;

ucDigDot6=0;

ucDigDot5=0;

ucDigDot4=0;

ucDigDot3=0;

ucDigDot2=0;

ucDigDot1=0;

EX0=1; //Enable external interrupt 0

IT0=1; //Falling edge triggers external interrupt 0 If it is 0, it means low level interrupt

/* Comment 3:

* Note that this system uses two interrupts, one is a timer interrupt and the other is an external interrupt.

* Therefore, the IP register must be set to make external interrupt 0 the highest priority so that external interrupt 0 can interrupt

* Timed interrupts.

*/

IP=0x01; //Set external interrupt 0 to the highest priority, which can interrupt low-priority interrupt services. Implement interrupt nesting function

EA=1; //Open the general interrupt

ET0=1; //Enable timer interrupt

TR0=1; //Start timing interrupt

}

Closing remarks:

This section describes the basic program template of external interrupts. In the next section, I will talk about an example of a practical application of external interrupts. For more details, please listen to the next section for details: Using external interrupts to simulate serial port data transmission and reception.

Keywords:MCU Reference address:Section 66: The basis of microcontroller external interrupts

Previous article:Section 65: Division of large data
Next article:Section 67: Using external interrupts to simulate serial port data transmission and reception

Recommended ReadingLatest update time:2024-11-16 22:32

NHTSA asks Tesla to recall 8GB eMMC NAND flash. What do you think?
  After the previous recall petition for loss of control risk was rejected, on January 13, the U.S. National Highway Traffic Safety Administration (NHTSA) required Tesla to recall some Model S and Model X vehicles, totaling 158,000 vehicles, because the failure of eMMC NAND flash would lead to safety risks. This is v
[Embedded]
NHTSA asks Tesla to recall 8GB eMMC NAND flash. What do you think?
Design of Ultra-low Frequency Signal for AT89C52 Single Chip Microcomputer
    Low-frequency and ultra-low-frequency signals are widely used in medicine, electrochemical research and experimental teaching. Especially in the field of electrochemistry, ultra-low-frequency signal generators have become an indispensable part of electrochemical instruments. Electrochemical instruments equipped wi
[Microcontroller]
Design of Ultra-low Frequency Signal for AT89C52 Single Chip Microcomputer
STC MCU internal EEPROM program
#include #include #define uchar unsigned char #define uint unsigned int   sfr ISP_DATA = 0x00E2; //EEPORM data register sfr ISP_ADDRH = 0x00E3; //Address register high eight bits sfr ISP_ADDRL = 0x00E4; //Address register low eight bits sfr ISP_CMD = 0x00E5; //EEPORM command register sfr ISP_TRIG = 0x00E6; /
[Microcontroller]
An efficient and fast algorithm for calculating square roots using a single chip microcomputer
When we use 8-bit MCU programming and use the square root algorithm, if we call the built-in subroutine, the generated code is too much and the efficiency is low. Because the software's built-in algorithm uses floating-point type for calculation, in reality we often only need to return integer data. Using the followin
[Microcontroller]
Complete solution to the difficult problems of 51 single-chip microcomputer crystal oscillator
Looking back, when I first learned about 51 single-chip microcomputers, I always encountered many problems related to crystal oscillators. In fact, the crystal oscillator is like the human heart, the blood and the pulse. Once the crystal oscillator problem of the single-chip microcomputer is understood, other problems
[Microcontroller]
Reading matrix keyboard based on single chip, using corresponding key value
/***************************************************** 007.ASM Wiring: P0 port connects to digital tube, P2 port connects to matrix keyboard Use timer interrupt, if there is no operation for a period of time, the digital tube will enter the self-circulation display 2010.09.05 ****************
[Microcontroller]
PIC microcontroller interrupt service routine
There is a special definition method for interrupt service routines: void interrupt ISR(void); the function name "ISR" can be changed to any legal combination of letters or numbers, but its input parameter and return parameter types must be "void", that is, there are no input parameters and return parameters, and ther
[Microcontroller]
Using a single chip microcomputer to make a multi-input voltage meter
In industrial control and intelligent instruments, single-chip microcomputers are often used for real-time control and real-time data processing. The information processed by the single-chip microcomputer is digital, while the relevant parameters of the controlled or measured object are often continuously changing anal
[Microcontroller]
Using a single chip microcomputer to make a multi-input voltage meter
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号