欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1612|回復: 0
打印 上一主題 下一主題
收起左側

STM32單片機矩陣鍵盤

[復制鏈接]
跳轉到指定樓層
樓主
ID:949309 發表于 2022-4-28 15:54 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#include "matrix_key.h"
#include "stm32f10x.h"
#include "delay.h"

void KEYPad_Init1(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);//開啟時鐘,使能GPIOB時鐘
        
        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);//開啟時鐘,使能GPIOB時鐘
        
        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);//再次讀取狀態
                        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);//再次讀取狀態
                        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)//檢測按鍵松開
                        {
                                readvalue = GPIO_ReadInputData(GPIOF);//再次讀取狀態
                                readvalue |= 0x00f0;
                                readvalue &= 0x00ff;
                        }
                        //return readvalue;
                }
        }
        return keyvalue;
}


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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表