//==========================Keyboard.h===============================///*----------------------------------------------本程序实现4*5键盘的扫描从左到右,从上到下,键值依次为1-20------------------------------------------------*/#ifndef__KEYBOARD_H#define__KEYBOARD_H#includestm32f10x_lib.h//选择扫描模式#defineInterrupt_Scan//中断扫描模式,要在NVIC在中打开对应中断/*可以自己定义其它扫描方式*/#defineDELAY_COUNT0x0FFFF/*键盘控制引脚定义*/#defineKeyboard_Control_PortGPIOD#defineKeyboard_Line_1GPIO_Pin_0#defineKeyboard_Line_2GPIO_Pin_1#defineKeyboard_Line_3GPIO_Pin_2#defineKeyboard_Line_4GPIO_Pin_3#defineKeyboard_Line_5GPIO_Pin_4#defineKeyboard_Row_1GPIO_Pin_5#defineKeyboard_Row_2GPIO_Pin_6#defineKeyboard_Row_3GPIO_Pin_7#defineKeyboard_Row_4GPIO_Pin_8#defineKeyboard_LineBaseKeyboard_Line_1#defineKeyboard_RowBaseKeyboard_Row_1#defineKeyboard_Line(Keyboard_Line_1|Keyboard_Line_2|Keyboard_Line_3|Keyboard_Line_4|Keyboard_Line_5)#defineKeyboard_Row(Keyboard_Row_1|Keyboard_Row_2|Keyboard_Row_3|Keyboard_Row_4)#ifdefInterrupt_Scan/*中断扫描模式宏定义*/#defineKeyboard_EXTI_Row1EXTI_Line5#defineKeyboard_EXTI_Row2EXTI_Line6#defineKeyboard_EXTI_Row3EXTI_Line7#defineKeyboard_EXTI_Row4EXTI_Line8#defineKeyboard_EXTI_PortSourceGPIO_PortSourceGPIOD#defineKeyboard_EXTI_PinSource1GPIO_PinSource5#defineKeyboard_EXTI_PinSource2GPIO_PinSource6#defineKeyboard_EXTI_PinSource3GPIO_PinSource7#defineKeyboard_EXTI_PinSource4GPIO_PinSource8#defineKeyboard_IRQ_ChannelEXTI9_5_IRQChannel#defineKeyboard_EXTI_Line(Keyboard_EXTI_Row1|Keyboard_EXTI_Row2|Keyboard_EXTI_Row3|Keyboard_EXTI_Row4)#endif/*中断扫描模式宏定义*//*键盘全局变量声明*/externunsignedintKeyboard_Val;//当前键值externunsignedcharKeyboard_Change_Flag;//键值改变标志,读取新的键值后由主程序清零/*键盘接口函数声明*/#ifdefInterrupt_ScanexternvoidInit_Keyboard_Interrupt(void);//键盘初始化为键盘扫描模式#endifexternvoidDelay(vu32nCount);//用于延时消抖#endif/*KEYBOARD_H*///=================================================================////============================Keyboard.c=============================//#includestm32f10x_lib.h#includeKeyboard.hunsignedintKeyboard_Val=0;//保存键值unsignedcharKeyboard_Change_Flag=0;//键值改变标志,读取键值后清零/****************************************************************函数名称:Init_Keyboard_Interrupt功能:键盘初始化为中断扫描模式初始化键盘需要的IO,Line1-Line5设为输出低Row1-Row4接上拉电阻,使能下降沿中断参数:无返回值:无*****************************************************************/voidInit_Keyboard_Interrupt(void){GPIO_InitTypeDefGPIO_InitStructure;EXTI_InitTypeDefEXTI_InitStructure;EXTI_DeInit();//Line1-Line5设为输出高GPIO_InitStructure.GPIO_Pin=Keyboard_Line;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(Keyboard_Control_Port,&GPIO_InitStructure);GPIO_SetBits(Keyboard_Control_Port,Keyboard_Line);//Row1-Row4设置为下拉输入,用来接收上升沿中断GPIO_InitStructure.GPIO_Pin=Keyboard_Row;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;//下拉输入GPIO_Init(Keyboard_Control_Port,&GPIO_InitStructure);EXTI_ClearITPendingBit(Keyboard_EXTI_Row1);//清除中断标志位GPIO_EXTILineConfig(Keyboard_EXTI_PortSource,Keyboard_EXTI_PinSource1);//Pinsource不能取或EXTI_ClearITPendingBit(Keyboard_EXTI_Row2);//清除中断标志位GPIO_EXTILineConfig(Keyboard_EXTI_PortSource,Keyboard_EXTI_PinSource2);EXTI_ClearITPendingBit(Keyboard_EXTI_Row3);//清除中断标志位GPIO_EXTILineConfig(Keyboard_EXTI_PortSource,Keyboard_EXTI_PinSource3);EXTI_ClearITPendingBit(Keyboard_EXTI_Row4);//清除中断标志位GPIO_EXTILineConfig(Keyboard_EXTI_PortSource,Keyboard_EXTI_PinSource4);//设置Row1-Row4为上升沿中断EXTI_InitStructure.EXTI_Line=Keyboard_EXTI_Line;EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;EXTI_InitStructure.EXTI_LineCmd=ENABLE;EXTI_Init(&EXTI_InitStructure);}/****************************************************************函数名称:Delay功能:在扫描按键时,用于延时消抖参数:nCount--延时长度返回值:无*****************************************************************/voidDelay(vu32nCount){for(;nCount!=0;nCount--);}//==============================================================////=========================stm32f10x_it.c============================//voidEXTI9_5_IRQHandler(void){unsignedlongtmpFlag=0;//保存需要的中断标志位unsignedchari=0;//循环标量tmpFlag=EXTI-PR&Keyboard_EXTI_Line;//只取设定过的标志位EXTI-PR=tmpFlag;switch(tmpFlag)//判断是哪个标志位置位{caseKeyboard_EXTI_Row1:GPIO_ResetBits(Keyboard_Control_Port,Keyboard_Line);for(i=0;i5;i++)//扫描方式判断按键{GPIO_SetBits(Keyboard_Control_Port,(Keyboard_LineBasei));if(GPIO_ReadInputDataBit(Keyboard_Control_Port,Keyboard_Row_1)){Delay(DELAY_COUNT);//延时消抖if(GPIO_ReadInputDataBit(Keyboard_Control_Port,Keyboard_Row_1)){Keyboard_Val=1+i;Keyboard_Change_Flag=1;break;}}}GPIO_SetBits(Keyboard_Control_Port,Keyboard_Line);EXTI-PR=Keyboard_EXTI_Row1;//清除中断标志break;caseKeyboard_EXTI_Row2:GPIO_ResetBits(Keyboard_Control_Port,Keyboard_Line);for(i=0;i5;i++)//扫描方式判断按键{GPIO_SetBits(Keyboard_Control_Port,(Keyboard_LineBasei));if(GPIO_ReadInputDataBit(Keyboard_Control_Port,Keyboard_Row_2)){Delay(DELAY_COUNT);//延时消抖if(GPIO_ReadInputDataBit(Keyboard_Control_Port,Keyboard_Row_2)){Keyboard_Val=6+i;Keyboard_Change_Flag=1;break;}}}GPIO_SetBits(Keyboard_Control_Port,Keyboard_Line);EXTI-PR=Keyboard_EXTI_Row2;