External Interrupt Experiment1. Learning ObjectivesExternal InterruptAdvantages and functions of external interrupts2. Hardware construction3. Experimental steps1. Open the SYSCONFIG configuration tool2. Configure pin mode3. Write the program4. Compile4. Program Analysis5. Experimental phenomenon
An interrupt is when the CPU is executing a program and there are other events that need to occupy the CPU. After receiving the interrupt request, the CPU pauses the execution of the original program and executes the interrupt function. After the interrupt function is executed, it returns to the original program and continues to execute.
An external interrupt is an interrupt triggered by an external hardware event (usually a signal from outside the microcontroller). This interrupt can interrupt the current program execution flow of the microcontroller, thereby quickly responding to external hardware events.
The triggering of external interrupts is usually associated with specific pins (GPIO pins), such as buttons, sensor signals, timer signals, etc. These pins are usually configured to detect specific signal changes (such as rising edges, falling edges, or level changes) to trigger interrupts.
Advantages:
Function:
This course does not require additional hardware equipment, and directly uses the onboard function keys on the MSPM0G3507 motherboard.
MSPM0G3507 main control diagram
Button schematic diagram
In this lesson, the PA18 button will be used to trigger the LED1 of PB2 to turn on and off, and the CPU interrupt method will be used in the software. For the configuration of the PB2 pin of LED1, please refer to the Light up the LED course.
Create a blank project in CCS.
Find and open the empty.syscfg file in the left workspace of CCS.
In SYSCONFIG, select MCU peripherals on the left, find the GPIO option and click to enter. Click ADD in GPIO to add two sets of pin peripherals, one set of LED pins and one set of button pins. For LED pins, refer to the previous Light up LED course. Here we only talk about the configuration of button pins.
Configure pin parameters, name the GPIO instance as KEY
, and select PORTA
for Port because it is connected to PA18.
Set the pin name to PIN_18
and the input mode to Input
. Set the Internal Resistor to the pull-down resistor Pull-Down Resistor
, and set the Assigned Pin to control the pin to 18.
Configure pin external interrupts. All GPIOA ports on the chip can set external interrupt functions.
Enable Interrupts enables interrupts, Interrupt Priority sets the interrupt priority to default. When multiple interrupts are triggered at the same time, they are processed according to the chip's default priority rules.
Trigger Polarity The interrupt is triggered by a rising edge. When a key is pressed, the level of the GPIO pin changes from low level (0) to high level (1). This process is called a rising edge. This means that the external interrupt will be triggered when the pin state changes from low to high.
Event Publishing channel The event publishing channel is not enabled. The event publishing channel is a mechanism that publishes interrupt events to a specified channel. Other peripherals can respond by subscribing to events on the channel.
Save the configuration in the .syscfg file using the shortcut key Ctrl+S.
According to the user manual of the MSPM0G series, we know that after the GPIO interrupt is triggered, the interrupt is released to the bus through the GRP1 line
. After the bus recognizes it, it enters the interrupt service function to execute the content.
After configuring the pin interrupt, we also need to manually write the interrupt service function of the external interrupt. Because we have enabled the pin falling edge interrupt, when our button is pressed and released, the falling edge will trigger an interrupt, and the interrupt service function will be executed after the trigger. The interrupt service function name corresponding to each interrupt is usually fixed and cannot be modified at will, otherwise the interrupt service function will not be entered correctly. The specific name of the interrupt service function is GROUP1_IRQHandler.
In the empty.c file, write the following code
x#include "ti_msp_dl_config.h"
int main(void)
{
SYSCFG_DL_init();
NVIC_EnableIRQ(KEY_INT_IRQN);//开启按键引脚的GPIOA端口中断 Enable GPIOA port interrupt for button pin
while (1)
{
}
}
void GROUP1_IRQHandler(void)//Group1的中断服务函数 Interrupt service function of Group1
{
//读取Group1的中断寄存器并清除中断标志位
// Read the interrupt register of Group1 and clear the interrupt flag
switch( DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP_1) )
{
//检查是否是KEY的GPIOA端口中断,注意是INT_IIDX,不是PIN_18_IIDX
// Check if it is the GPIOA port interrupt of KEY, note that it is INT_IIDX, not PIN_18_IIDX
case KEY_INT_IIDX:
//如果按键按下变为高电平 If the button is pressed, it becomes high level
if( DL_GPIO_readPins(KEY_PORT, KEY_PIN_18_PIN) > 0 )
{
//设置LED引脚状态翻转 Set LED pin state toggle
DL_GPIO_togglePins(LED1_PORT, LED1_PIN_2_PIN);
}
break;
}
}
If the compilation is successful, you can download the program to the development board.
uint32_t DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP group) is used to obtain the highest priority pending interrupt index in the specified interrupt group.
group: An enumeration type DL_INTERRUPT_GROUP
, used to specify the interrupt group to be checked.
This code implements a basic interrupt service function, mainly controlling the state of an LED by triggering an interrupt with a key.
After the main()
function is initialized, the GPIOA port interrupt of the key is enabled. The GROUP1_IRQHandler()
interrupt service function obtains the highest priority suspend flag in Group1, checks whether a key interrupt has occurred, and then calls DL_GPIO_readPins(KEY_PORT, KEY_PIN_18_PIN)
to read the level change of the key pin. If it becomes a high level, the LED light pin state flips.
After the program is downloaded, the state of the LED light can be controlled by the key. Press once to turn on the LED light, and press again to turn off the LED light.