为什么开关不能控制小灯亮灭,stm32控制器

这是程序代码 #include "stm32f10x.h"#include "LQ12864.h"#include "adc.h"#include "dth11.h"#define PUSH_UP 1#define PUSH_DOWN 2#define PUSH_OK 3#define PUSH_NONE 4void main_delay(u32 ms){ int i, j; for(i = 0; i < ms; i++) { for(j = 0; j < 1000; j++) { ; } }}//按键初始化函数void KEY_Init(void) //IO初始化{ GPIO_InitTypeDef GPIO_InitStructure; //初始化KEY0-->GPIOA.1 上拉输入 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能PORTA, GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;//PE2~4 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA1}int KEY_Read(){ if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0) { return PUSH_UP; } else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0) { return PUSH_DOWN; } else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2) == 0) { return PUSH_OK; } else return PUSH_NONE;}void FAN_Init(){ GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_SetBits(GPIOA,GPIO_Pin_1); }void Beep_Init(){ GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_4; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_SetBits(GPIOA,GPIO_Pin_4); }void LED_Init(){ GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_SetBits(GPIOA,GPIO_Pin_2); GPIO_SetBits(GPIOA,GPIO_Pin_3); }int main(void){ int key; int times; int status[4] = { 0 }; uint16_t val; u8 temp = 0, hum = 0; int i = 2; char buf[100] = { 0 }; delay_init() ; LCD_Init() ; Adc_Init(); DHT11_Init(); KEY_Init(); LED_Init(); FAN_Init(); Beep_Init(); while(1) { DHT11_Read_Data(&temp, &hum); sprintf(buf, "temp: %d hum:%d", temp, hum); LCD_P6x8Str(1, 0, buf); val = Get_Adc(); //得到对应通道的adc值 sprintf(buf, "light:%d ", val); LCD_P6x8Str(1, 1, buf); LCD_P6x8Str(1, 2, "LED1 "); LCD_P6x8Str(1, 3, "LED2 "); LCD_P6x8Str(1, 4, "BEEP "); LCD_P6x8Str(1, 5, "FAN "); LCD_P6x8Str(40, i, "*"); // sprintf(buf, "L1:%d L2:%d BE:%d FAN:%d", status[0], status[1], status[2], status[3]); LCD_P6x8Str(1, 6, buf); for (times = 0; times < 5000; ++times) { key = KEY_Read(); if (key != PUSH_NONE) { main_delay(200); break; } } if(key == PUSH_DOWN) { i++; if(i > 5) i = 2; } else if(key == PUSH_UP) { i--; if(i < 2) i = 5; } else if(key == PUSH_OK) { if(i == 2) { status[0] = !status[0]; status[0] ? GPIO_ResetBits(GPIOA,GPIO_Pin_2) : GPIO_SetBits(GPIOA,GPIO_Pin_2); } else if(i == 3) { status[1] = !status[1]; status[1] ? GPIO_ResetBits(GPIOA,GPIO_Pin_3) : GPIO_SetBits(GPIOA,GPIO_Pin_3); } else if(i == 4) { status[2] = !status[2]; status[2] ? GPIO_ResetBits(GPIOA,GPIO_Pin_4) : GPIO_SetBits(GPIOA,GPIO_Pin_4); } else if(i == 5) { status[3] = !status[3]; status[3] ? GPIO_ResetBits(GPIOA,GPIO_Pin_1) : GPIO_SetBits(GPIOA,GPIO_Pin_1); } } }}

虽然不知道你这个是想干啥,这逻辑很混乱啊,几个按键交互而且按键没有防抖,就算碰巧可以亮也没有逻辑可言