stm32 按键
//°´¼ü³õʼ»¯º¯Êý void KEY_Init(void) //IO³õʼ»¯ { GPIO_InitTypeDef GPIO_InitStructure; //³õʼ»¯KEY0-->GPIOA.13,KEY1-->GPIOA.15 ÉÏÀÊäÈë IPU RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//ʹÄÜPORTA,PORTEʱÖÓ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//PE2~4 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖóÉÉÏÀÊäÈë GPIO_Init(GPIOE, &GPIO_InitStructure);//³õʼ»¯GPIOE2,3,4 //³õʼ»¯ WK_UP-->GPIOA.0 ÏÂÀÊäÈë IPD GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0ÉèÖóÉÊäÈ룬ĬÈÏÏÂÀ GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯GPIOA.0 } u8 KEY_Scan(u8 mode) { static u8 key_up=1;//°´¼ü°´ËÉ¿ª±êÖ¾ if(mode)key_up=1; //Ö§³ÖÁ¬°´ if(key_up&&(KEY0==0||KEY1==0||KEY2==0||KEY3==1)) //°´ÏÂÈ¥µÄ״̬key { delay_ms(10);//È¥¶¶¶¯ key_up = 0; if(KEY0==0)return KEY_RIGHT; else if(KEY1==0)return KEY_DOWN; else if(KEY2==0)return KEY_LEFT; else if(KEY3==1)return KEY_UP; } else if(KEY0==1&&KEY1==1&&KEY2==1&&KEY3==0)key_up=1; return 0; // ÎÞ°´¼ü°´Ï }
while(1) { t=KEY_Scan(0); if(t) { switch(t) { case KEY_UP: //µÆÈ«Ã𣬷äÃùÆ÷Ïì2Éù PA.0¿ØÖÆPB.8 LED0=1; LED1=1; BEEP = 1; delay_ms(100); BEEP = 0; delay_ms(200); BEEP = 1; delay_ms(100); BEEP = 0; delay_ms(200); break; case KEY_LEFT: //LED0ÉÁÒ»´Î PE.3¿ØÖÆPE.5 LED0=0; delay_ms(200); LED0=1; break; case KEY_DOWN: //¿ØÖÆLED1·×ª PE.2¿ØÖÆPB.5 LED1=!LED1; break; case KEY_RIGHT: //ͬʱ¿ØÖÆLED0,LED1·×ª PE.4 LED0=0; delay_ms(200); LED0=1; break; } }else delay_ms(10); } }