0.png (6.27 KB, 下載次數: 48)
下載附件
2018-1-9 01:18 上傳
單片機源程序如下:
- //通過GPIOA的0—7腳pa.0-pa.3接列,上拉輸入。pa.4-pa.7接行推挽輸出。無按鍵按下時pa.0-pa.3均為高電平,當當對應按鍵按下時接通的列輸出低電平將其引腳拉低返回相應鍵值。
- //通過GPIOA的pa.8-pa.11引腳接通4輸入數碼管,當對應引腳按下時數碼管顯相應的值,key1對應數碼管顯0.......key16對應顯f
- #include "stm32f10x.h"
- #include "delay.h"
- #include "stm32f10x_it.h"
- #include "4X4.h"
- u16 key_Pressed ,key_val=0; //存放鍵值
- u8 key_Map[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; /*******************************************
- *******************************************
- * Check_Key(),檢查按鍵,確認鍵值
- *********************************************/
- /*******************************************
- 函數名稱:Check_Key
- 功 能:掃描鍵盤的IO端口,獲得鍵值
- 參 數:無
- 返回值 :key_val
- ********************************************/
- int Check_Key(void)
- {
- u16 row ,col,tmp1,tmp2;
- tmp1 = 0x10;
- for(row = 0;row < 4;row++) //行掃描
- {
- GPIOA->ODR|=0x00f0; //PA.4-PA.7輸出1
- GPIOA->ODR -= tmp1; //Pa.4~p.7輸出四位中有一個為0
- tmp1 <<=1;
- if ((GPIOA->IDR & 0x0f) < 0x0f) //是否PAIN的PA.0~PA.3中有一位為0
- {
- tmp2 = 0x01; // tmp2用于檢測出那一位為0
- for(col = 0;col < 4;col++) // 列檢測
- {
- if((GPIOA->IDR&tmp2) == 0x00) // 是否是該列,等于0為是
- {
- key_val = key_Map[row * 4 + col]; // 獲取鍵值
- return(key_val); // 退出循環
- }
- tmp2 <<= 1; // tmp2右移1位
- }
- }
- }
- }
- void GPIO_Inita(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口時鐘
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //PA.4——PA.11端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度為50MHz
- GPIO_Init(GPIOA, &GPIO_InitStructure); //根據設定參數初始化GPIOA.8
- GPIO_SetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); //PA.8 輸出高
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //LED1-->PD.2 端口配置, 推挽輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //推挽輸出 ,IO口速度為50MHz
- GPIO_SetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3); //PD.2 輸出高
- }
復制代碼
所有資料51hei提供下載:
4X4.zip
(1.54 KB, 下載次數: 27)
2018-1-5 22:53 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|