UCOSIII外部中断控key0 key1 key2按键代码
时间: 2023-07-09 07:33:50 浏览: 99
外部中断和按键
下面是一个UCOSIII外部中断控制key0、key1、key2按键的代码示例:
```c
#include "stm32f4xx.h"
#include "ucos_ii.h"
#define KEY0_GPIO_PORT GPIOA
#define KEY0_GPIO_PIN GPIO_Pin_0
#define KEY0_EXTI_LINE EXTI_Line0
#define KEY0_EXTI_PORT EXTI_PortSourceGPIOA
#define KEY0_EXTI_PIN EXTI_PinSource0
#define KEY0_EXTI_IRQ EXTI0_IRQn
#define KEY1_GPIO_PORT GPIOE
#define KEY1_GPIO_PIN GPIO_Pin_2
#define KEY1_EXTI_LINE EXTI_Line2
#define KEY1_EXTI_PORT EXTI_PortSourceGPIOE
#define KEY1_EXTI_PIN EXTI_PinSource2
#define KEY1_EXTI_IRQ EXTI2_IRQn
#define KEY2_GPIO_PORT GPIOE
#define KEY2_GPIO_PIN GPIO_Pin_3
#define KEY2_EXTI_LINE EXTI_Line3
#define KEY2_EXTI_PORT EXTI_PortSourceGPIOE
#define KEY2_EXTI_PIN EXTI_PinSource3
#define KEY2_EXTI_IRQ EXTI3_IRQn
static OS_EVENT* key_sem;
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY0_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(KEY0_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN;
GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(KEY0_EXTI_PORT, KEY0_EXTI_PIN);
SYSCFG_EXTILineConfig(KEY1_EXTI_PORT, KEY1_EXTI_PIN);
SYSCFG_EXTILineConfig(KEY2_EXTI_PORT, KEY2_EXTI_PIN);
EXTI_InitStructure.EXTI_Line = KEY0_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = KEY1_EXTI_LINE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = KEY2_EXTI_LINE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f;
NVIC_InitStructure.NVIC_IRQChannel = KEY0_EXTI_IRQ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = KEY1_EXTI_IRQ;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = KEY2_EXTI_IRQ;
NVIC_Init(&NVIC_InitStructure);
}
void Key_ISR(uint8_t key)
{
OS_ERR err;
EXTI_ClearITPendingBit(key == 0 ? KEY0_EXTI_LINE : key == 1 ? KEY1_EXTI_LINE : KEY2_EXTI_LINE);
OSSemPost(key_sem, OS_OPT_POST_ALL, &err);
}
void Key_Task(void* pdata)
{
OS_ERR err;
uint8_t key;
while (1) {
OSSemPend(key_sem, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
if (GPIO_ReadInputDataBit(KEY0_GPIO_PORT, KEY0_GPIO_PIN) == RESET) {
key = 0;
} else if (GPIO_ReadInputDataBit(KEY1_GPIO_PORT, KEY1_GPIO_PIN) == RESET) {
key = 1;
} else if (GPIO_ReadInputDataBit(KEY2_GPIO_PORT, KEY2_GPIO_PIN) == RESET) {
key = 2;
}
// 处理按键按下事件
printf("Key%d pressed!\r\n", key);
}
}
int main(void)
{
OS_ERR err;
OSInit();
Key_Init();
key_sem = OSSemCreate(0, &err);
OSTaskCreate(Key_Task, NULL, OSTaskStkChk, 10, 0, NULL, &err);
OSStart();
return 0;
}
void HardFault_Handler(void)
{
while (1);
}
```
上述代码中,`Key_Init()`函数用于初始化三个按键所对应的GPIO口、中断线和中断服务函数。在中断服务函数`Key_ISR()`中,使用`OSSemPost()`函数发送信号量,以通知任务处理按键按下事件。而在任务函数`Key_Task()`中,则使用`OSSemPend()`函数等待信号量,并在信号量被触发时通过GPIO口读取按键状态,从而确定是哪个按键被按下,并处理相应的事件。
阅读全文