30 Examples of MCU Interrupt Problems and Their Solutions

Publisher:熙风细雨Latest update time:2022-05-28 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. The MCU external interrupt INT0 is triggered by a falling edge. When the interrupt is triggered, the CPU executes the interrupt program. What if another same falling edge interrupt signal comes from INT0 before the program of this interrupt is executed? How will the CPU handle it? What if it is a timer interrupt? What if it is a serial port interrupt? Please explain


Answer: Another INT0 signal will not be executed. The same priority will not interrupt the interrupt being executed.
1. If a high-priority interrupt comes, it will interrupt the low-priority interrupt being executed and execute the high-priority interrupt. The
default interrupt priority of the 51 microcontroller (the IP register is not set at this time) is:
external interrupt 0 > timer/counter 0 > external interrupt 1 > timer/counter 1 > serial interrupt;
when several interrupts arrive at the same time, the high-priority interrupt will be serviced first.
For example: when the counter 0 interrupt and external interrupt 1 (priority counter 0 interrupt > external interrupt 1) arrive at the same time, the interrupt service function of timer 0 will be entered; but when the interrupt service function of external interrupt 1 is being served, no interrupt can interrupt it at this time, including the external interrupt 0 counter 0 interrupt with a higher logical priority than it.
The interrupt priority control register IP of the 51 MCU can set the default interrupt priority to high or low.
For example, the default is external interrupt 0 > timer/counter 0 > external interrupt 1 > timer/counter 1 > serial interrupt;
now set timer 1 and serial interrupt as high priority and others as low, then interrupt 0 will be interrupted by timer 1 or serial interrupt when it is executed. If the two high-priority timer/counter 1 and serial interrupt are responded at the same time, they will naturally queue up and execute timer 1 interrupt first and then serial interrupt.

2. MCU interrupt problem, why is interrupt 3 not executed, what is wrong with the whole program?
#include
#define uint unsigned int
#define uchar unsigned char
sbit p1_0=P1^0;
sbit p1_1=P1^1;
sbit p1_2=P1^2;
sbit p1_3=P1^3;
sbit p1_4=P1^4;
sbit p1_5=P1^5;

