BS8116A-3
IRQ 外部中断请求 NMOS输出内部上拉
SCL
SDA IIC通信接口
VDD 供电电压2.2-5.5V
Ct电容: 0~25 pF
电容越大灵敏度越低
1、 软件使用流程
初始化
将IIC的两个引脚初始化为复用开漏模式
按键引脚设置上拉输入 下降沿触发外部中断
void KEY_Init(void)
{
//uint8_t status;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOG,ENABLE); //使能复用功能时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; // 端口配置
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIO
GPIO_SetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5);
EXTI_InitStructure.EXTI_Line=EXTI_Line5; //KEY2
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键WK_UP所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
}
当产生外部中断开始读取键值
void EXTI9_5_IRQHandler(void)
{
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL(); // Tell uC/OS-II that we are starting an ISR //
OSIntEnter();
OS_EXIT_CRITICAL();
if(EXTI_GetITStatus(EXTI_Line5) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line5);
Touch_key = Touch_Key_Analysis();//读取Key 并返回
if( Touch_key != 0xFF && EnsureKeyFlag != 1 )
OSSemPost(KEYBOARDFLAG);
}
OSIntExit(); /* Tell uC/OS-II that we are leaving the ISR */
}
uint16_t Touch_Key_Analysis(void)
{
uint16_t Key_Val;
uint16_t Key_Val_H;
uint16_t Key_Val_L;
Key_Val_H = GetKey(0x09);
Key_Val_L = GetKey(0x08);
Key_Val = (Key_Val_H<<8)|Key_Val_L;
switch(Key_Val)
{
case Key1_Pres :
return 1;//#0x23
case Key2_Pres:
return 4;
case Key3_Pres:
return 7;
case Key4_Pres:
return '*';
case Key5_Pres:
return 2;//*0x2A
case Key6_Pres:
return 5;
case Key7_Pres:
return 8;
case Key8_Pres:
return 0;
case Key9_Pres:
return 3;
case Key10_Pres:
return 6;
case Key11_Pres:
return 9; // 10
case Key12_Pres:
return '#'; // 11
case Key13_Pres:
return 12; // 12
default: return 0xFF;
}
}