新闻中心

EEPW首页>嵌入式系统>设计应用> LPC1114外中断应用

LPC1114外中断应用

作者: 时间:2016-11-10 来源:网络 收藏
LPC1114微处理器每一个GPIO都可以中断,不过在设计中断时需要注意,下面就举例说明:

/**************************************************************************************
* global variable
**************************************************************************************/
volatile uint8 KeyValue = 0;

/**************************************************************************************
* FunctionName : KeyInit()
* Description : 初始化按键
* EntryParameter : None
* ReturnValue : None
**************************************************************************************/
void KeyInit(void)
{
// 设置为输入端口
GPIOSetDir(PORT1, KEY1, 0);
GPIOSetDir(PORT1, KEY2, 0);
GPIOSetDir(PORT1, KEY3, 0);
GPIOSetDir(PORT1, KEY4, 0);
GPIOSetDir(PORT1, KEY5, 0);
GPIOSetDir(PORT1, KEY6, 0);

本文引用地址://m.amcfsurvey.com/article/201611/317380.htm

// 设置为低电平中断
GPIOSetInterrupt(PORT1, KEY1, 1, 0, 0);
GPIOSetInterrupt(PORT1, KEY2, 1, 0, 0);
GPIOSetInterrupt(PORT1, KEY3, 1, 0, 0);
GPIOSetInterrupt(PORT1, KEY4, 1, 0, 0);
GPIOSetInterrupt(PORT1, KEY5, 1, 0, 0);
GPIOSetInterrupt(PORT1, KEY6, 1, 0, 0);

// 使能中断
GPIOIntEnable(PORT1, KEY1);
GPIOIntEnable(PORT1, KEY2);
GPIOIntEnable(PORT1, KEY3);
GPIOIntEnable(PORT1, KEY4);
GPIOIntEnable(PORT1, KEY5);
GPIOIntEnable(PORT1, KEY6);

NVIC_EnableIRQ(EINT1_IRQn); // 使能PORT1中断
}

/**************************************************************************************
* FunctionName : PIOINT1_IRQHandler()
* Description : 中断服务函数
* EntryParameter : None
* ReturnValue : None
**************************************************************************************/
void PIOINT1_IRQHandler(void)
{
uint32 key;

key = GPIOIntStatus(PORT1, KEY1);
if (key == 1)
{
KeyValue = K1;
GPIOIntClear(PORT1, KEY1);
return ;
}

key = GPIOIntStatus(PORT1, KEY2);
if (key == 1)
{
KeyValue = K2;
GPIOIntClear(PORT1, KEY2);
return ;
}

key = GPIOIntStatus(PORT1, KEY3);
if (key == 1)
{
KeyValue = K3;
GPIOIntClear(PORT1, KEY3);
return ;
}

key = GPIOIntStatus(PORT1, KEY4);
if (key == 1)
{
KeyValue = K4;
GPIOIntClear(PORT1, KEY4);
return ;
}

key = GPIOIntStatus(PORT1, KEY5);
if (key == 1)
{
KeyValue = K5;
GPIOIntClear(PORT1, KEY5);
return ;
}

key = GPIOIntStatus(PORT1, KEY6);
if (key == 1)
{
KeyValue = K6;
GPIOIntClear(PORT1, KEY6);
return ;
}
}

/**************************************************************************************
* End Of File
**************************************************************************************/

在中断函数中需要注意的是,如果直接使用如下格式,将无法读取端口中断状态。

if (GPIOIntStatus(PORT1, KEY6) == 1)
{
KeyValue = K6;
GPIOIntClear(PORT1, KEY6);
return ;
}



需要先定义一个临时变量,读取端口状态,再通过变量值进行判断,此问题已经记过验证。



关键词:LPC1114外中

评论


技术专区

关闭