|
單片機(jī)源程序如下:
- //通過(guò)GPIOA的0—7腳pa.0-pa.3接列,上拉輸入。pa.4-pa.7接行推挽輸出。無(wú)按鍵按下時(shí)pa.0-pa.3均為高電平,當(dāng)當(dāng)對(duì)應(yīng)按鍵按下時(shí)接通的列輸出低電平將其引腳拉低返回相應(yīng)鍵值。
- //通過(guò)GPIOA的pa.8-pa.11引腳接通4輸入數(shù)碼管,當(dāng)對(duì)應(yīng)引腳按下時(shí)數(shù)碼管顯相應(yīng)的值,key1對(duì)應(yīng)數(shù)碼管顯0.......key16對(duì)應(yīng)顯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(),檢查按鍵,確認(rèn)鍵值
- *********************************************/
- /*******************************************
- 函數(shù)名稱:Check_Key
- 功 能:掃描鍵盤的IO端口,獲得鍵值
- 參 數(shù):無(wú)
- 返回值 :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輸出四位中有一個(gè)為0
- tmp1 <<=1;
- if ((GPIOA->IDR & 0x0f) < 0x0f) //是否PAIN的PA.0~PA.3中有一位為0
- {
- tmp2 = 0x01; // tmp2用于檢測(cè)出那一位為0
- for(col = 0;col < 4;col++) // 列檢測(cè)
- {
- if((GPIOA->IDR&tmp2) == 0x00) // 是否是該列,等于0為是
- {
- key_val = key_Map[row * 4 + col]; // 獲取鍵值
- return(key_val); // 退出循環(huán)
- }
- tmp2 <<= 1; // tmp2右移1位
- }
- }
- }
- }
- void GPIO_Inita(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口時(shí)鐘
-
- 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); //根據(jù)設(shè)定參數(shù)初始化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 輸出高
- }
復(fù)制代碼
所有資料51hei提供下載:
4X4.zip
(1.54 KB, 下載次數(shù): 27)
2018-1-5 22:53 上傳
點(diǎn)擊文件名下載附件
|
|