TM4C123G的外部中断初始化程序
1、硬件
芯片型号:TM4C1233/123G/1237等123x系列
按键:PM3
LED:PM0
2、软件
keil5
3、程序
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "delay.h"
void PortMIntHandler(void); //PortM的中断服务函数
void main(void)
{
//配置时钟
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
//配置中断
//使能中断要使用的端口
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOM);
//GPIO注册中断
GPIOIntRegister(GPIO_PORTM_BASE, PortMIntHandler);
//PM3按键
GPIOPinTypeGPIOInput(GPIO_PORTM_BASE, GPIO_PIN_3);
//配置上拉电阻,输出电流能力2mA
GPIOPadConfigSet(GPIO_PORTM_BASE,GPIO_PIN_3,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
//下降沿触发
GPIOIntTypeSet(GPIO_PORTM_BASE, GPIO_PIN_3 , GPIO_FALLING_EDGE);
//使能PM3中断
GPIOIntEnable(GPIO_PORTM_BASE, GPIO_PIN_3);
//全局中断
IntMasterEnable();
//配置LED
//使能LED要使用的端口,我的与中断在同一端口可以省
//SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOM);
//PM0配置为输出模式
GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_0);
//PM0高电平亮LED
GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_0, GPIO_PIN_0);
while(1)
{
// TODO
}
}
void PortMIntHandler(void)
{
//清除中断标志
GPIOIntClear(GPIO_PORTM_BASE, GPIO_PIN_3);
//亮LED
GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_0, GPIO_PIN_0);
//延时
delay_ms(500);
//灭LED
GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_0,0);
//延时
delay_ms(500);
}