uchar PWM_T1 = 0;
uchar PWM_T2 = 0;
uint i,m;
void delay(uint z)
{
for(i=z;i>0;i--)
for(m=0;m<110;m++);
}
void PWM_value_left(int pwm_set)
{
PWM_T1=pwm_set;
}
void PWM_value_right(int pwm_set)
{
PWM_T2=pwm_set;
}
void main(void)
{
bit flag = 1;
uint t ++ ; if
(
t == 10 ) { t = 0 ; p1_0 = 1 ; p1_1 = 0 ; } if ( PWM_T1 == t ) P1 = P1 & 0xfc ;​​ } timer1() interrupt 3 { static uint t1 ; t1++; if(t1==10) { t1=0; p1_2=1; p1_3=0; } if(PWM_T2==t1) P1=P1&0xf3; } Answer: There is no main loop, the program runs away without waiting for interrupt 3 to run once! ! ! In void main(void) { //...your program //Add an infinite loop here, waiting for the interrupt while(1) { ; } } Moreover, the interrupt flag must be cleared in the interrupt response function (yours does not have it)! 3. Can you please help me check if there is any problem with the interrupt of the 51 microcontroller C program I wrote? After executing the interrupt, the main program cannot be executed. Note: P3.2 port is always connected to ground. The program is as follows: #include sbit dula=P2^6;




































































0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; void delay( uint z ) ; void main ( ) { EA = 1 ; EX0 = 1 ; IT0 = 0
; wela=1; P0=0xc0; wela=0; while(1) { for(num=0;num<16;num++) { dula=1; P0=table[num]; dula=0; delay(1000); } } } void delay(uint z) { uint a,b; for(a=z;a>0;a--) for(b=110;b>0;b--); } void exter0() interrupt 0 { uint c; for(c=0;c<25000;c++); d0=0; for(c=0;c<25000;c++); d0=1; for(c=0;c<25000;c++); d1=0; for(c=0;c<25000;c++); d1=1; for(c=0;c<25000;c++); d2=0; for(c=0;c<25000;c++); d2=1; for(c=0;c<25000;c++); d3=0; for(c=0;c<25000;c++); d3=1; for(c=0;c<25000;c++); d4=0; for(c=0;c<25000;c++); d4=1; for(c=0;c<25000;c++); d5=0; for(c=0;c<25000;c++); d5=1; for(c=0;c<25000;c++); d6=0; for(c=0;c<25000;c++); d6=1; for(c=0;c<25000;c++); d7=0; for(c=0;c<25000;c++); d7=1; } Answer: IT0=0;//Low level trigger, as long as the MCU detects a low level, it will trigger an interrupt. If your P3.2 is always grounded, it is always low level, then the interrupt will be executed continuously, and of course it will not return to the main program. Change to IT0=1;//Falling edge trigger, the MCU detects a high level to level jump, and triggers an interrupt. Even if P3.2 is always grounded, it will only be triggered once. After the interrupt is executed, it will return to the main program. 4. Why can't my MCU execute the entire program completely? Can anyone tell me! I just can't execute 3 times of light and dark! 2010-10-20 21:40 Asked by: 3865203bin3 | Reward points: 10 ORG 0000H AG:MOV A,#11111110B AG1:RL A MOV P0,A ACALL DELAY DJNZ R0,AG1 ACALL DELAY MOV A,#11111111B MOV P1,A ACALL DELAY MOV P0,#00000000B ACALL DELAY MOV P0,#11111111B ACALL DELAY MOV P0,#00000000B ACALL DELAY MOV P0,#11111111B ACALL DELAY SJMP AG DELAY:MOV R3,#10 D3:MOV R2,#200 D1:MOV R1,#250 D2JNZ R1,D2 DJNZ R2,D1 DJNZ R3,D3 RET END



















































































































I want to execute the light-on and light-off 3 times after the running lights are on! But it won't jump to light-on and light-off 3 times~~

Answer:
R0 is not initialized! In addition, it is recommended not to use the first 2B addresses, because the 51 interrupt vector is in this space. It is recommended to start from 0030H. The following program is successfully simulated in keil4.
ORG 0000H
AJMP AG
ORG 0030H
AG:MOV A,#11111110B
MOV R0,#8
AG1:RL A
MOV P0,A
ACALL DELAY
DJNZ R0,AG1
ACALL DELAY
MOV A,#11111111B
MOV P1,A
ACALL DELAY
MOV P0,#00000000B
ACALL DELAY
MOV P0,#11111111B
ACALL DELAY
MOV P0,#00000000B
ACALL DELAY
MOV P0,#11111111B
ACALL DELAY
SJMP AG
DELAY:MOV R3,#10
D3:MOV R2,#200
D1:MOV R1,#250
D2JNZ R1,D2
DJNZ R2,D1
DJNZ R3,D3
RET
END

5. STC89C52 controls the lighting and extinguishing of the digital tubes of P1.0 and P1.1 through two external interrupts P3.2 and P3.3. Keil C
explains: through external interrupt 0 [P3.2], the digital tube of P1.0 is on, and after the interruption, the digital tube is off; then through external interrupt 1 [P3.3], the digital tube of P1.1 is on, and after the interruption, the digital tube is off; in short, one interrupt controls only one digital tube, and there is no necessary functional connection between interrupts. The programming environment is keil C51. P1.0 and P1.1 ports are connected to ordinary LED small lights, and digital tubes = LED small lights.

The following is the program I wrote, which is to control one digital tube with one interrupt, but when there are two interrupts, I am at a loss.
#include
sbit D1=P1^0;
void main()
{
D1=1;
EA=1;
EX0=1;
}
void exter() interrupt 0
{
D1=0;
}
I need an expert to help me modify the program and change it to the one with two interrupts. The functional requirements are all written above~ [I hope there will be program comments] 3Q, I will simulate it in Keil~~o()^))oAnswer

