基于STM32的矩阵键盘驱动(中断方式)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//==========================Keyboard.h
//
/* --------------------------------------------- 本程序实现 4*5 键盘的扫描 从左到右,从上到下,键值 依次为 1-20
------------------------------- */ #ifndef __KEYBOARD_H #define __KEYBOARD_H #include "stm32f10x_lib.h" //选择扫描模式
#define Interrupt_Scan // 中断扫描模式 ,要在 NVIC 在中打开对应中断
/*可以自己定义其它扫描方式 */ #define DELAY_COUNT 0x0FFFF /* 键盘控制引脚定义 */ #define Keyboard_Control_Port GPIOD #define Keyboard_Line_1 #define Keyboard_Line_2 #define Keyboard_Line_3 #define Keyboard_Line_4 #define Keyboard_Line_5 #define Keyboard_Row_1 #define Keyboard_Row_2 #define Keyboard_Row_3 #define Keyboard_Row_4
#define Keyboard_LineBase Keyboard_Line_1
#define Keyboard_RowBase
Keyboard_Row_1 #define Keyboard_Line
(Keyboard_Line_1 | Keyboard_Line_2 | Keyboard_Line_3 | Keyboard_Line_4 | Keyboard_Line_5) #define Keyboard_Row
(Keyboard_Row_1 | Keyboard_Row_2 | Keyboard_Row_3 | Keyboard_Row_4)
#ifdef Interrupt_Scan /* 中断扫描模式宏定义 */ #define Keyboard_EXTI_Row1 #define Keyboard_EXTI_Row2
#define Keyboard_EXTI_Row3
#define Keyboard_EXTI_Row4
EXTI_Line5
EXTI_Line6 EXTI_Line7 EXTI_Line8 #define Keyboard_EXTI_PortSource GPIO_PortSourceGPIOD
#define Keyboard_EXTI_PinSource1 GPIO_PinSource5
#define Keyboard_EXTI_PinSource2 GPIO_PinSource6
GPIO_Pin_0
GPIO_Pin_1 GPIO_Pin_2 GPIO_Pin_3 GPIO_Pin_4 GPIO_Pin_5 GPIO_Pin_6
GPIO_Pin_7 GPIO_Pin_8
#define Keyboard_EXTI_PinSource3 GPIO_PinSource7
#define Keyboard_EXTI_PinSource4 GPIO_PinSource8 #define Keyboard_IRQ_Channel
EXTI9_5_IRQChannel
#define Keyboard_EXTI_Line (Keyboard_EXTI_Row1 | Keyboard_EXTI_Row2 |
Keyboard_EXTI_Row3 | Keyboard_EXTI_Row4)
#endif /* 中断扫描模式宏定义*/
/* 键盘全局变量声明*/
extern unsigned int Keyboard_Val ; //当前键值
extern unsigned char Keyboard_Change_Flag ; // 键值改变标志,读取新的键值后由主程
序清零
/* 键盘接口函数声明*/
#ifdef Interrupt_Scan
extern void Init_Keyboard_Interrupt(void) ;// 键盘初始化为键盘扫描模式
#endif
extern void Delay(vu32 nCount) ; // 用于延时消抖
#endif /* KEYBOARD_H */
//=================================================================//
//============================Keyboard.c=============================// #include "stm32f10x_lib.h"
#include "Keyboard.h"
unsigned int Keyboard_Val = 0 ; //保存键值
unsigned char Keyboard_Change_Flag = 0 ; // 键值改变标志,读取键值后清零
/****************************************************************
函数名称: Init_Keyboard_Interrupt
功能: 键盘初始化为中断扫描模式初始化键盘需要的IO,Line1-Line5 设为输出低Row1-Row4 接上拉电阻,使能下降沿中断
参数: 无
返回值: 无
*****************************************************************/
void Init_Keyboard_Interrupt(void)
{
GPIO_InitTypeDef GPIO_InitStructure ;
EXTI_InitTypeDef EXTI_InitStructure ;
EXTI_DeInit() ;
//Line1-Line5 设为输出高