? ? ? ? 由于采用延时函数会影响程序速度,所以采用定时器中断写的矩阵扫描按键。利用简单的状态机思想。
1.定时器初始化
//定时器初始化
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period=1000-1; //自动重装载寄存器周期的值(计数值)
/* 累计 TIM_Period个频率后产生一个更新或者中断 */
TIM_TimeBaseStructure.TIM_Prescaler= (72 - 1); //时钟预分频数 72M/72
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update); // 清除溢出中断标志
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//设置20ms定时器中断
void TIM2_IRQHandler(void)
{
if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) != RESET )
{
if(count_aj<20 )
{
count_aj++;
}
else
{
count_aj = 0;
flag_aj = 1;
}
}
TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update);
}
2.按键初始化
// 按键初始化
void Key_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/********4行输出*********/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/********4列输入*********/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 ;//| GPIO_Pin_15; // 可换4x4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
3.key.c
void Key_Scan(void)
{
// 第一行按键检查 //
if(flag_aj) //设计定时器的循环时间,在时间内循环扫描
{
Hang_00_L;//把第一行输出低电平
Hang_01_H;
Hang_02_H;
Hang_03_H;
if(Lie_00_V == 0)
{
key0_up=0;
}
else
{
key0_up = 1 ;
}
if(Lie_01_V == 0)
{
key1_up=0;
}
else
{
key1_up = 1 ;
}
if(Lie_02_V == 0)
{
key2_up=0;
}
else
{
key2_up = 1 ;
}
if ((key0_dw == 1)&&(key0_up == 0))
{
key1_on = 1;
}
if ((key1_dw == 1)&&(key1_up == 0))
{
key2_on = 1;
}
if ((key2_dw == 1)&&(key2_up == 0))
{
key3_on = 1;
}
if(key1_on ==1)
{
key1_mima = 0x01;
key1_on=0;
}
if(key2_on ==1)
{
key2_mima = 0x02;
key2_on=0;
}
if(key3_on ==1)
{
key3_mima = 0x03;
key3_on=0;
}
key0_dw = key0_up;
key1_dw = key1_up;
key2_dw = key2_up;
// 第二行按键检查 //
Hang_00_H;
Hang_01_L;//把第二行输出低电平
Hang_02_H;
Hang_03_H;
if(Lie_00_V == 0)
{
key3_up=0;
}
else
{
key3_up = 1 ;
}
if(Lie_01_V == 0)
{
key4_up=0;
}
else
{
key4_up = 1 ;
}
if(Lie_02_V == 0)
{
key5_up=0;
}
else
{
key5_up = 1 ;
}
if ((key3_dw == 1)&&(key3_up == 0))
{
key4_on = 1;
}
if ((key4_dw == 1)&&(key4_up == 0))
{
key5_on = 1;
}
if ((key5_dw == 1)&&(key5_up == 0))
{
key6_on = 1;
}
if(key4_on ==1)
{
key4_mima = 0x04;
key4_on=0;
}
if(key5_on ==1)
{
USART1_Send_Byte(5);
key5_on=0;
}
if(key6_on ==1)
{
USART1_Send_Byte(6);
key6_on=0;
}
key3_dw = key3_up;
key4_dw = key4_up;
key5_dw = key5_up;
// 第三行按键检查 //
Hang_00_H;
Hang_01_H;
Hang_02_L;//把第三行输出低电平
Hang_03_H;
if(Lie_00_V == 0)
{
key6_up=0;
}
else
{
key6_up = 1 ;
}
if(Lie_01_V == 0)
{
key7_up=0;
}
else
{
key7_up = 1 ;
}
if(Lie_02_V == 0)
{
key8_up=0;
}
else
{
key8_up = 1 ;
}
if ((key6_dw == 1)&&(key6_up == 0))
{
key7_on = 1;
}
if ((key7_dw == 1)&&(key7_up == 0))
{
key8_on = 1;
}
if ((key8_dw == 1)&&(key8_up == 0))
{
key9_on = 1;
}
if(key7_on ==1)
{
USART1_Send_Byte(7);
key7_on=0;
}
if(key8_on ==1)
{
USART1_Send_Byte(8);
key8_on=0;
}
if(key9_on ==1)
{
USART1_Send_Byte(9);
key9_on=0;
}
key6_dw = key6_up;
key7_dw = key7_up;
key8_dw = key8_up;
// 第四行按键检查 //
Hang_00_H;
Hang_01_H;
Hang_02_H;
Hang_03_L;//把第四行输出低电平
if(Lie_00_V == 0)
{
key9_up=0;
}
else
{
key9_up = 1 ;
}
if(Lie_01_V == 0)
{
keyA_up=0;
}
else
{
keyA_up = 1 ;
}
if(Lie_02_V == 0)
{
keyB_up=0;
}
else
{
keyB_up = 1 ;
}
if ((key9_dw == 1)&&(key9_up == 0))
{
key10_on = 1;
}
if ((keyA_dw == 1)&&(keyA_up == 0))
{
key0_on = 1;
}
if ((keyB_dw == 1)&&(keyB_up == 0))
{
key11_on = 1;
}
if(key10_on ==1)
{
USART1_Send_Byte(10);
key10_on=0;
}
if(key0_on ==1)
{
USART1_Send_Byte(0);
key0_on=0;
}
if(key11_on ==1)
{
USART1_Send_Byte(11);
key11_on=0;
}
key9_dw = key9_up;
keyA_dw = keyA_up;
keyB_dw = keyB_up;
flag_aj=0;
}
}
|