:
/*Module low level is valid, external interrupts 0 and 1 are triggered by low level*/
#include "reg52.h"
void delay( char i)
{
unsigned char t;
while(i--)
{
for(t=0;t<108;t++);
}
}
void INT0_ROUTING() interrupt 0//External interrupt 0 subroutine
{
P0=0xfe;//LED0 lights
upwhile((P3|0xfb)==0xff);//Wait for external interrupt 0 (P3^2 is released)
delay(10);//Delay debounce
P0=0xff;//LED0 turns off
}
void INT0_ROUTING() interrupt 2
{
P0=0xfd;//LED1 lights
upwhile((P3|0xf7)==0xff);//Wait for external interrupt 1 (P3^3 is released)
delay(10);//Delay debounce
P0=0xff;//LED1 turns off
}

void main()
{
EA=1;//Interrupt master switch
EX0=1;//External interrupt 0 is on
EX1=1;//External interrupt 1 is on
/*Default low level trigger*/
while(1);//Infinite loop to prevent running away
}

 

6. Microcontroller interrupt problem. For some reason, the following program only interrupts once and then stops responding.

#include // Header file containing 51 MCU register definitions
#define uint unsigned int
sbit key1=P1^4;
sbit key2=P1^5;

void delay1ms(uint i)
{
uchar j;
while(i--)
{
for(j=0;j<125;j++) //1ms benchmark delay program
{
;
}
}
}
void init()
{
EA=1; //Open general interrupt
ES=1; //Open serial port interrupt
TMOD= 0x21; //Timer 1 timing mode 2, timer 1 works in 8-bit automatic reload mode, used to generate baud rate
SCON = 0x50; //Set serial port working mode 1 to allow reception
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1= 0xfd; //11.0592M baud rate 9600
TL1= 0xfd;
PCON =0x00; // baud rate does not double
TR1= 1; // start timer T1
TR0=1; // start timer T0
ET0=1; // turn on T0 interrupt
}
void key()
{
if(key2==0)
P0=0x3f;
delay1ms(5000);
P0=0xf3;
}
void mainxh()
{
while(1)
{
key();
P0=0x32;
}
}
void keybreak()
{
P0=0xf1;
delay1ms(5000);
P0=0x1f;
mainxh();
}
void main(void)
{
init();
mainxh();
}
void Time0(void) interrupt 1
{
TH0=(65536-50000)/256; // re-initialize the high 8 bits of timer T0
TL0=(65536-50000)%256; //Re-initialize the high 8 bits of timer T0
if(key1==0)
keybreak();
}

After power-on, this program displays 0x32 on port P0; pressing key2 displays 0x3f; key1 is used for interruption, and every 20ms it detects whether key1 is pressed. If so, port P0 displays 0xf1.

Answer:
ORG 0000H AJMP MAIN ORG 0001H LJMP INT_0 ORG 30H MAIN:MOV SP,#8FH MOV P0,#0FFH MOV P3,#0FFH SETB IT0 SETB EA SETB EX0 LJMP START START: MOV A,#10000000B LOOP: MOV P0,A RLC A LCALL DELAY LCALL DELAY LJMP LOOP LJMP START;
This sentence is redundant and will not be executed at all INT_0: PUSH ACC;
Since A is set to 10 in interrupt p, it is meaningless to shift A after interrupt 5/7 returns, o A is always 10, and it does not mean that it can only interrupt once.
In addition, it is not clear what interrupt key1 is. It seems to be keyboard scanning.
while(1)
{
key();
P0=0x32;
}
has entered an infinite loop, so it cannot jump out and only interrupts once.

7. A novice learning avr microcontroller ATmage 128 encountered a problem, the interrupt program was ignored, and the reason could not be found.
When avr studio 4 software simulation, the compilation passed, but the interrupt program was ignored in the compilation information column. It was also found that the interrupt program was not executed during software simulation. I don't know where the problem is. I use avr studio 4 ATmage 128 microcontroller.

The program is as follows

