找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2007|回复: 0
打印 上一主题 下一主题
收起左侧

基于stm32f01 控制4x4矩阵按钮的程序(我试过没问题,改引脚很方便)

[复制链接]
跳转到指定楼层
楼主
ID:224556 发表于 2017-8-3 23:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "SysTick.h"  //系统延时
#include "stm32f10x.h"
#include "usart.h"  
unsigned char temp;
////*********4x4矩阵引出8个引脚,分别接A_Pin0~A_Pin7**********///
void InitKey(void) //
{  


        GPIO_InitTypeDef  GPIOStru;   
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
        GPIOStru.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;        
       
        GPIOStru.GPIO_Mode = GPIO_Mode_Out_PP;  //   

        GPIOStru.GPIO_Speed = GPIO_Speed_50MHz;      

        GPIO_Init(GPIOA,&GPIOStru);  


        GPIOStru.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;   
       
        GPIOStru.GPIO_Mode = GPIO_Mode_IPD;  //   上拉输入


        GPIOStru.GPIO_Speed = GPIO_Speed_50MHz;     


        GPIO_Init(GPIOA,&GPIOStru);   
       
        GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);//拉高
}
   
int key(void)  //
{  
        int KeyVal=0;  //  

        GPIO_Write(GPIOA,0x0001); //
        temp=GPIOA_IDR;
        temp=temp & 0x00f0;
        if(temp != 0x0000)
        {
                delay_ms(6);
                if(temp != 0x0000)
                {
                                temp=GPIOA_IDR;
                                temp=temp & 0x00f0;
                                switch(temp)                  //
                                {  
                                        case 0x0010: KeyVal=15; break;   
                                        case 0x0020: KeyVal=11; break;   
                                        case 0x0040: KeyVal=7; break;   
                                        case 0x0080: KeyVal=3; break;  
                                }
                                while(GPIOA->IDR & 0x00f0 != 0x0000);
                }
        }
               
        GPIO_Write(GPIOA,(GPIOA->ODR & 0xfff0 | 0x2)); //  
        if(GPIOA->IDR & 0x00f0 != 0x0000)
        {
                delay_ms(5);
                if(GPIOA->IDR & 0x00f0 != 0x0000)
                {
                        switch(GPIOA->IDR & 0x00f0)                  //
                        {   
                                case 0x0010: KeyVal=14; break;   
                                case 0x0020: KeyVal=10; break;   
                                case 0x0040: KeyVal=6; break;   
                                case 0x0080: KeyVal=2; break;
                        }  
                        while(GPIOA->IDR & 0x00f0 != 0x0000);                       
                }
        }
        GPIO_Write(GPIOA,(GPIOA->ODR & 0xfff0 | 0x4)); //  
        if(GPIOA->IDR & 0x00f0 != 0x0000)
        {
                delay_ms(5);
                if(GPIOA->IDR & 0x00f0 != 0x0000)
                {
                        switch(GPIOA->IDR & 0x00f0)                  //
                        {   
                                case 0x0010: KeyVal=13; break;   
                                case 0x0020: KeyVal=9; break;   
                                case 0x0040: KeyVal=5; break;   
                                case 0x0080: KeyVal=1; break;
                        }
                        while(GPIOA->IDR & 0x00f0 != 0x0000);       
                }
        }               
        GPIO_Write(GPIOA,(GPIOA->ODR & 0xfff0 | 0x8)); //   
        if(GPIOA->IDR & 0x00f0 != 0x0000)
        {
                delay_ms(5);
                if(GPIOA->IDR & 0x00f0 != 0x0000)
                {       
                        switch(GPIOA->IDR & 0x00f0)                  //
                        {   
                                case 0x0010: KeyVal=12; break;   
                                case 0x0020: KeyVal=8; break;   
                                case 0x0040: KeyVal=4; break;   
                                case 0x0080: KeyVal=0; break;
                        }
                        while(GPIOA->IDR & 0x00f0 != 0x0000);       
                }
        }                       
        return KeyVal;
}


int main()
{
   int a;
   a=key();
   switch(a)
{
  case 1: break;
  case 1: break;
  case 1: break;
  case 1: break;
  case 1: break;
  case 1: break;
  case 1: break;
  case 1: break;
  case 1: break;
}
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表