作业要求:
检测中断到来时,让LED灯状态取反,并且在串口工具上打印一句话: 当按键2按下之后,让LED2状态取反,并打印LED2 down” 当按键3按下之后,让LED3状态取反,并打印“LED3 down” 火焰传感器/人体红外/光电开关实验要求如上
gpio.c文件
//重写下降沿函数
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin)
{
case GPIO_PIN_9:
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_10 );
printf("led1 down\n");
break;
case GPIO_PIN_7:
HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_10);
printf("led2 down\n");
break;
case GPIO_PIN_8:
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_8);
printf("led3 down\n");
break;
}
}
//重写上升沿函数
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin)
{
case GPIO_PIN_15:
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_10 );
printf("led1 down\n");
break;
case GPIO_PIN_5:
HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_10);
printf("led2 down\n");
break;
case GPIO_PIN_12:
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_8);
printf("led3 down\n");
break;
}
}
uart.s文件:
int fputc(int ch,FILE *stream)
{
//判断发送数据寄存器是否为空
while(!(huart4.Instance->ISR & (0x1<<7)));
//将要发送的字符放到发送寄存器中
huart4.Instance->TDR = ch;
//判断是否发送成功
if(ch == '\n' )
{
while(!(huart4.Instance->ISR &(0x1 << 7)));
huart4.Instance->TDR = '\r';
}
}
实验结果: