void keyboard_scan(void)
{
u16 i,j;
u8 x0, x1, x2, x3;
u8 temp, current;
if(keyboard.active == 0)
return;
//关中断
HAL_NVIC_DisableIRQ(EXTI3_IRQn);
HAL_NVIC_DisableIRQ(EXTI4_IRQn);
HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
//X0~3切换为输入状态
pin_io_switch(KEY_X0_PORT, KEY_X0_Pin, GPIO_IN); //X0 PC13
pin_io_switch(KEY_X1_PORT, KEY_X1_Pin, GPIO_IN); //X1 PC14
pin_io_switch(KEY_X2_PORT, KEY_X2_Pin, GPIO_IN); //X2 PC15
pin_io_switch(KEY_X3_PORT, KEY_X3_Pin, GPIO_IN); //X3 PB7
//只将相应的一路Y口切换为输出状态,Y的数值,在中断函数中设置过
switch(keyboard.y)
{
case 0:
pin_io_switch(KEY_Y0_PORT, KEY_Y0_Pin, GPIO_OUT);
KEY_Y0_SET(0); //Y0 PB6
break;
case 1:
pin_io_switch(KEY_Y1_PORT, KEY_Y1_Pin, GPIO_OUT);
KEY_Y1_SET(0); //Y0 PB5
break;
case 2:
pin_io_switch(KEY_Y2_PORT, KEY_Y2_Pin, GPIO_OUT);
KEY_Y2_SET(0); //Y0 PB4
break;
case 3:
pin_io_switch(KEY_Y3_PORT, KEY_Y3_Pin, GPIO_OUT);
KEY_Y3_SET(0); //Y0 PB3
break;
case 4:
pin_io_switch(KEY_Y4_PORT, KEY_Y4_Pin, GPIO_OUT);
KEY_Y4_SET(0); //Y0 PA15
break;
}
//读取X轴的状态
x0 = KEY_X0_READ(); //X0 PC13
x1 = KEY_X1_READ(); //X1 PC14
x2 = KEY_X2_READ(); //X2 PC15
x3 = KEY_X3_READ(); //X3 PB7
if(x0 == 0)
keyboard.x = 3; //X方向从左开始为x3, x2, x1, x0
else if(x1 == 0)
keyboard.x = 2; //与电路图上相反,下一版电路要把方向改过来,改为x0,x1,x2,x3
else if(x2 == 0)
keyboard.x = 1;
else
keyboard.x = 0;
keyboard.num = keyboard.y*4 + keyboard.x; //计算键值,0~19
//将Y口配置为EXTI,上拉
pin_io_switch(KEY_Y0_PORT, KEY_Y0_Pin, GPIO_EXTI);
pin_io_switch(KEY_Y1_PORT, KEY_Y1_Pin, GPIO_EXTI);
pin_io_switch(KEY_Y2_PORT, KEY_Y2_Pin, GPIO_EXTI);
pin_io_switch(KEY_Y3_PORT, KEY_Y3_Pin, GPIO_EXTI);
pin_io_switch(KEY_Y4_PORT, KEY_Y4_Pin, GPIO_EXTI);
//将X口配置为输出,低电平
pin_io_switch(KEY_X0_PORT, KEY_X0_Pin, GPIO_OUT);
pin_io_switch(KEY_X1_PORT, KEY_X1_Pin, GPIO_OUT);
pin_io_switch(KEY_X2_PORT, KEY_X2_Pin, GPIO_OUT);
pin_io_switch(KEY_X3_PORT, KEY_X3_Pin, GPIO_OUT);
KEY_X0_SET(0);
KEY_X1_SET(0);
KEY_X2_SET(0);
KEY_X3_SET(0);
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}