void LED_Init(void) { //GPIOF9初始化設置 GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF時鍾 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//LED對應IO口 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 GPIO_SetBits(GPIOF,GPIO_Pin_9);//GPIOF9設置高,燈滅 //GPIO_ResetBits(GPIOF,GPIO_Pin_9);//輸出低電平,燈亮 } void Key_InIt(void) { //GPIOF9初始化設置 GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOF時鍾 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//LED對應IO口 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//輸出模式 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIO } u8 KEY_Scan(u8 mode) { static u8 key_up=1;//按鍵按松開標志 if(mode)key_up=1; //支持連按 if(key_up&&(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0)) { delay_ms(10);//去抖動 key_up=0; if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0)return 1; }else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==1)key_up=1; return 0;// 無按鍵按下 } int main(void) { u8 key; u8 sw=0; LED_Init(); Key_InIt(); //保存鍵值 delay_init(168); //初始化延時函數 while(1) { key=KEY_Scan(0); //得到鍵值 if(key) { if(sw==0) { GPIO_ResetBits(GPIOF,GPIO_Pin_9);//輸出低電平,燈亮 sw=1; } else { GPIO_SetBits(GPIOF,GPIO_Pin_9);//GPIOF9設置高,燈滅 sw=0; } }else delay_ms(10); } }