网站建设哪家企业好江西省住房和城乡建设网站
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. 写程序
uart.h
#ifndef _UART_H_
#define _UART_H_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);
char getchar(void);
void putchar(char c);#endif
 
uart.c
#include "uart.h"
void uart_init(void)
{USART_TypeDef *usart1 = (USART_TypeDef *)0x40013800;//使能GPIOA/USART1模块unsigned int *pRcc = (unsigned int *)(0x40021000 +  0x18);*pRcc |= (1<<2);pRcc = (unsigned int *)(0x40021000 +  0x18);*pRcc |= (1<<14);//配置引脚功能unsigned int *pMode = (unsigned int *)(0x40010800 + 0x04);*pMode &= ~((3<<4) | (3<<6));//PA9 -TX*pMode |= (1<<4) | (2<<6);*pMode &= ~((3<<8) | (3<<10));//PA10 -RX*pMode |= (0<<8) | (1<<10);/*设置波特率* 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*/
#define DIV_Mantissa 4
#define DIV_Fraction 5usart1 -> BRR = (DIV_Mantissa << 4) | (5);//设置数据格式usart1 -> CR1 = (1 << 13) | (0 << 12) | (0 << 10) | (1 << 3) | (1 << 2);usart1 -> CR2 &= ~(0x11 << 12); //1位停止位//使能USART1//在配置CR1时已经使能tx,rx
}char getchar(void)
{USART_TypeDef *usart1 = (USART_TypeDef *)0x40013800;while((usart1 -> SR & (1 << 5)) == 0);//出来循环就是有数据return usart1 -> DR;
}void putchar(char c)
{USART_TypeDef *usart1 = (USART_TypeDef *)0x40013800;while((usart1 -> SR & (1 << 7)) == 0);//出来循环就是发送成功usart1 -> DR = c;} 
main.c
#include "uart.h"
void delay(int time)
{while(time --);}
int main()
{uart_init();char c;putchar('1');putchar('0');putchar('0');putchar('a');putchar('s');putchar('k');putchar('\n');putchar('\r');    while(1){c = getchar();putchar(c);putchar(c+1);}}
 
测试结果:


