找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1562|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

STM32單片機(jī)矩陣鍵盤

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:949309 發(fā)表于 2022-4-28 15:54 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include "matrix_key.h"
#include "stm32f10x.h"
#include "delay.h"

void KEYPad_Init1(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);//開啟時(shí)鐘,使能GPIOB時(shí)鐘
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;        //上拉輸入 B0-B3
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;        //下拉輸入 F4-B7
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
}

void KEYPad_Init2(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);//開啟時(shí)鐘,使能GPIOB時(shí)鐘
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;        //下拉輸入 B0-B3
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;        //上拉輸入 B4-B7
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
}

int KEYPAD_Scan()
{
        static u16 keyvalue = 0;
        u32 readvalue = 0;
        KEYPad_Init1();
        readvalue = GPIO_ReadInputData(GPIOF);
        readvalue &= 0x00ff;
        //return readvalue;

        
        
        if(readvalue != 0x000f)
        {
                delay_ms(8);//消抖10ms
                if(readvalue != 0x000f)
                {
                        readvalue = GPIO_ReadInputData(GPIOF);//再次讀取狀態(tài)
                        readvalue &= 0x00ff;
                        //return readvalue;
                        switch(readvalue)
                        {
                                case (0x001f):keyvalue = 0;break;
                                case (0x002f):keyvalue = 1;break;
                                case (0x004f):keyvalue = 2;break;
                                case (0x008f):keyvalue = 3;break;
                        }
                        //return readvalue;
                        KEYPad_Init2();
                        delay_ms(5);
                        readvalue = GPIO_ReadInputData(GPIOF);//再次讀取狀態(tài)
                        readvalue |= 0x00f0;
                        readvalue &= 0x00ff;
                        //return readvalue;
                        switch(readvalue)
                        {
                                case (0x00f1):keyvalue += 0;break;
                                case (0x00f2):keyvalue += 4;break;
                                case (0x00f4):keyvalue += 8;break;
                                case (0x00f8):keyvalue += 12;break;
                        }
                        
                        while(readvalue != 0x00f0)//檢測(cè)按鍵松開
                        {
                                readvalue = GPIO_ReadInputData(GPIOF);//再次讀取狀態(tài)
                                readvalue |= 0x00f0;
                                readvalue &= 0x00ff;
                        }
                        //return readvalue;
                }
        }
        return keyvalue;
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表