#include
#include
void main() //TC0 overflow interrupt is used to control the eight-bit LED to flash for one second
{
PORTE = 0xFF; //LED off port high level bit off
DDRE = 0xFF;
MCUCR |=(1<sei(); //Turn on global interrupt
TIMSK|=(1<TCNT0 =155; //Timer initial value
TCCR0 |= (1 << CS01); //8 division
while (1);

}

volatile unsigned int j =0;
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 = 156; //Set initial value
j++;
if(j <= 5000) //Execute LED level flip after 5000 interrupts
PORTE ^= 0xFF; //LED level flip

}


../lesson2.c:18: warning: ignoring #pragma interrupt_handler timer0_ovf_isr

The above is the program written. There are also words in the compilation information column.

A:
No, your sentence #pragma interrupt_handler timer0_ovf_isr:17 is the way to write interrupts in ICCAVR compiler software, and looking at your header files #include and #include , it should be the way to write interrupts using GCCavr compiler software installed by avr studio. You add the interrupt to
SIGNAL(SIG_OVERFLOW0)
{
TCNT0 = 156; //Set initial value
j++;
if(j <= 5000) //Execute LED level flip after 5000 interrupts
PORTE ^= 0xFF; //LED level flip

}
Look, remember, this is the way to write GCCAVR compiler software

8 I just learned C51 microcontroller and wrote a level-triggered interrupt program. I don't know why it is the same as the jump edge. Can you help me take a look.
#include
#define uchar unsigned char
#define uint unsigned int
sbit d1=P1^0;
sbit dula=P2^6;
sbit wela=P2^7;
void delay(uint z);
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void main()
{
EA=1;
EX0=1;
IT0=0;
while(1)
{ d1=1;
dula=1;
P0=table[1];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(500);

dula=1;
P0=table[2];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(500);

dula=1;
P0=table[3];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(500);

dula=1;
P0=table[4];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(500);

dula=1;
P0=table[5];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(500);

dula=1;
P0=table[6];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
delay(500);


}
}
void delay(uint z)
{
uint x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
void enter() interrupt 0
{
d1=0;
}

Answer: In this program, you set IT0=0, which means it is a low-level trigger. Therefore, as long as the P3^2 port is always at a low level, the main program stops, so the LED lights up. If the P3^2 port changes to a high level, the main program continues, and the LED goes out. Another situation is when IT0=1, it is a negative jump trigger, that is, when the P3^2 port detects a jump from a high level to a low level, it triggers an interrupt, lights up the light in the interrupt function, and immediately interrupts. When d1=1 is executed, the light goes out. The phenomenon you see is that the light flashes until a negative jump is detected again, and the light flashes again. The phenomena of the two triggering methods are different, if your hardware is fine. You can write the interrupt function as d1=!d1 and try it.

9. In the C51 microcontroller, how does the interrupt service program cancel the low level on the interrupt pin?
What I use is that the first microcontroller outputs a low level to the P3^2 of the second microcontroller. The second microcontroller is a level-triggered interrupt.
Low-level trigger mode: The low level is required to be maintained until the CPU actually responds. In order to prevent the CPU from responding to the interrupt again, the low level on the interrupt pin should be removed in the interrupt service program. How to cancel it? How to write it in the interrupt service program? Is it okay to write P3^2=1 directly?
Answer:
Who wrote the program for the first microcontroller? If it is the original poster, then it is easy. The
second microcontroller completes the interrupt function. Before exiting, it can send a pulse back to the first microcontroller;
when the first microcontroller receives this pulse, it should cancel the interrupt request signal sent to the second microcontroller.
----
In addition, if the time to complete the interrupt can be calculated, the request signal sent by the first microcontroller should not exceed this time, and the request signal should be canceled in time and automatically. The
request signal sent by the first microcontroller should not be too short, and it should be detectable by the other party.

10. The program is as follows. The effect I want is that the left motor rotates for 1 second, and the yellow light is on, and the right motor rotates for 1 second, and the blue light is on, and so on. However, after using this program, the left motor rotates> the right motor> the left motor> and then it keeps rotating on the left without switching. Can anyone help me solve this problem? I would be very grateful! !
#include
sbit m=P2^0;
sbit b=P2^6;
sbit y=P2^7;
unsigned char count;
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1;
EA=1;
count=0;
m=!0;
b=!1;
y=!0;
while(1)
{
if(TF0==1)
{
count++;
if(count==20)
{
m=0;
b=1;
y=0;
}
if(count==40)
{
m=!0;
b=!1;
y=!0;
}
TF0=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
}
}
}


Answer
#include
sbit m=P2^0;
sbit b=P2^6;
sbit y=P2^7;
unsigned char count;
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
count=0;
m=!0;
b=!1;
y=!0;
while(1) {
if(TF0==1) {
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TF0=0;
count++;
if(count==20) {
m=0; b=1; y=0;
}
if(count==40) {
count=0; //Add this sentence.
m=!0; b=!1; y=!0;
}
}
}
}!
Use T0 to time 50ms, overflow 20 times, overflow 40 times, which represent specific time respectively. After overflow 40 times, the overflow times should be counted from the beginning, so count=0; should be here. The original program of the OP lacks count=0;, so it will continue to increase until 65536, and then automatically return to 0. In this way, time is difficult to control.

