基于STM32的薄片矩陣鍵盤,實驗現象為1,2,3,A為d的加減,分精調和粗調。4,5,6,B為a的精調和粗調,調完后需按下7確定,才能在串口中顯示
單片機源程序如下:
- #include "stm32f10x.h"
- #include "key.h"
- #include "sys.h"
- #include "delay.h"
-
- void KEY_Init(void) //IO初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能PORTA,PORTC時鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//PA0~4
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA0,1,2,3,
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA4,5,6,7
- GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
- }
- u8 Read_KeyValue(void)
- {
- u8 KeyValue=0;
- if((GPIO_ReadInputData(GPIOA)&0xff)!=0x0f)
- {
- delay_ms(10);
- if((GPIO_ReadInputData(GPIOA)&0xff)!=0x0f)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_0);
- GPIO_ResetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
- switch(GPIO_ReadInputData(GPIOA)&0xff)
- {
- case 0x11:KeyValue=1;break;
- case 0x21:KeyValue=2;break;
- case 0x41:KeyValue=3;break;
- case 0x81:KeyValue=10;break;
- }
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3);
- switch(GPIO_ReadInputData(GPIOA)&0xff)
- {
- case 0x12:KeyValue=4;break;
- case 0x22:KeyValue=5;break;
- case 0x42:KeyValue=6;break;
- case 0x82:KeyValue=11;break;
- }
- GPIO_SetBits(GPIOA,GPIO_Pin_2);
- GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3);
- switch(GPIO_ReadInputData(GPIOA)&0xff)
- {
- case 0x14:KeyValue=7;break;
- case 0x24:KeyValue=8;break;
- case 0x44:KeyValue=9;break;
- case 0x84:KeyValue=12;break;
- }
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
- switch(GPIO_ReadInputData(GPIOA)&0xff)
- {
- case 0x18:KeyValue=14;break;
- case 0x28:KeyValue=0;break;
- case 0x48:KeyValue=15;break;
- case 0x88:KeyValue=13;break;
- }
- GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
- while((GPIO_ReadInputData(GPIOA)&0xff)!=0x0f);
- return KeyValue;
- }
- }
- return 0;
- }
復制代碼
所有資料51hei提供下載:
按鍵輸入.7z
(181.04 KB, 下載次數: 12)
2019-8-9 17:31 上傳
點擊文件名下載附件
|