打印
[STM32F1]

按键扩展

[复制链接]
1060|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
z735155678|  楼主 | 2015-5-14 10:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
stm32f103mini开发板按键不够用,怎么用外接按键。不用扩展芯片,只是用I/O口扩展
沙发
z735155678|  楼主 | 2015-5-14 10:30 | 只看该作者
#ifndef __KEY4X4_H
#define __KEY4X4_H         
#include "sys.h"
////#include "stm32f10x_lib.h"


#define S0   GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)//
#define S1   GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5)//
#define S2   GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)//
#define S3   GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)//

#define S0_D0   5
#define S1_D1         6
#define S2_D2         7
#define S3_D3         8

void KEY4x4_Init(void);
u8 Key_scan(u8);

#endif
#include "key4x4.h"
#include "sys.h"
#include "usart.h"
#include "delay.h"

void KEY4x4_Init(void) //IO³õʼ»¯
{
//        
        //ÍÆÍìÊä³ö
        GPIO_InitTypeDef GPIO_InitStructure;
//               
//       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//ʹÄÜPORTEʱÖÓ
//        GPIO_InitStructure.GPIO_Pin = Keyboard_Line ;
//        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÉèÖóÉÍÆÍìÊä³ö
//        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//        GPIO_Init(Keyboard_Control_Port, &GPIO_InitStructure);//³õʼ»¯GPIOE7,8,9,10
//        GPIO_SetBits(Keyboard_Control_Port,Keyboard_Line);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//ʹÄÜPORTA,PORTCʱÖÓ

        GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//¹Ø±Õjtag£¬Ê¹ÄÜSWD£¬¿ÉÒÔÓÃSWDģʽµ÷ÊÔ
       
        GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4;//PA15
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÉÏÀ­ÊäÈë
        GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15
       
        GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_5;//PA15
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÉÏÀ­ÊäÈë
        GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15
       
        GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_6;//PA15       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÉÏÀ­ÊäÈë
        GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15
       
        GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_7;//PA15
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÉÏÀ­ÊäÈë
        GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15

//        //ÏÂÀ­ÊäÈë
//        GPIO_InitStructure.GPIO_Pin  = Keyboard_Row;
//        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0ÉèÖóÉÊäÈ룬ĬÈÏÏÂÀ­          
//        GPIO_Init(Keyboard_Control_Port, &GPIO_InitStructure);//³õʼ»¯GPIOE
//       
////        GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
////        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0ÉèÖóÉÊäÈ룬ĬÈÏÏÂÀ­          
////        GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯GPIOE
}

u8  Key_scan(u8 mode)
{                         
        static u8 key_up=1;//°´¼ü°´ËÉ¿ª±êÖ¾
        if(mode)key_up=1;  //Ö§³ÖÁ¬°´               
                if(key_up&&(S0==0||S1==0||S2==0||S3==0))
                {
                        delay_ms(15);
                        key_up=0;
                        if(S0==0) return S0_D0;
                                else if(S1==0) return S1_D1;
                                        else if(S2==0) return S2_D2;
                                                else if(S3==0) return S3_D3;       
                }else if(S0==1&&S1==1&&S2==1&&S3==1)key_up=1;
                        return 0;
}
这是.c和.h文件
while(1)
                                                        {
                                                                DD=Key_scan(0);
                                                                if(DD)
                                                                {
                                                                        switch(DD)
                                                                        {
                                                                                case S0_D0:
                                                                                        PDout(2)=!PDout(2);break;
                                                                                case S1_D1:
                                                                                        PDout(2)=!PDout(2);break;
                                                                                case S2_D2:
                                                                                        PDout(2)=!PDout(2);break;
                                                                                case S3_D3:
                                                                                        PDout(2)=!PDout(2);break;
                                                                        }
                                                                }else delay_ms(10);
                                                        }
这是主函数,我接的是开发板的外部按键,不知道哪里出了问题,总是不管用。

使用特权

评论回复
板凳
mmuuss586| | 2015-5-14 18:41 | 只看该作者

按键不够用,参考51的,IO扩展按键方式(行列交叉)

使用特权

评论回复
地板
309030106| | 2015-5-14 22:32 | 只看该作者
对呀,可以参考矩阵键盘啊,不知道你要扩多少呢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