找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32按键输入程序,小白求助

[复制链接]
跳转到指定楼层
楼主
ID:525734 发表于 2019-12-6 21:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
int a=0;
void Set_angle(void){
        key=KeyScan();
        switch(key){
                        case 1 : str[str_cur++]=1;  break ;
                        case 2 : str[str_cur++]=2;  break ;
                        case 3 : str[str_cur++]=3;        break ;
                        case 4 : str[str_cur++]=4;         break ;
                        case 5 : str[str_cur++]=5;  break ;
                        case 6 : str[str_cur++]=6;         break ;
                        case 7 : str[str_cur++]=7;        break ;
                        case 8 : str[str_cur++]=8;         break ;
                        case 9 : str[str_cur++]=9;  break ;
                        case 10 : str[str_cur++]=0;         break ;
                        case 11 : {
                                angle=str[0]*100+str[1]*10+str[2];
                                str[0]=0;str[1]=0;str[2]=0;str_cur = 0;
                                a=1;
                        }
                        break ;
                        default: break;
                }
                LCD_ShowNum(60+48,100,str[0],1,24);        
                LCD_ShowNum(60+48+12,100,str[1],1,24);        
                LCD_ShowNum(60+48+24,100,str[2],1,24);
          LCD_ShowString(80,50,200,16,16,"setting angle");
}
//éèÖð뾶
void Set_R(void){

        key=KeyScan();
                GPIO_SetBits(GPIOE,GPIO_Pin_5);
        switch(key){
                        case 1 : str[str_cur++]=1;  break ;
                        case 2 : str[str_cur++]=2; break ;
                        case 3 : str[str_cur++]=3;          break ;
                        case 4 : str[str_cur++]=4;          break ;
                        case 5 : str[str_cur++]=5;   break ;
                        case 6 : str[str_cur++]=6;         break ;
                        case 7 : str[str_cur++]=7;         break ;
                        case 8 : str[str_cur++]=8;         break ;
                        case 9 : str[str_cur++]=9;  break ;
                        case 10 : str[str_cur++]=0;         break ;
                        case 11 :
                                {
                                R=str[0]*10+str[1];
                                str[0]=0;str[1]=0;str_cur = 0;
                                a=1;
                        }
                        break ;
                        default: break;
                }        GPIO_ResetBits(GPIOE,GPIO_Pin_5);

                LCD_ShowNum(60+48,100,str[0],1,24);        
                LCD_ShowNum(60+48+12,100,str[1],1,24);        
          LCD_ShowString(80,50,200,16,16,"setting R");
}
这段程序,我用的是4*4的矩阵按键,按键能用,但是执行到这段程序的时候,按键没用了。
void KEY1_Init(void)
{            
  GPIO_InitTypeDef  GPIO_InitStructure1,GPIO_InitStructure2;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  //GPIOD2,D3,D4,D53õê¼»ˉéèÖÃ
  GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3| GPIO_Pin_6| GPIO_Pin_7; //DD
  GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOD, &GPIO_InitStructure1);
        
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
        
        //GPIOG2,G3,G4,G53õê¼»ˉéèÖÃ
  GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3| GPIO_Pin_4| GPIO_Pin_5;
  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
        GPIO_InitStructure2.GPIO_PuPd =GPIO_PuPd_DOWN;
  GPIO_Init(GPIOG, &GPIO_InitStructure2);


}


void LED_Init(void)
{            
  GPIO_InitTypeDef  GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
        
        GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);

}

u8 KeyScan(void)
{
u8 a=0;
static u8 key_up=1;
G2_O=0,G3_O=0,G4_O=0,G5_O=0;

if(key_up&&(D2_I==0||D3_I==0||D4_I==0||D5_I==0))
{
//  delay_ms(3);
  key_up=0;
  if(D2_I==0) a=0;
  if(D3_I==0) a=4;
  if(D4_I==0) a=8;
        if(D5_I==0) a=12;

  G2_O=0,G3_O=1,G4_O=1,G5_O=1;
  if(D2_I==0||D3_I==0||D4_I==0||D5_I==0) a+=1;

  G2_O=1,G3_O=0,G4_O=1,G5_O=1;
  if(D2_I==0||D3_I==0||D4_I==0||D5_I==0) a+=2;

  G2_O=1,G3_O=1,G4_O=0,G5_O=1;
  if(D2_I==0||D3_I==0||D4_I==0||D5_I==0) a+=3;
         
        G2_O=1,G3_O=1,G4_O=1,G5_O=0;
  if(D2_I==0||D3_I==0||D4_I==0||D5_I==0) a+=4;         
}
else if(D2_I==1&&D3_I==1&&D4_I==1&&D5_I==1) {
         key_up=1;a=0;
}
return a;
}
这是按键扫描程序。

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

使用道具 举报

沙发
ID:470332 发表于 2019-12-7 17:01 | 只看该作者
不知道你要问什么,这么一长串代码估计也没人会看
回复

使用道具 举报

板凳
ID:416239 发表于 2019-12-7 21:46 | 只看该作者
出现了什么问题啊
回复

使用道具 举报

地板
ID:525734 发表于 2019-12-8 20:13 | 只看该作者
李泽训 发表于 2019-12-7 21:46
出现了什么问题啊

就是一个按键扫描程序,但是进不去,按键没用,可是我按键的接线没问题,其他按键控制都没问题,但一到输出字符串的时候,就没办法用了。
回复

使用道具 举报

5#
ID:122686 发表于 2019-12-9 00:04 | 只看该作者
正好需要,下载下来看看。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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