MoistureSoftware/Source/usart.c

149 lines
5.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**************** (C) COPYRIGHT 2023 山东云唐智能科技有限公司 *******************
* 文 件 名:
* 创 建 者: Kaiser
* 描 述 :
* 最后修改:
*********************************** 修订记录 ************************************
* 版 本:
* 修订人:
********************************************************************************/
/******************************** 功能说明 *************************************
*
*******************************************************************************/
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/******************************** 功能说明 *************************************
* 串口3端口配置
*******************************************************************************/
void BIOS_USART3_Init(void)
{
USART_TypeDef *USARTx = USART3;
SET_BIT(RCC->APB1ENR, RCC_APB1ENR_USART3EN);
// USART1 configured as follow:
// - BaudRate = 9600 baud
// - Word Length = 8 Bits
// - One Stop Bit
// - No parity
// - Hardware flow control disabled (RTS and CTS signals)
// - Receive disable and transmit enabled
USARTx->BRR = SystemCoreClock / 9600u / 2u; /* 115200 bps */
USARTx->CR1 = 0x0000u; /* 1 start bit, 8 data bits */
USARTx->CR2 = 0x0000u; /* 1 stop bit */
USARTx->CR3 = 0x0000u; /* no flow control */
SET_BIT(USARTx->CR1, USART_CR1_TE); /* enable TX */
SET_BIT(USARTx->CR1, USART_CR1_RE); /* enable TX */
SET_BIT(USARTx->CR1, USART_CR1_UE); /* Enable USARTx */
USART_ITConfig(USART3, /*USART_IT_TXE |*/ USART_IT_RXNE, ENABLE);
/* Configure USART1 Rx (PA10) as input floating */
/* Configure USART1 Tx (PA9) as alternate function push-pull */
SET_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPBEN);
MODIFY_REG(GPIOB->CRH, 0x0000FF00u, 0x00004B00u);
NVIC_EnableIRQ(USART3_IRQn);
}
/******************************** 功能说明 *************************************
* 访问 USART3实现RS232收发
*******************************************************************************/
void UART3_Send(uint8_t OutByte)
{
USART_TypeDef *USARTx = USART3;
while (!(READ_BIT(USARTx->SR, USART_SR_TXE))) {
;
}
USARTx->DR = OutByte;
}
/******************************** 功能说明 *************************************
* 访问 USART3实现RS232收发
*******************************************************************************/
uint8_t UART3_Received(void)
{
USART_TypeDef *USARTx = USART3;
while (!(READ_BIT(USARTx->SR, USART_SR_RXNE))) {
;
}
return USARTx->DR;
}
/******************************** 功能说明 *************************************
* 串口2端口配置
*******************************************************************************/
void BIOS_USART2_Init(uint32_t baudrate)
{
USART_TypeDef *USARTx = USART2;
SET_BIT(RCC->APB1ENR, RCC_APB1ENR_USART2EN);
// USART1 configured as follow:
// - BaudRate = 9600 baud
// - Word Length = 8 Bits
// - One Stop Bit
// - No parity
// - Hardware flow control disabled (RTS and CTS signals)
// - Receive disable and transmit enabled
USARTx->BRR = SystemCoreClock / baudrate / 2u; /* 115200 bps */
USARTx->CR1 = 0x0000u; /* 1 start bit, 8 data bits */
USARTx->CR2 = 0x0000u; /* 1 stop bit */
USARTx->CR3 = 0x0000u; /* no flow control */
SET_BIT(USARTx->CR1, USART_CR1_TE); /* enable TX */
SET_BIT(USARTx->CR1, USART_CR1_RE); /* enable TX */
SET_BIT(USARTx->CR1, USART_CR1_UE); /* Enable USARTx */
/* Configure USART1 Rx (PA10) as input floating */
/* Configure USART1 Tx (PA9) as alternate function push-pull */
SET_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPAEN);
MODIFY_REG(GPIOA->CRL, 0x0000FF00u, 0x00004B00u);
}
/******************************** 功能说明 *************************************
* 访问 USART2实现TTL发
*******************************************************************************/
void USART2_Send(uint8_t OutByte)
{
USART_TypeDef *USARTx = USART2;
while (!(READ_BIT(USARTx->SR, USART_SR_TXE))) {
;
}
USARTx->DR = OutByte;
}
extern void delay_us(uint32_t us);
void USART2Print(uint8_t *buffer, uint8_t lenth)
{
uint8_t i;
for (i = 0u; i < lenth; i++) {
USART2_Send(*buffer++);
while (!READ_BIT(USART2->SR, USART_SR_TC))
delay_us(50u);
}
}
/******** (C) COPYRIGHT 2023 山东云唐智能科技有限公司 **** End Of File ********/