重写FPUTC函数
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*//* Includes ------------------------------------------------------------------*/
#include "usart.h"/* USER CODE BEGIN 0 */
int putc(int ch, FILE* stream)
{
/*1、判断发送寄存器是否为空*/
while(!(huart4.Instance->ISR &(0x1<<7)))
{
/*2、将要发送的数据,写入到发送寄存器中*/
huart4.Instance->TDR = ch;
/*3、判断是否为'\n'*/
if('\n' == ch)
{
//判断发送寄存器是否为空
while(!(huart4.Instance->ISR & (0x1 << 7)))
{
huart4.Instance->TDR = '\r';
}
}
}
return ch;
}/* USER CODE END 0 */
UART_HandleTypeDef huart4;
/* UART4 init function */
void MX_UART4_Init(void)
{/* USER CODE BEGIN UART4_Init 0 */
/* USER CODE END UART4_Init 0 */
/* USER CODE BEGIN UART4_Init 1 */
/* USER CODE END UART4_Init 1 */
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART4_Init 2 *//* USER CODE END UART4_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(uartHandle->Instance==UART4)
{
/* USER CODE BEGIN UART4_MspInit 0 *//* USER CODE END UART4_MspInit 0 */
if(IS_ENGINEERING_BOOT_MODE())
{
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_UART24;
PeriphClkInit.Uart24ClockSelection = RCC_UART24CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}}
/* UART4 clock enable */
__HAL_RCC_UART4_CLK_ENABLE();__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**UART4 GPIO Configuration
PG11 ------> UART4_TX
PB2 ------> UART4_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_UART4;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);/* USER CODE BEGIN UART4_MspInit 1 */
/* USER CODE END UART4_MspInit 1 */
}
}void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{if(uartHandle->Instance==UART4)
{
/* USER CODE BEGIN UART4_MspDeInit 0 *//* USER CODE END UART4_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_UART4_CLK_DISABLE();/**UART4 GPIO Configuration
PG11 ------> UART4_TX
PB2 ------> UART4_RX
*/
HAL_GPIO_DeInit(GPIOG, GPIO_PIN_11);HAL_GPIO_DeInit(GPIOB, GPIO_PIN_2);
/* USER CODE BEGIN UART4_MspDeInit 1 */
/* USER CODE END UART4_MspDeInit 1 */
}
}/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
重写中断触发函数
/**
******************************************************************************
* @file gpio.c
* @brief This file provides code for the configuration
* of all used GPIO pins.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*//* Includes ------------------------------------------------------------------*/
#include "gpio.h"/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*----------------------------------------------------------------------------*/
/* Configure GPIO */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 *//* USER CODE END 1 */
/** Configure pins
*/
void MX_GPIO_Init(void)
{GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_10|GPIO_PIN_8, GPIO_PIN_RESET);/*Configure GPIO pin : PE15 */
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);/*Configure GPIO pins : PF5 PF12 */
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);/*Configure GPIO pins : PE10 PE8 */
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);/*Configure GPIO pins : PF7 PF8 PF9 */
GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI5_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(EXTI5_IRQn);HAL_NVIC_SetPriority(EXTI7_IRQn, 7, 0);
HAL_NVIC_EnableIRQ(EXTI7_IRQn);HAL_NVIC_SetPriority(EXTI8_IRQn, 8, 0);
HAL_NVIC_EnableIRQ(EXTI8_IRQn);HAL_NVIC_SetPriority(EXTI9_IRQn, 9, 0);
HAL_NVIC_EnableIRQ(EXTI9_IRQn);HAL_NVIC_SetPriority(EXTI12_IRQn, 12, 0);
HAL_NVIC_EnableIRQ(EXTI12_IRQn);HAL_NVIC_SetPriority(EXTI15_IRQn, 15, 0);
HAL_NVIC_EnableIRQ(EXTI15_IRQn);}
/* USER CODE BEGIN 2 */
/*__weak:是给编辑器进行使用
__weak:修饰的函数,表示弱化这个函数
__weak:修饰的函数重写之后,下次跳转的时候,就跳转到自己编写的函数中
*/
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
//让按键按下的时候,LED的状态进行取反
switch(GPIO_Pin)
{
/*KEY2----->LED2*/
case GPIO_PIN_7:
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
printf("KEY2 DOWN############");
break;
/*KEY3----->LED1*/
case GPIO_PIN_8:
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_10);
printf("KEY3 DOWN############");
break;
/*KEY1----->LED3*/
case GPIO_PIN_9:
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_8);
printf("KEY1 DOWN############");
break;
}
}void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
//当光电开关/火焰传感器/人体红外检测到中断来的时候,LED的状态进行取反
switch(GPIO_Pin)
{
/*光电开关----->LED2*/
case GPIO_PIN_15:
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
printf("光电开关############");
break;
/*火焰传感器----->LED1*/
case GPIO_PIN_5:
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_10);
printf("火焰传感器############");
break;
/*人体红外----->LED3*/
case GPIO_PIN_12:
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_8);
printf("人体红外传感器############");
break;
}
}
/* USER CODE END 2 *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/