在学习M3时中断所遇到的问题,在网上找了些例程自己做时总进不了中断,
主程序
#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_nvic.h"
#include "hw_types.h"
#include "debug.h"
#include "gpio.h"
#include "interrupt.h"
#include "systick.h"
#include "sysctl.h"
#define PINS GPIO_PIN_5
void delay(int d)//延时函数
{
for( ; d; --d);
}
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_6MHZ);//配置系统时钟,
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//使能外设GIOPB
GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_0, GPIO_DIR_MODE_OUT);//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
// 使能 KEY所在的GPIO端口
//GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_4);
GPIOPadConfigSet(GPIO_PORTC_BASE, GPIO_PIN_4,
GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
GPIODirModeSet(GPIO_PORTC_BASE,GPIO_PIN_4,GPIO_DIR_MODE_IN);
// 设置 KEY所在管脚为输入
GPIOIntTypeSet(GPIO_PORTC_BASE, GPIO_PIN_4, GPIO_LOW_LEVEL); //低电平中断
// 设置 KEY管脚的中断类型
GPIOPinIntEnable(GPIO_PORTC_BASE, GPIO_PIN_4);
// 使能处理器中断
IntMasterEnable();
// 使能 KEY所在管脚的中断
IntEnable(INT_GPIOC); //好像程序进到这里就走不下去了
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0XFF); //因为IntEnable(INT_GPIOC);所以没有被执行了,放在它前面就可以
while(1) {
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0);//输出高电平
delay(20000);//延时
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0XFF);//输出低电平
delay(20000);//延时
}
}
// GPIOD 的中断服务函数
void GPIO_Port_C_ISR(void)
{
unsigned char ucVal;
unsigned long ulStatus;
ulStatus = GPIOPinIntStatus (GPIO_PORTC_BASE, true); // 读取中断状态
GPIOPinIntClear(GPIO_PORTC_BASE, ulStatus); // 清除中断状态,重要
if (ulStatus & GPIO_PIN_4) // 如果 KEY的中断状态有效
{
ucVal = GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_5); // 翻转LED
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5, ~ucVal);
SysCtlDelay(300000);
// 延时约 10ms,消除按键抖动
while (GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4) == 0x00);
// 等待 KEY抬起
SysCtlDelay(300000);
// 延时约 10ms,消除松键抖动
}
}
中断注册
// Enable the IAR extensions for this source file.
//
//*****************************************************************************
#pragma language=extended
//*****************************************************************************
//
// Forward declaration of the default fault handlers.
//
//*****************************************************************************
void ResetISR(void);
static void NmiSR(void);
static void FaultISR(void);
static void IntDefaultHandler(void);
extern void GPIO_Port_C_ISR(void); //***************************************************************************** //***************************************************************************** //***************************************************************************** //***************************************************************************** //***************************************************************************** //***************************************************************************** //***************************************************************************** //*****************************************************************************
//*****************************************************************************
//
// External declarations for the interrupt handlers used by the application.
//
//*****************************************************************************
//
// The entry point for the application startup code.
//
//*****************************************************************************
extern void __iar_program_start(void);
//
// Reserve space for the system stack.
//
//*****************************************************************************
static unsigned long pulStack[64] @ ".noinit";
//
// A union that describes the entries of the vector table. The union is needed
// since the first entry is the stack pointer and the remainder are function
// pointers.
//
//*****************************************************************************
typedef union
{
void (*pfnHandler)(void);
unsigned long ulPtr;
}
uVectorEntry;
//
// The vector table. Note that the proper constructs must be placed on this to
// ensure that it ends up at physical address 0x0000.0000.
//
//*****************************************************************************
__root const uVectorEntry __vector_table[] @ ".intvec" =
{
{ .ulPtr = (unsigned long)pulStack + sizeof(pulStack) },
// The initial stack pointer
ResetISR, // The reset handler
NmiSR, // The NMI handler
FaultISR, // The hard fault handler
IntDefaultHandler, // The MPU fault handler
IntDefaultHandler, // The bus fault handler
IntDefaultHandler, // The usage fault handler
0, // Reserved
0, // Reserved
0, // Reserved
0, // Reserved
IntDefaultHandler, // SVCall handler
IntDefaultHandler, // Debug monitor handler
0, // Reserved
IntDefaultHandler, // The PendSV handler
IntDefaultHandler, // The SysTick handler
IntDefaultHandler, // GPIO Port A
IntDefaultHandler, // GPIO Port B
// IntDefaultHandler, // GPIO Port c
GPIO_Port_C_ISR, // GPIO Port C
IntDefaultHandler, // GPIO Port D
IntDefaultHandler, // GPIO Port E
IntDefaultHandler, // UART0 Rx and Tx
IntDefaultHandler, // UART1 Rx and Tx
IntDefaultHandler, // SSI0 Rx and Tx
IntDefaultHandler, // I2C0 Master and Slave
IntDefaultHandler, // PWM Fault
IntDefaultHandler, // PWM Generator 0
IntDefaultHandler, // PWM Generator 1
IntDefaultHandler, // PWM Generator 2
IntDefaultHandler, // Quadrature Encoder 0
IntDefaultHandler, // ADC Sequence 0
IntDefaultHandler, // ADC Sequence 1
IntDefaultHandler, // ADC Sequence 2
IntDefaultHandler, // ADC Sequence 3
IntDefaultHandler, // Watchdog timer
IntDefaultHandler, // Timer 0 subtimer A
IntDefaultHandler, // Timer 0 subtimer B
IntDefaultHandler, // Timer 1 subtimer A
IntDefaultHandler, // Timer 1 subtimer B
IntDefaultHandler, // Timer 2 subtimer A
IntDefaultHandler, // Timer 2 subtimer B
IntDefaultHandler, // Analog Comparator 0
IntDefaultHandler, // Analog Comparator 1
IntDefaultHandler, // Analog Comparator 2
IntDefaultHandler, // System Control (PLL, OSC, BO)
IntDefaultHandler, // FLASH Control
IntDefaultHandler, // GPIO Port F
IntDefaultHandler, // GPIO Port G
IntDefaultHandler, // GPIO Port H
IntDefaultHandler, // UART2 Rx and Tx
IntDefaultHandler, // SSI1 Rx and Tx
IntDefaultHandler, // Timer 3 subtimer A
IntDefaultHandler, // Timer 3 subtimer B
IntDefaultHandler, // I2C1 Master and Slave
IntDefaultHandler, // Quadrature Encoder 1
IntDefaultHandler, // CAN0
IntDefaultHandler, // CAN1
IntDefaultHandler, // CAN2
IntDefaultHandler, // Ethernet
IntDefaultHandler, // Hibernate
IntDefaultHandler, // USB0
IntDefaultHandler, // PWM Generator 3
IntDefaultHandler, // uDMA Software Transfer
IntDefaultHandler, // uDMA Error
IntDefaultHandler, // ADC1 Sequence 0
IntDefaultHandler, // ADC1 Sequence 1
IntDefaultHandler, // ADC1 Sequence 2
IntDefaultHandler, // ADC1 Sequence 3
IntDefaultHandler, // I2S0
IntDefaultHandler, // External Bus Interface 0
IntDefaultHandler // GPIO Port J
};
//
// This is the code that gets called when the processor first starts execution
// following a reset event. Only the absolutely necessary set is performed,
// after which the application supplied entry() routine is called. Any fancy
// actions (such as making decisions based on the reset cause register, and
// resetting the bits in that register) are left solely in the hands of the
// application.
//
//*****************************************************************************
void
ResetISR(void)
{
//
// Call the application's entry point.
//
__iar_program_start();
}
//
// This is the code that gets called when the processor receives a NMI. This
// simply enters an infinite loop, preserving the system state for examination
// by a debugger.
//
//*****************************************************************************
static void
NmiSR(void)
{
//
// Enter an infinite loop.
//
while(1)
{
}
}
//
// This is the code that gets called when the processor receives a fault
// interrupt. This simply enters an infinite loop, preserving the system state
// for examination by a debugger.
//
//*****************************************************************************
static void
FaultISR(void)
{
//
// Enter an infinite loop.
//
while(1)
{
}
}
//
// This is the code that gets called when the processor receives an unexpected
// interrupt. This simply enters an infinite loop, preserving the system state
// for examination by a debugger.
//
//*****************************************************************************
static void
IntDefaultHandler(void)
{
//
// Go into an infinite loop.
//
while(1)
{
}
}