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

 

17. Why can't several output frequencies be changed when the microcontroller interrupt changes the frequency? What is the procedure?
#include
#define uchar unsigned char
#define uint unsigned int
uchar T,t1;
unsigned char data table[5] = {486,236,151,111,86} ;
sbit CLK=P2^3 ;
sbit EN=P2^0 ;
void init();
void main()
{
init();
}
void init()
{
EN=1;
T=0;
TMOD=0x01;
EA=1;
TR0=1;
ET0=1;
t1=table[T];
TH0=(65536-t1)/256;
TL0=(65536-t1)%256;
}
void timer0() interrupt 1
{
TMOD=0x01;
EA=1;
TR0=1;
ET0=1;
t1=table[T];
TH0=(65536-t1)/256;
TL0=(65536-t1)%256;
CLK=~CLK;
}
Answer:
You only assign 0 value to T, how can the frequency change? You just need to add a T assignment statement in the main function, for example: while(T){T--;delay1s();}

18. How to write a microcontroller interrupt program?
Answer:
Standard form:
void function name (void) interrupt n using m
{function body statement}
n ---- interrupt number
m----- working register group number to be used

19. I want to know how the microcontroller buzzer music program interrupt responds? How to get to the interrupt program from the main program? What are the specific steps? Thank you!
#include
sbit speaker = P1^5;
unsigned char timer0h, timer0l, time;
//--------------------------------------
//The single-chip crystal oscillator adopts 11.0592MHz
// Frequency-half-cycle data table high eight bits This software saves a total of 28 frequency data of four octaves
code unsigned char FREQH[] = {
0xF2, 0xF3, 0xF5, 0xF6, 0xF7, 0xF8, //Bass 1234567
0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i
0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //Treble 234567
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //Super high pitch 1234567
// Frequency-half-cycle data table lower eight bits
code unsigned char FREQL[] = {
0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //Bass 1234567
0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i
0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, ​​//High pitch 234567
0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //Super high pitch 1234567
//--------------------------------------
//There is only one good mother in the world. To play different music, just modify this data table
code unsigned char sszymmh[] = {5,1,1,5,1,1,6,1,2,5,1,2,1,2,2,7,1,4,5,1,1,5,1,1,6,1,2,5,1,2,2,2,1,2,4,
5,1,1,5,1,1,5,2,2,3,2,2,1,2,2,7,1,2
};
//--------------------------------------
void t0int() interrupt 1 //T0 interrupt program, control the tone of pronunciation
{
TR0 = 0; //Close T0 first
speaker = !speaker; //Output square wave, pronunciation
TH0 = timer0h; //Next interrupt time, this time, Control the pitch
TL0 = timer0l;
TR0 = 1; //Start T0
}
//--------------------------------------
void delay(unsigned char t) //Delay program, control the length of pronunciation
{
unsigned char t1;
unsigned long t2;
for(t1 = 0; t1 < t; t1++) //Double loop, delay t half beats in total
for(t2 = 0; t2 < 8000; t2++); //During the delay period, you can enter T0 interrupt to pronounce
TR0 = 0; //Turn off T0 and stop pronouncing
}
//--------------------------------------
void song() //Play a note
{
TH0 = timer0h; //Control the tone
TL0 = timer0l;
TR0 = 1; //Start T0, and T0 outputs a square wave to pronounce
delay(time); //Control the time length
}
//--------------------------------------
void main(void)
{
unsigned char k, i;
TMOD = 1; //Set T0 timing working mode 1
ET0 = 1; //Open T0 interrupt IE=0x82;
EA = 1; //Open CPU interrupt
while(1) {
i = 0;
time = 1;
while(time) {
k = sszymmh[i] + 7 * sszymmh[i + 1] - 1;
//The i-th note is a note, The i+1th is which octave
timer0h = FREQH[k]; //Read the frequency value from the data table
timer0l = FREQL[k]; //Actually, it is the time length of the timing
time = ssz
ymmh[i + 2]; //Read the time length value
i += 3;
song(); //Send a note
}
}
}
Answer:
Just look at the main() function. First, define k and i, and then define the type of interrupt (the program uses the timer interrupt). This timer is a bit special. Its function is to define the frequency. The smaller the frequency interval, the higher the pitch, and vice versa. This frequency is the reciprocal of time, so the larger the TH value, the higher the pitch; the smaller the TL value, the lower the pitch. Then go down, while(1) means waiting for the interrupt. The timer interrupt in this program does not set the initial value, so the interrupt is almost not waiting and is triggered all the time (if there is a waiting time, the music will not be connected). In summary: this timer interrupt completes two tasks: 1. Make the MCU trigger all the time (waiting time is almost 0); 2. Control the playing frequency of the notes.
The while(time) loop is the operation of assigning values ​​to the played notes.

20. When should the MCU interrupt enter?
Answer:
It depends on whether the interrupt is an external interrupt, a timer or a serial port interrupt.
If it is an external interrupt, it is when p3.2 and p3.3 detect the level change of these two ports (assuming that these two ports are connected to a key, then when the key is pressed, it indicates that an interrupt is generated), and then jump to the interrupt program execution.
If it is a timer interrupt, there is an interrupt flag TFx (x represents 0 or 1). For example, if you set a 1S timer program, you use 50ms as the base, generate 1s time 20 times, and then when 50ms is over, the flag bit changes, and the timer interrupt program is executed! The
serial port interrupt also has a flag bit. After receiving or sending data, the flag bit changes, and then the interrupt is executed!

 

21. When is the AD interrupt of the PIC microcontroller turned on? How should we understand the AD interrupt?
Answer:
AD analog-to-digital conversion is to sample the analog voltage value and then convert it into a digital signal. This sampling and conversion takes time. It is not possible to read the digital signal data as soon as
AD is turned on. Generally speaking, the time is only a few microseconds to a few hundred microseconds (depending on the settings). If the microcontroller has no other work, you can use a loop waiting method to wait for the AD conversion to end (the DONE bit will be set after the conversion). But if your microcontroller has other work, there is no need to spend time waiting for it. You can turn on the AD operation and continue to execute other programs. After the conversion is completed, the AD interrupt can temporarily disconnect the existing hype and read the AD data in. This is the role of the AD interrupt.

When (under what circumstances) do the five interrupts of the 2251 microcontroller execute the program inside!
Answer:
External interrupt 0: P3.2 port has a low level (IT0=0)/falling edge (IT0=1).
External interrupt 1: P3.3 port has a low level (IT1=0)/falling edge (IT1=1).
Timer 0 interrupt: When timer 0 counts to FFFF and overflows
Timer 1 interrupt: When timer 1 counts to FFFF and overflows
Serial port interrupt: When the serial port receives a frame or sends a frame of data, an interrupt will be generated.
You can look up TCON and SCON on the Internet. What conditions change the value of the interrupt flag? Then it will enter the interrupt service program.

23. In the 51 single-chip microcomputer, if the interrupt function is relatively long and the interrupt is triggered halfway through execution, will the program stop and execute from the beginning, or respond after the execution is completed?
Answer:
In the 51 single-chip microcomputer, interrupts are divided into high and low priorities. High-priority interrupts can interrupt low-priority interrupts.
But interrupts of the same level cannot interrupt interrupts of the same level! No matter how long the interrupt function is, if the interrupt occurs again halfway through execution, it will still have to wait until the interrupt function is executed and another main program instruction is executed before entering the interrupt again.
However, if the host happens to modify the interrupt to a high priority in this low-priority interrupt service program, then if the interrupt function is relatively long and the interrupt is triggered halfway through execution, the interrupt function will be executed again from the beginning (interrupt nesting). This is because, except for the serial port interrupt, the interrupt flag of other interrupts has been cleared before the CPU responds to the interrupt and the program enters the interrupt function.
In addition, the serial port interrupt of 51 is special because the software needs to clear the serial port interrupt flag. Therefore, as long as the serial port interrupt flag is not cleared, the above interrupt nesting will not occur.

24. Find the 51 microcontroller program, two counters, mainly the function name and initialization settings of the interrupt function.
Answer:
void into_into() interrupt 1 Timer 0 interrupt entry function
{
. . . . Interrupt service program . . . .
TH0=0; //
TL0=0; // Reassign T0 value
}
void into_into() interrupt 3 Timer 1 interrupt entry function
{
. . . . Interrupt service program . . . .
TH1=0;//
TL1=0;// Re-assign value to T1
}
void to_to()
{
TMOD=0x11; //Timer T0 and T1 work mode 1
TH0=0;//
TL0=0;// Initialize T0
TH1=0;//
TL1=0// Initialize T1
TR0=1;// Start counting
ET0=1;// Enable T0 interrupt
TR1=1;// Start counting
ET1=1;// Enable T1 interrupt
EA=1; // Enable general interrupt
}
void main()
{
INIT_T0(); //Timer interrupt initialization
while(1)
{
...........
}

25. I would like to ask a question about microcontroller interrupts: For example, a pulse comes and the interrupt starts, but when the program in the interrupt is halfway executed, another pulse comes. At this time, does the program in the interrupt start from the beginning or continue? What
I mean is that the program is just an interrupt A, a pulse comes, A executes, and when A is halfway executed, another pulse comes, notifying A to execute. Should A be executed from the beginning or ignored?
Answer:
It depends on the specific situation, because different microcontrollers have subtle differences in interrupt mechanisms, and you need to check their information.
Generally speaking, when an interrupt source requests an interrupt, it is a one-time "interrupt registration" for the CPU. If the interrupt is not responded to at that time because the conditions are not met (for example, the CPU is "disabling interrupts", that is, "interrupt enable" is not turned on), the registration information is still there. In this way, once the interrupt enable is turned on in the future, it will still respond, just a little later.
After the interrupt is responded, there must be a way to eliminate this "interrupt registration". The function of some CPUs is: as long as the interrupt is responded, the registration is automatically eliminated. Some CPUs cannot automatically clear the registration, and the operation of "clearing interrupt registration" must be programmed in the interrupt service program, otherwise, once the interrupt enable is turned on, it will interrupt again.

In most current microcontrollers, the interrupt controller and the CPU are in the same chip, which can automatically eliminate the interrupt registration. In the past, many types of CPUs were equipped with an interrupt controller on another chip, so they naturally could not be automatically eliminated.

Some CPUs also have another kind of "unregistered" interrupt request. It must be kept continuously applied by the outside world (the device that issued the interrupt request), and when the interrupt is responded to, it will try to notify the device to cancel the application (for example, send an output signal in the interrupt service program).

In most CPUs, once the service program is entered after responding to the interrupt, the "interrupt enable" is turned off. Therefore, if the next interrupt request comes at this time, it cannot be responded immediately, and can only be registered, and can only be responded to when the interrupt is "opened" later. If the programmer wants to be able to "nest interrupts" (that is, enter another interrupt service program in the middle of the execution of an interrupt service program), it is necessary to program the "open interrupt" operation in the service program.

In principle, "nested interrupts" allow "nesting themselves", that is, the execution of an interrupt service program is interrupted in the middle and enters the same interrupt service program as itself, and executes it from beginning to end. After the end, it returns to the point where it was previously interrupted and continues to execute the second half of the service program. What effect this situation will have is something that programmers need to consider.

Some CPUs also have a "priority" mechanism, which can prohibit other interrupts with a lower priority than itself from interrupting itself in a certain level of interrupt service program. At the same time, it also provides programmers with the flexibility of "giving up priority" and "modifying priority".
The interrupt requests with lower priority that are temporarily "shielded" by the priority mechanism are still registered, and can be responded to after the high-priority interrupt ends.

However, it should be noted that in most CPUs, only one "interrupt registration" can be registered. In other words, before the number registered by the previous interrupt application is cleared, the next interrupt application comes, then the second registration cannot be registered.

However, in some processors, interrupt registration may be divided into several levels: one level inside the CPU, and another level of "preparatory registration" for each specific device in the periphery, which is more complicated. In addition,

a few more words: As mentioned above, programmers can decide whether your interrupt service program allows or does not allow "nesting".

If not allowed, you can use the method of turning off interrupts, or use the priority mechanism to shield the second interrupt request from the same interrupt source.
In this way, the second interrupt will not be responded. But it can still register a number (as long as it occurs after the previous interrupt registration number has been cleared). Then, when the interrupt service program ends, it will generally open the interrupt and release the priority mask. Then, the second interrupt request will be responded to, and the interrupt service program will be executed again.

If "nesting" is allowed, it will be as I said above:
the execution of an interrupt service program is interrupted in the middle and enters the same interrupt service program as itself, and executes it from beginning to end. After the end, it returns to the point where it was previously interrupted and continues to execute the second half of the service program.

[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 11:00

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号