久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
STM32 4X4按鍵矩陣的小程序
[打印本頁]
作者:
MrHuan991
時間:
2019-2-16 04:28
標題:
STM32 4X4按鍵矩陣的小程序
前篇帖子寫了基本的GPIO復用技術,現在補上按鍵矩陣技術,由于STM32的GPIO口不像傳統51,準雙向口,故而不能使用線反轉法,我采用了行列掃描的技術,GPIO口用了A組,故而庫函數沒法直接調用賦值和讀取同時16位的函數,只能調用一個個pin讀取函數和賦值函數,顯得有點臃腫。但是STM32性能牛逼,這么多if的語句還跑的這么流暢,如果是C51早gg了,在STC12勉強可以看看。僅供參考。STM32學起來比51更費勁,英語不好的話,庫函數弄死人。
此工程,主要用于編程4X4按鍵矩陣的識別和編碼的功能
單片機源程序如下:
#include <STM32F10X.H>
#include <misc.h>
#define PA0_Low GPIO_ResetBits(GPIOA,GPIO_Pin_0)
#define PA0_High GPIO_SetBits(GPIOA,GPIO_Pin_0)
#define PA1_Low GPIO_ResetBits(GPIOA,GPIO_Pin_1)
#define PA1_High GPIO_SetBits(GPIOA,GPIO_Pin_1)
#define PA2_Low GPIO_ResetBits(GPIOA,GPIO_Pin_2)
#define PA2_High GPIO_SetBits(GPIOA,GPIO_Pin_2)
#define PA3_Low GPIO_ResetBits(GPIOA,GPIO_Pin_3)
#define PA3_High GPIO_SetBits(GPIOA,GPIO_Pin_3)
#define PA4 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)
#define PA5 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5)
#define PA6 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)
#define PA7 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)
#define PC15_Low GPIO_WriteBit(GPIOC,GPIO_Pin_15,Bit_RESET)
#define PC15_High GPIO_WriteBit(GPIOC,GPIO_Pin_15,Bit_SET)
void RCC_Configuration(void); //時鐘與復位寄存器
void GPIO_Configuration(void); //GPIO口初始化函數
void Delay_ms(u16 n); //ms級別延時函數
u16 Array_Key(void); //4X4按鍵矩陣檢測函數
void Led_Show(u16 keystatus); //按鍵檢測后的狀態顯示函數
u16 ledcount=0;
u16 status=0;
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
while(1)
{
Led_Show(Array_Key());
}
}
void RCC_Configuration(void)
{
SystemInit();
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_Struct; //定義一個GPIO初始化的結構體變量
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Struct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_Init(GPIOA,&GPIO_Struct);
GPIO_Struct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Struct.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOA,&GPIO_Struct);
GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Struct.GPIO_Pin=GPIO_Pin_15;
GPIO_Init(GPIOC,&GPIO_Struct);
PA0_High;PA1_High;PA2_High;PA3_High;
PC15_Low;
}
u16 Array_Key(void)
{
PA0_Low;PA1_High;PA2_High;PA3_High;
if(PA4 == 0)
{
Delay_ms(10);
if(PA4 == 0)
{
while(!PA4);
status = 1;
}
}
if(PA5 == 0)
{
Delay_ms(10);
if(PA5 == 0)
{
while(!PA5);
status = 2;
}
}
if(PA6 == 0)
{
Delay_ms(10);
if(PA6 == 0)
{
while(!PA6);
status = 3;
}
}
if(PA7 == 0)
{
Delay_ms(10);
if(PA7 == 0)
{
while(!PA7);
status = 4;
}
}
PA0_High;PA1_Low;PA2_High;PA3_High;
if(PA4 == 0)
{
Delay_ms(10);
if(PA4 == 0)
{
while(!PA4);
status = 5;
}
}
if(PA5 == 0)
{
Delay_ms(10);
if(PA5 == 0)
{
while(!PA5);
status = 6;
}
}
if(PA6 == 0)
{
Delay_ms(10);
if(PA6 == 0)
{
while(!PA6);
status = 7;
}
}
if(PA7 == 0)
{
Delay_ms(10);
if(PA7 == 0)
{
while(!PA7);
status = 8;
}
}
PA0_High;PA1_High;PA2_Low;PA3_High;
if(PA4 == 0)
{
Delay_ms(10);
if(PA4 == 0)
{
while(!PA4);
status = 9;
}
}
if(PA5 == 0)
{
Delay_ms(10);
if(PA5 == 0)
{
while(!PA5);
status = 10;
}
}
if(PA6 == 0)
{
Delay_ms(10);
if(PA6 == 0)
{
while(!PA6);
status = 11;
}
}
if(PA7 == 0)
{
Delay_ms(10);
if(PA7 == 0)
{
while(!PA7);
status = 12;
}
}
PA0_High;PA1_High;PA2_High;PA3_Low;
if(PA4 == 0)
{
Delay_ms(10);
if(PA4 == 0)
{
while(!PA4);
status = 13;
}
}
if(PA5 == 0)
{
Delay_ms(10);
if(PA5 == 0)
{
while(!PA5);
status = 14;
}
}
if(PA6 == 0)
{
Delay_ms(10);
if(PA6 == 0)
{
while(!PA6);
status = 15;
}
}
if(PA7 == 0)
{
Delay_ms(10);
if(PA7 == 0)
{
while(!PA7);
status = 16;
}
}
return status;
}
void Delay_ms(u16 n)
{
u16 i;
while(n--)
{
i=12000;
while(i--);
}
}
void Led_Show(u16 keystatus)
{
if(keystatus != 0)
{ledcount++;status=0;}
if(ledcount % 2 == 0)
PC15_Low;
else
PC15_High;
}
復制代碼
所有資料51hei提供下載:
Key_Array.7z
(192.09 KB, 下載次數: 49)
2019-2-17 03:07 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://m.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
91手机看片
|
午夜影院在线免费观看
|
999热视频
|
av网站观看
|
亚洲天堂男人
|
日本久久一区二区
|
婷婷免费视频
|
91精品国产色综合久久不卡98
|
特黄aaaaaaaaa真人毛片
|
91偷拍视频
|
国产精品久久久久久久
|
日韩理论在线
|
六月激情婷婷
|
日本理论片午伦夜理片在线观看
|
久久视频一区二区
|
中文字幕不卡在线观看
|
超碰国产在线
|
日本天堂网
|
黄色欧美视频
|
精品在线播放
|
国产一区在线看
|
国产精品日韩在线
|
a毛片视频
|
色黄视频在线观看
|
久久免费观看视频
|
一本色道久久综合亚洲精品酒店
|
精品粉嫩小bbwbbwbbw
|
成av人片一区二区三区久久
|
国产麻豆xxxvideo实拍
|
日韩高清在线播放
|
精品一区二区三
|
日本在线观看
|
日本一级做a爱片
|
成人午夜毛片
|
国产又粗又猛又黄又爽的视频
|
欧美资源在线
|
亚洲免费观看视频
|
亚洲视频在线播放
|
一区二区三区四区在线
|
91们嫩草伦理
|
色综合久久综合
|