UART编程 #
1. 串口编程步骤
1.1 看原理图确定引脚
- 有很多串口,使用哪一个?看原理图确定
1.2 配置引脚为UART功能
- 至少用到发送、接收引脚:txd、rxd
- 需要把这些引脚配置为UART功能,并使能UART模块
1.3 设置串口参数
- 有哪些参数?
- 波特率
- 数据位
- 校验位
- 停止位
- 示例:
比如15200,8n1
表示波特率为115200,8个数据为,没有校验位,1个停止位
1.4 根据状态寄存器读写数据
- 肯定有一个数据寄存器,程序把数据写入,即刻通过串口向外发送数据
- 肯定有一个数据寄存器,程序读取这个寄存器,就可以获得先前接收到的数据
- 很多有状态寄存器
- 判断数据是否发送出去?是否发送成功?
- 判断是否接收到了数据?
2. STM32F103串口框架
各类芯片的UART框图都是类似的,当设置好UART后,程序读写数据寄存器就可以接收、发送数据了。
3. STM32F103串口操作
3.1 看原理图确定引脚
- 100ASM STM32F103的USART1接到一个USB串口芯片,然后就可以通过USB线连接电脑了
- 原理图如下
- 上图中的USART1_RX、USART1_TX,接到了PA9、PA10
3.2 配置引脚为UART功能
3.2.1 使能GPIOA/USART1模块
需要设置GPIOA的寄存器,选择引脚功能:所以要使能GPIOA模块。
GPIOA模块、USART1模块的使能都是在同一个寄存器里实现。
3.2.2 配置引脚功能
从上图可以知道,PA9、PA10有三种功能:GPIO、USART1、TIMER1。
3.3 设置串口参数
3.3.1 设置波特率
波特率算公式:
USARTDIV由整数部分、小数部分组成,计算公式如下:
USARTDIV = DIV_Mantissa + (DIV_Fraction / 16)
DIV_Mantissa和DIV_Fraction来自USART_BRR寄存器,如下图:
3.3.2 设置数据格式
比如数据位设置为8,无校验位,停止位设置为1。
需要设置2个寄存器。
- USART1_CR1:用来设置数据位、校验位,使能USART
- USART_CR2:用来设置停止位
3.4 根据状态寄存器读写数据
- 状态寄存器
- 数据寄存器
写、读这个寄存器,就可:发送、读取串口数据,如下图:
3.5 USART1的寄存器地址
- 基地址
- USART寄存器
用结构体来表示比较方便:
typedef unsigned int uint32_t;
typedef struct
{
volatile uint32_t SR; /*!< USART Status register, Address offset: 0x00 */
volatile uint32_t DR; /*!< USART Data register, Address offset: 0x04 */
volatile uint32_t BRR; /*!< USART Baud rate register, Address offset: 0x08 */
volatile uint32_t CR1; /*!< USART Control register 1, Address offset: 0x0C */
volatile uint32_t CR2; /*!< USART Control register 2, Address offset: 0x10 */
volatile uint32_t CR3; /*!< USART Control register 3, Address offset: 0x14 */
volatile uint32_t GTPR; /*!< USART Guard time and prescaler register, Address offset: 0x18 */
} USART_TypeDef;
USART_TypeDef *usart1 = (USART_TypeDef *)0x40013800;
4. 写程序
typedef unsigned int uint32_t;
typedef struct
{
volatile uint32_t SR; /*!< USART Status register, Address offset: 0x00 */
volatile uint32_t DR; /*!< USART Data register, Address offset: 0x04 */
volatile uint32_t BRR; /*!< USART Baud rate register, Address offset: 0x08 */
volatile uint32_t CR1; /*!< USART Control register 1, Address offset: 0x0C */
volatile uint32_t CR2; /*!< USART Control register 2, Address offset: 0x10 */
volatile uint32_t CR3; /*!< USART Control register 3, Address offset: 0x14 */
volatile uint32_t GTPR; /*!< USART Guard time and prescaler register, Address offset: 0x18 */
} USART_TypeDef;
void uart_init(void)
{
USART_TypeDef *usart1 = (USART_TypeDef *)0x40013800;
volatile unsigned int *pReg;
/* 使能GPIOA/USART1模块 */
/* RCC_APB2ENR */
pReg = (volatile unsigned int *)(0x40021000 + 0x18);
*pReg |= (1<<2) | (1<<14);
/* 配置引脚功能: PA9(USART1_TX), PA10(USART1_RX)
* GPIOA_CRH = 0x40010800 + 0x04
*/
pReg = (volatile unsigned int *)(0x40010800 + 0x04);
/* PA9(USART1_TX) */
*pReg &= ~((3<<4) | (3<<6));
*pReg |= (1<<4) | (2<<6); /* Output mode, max speed 10 MHz; Alternate function output Push-pull */
/* PA10(USART1_RX) */
*pReg &= ~((3<<8) | (3<<10));
*pReg |= (0<<8) | (1<<10); /* Input mode (reset state); Floating input (reset state) */
/* 设置波特率
* 115200 = 8000000/16/USARTDIV
* USARTDIV = 4.34
* DIV_Mantissa = 4
* DIV_Fraction / 16 = 0.34
* DIV_Fraction = 16*0.34 = 5
* 真实波特率:
* DIV_Fraction / 16 = 5/16=0.3125
* USARTDIV = DIV_Mantissa + DIV_Fraction / 16 = 4.3125
* baudrate = 8000000/16/4.3125 = 115942
*/
usart1->BRR = (DIV_Mantissa<<4) | (DIV_Fraction);
/* 设置数据格式: 8n1 */
usart1->CR1 = (1<<13) | (0<<12) | (0<<10) | (1<<3) | (1<<2);
usart1->CR2 &= ~(3<<12);
/* 使能USART1 */
}
int getchar(void)
{
USART_TypeDef *usart1 = (USART_TypeDef *)0x40013800;
while ((usart1->SR & (1<<5)) == 0);
return usart1->DR;
}
int putchar(char c)
{
USART_TypeDef *usart1 = (USART_TypeDef *)0x40013800;
while ((usart1->SR & (1<<7)) == 0);
usart1->DR = c;
return c;
}