Course content: External interrupt reference-------MCU Tutorial Network example ← Link
MCU in the autonomous operation of the time is generally in the execution of a dead loop program, in the absence of external interference (input signal) when it is basically in a closed state. such as an electronic clock, it will be on time, minutes, seconds of the law to run autonomously and through the output device (such as the LCD screen) to display the time. It does not require external intervention when it does not need to be tuned, and operates autonomously and in a closed manner. If the clock is accurate enough and does not power down, it may be in this closed state of operation. But things tend not to be so simple, when the clock just power up, or the clock needs to be recalibrated, or even the clock is brought to different time zones, it is necessary to re-adjust the clock, then the clock must have a calibration function. Therefore, SCM system is often not a simple closed systems, it sometimes just needs external intervention, which is the root cause of external interruption.
In fact, in the second example demonstration, we have already given an example of key input, but the method used at that time was not an external interrupt, but a program query. Here's an example of changing the blink speed by pressing a key in the second example using an external interrupt method (the second example). The circuit structure and wiring are unchanged, only the program is changed to the following form.
#include <iom16.h>
unsigned int t=500; Define a global variable T and set the initial value to 500 times
=========== Delay sub-function, approximately 1ms============= at 8MHz Crystal oscillator
void Delay_ms (unsigned int k)
{
unsigned int i,j;
for (i=0;i<k;i++)
{
for (j=0;j<1140;j++)
;
}
}
============ Main function ==================================
void Main (void)
{
DDRB = 0xFF; Set Port B for the output direction
PORTB = 0xFF; Set the output of port B to full high
DDRD = 0x00; Set port D for input direction
PORTD = 0xFF; Set Port D as internal pull-up mode, high status when no signal input
MCUCR = 0x0A; Set INT0, INT1 to trigger for falling edge
GICR = 0xC0; Enable INT0, INT1 interrupts
Sreg = 0x80; Total interruption of the ability
while (1)
{
PORTB = 0x55; Allow the LED on port B to display 01010101
Delay_ms (t); Delay T MS
PORTB = 0xAA; Allow the LED on port B to display 01010101
Delay_ms (t); Delay T MS
}
}
============ Interrupt function (external 0) ==========================
#pragma vector = Int0_vect
__interrupt void Int0_server (void)
{
t = 100; Set the value of T to 100 times
}
============ Interrupt function (external 1) ==========================
#pragma vector = Int1_vect
__interrupt void Int1_server (void)
{
t = 500; Set the value of T to 500 times
}
Compile the above program and download to the microcontroller, you can see the result is exactly the same as in the second example. The following is to analyze the keyboard interrupt program principle.
Before analyzing the program, let's look at what is called an "external interrupt". As previously described, in the absence of interruptions, the microcontroller program in the closed state of autonomous operation, but if in a moment to respond to an external event (such as a button is pressed), then need to use an external interrupt. Specifically, the external interrupt is on a single-chip computer pin, due to external factors resulting in a level change (such as from high to low), and by capturing this change, the microcontroller internal self-executing program is temporarily interrupted, to execute the corresponding interrupt handler, After execution, return to the original interrupted place to continue to carry out the original procedure. The level change on this pin applies an external interrupt event, and the pin that can request an external interrupt is the trigger pin for the external interrupt. In the above example, you can see two keys S1, S2 was received ATMega16 PD3 and PD2 pin, which is the microcontroller's two external interrupts (INT1 and INT0) trigger pin (second function). When the key is not pressed, both pins are high (performed Portd=0xff), when the key is pressed, the pin level jumps to low level, if the microcontroller is set to allow interrupt request, it will trigger an external interrupt event, thereby transferring to execute the Interrupt service program. Once you understand the process, you can then analyze the program.
After the program executes, the main program keeps running the dead loop within the while (1), allowing the LEDs to blink alternately with the initial value of the t=500ms until an external interrupt is interrupted. Assuming that the key S2 is pressed at a certain moment, when the level of the pin PD2 is suddenly lowered and an external interrupt 0 (INT0) is applied, the program goes to the Interrupt service program (that is, the __interrupt void int0_server (void) function) that executes the external interrupt 0. At this point the value of the global variable T is re-assigned to the function of 100 (that is, the delay is 100ms), and then go back to the main function in the while (1) to continue execution, so the speed of the LED flashing faster.
The watcher can see that if there is no interruption to call the interrupt service subroutine, there is no statement in the main program to mobilize it. This means that if there is no external interrupt, the interrupt service subroutine (that is, the __interrupt void int0_server (void) function) is never executed. This also shows that the interrupt service subroutine is a special kind of subroutine that cannot be called by the main program and can only be called by the interrupt request. Therefore, the interrupt service subroutine has its fixed format and notation. The format of the interrupt service subroutine under the IAR is not exactly the same as in different compilation systems.
#pragma vector = Int0_vect
__interrupt void Int0_server (void)
{
Interrupt Service Program code
}
The above is a fixed format, except the italic part, the rest of the parts cannot be changed. The Int0_vect in the Italic section represents the broken vector number, with different interrupt names (prototypes in the header file iom16.h). The Int0_server in the Italic section is the name of the interrupt function, which is defined by the developer itself. Although it can be customized, but the name is to get "see the name of understanding," so that you know what is interrupted service.
Precautions:
1, the Interrupt Service sub-program can not be qain, the solution can refer to the above example
2, Code standard format is very important, to develop a good habit of writing code, otherwise you will die in the wrong place!!!! Details link → point here
2. Use of multi-file global variables
A) first create an. h file with the extern data type variable name such as:extern intvar;
b) Add #include ". h file name" to the file in which the variable is used to assign an initial value in any. c file (The initial value can be assigned only in one file, otherwise an error is made!!!). )
Such as:
#include ". h filename"//must be double quotation marks cannot use angle brackets
unsigned int var1=58;
2016.06.05 Sunday Training Lesson Lesson II "interruption"