按键判断程序,供参考while (1)
{
//正向运动按键 KEY1 判断
if(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1))
{
HAL_Delay(100);
if(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1))
{
printf("key1 press down\r\n");
if(motor1_dir_flg==zheng)
motor1_dir_flg = fan;
else
motor1_dir_flg = zheng;
}
HAL_Delay(10);
}
if(motor1_dir_flg)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_9,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10,GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_9,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10,GPIO_PIN_RESET);
}
HAL_Delay(10);
//反向运动按键 KEY2 判断
if(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2))
{
HAL_Delay(10);
if(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2))
{
printf("key2 press down\r\n");
if(motor2_dir_flg==zheng)
motor2_dir_flg=fan;
else
motor2_dir_flg=zheng;
}
HAL_Delay(10);
}
if(motor2_dir_flg)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
}
HAL_Delay(10);
//调速控制按键 KEY3 判断
if(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3))
{
HAL_Delay(10);
if(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3))
{
if(motor_speed<1000)
{
printf("the current speed: %d\r\n",motor_speed);
motor_speed+=100;
__HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_1,motor_speed);
}
else
{
motor_speed = 100;
__HAL_TIM_SetCompare(&htim1,TIM_CHANNEL_1,motor_speed);
printf("the current speed: %d\r\n",motor_speed);
}
HAL_Delay(500);
}
} |