標(biāo)題: STM32按鍵輸入程序,小白求助 [打印本頁]

作者: 今晚月色真美20    時(shí)間: 2019-12-6 21:57
標(biāo)題: STM32按鍵輸入程序,小白求助
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的矩陣按鍵,按鍵能用,但是執(zhí)行到這段程序的時(shí)候,按鍵沒用了。
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;
}
這是按鍵掃描程序。


作者: moluxiyan    時(shí)間: 2019-12-7 17:01
不知道你要問什么,這么一長串代碼估計(jì)也沒人會(huì)看
作者: 李澤訓(xùn)    時(shí)間: 2019-12-7 21:46
出現(xiàn)了什么問題啊

作者: 今晚月色真美20    時(shí)間: 2019-12-8 20:13
李澤訓(xùn) 發(fā)表于 2019-12-7 21:46
出現(xiàn)了什么問題啊

就是一個(gè)按鍵掃描程序,但是進(jìn)不去,按鍵沒用,可是我按鍵的接線沒問題,其他按鍵控制都沒問題,但一到輸出字符串的時(shí)候,就沒辦法用了。

作者: a77303383    時(shí)間: 2019-12-9 00:04
正好需要,下載下來看看。





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1