51 MCU external interrupt programming example 2

Publisher:HeavenlyWonderLatest update time:2016-09-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. The five major interrupt sources of the 51 MCU: serial port interrupt, timer interrupt 1, external interrupt 1, timer interrupt 0, external interrupt 0;
2. Interrupt source numbering: serial port interrupt is 4, timer interrupt 1 is 3, external interrupt 1 is 2, timer interrupt 0 is 1, external interrupt 0 is 0;
3. Interrupt source priority: in the above order, serial port interrupt is the lowest and external interrupt 0 is the highest;
4. When using external interrupts 0 and 1, the TCON register must be set to set its trigger mode to low level trigger (0) or falling edge trigger (1);
5. Before use, the general interrupt and its own interrupt must be enabled through the IE register;

1. External interrupt basic routine (interrupt not used, keyboard scan is general port scan)
#include .h>
sbit k1=P3^2;
sbit led=P2^7;
void delay_ms(unsigned int xms); //ms level delay subroutine
void key_scan(); //Declare keyboard scan subfunction
void main()
{
led=1; //Power-on initialization, LED light is not on
while(1)
{
key_scan();
delay_ms(3000);
}
}
void delay_ms(unsigned int xms) //ms level delay subroutine
{ unsigned int x,y; 
for(x=xms;x>0;x--)
for(y=130;y>0;y--);}
void key_scan() //Keyboard scan subfunction
{ if(k1==0) //Is any key pressed? (k1=0?)
{ delay_ms(10); //delay to eliminate jitter
if(k1==0) //A key is indeed pressed, then:
{led=!led; //flip the light status
    while(!k1);} //wait for the key to be released

}

2. External interrupt basic routine (single keyboard external interrupt 0 scan processing)
//Use a key to control the on and off of a light
#include
sbit k1=P3^2;
sbit led=P2^7;
void delay_ms(unsigned int xms); //ms level delay subroutine
void key_scan() interrupt 0 //Keyboard scan subroutine using external interrupt 0. It can also be placed in the main function without pre-declaration
{ if(k1==0) //Is there a key pressed? (k1=0?)
{ delay_ms(10); //delay debounce
if(k1==0) //A key is indeed pressed, then:
{led=!led; //flip the state of the light
    while(!k1);} //wait for the key to be released

}
void main()
{
led=1; //power-on initialization, the LED light is not on
TCON=0x01; //turn on external interrupt 0 and set it to falling edge trigger
IE=0x81; //turn on total interrupt
while(1)
{
delay_ms(3000); //note that there is no keyboard scanning program in the main function
}
}
void delay_ms(unsigned int xms) //ms level delay subroutine
{ unsigned int x,y; 
for(x=xms;x>0;x--)
for(y=130;y>0;y--);

}

3. External interrupt basic routine (external interrupt 1 scan processing of a single keyboard)

Author: CJoy, School of Mechanical Engineering, Jiangsu University of Science and Technology
//The function is the same as the previous example, using a button to control the on and off of a light, but using external interrupt 1
#include
sbit k2=P3^3;  
sbit led=P2^7;
void delay_ms(unsigned int xms); //ms-level delay subroutine
void main()
{
led=1; //Power-on initialization, the LED light is not on
TCON=0x04; //Turn on external interrupt 1 and set it to falling edge trigger
IE=0x84; //Turn on total interrupt and external interrupt 1
while(1)
{
delay_ms(3000);
}
}
void delay_ms(unsigned int xms) //ms-level delay subroutine
{ unsigned int x,y; 
for(x=xms;x>0;x--)
for(y=130;y>0;y--);}
void key_scan() interrupt 2 //Keyboard scanning subfunction using external interrupt 1
{ if(k2==0) //Is a key pressed? (k1=0?)
{ delay_ms(10); //Delay to eliminate jitter
if(k2==0) //A key is indeed pressed, then:
{led=!led; //Flip the state of the light
    while(!k2);} //Wait for the key to be released

}

 4. External interrupt basic routine (using external interrupt 0 and external interrupt 1 at the same time)
//Use one key to control the on and off of the eight lights connected to port P0, and another key to control the on and off of one light
#include
sbit k1=P3^2;
sbit k2=P3^3; 
sbit led=P2^7;
void delay_ms(unsigned int xms); //ms level delay subroutine
void main()
{
P0=0xff; //Power-on initialization, P0 all off
led=1; //Power-on initialization, LED light is not on
TCON=0x05; //Open external interrupts 1 and 2, and set them to fall edge trigger
IE=0x85; //Open general interrupt and external interrupts 1 and 2

while(1)
{
delay_ms(3000);
}
}
void delay_ms(unsigned int xms) //ms level delay subroutine
{ unsigned int x,y; 
for(x=xms;x>0;x--)
for(y=130;y>0;y--);}
void key_scan_1() interrupt 0 //Keyboard scan subroutine using external interrupt 0
{ if(k1==0) //Is there a key pressed? (k1=0?)
{ delay_ms(10); //Delay debounceif
(k1==0) //A key is indeed pressed, then:
{P0=~P0; //Flip the status of the 8 lightswhile
    (!k1);} //Wait for the key to be released

}
void key_scan_2() interrupt 2 //Keyboard scan subroutine using external interrupt 1
{ if(k2==0) //Is there a key pressed? (k1=0?)
{ delay_ms(10); //delay to eliminate jitter
if(k2==0) //A key is indeed pressed, then:
{led=!led; //flip the state of a single light
    while(!k2);} //wait for the key to be released

}

5. External interrupt extension application routine (8 buttons control the on and off of 8 lights respectively)


//The eight buttons are connected to the external interrupt pin 0 through eight diodes for interrupt expansion, thus solving the problem of insufficient external interrupt ports.
#include
sbit k0=P2^0; //The eight buttons are connected to the pins of P2 port respectively,
sbit k1=P2^1; //At the same time, they are also connected to the external interrupt pin 0 (P3.2) through diodes
sbit k2
=P2^2;
sbit k3=P2^3;
sbit k4=P2^4;
sbit k5=P2^5; sbit k6=P2^6;
sbit k7=P2^7;

sbit led0=P0^0; //The 8 LED lights connected to the P0 pin are controlled by the above 8 buttons respectively
sbit led1=P0^1;
sbit led2=P0^2;
sbit led3=P0^3;
sbit led4=P0^4;
sbit led5=P0^5;
sbit led6=P0^6;
sbit led7=P0^7;

void delay_ms(unsigned int xms); //ms level delay subroutine
void main()
{
   //Power-on initialization, LED light is off
TCON=0x01; //Open external interrupt 0, and set it to falling edge trigger
IE=0x81; //Open total interrupt
while(1)
{
delay_ms(3000);
}
}
void delay_ms(unsigned int xms) //ms level delay subroutine
{ unsigned int x,y; 
for(x=xms;x>0;x--)
for(y=130;y>0;y--);

}
void key_scan() interrupt 0 //Keyboard scan subfunction using external interrupt 0
{ if(k0==0) //Is there a key pressed? (k0=0?)
{ delay_ms(10); //Delay debounce
if(k0==0) //A key is indeed pressed, then:
{led0=~led0; //Flip the state of the light
    while(!k0);} //Wait for the key to be released
}
if(k1==0) //Is there a key pressed? (k1=0?)
{ delay_ms(10); //Delay debounce
if(k1==0) //A key is indeed pressed, then:
{led1=!led1; //Flip the state of the light
    while(!k1);} //Wait for the key to be released
}
if(k2==0) //Is there a key pressed? (k2=0?)
{ delay_ms(10); //Delay debounce
if(k2==0) //A key is pressed, then:
{led2=!led2; //Flip the state of the light
    while(!k2);} //Wait for the key to be released
}
if(k3==0) //Is a key pressed? (k3=0?)
{ delay_ms(10); //Delay debounce
if(k3==0) //A key is pressed, then:
{led3=!led3; //Flip the state of the light
    while(!k3);} //Wait for the key to be released
}
if(k4==0) //Is a key pressed? (k4=0?)
{ delay_ms(10); //Delay debounce
if(k4==0) //A key is pressed, then:
{led4=!led4; //Flip the state of the light
    while(!k4);} //Wait for the key to be released
}
if(k5==0) //Is a key pressed? (k5=0?)
{ delay_ms(10); //Delay debounce
if(k5==0) //A key is pressed, then:
{led5=!led5; //Flip the state of the light
    while(!k5);} //Wait for the key to be released
}
if(k6==0) //Is a key pressed? (k6=0?)
{ delay_ms(10); //Delay debounce
if(k6==0) //A key is pressed, then:
{led6=!led6; //Flip the state of the light
    while(!k6);} //Wait for the key to be released
}
if(k7==0) //Is a key pressed? (k7=0?)
{ delay_ms(10); //Delay debounce
if(k7==0) //A key is pressed, then:
{led7=!led7; //Flip the state of the light
    while(!k7);} //Wait for the key to be released


Reference address:51 MCU external interrupt programming example 2

Previous article:Principle of single chip microcomputer control GPS/GSM (virtual serial port)
Next article:Design of MP3 player based on AT89C51SND1C single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 15:48

Brief introduction of interrupt request source of mcs51 microcontroller
(1) External interrupt request source: external interrupts 0 and 1 are introduced through external pins. There are two pins on the microcontroller, named INT0 and INT1, which are P3.2 and P3.3. There are four bits in the internal TCON that are related to external interrupts.  IT0: INT0 trigger mode control bit, whic
[Microcontroller]
Zero-point correction technology of infrared axle temperature detector based on C8051F single-chip microcomputer
Introduction In data acquisition systems, the zero drift of the preamplifier is the main source of detection errors, especially under harsh environmental conditions. For example, the infrared probe used for railway axle temperature detection must work reliably within the range of ambient temperature from -40℃
[Microcontroller]
Zero-point correction technology of infrared axle temperature detector based on C8051F single-chip microcomputer
51 MCU Preparatory Knowledge
1. The concept of single chip microcomputer: A microprocessor, memory, and I/O interface circuit are integrated on an integrated circuit chip to form a single-chip microcomputer, namely a single-chip microcomputer. 2. The purpose of single chip microcomputer: Industrial automation: data acquisition, m
[Microcontroller]
[51 microcontroller] keil establishment project and burning software operation (1)
Keil build project 1. Enter the main interface 2. Project - Create a new μVision project 3. Select a device. Atmel-AT89C52 4. Create a new blank page and save it in C file format (.c) 5. Double-click the source group-add the C file to the source group 6. Goal setting Output-check Create HEX File (check i
[Microcontroller]
[51 microcontroller] keil establishment project and burning software operation (1)
Data copy program (ASM) of MCS51 microcontroller
Data copy program (ASM) of MCS51 microcontroller ;Internal RAM data copy program ; Entry: R0, R7 ; Occupied resources: A ; Stack requirement: 2 bytes ; Exit: R1 IBMOV    :MOV    A,R0           ADD    A,R7           MOV    R0,A           MOV    A,R1           ADD    A,R7           MOV    R1,A IBM1     :DEC    R0     
[Microcontroller]
51 MCU binary conversion
#include #define LongToBin(n) \ (\ ((n 21) & 0x80) | \ ((n 18) & 0x40) | \ ((n 15) & 0x20) | \ ((n 12) & 0x10) | \ ((n 9) & 0x08) | \ ((n 6) & 0x04) | \ ((n 3) & 0x02) | \ ((n ) & 0x01) \ ) #define Bin(n) LongToBin(0x##n##l) void main(void) { unsigned char c; c = Bin(10101001); // c = 0xA9 }
[Microcontroller]
Design of stepper motor control system using 89C51 microcontroller
This article brings you the stepper motor control system design of two 89C51 microcontrollers. Design of stepper motor control system for 89C51 microcontroller The overall system diagram is shown in Figure 1. This system uses external interrupt mode. Port p0 is used as the signal input part, port p1 is the light-emi
[Microcontroller]
Design of stepper motor control system using 89C51 microcontroller
Application of μPSD32xx MCU Based on 51 Core in Tax Controller
1. Introduction Tax control equipment is one of the inevitable means for the national taxation department to realize the informatization of taxation management. As one of the tax control equipment, the tax controller is an electronic device that realizes the tax control function in cooperation with computer
[Microcontroller]
Application of μPSD32xx MCU Based on 51 Core in Tax Controller
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号