USART3 外接 RS485 UART7 外接 RS232 //USART支持同步传输,UART只支持异步传输,其他一样,备注一下 //本例程都做异步传输 1-5步在main.c文件中 1、STM32CubeMX自动生成
UART_HandleTypeDef huart7;
UART_HandleTypeDef huart3;
2、新增接收缓冲区
u8 aRxBuffer_uart3[64]={0};
u8 aRxBuffer_uart7[64]={0};
3、STM32CubeMX自动生成
static void MX_UART7_Init(void);
static void MX_USART3_UART_Init(void);
4、STM32CubeMX自动生成
int main(void)
{
......
MX_UART7_Init();
MX_USART3_UART_Init();
......
}
5、STM32CubeMX自动生成
static void MX_UART7_Init(void)
{
huart7.Instance = UART7;
huart7.Init.BaudRate = 115200;
huart7.Init.WordLength = UART_WORDLENGTH_8B;
huart7.Init.StopBits = UART_STOPBITS_1;
huart7.Init.Parity = UART_PARITY_NONE;
huart7.Init.Mode = UART_MODE_TX_RX;
huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart7.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart7) != HAL_OK)
{
Error_Handler();
}
HAL_UART_Receive_IT(&huart7, (u8 *)aRxBuffer_uart7, 1);**
}
static void MX_USART3_UART_Init(void)
{
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
HAL_UART_Receive_IT(&huart3, (u8 *)aRxBuffer_uart3, 1);
}
6、STM32CubeMX自动生成,如果没有自己加入 stm32f4xx.hal.msp.c文件中
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(huart->Instance==UART7)
{
......
HAL_NVIC_SetPriority(UART7_IRQn, 3, 3);
HAL_NVIC_EnableIRQ(UART7_IRQn);;
}
else if(huart->Instance==USART3)
{
......
HAL_NVIC_EnableIRQ(USART3_IRQn);
HAL_NVIC_SetPriority(USART3_IRQn, 3, 3);
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)函数中如果没有自动生成,加入: HAL_NVIC_DisableIRQ(UART7_IRQn); HAL_NVIC_DisableIRQ(USART3_IRQn); 这个不是必须的,不写也不影响.
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance==UART7)
{
__HAL_RCC_UART7_CLK_DISABLE();
HAL_GPIO_DeInit(GPIOF, GPIO_PIN_6|GPIO_PIN_7);
HAL_NVIC_DisableIRQ(UART7_IRQn);
}
else if(huart->Instance==USART3)
{
__HAL_RCC_USART3_CLK_DISABLE();
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11);
HAL_NVIC_DisableIRQ(USART3_IRQn);
}
}
7、STM32CubeMX自动生成,如果没有自己加入 stm32f4xx.it.c文件中
extern UART_HandleTypeDef huart3;
extern UART_HandleTypeDef huart7;
void USART3_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart3);
}
void UART7_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart7);
}
8、重写接收中断回调函数,测试 main.c中
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
static int a=0;
a++;
if(huart->Instance==USART3)
{
printf("USART3 %d..%d\n",a,aRxBuffer_uart3[0]);
HAL_UART_Receive_IT(&huart3, (uint8_t *)aRxBuffer_uart3, 1);
}
if(huart->Instance==UART7)
{
printf("UART7 %d..%d\n",a,aRxBuffer_uart7[0]);
HAL_UART_Receive_IT(&huart7, (uint8_t *)aRxBuffer_uart7, 1);
}
}
9、发送测试,这段代码可以放在main.c文件main()函数while(1){}中,或其他定时或外部中断函数中测试
static int TEAT_num=0;
TEAT_num++;
RS485_OUT=1;
delay_us(20);
HAL_UART_Transmit(&huart3, (uint8_t *)&TEAT_num, 1, 0xFFFF);
while(__HAL_UART_GET_FLAG(&huart3,UART_FLAG_TC)!=SET);
RS485_OUT=0;
HAL_UART_Transmit(&huart7, (uint8_t *)&TEAT_num, 4, 0xFFFF);
while(__HAL_UART_GET_FLAG(&huart7,UART_FLAG_TC)!=SET);
10、如果用printf 打印输出数据 则重定义int fputc(int ch, FILE *f);函数
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart7, (uint8_t *)&ch, 1, 0xFFFF);
while(__HAL_UART_GET_FLAG(&huart7,UART_FLAG_TC)!=SET);
return ch;
}
|