[1] [2] [3] [4]
Keywords:MCU Reference address:30 Examples of MCU Interrupt Problems and Their Solutions

Previous article:Introduction to 51 MCU - Use of interrupts
Next article:20 Questions and Solutions on MCU Delay Problems

Recommended ReadingLatest update time:2024-11-15 08:06

Intelligent temperature control fan design based on 51 microcontroller
Specific implementation functions: It is composed of 51 microcontroller + DS18B20 temperature sensor + common positive four-digit digital tube + fan + independent button + DC power supply . Specific functions: 1. Use DS18B20 temperature sensor to measure temperature and use digital tube to display it in real time;
[Microcontroller]
Intelligent temperature control fan design based on 51 microcontroller
SAM4E MCU Tour - 10. UART and MCK PLL
To use a higher baud rate, a higher peripheral clock frequency is required. At this time, a phase-locked loop (PLL) is needed. The phase-locked loop can divide and increase the frequency of the input clock and then output it. The phase-locked loop that MCK can use is PLLA, and the input clock of PLLA is MAINCK. This s
[Microcontroller]
SAM4E MCU Tour - 10. UART and MCK PLL
Principle of single chip digital tube display
  A small light is a simple LED that can only express simple information by turning on and off. In this class, we are going to learn a device that can express more clearly, the digital tube.   1. Basic introduction of digital tube   First, let me show you a schematic diagram, as shown in Figure 1. Figure 1 Sche
[Microcontroller]
Principle of single chip digital tube display
Original work LED meteor shower lamp (51 single chip microcomputer program code) product sharing!
Recently, our company has produced a meteor shower lamp! Now we share it with you!    1. Picture sharing:    2. PCB file sharing:      3. Schematic diagram sharing:   Four,    There are only 5 light tubes made above. If you need to add more light tubes, just slightly change the following pr
[Microcontroller]
Original work LED meteor shower lamp (51 single chip microcomputer program code) product sharing!
MCS-51 microcontroller assembly language: What are the arithmetic operation instructions?
Addition instruction ADD   A,  Rn          ; A←(A)+(Rn) ADD   A,  @Ri         ;A←(A)+((Ri)) ADD   A,  direct        ;A←(A)+(direct) ADD   A,  #data       ;A←(A)+#data Add with carry instruction ADDC   A,  Rn        ; A←(A)+(Rn)+(Cy) ADDC A, @Ri ;A←(A)+((Ri )) +(Cy) ADDC   A, direct        ;A←(A)+(direc
[Microcontroller]
Design of household smoke alarm based on MSP430 microcontroller
0 Introduction     In recent years, there are 6 to 7 million fires around the world each year, of which residential fires account for more than 80%. According to a statistical report "American Home Fire Smoke Alarms" from 2003 to 2006, it was found that for every 1,000 reported fires, if there are smoke alarms and wet
[Microcontroller]
Design of household smoke alarm based on MSP430 microcontroller
51 MCU - Optimize the code that does not support continuous pressing again
1. Still need to eliminate shaking You can see that in the code of the key that does not support continuous pressing in lectures 6 and 7, the infinite loop has "delay_ms(2);". Because most of the time the main loop has to do a lot of things, we think that this 2ms delay is the time consumed by many complex programs to
[Microcontroller]
51 MCU - Optimize the code that does not support continuous pressing again
Interrupt Architecture of C51 Single Chip Microcomputer
The interrupt system of 80C51 has 5 interrupt sources (8052 has 6), 2 priorities, and can realize two-level interrupt nesting. The structure of the interrupt system of MCS-51 series microcontroller is as follows: Special registers related to the interrupt system: 1) Interrupt enable control register (IE)------Contro
[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号