以USART1为例,解释
//以STM32F429IGT6为例
/** @addtogroup Peripheral_memory_map
* @{
*/
#define PERIPH_BASE ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region
/*!< Peripheral memory map */
#define APB2PERIPH_BASE (PERIPH_BASE + 0x00010000)
/*!< APB2 peripherals */
#define USART1_BASE (APB2PERIPH_BASE + 0x1000)
/** @addtogroup Peripheral_declaration
* @{
*/
#define USART1 ((USART_TypeDef *) USART1_BASE)//将USART1_BASE强转为指向USART_TypeDef类型的指针
//开辟一个以0x40000000+0x00010000+ 0x1000为地址的结构体指针
/**
* @brief Universal Synchronous Asynchronous Receiver Transmitter
*/
typedef struct
{
__IO uint32_t SR; /*!< USART Status register, Address offset: 0x00 */
__IO uint32_t DR; /*!< USART Data register, Address offset: 0x04 */
__IO uint32_t BRR; /*!< USART Baud rate register, Address offset: 0x08 */
__IO uint32_t CR1; /*!< USART Control register 1, Address offset: 0x0C */
__IO uint32_t CR2; /*!< USART Control register 2, Address offset: 0x10 */
__IO uint32_t CR3; /*!< USART Control register 3, Address offset: 0x14 */
__IO uint32_t GTPR; /*!< USART Guard time and prescaler register, Address offset: 0x18 */
} USART_TypeDef;
//初始化IO 串口1
//bound:波特率
void uart_init(u32 bound)
{
//UART 初始化设置
UART1_Handler.Instance=USART1; //USART1
UART1_Handler.Init.BaudRate=bound; //波特率
UART1_Handler.Init.WordLength=UART_WORDLENGTH_8B; //字长为8位数据格式
UART1_Handler.Init.StopBits=UART_STOPBITS_1; //一个停止位
UART1_Handler.Init.Parity=UART_PARITY_NONE; //无奇偶校验位
UART1_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //无硬件流控
UART1_Handler.Init.Mode=UART_MODE_TX_RX; //收发模式
HAL_UART_Init(&UART1_Handler); //HAL_UART_Init()会使能UART1
}
以USART1为例,解释
//以STM32F429IGT6为例
/** @addtogroup Peripheral_memory_map
* @{
*/
#define PERIPH_BASE ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region
/*!< Peripheral memory map */
#define APB2PERIPH_BASE (PERIPH_BASE + 0x00010000)
/*!< APB2 peripherals */
#define USART1_BASE (APB2PERIPH_BASE + 0x1000)
/** @addtogroup Peripheral_declaration
* @{
*/
#define USART1 ((USART_TypeDef *) USART1_BASE)//将USART1_BASE强转为指向USART_TypeDef类型的指针
//开辟一个以0x40000000+0x00010000+ 0x1000为地址的结构体指针
/**
* @brief Universal Synchronous Asynchronous Receiver Transmitter
*/
typedef struct
{
__IO uint32_t SR; /*!< USART Status register, Address offset: 0x00 */
__IO uint32_t DR; /*!< USART Data register, Address offset: 0x04 */
__IO uint32_t BRR; /*!< USART Baud rate register, Address offset: 0x08 */
__IO uint32_t CR1; /*!< USART Control register 1, Address offset: 0x0C */
__IO uint32_t CR2; /*!< USART Control register 2, Address offset: 0x10 */
__IO uint32_t CR3; /*!< USART Control register 3, Address offset: 0x14 */
__IO uint32_t GTPR; /*!< USART Guard time and prescaler register, Address offset: 0x18 */
} USART_TypeDef;
//初始化IO 串口1
//bound:波特率
void uart_init(u32 bound)
{
//UART 初始化设置
UART1_Handler.Instance=USART1; //USART1
UART1_Handler.Init.BaudRate=bound; //波特率
UART1_Handler.Init.WordLength=UART_WORDLENGTH_8B; //字长为8位数据格式
UART1_Handler.Init.StopBits=UART_STOPBITS_1; //一个停止位
UART1_Handler.Init.Parity=UART_PARITY_NONE; //无奇偶校验位
UART1_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //无硬件流控
UART1_Handler.Init.Mode=UART_MODE_TX_RX; //收发模式
HAL_UART_Init(&UART1_Handler); //HAL_UART_Init()会使能UART1
}
举报