1.USART的配置
(1)关键结构体
GPIO_InitTypeDef
USART_InitTypeDef
NVIC_InitTypeDef//用于接收的中断
1.1USART的TX在PA9,RX在PA10.
2.时钟线
2.1 USART时钟挂载在APB2上
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//配时钟
3.代码段
3.1(单发送)
void usart_init(void){
GPIO_InitTypeDef gpio_initStructure;
USART_InitTypeDef usart_initStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//配时钟
//配置usart的pa9 TX
gpio_initStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_initStructure.GPIO_Pin = GPIO_Pin_9;
gpio_initStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA,&gpio_initStructure);
//配置usart的pa10 RX receive
gpio_initStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio_initStructure.GPIO_Pin = GPIO_Pin_10;
gpio_initStructure.GPIO_Speed = GPIO_Speed_50MHz;
usart_initStructure.USART_BaudRate = 115200;
usart_initStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usart_initStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
usart_initStructure.USART_Parity = USART_Parity_No;
usart_initStructure.USART_StopBits = USART_StopBits_1;
usart_initStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &usart_initStructure);//初始化所使用的串口外设
USART_Cmd( USART1, ENABLE);//使能或者失能USART外设
}
//发送字符
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData(USARTx, Data);
while( USART_GetFlagStatus( USARTx, USART_FLAG_TXE)==RESET);
}
//发送字符串
void USARTSendstr(USART_TypeDef* USARTx,char *str)
{
uint16_t i=0;
do{
USARTSendByte(USARTx,*(str+i));
i++;
}
while(*(str+i)!='\0');
while(USART_GetFlagStatus( USARTx, USART_FLAG_TC)==RESET);
}
//重定义发送让32可以用stdio库
int fputc(int ch,FILE *f){
USART_SendData(USART1,(uint8_t)ch);//ch是传入参数,int强制转化为uint防止出现其他现象
while( USART_GetFlagStatus( USART1, USART_FLAG_TXE)==RESET);
return ch;
}
3.2 添加接收中断
NVIC_InitTypeDef NVICinitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置优先级分别组
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_IT_RXNE标志位检测,为接收中断标志位,也就是说,串口一接收到数据,这两个标志位都会被置位。
NVICinitStructure.NVIC_IRQChannel = USART1_IRQn;//在f10x.h里面
NVICinitStructure.NVIC_IRQChannelCmd = ENABLE;
NVICinitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVICinitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVICinitStructure);
3.3全部代码
#include "usart.h"
#include "stm32f10x.h"
#include "stdio.h"
void usart_init(void){
GPIO_InitTypeDef gpio_initStructure;
USART_InitTypeDef usart_initStructure;
NVIC_InitTypeDef NVICinitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//配时钟
//配置usart的pa9 TX
gpio_initStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_initStructure.GPIO_Pin = GPIO_Pin_9;
gpio_initStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA,&gpio_initStructure);
//配置usart的pa10 RX receive
gpio_initStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio_initStructure.GPIO_Pin = GPIO_Pin_10;
gpio_initStructure.GPIO_Speed = GPIO_Speed_50MHz;
usart_initStructure.USART_BaudRate = 115200;
usart_initStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usart_initStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
usart_initStructure.USART_Parity = USART_Parity_No;
usart_initStructure.USART_StopBits = USART_StopBits_1;
usart_initStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &usart_initStructure);//初始化所使用的串口外设
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd( USART1, ENABLE);//使能或者失能USART外设
NVICinitStructure.NVIC_IRQChannel = USART1_IRQn;//在f10x.h里面
NVICinitStructure.NVIC_IRQChannelCmd = ENABLE;
NVICinitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVICinitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVICinitStructure);
}
//发送字符
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData(USARTx, Data);
while( USART_GetFlagStatus( USARTx, USART_FLAG_TXE)==RESET);
}
//发送字符串
void USARTSendstr(USART_TypeDef* USARTx,char *str)
{
uint16_t i=0;
do{
USARTSendByte(USARTx,*(str+i));
i++;
}
while(*(str+i)!='\0');
while(USART_GetFlagStatus( USARTx, USART_FLAG_TC)==RESET);
}
int fputc(int ch,FILE *f){
USART_SendData(USART1,(uint8_t)ch);//ch是传入参数,int强制转化为uint防止出现其他现象
while( USART_GetFlagStatus( USART1, USART_FLAG_TXE)==RESET);
return ch;
}
int fgetc(FILE*f){
while( USART_GetFlagStatus( USART1, USART_FLAG_RXNE)==RESET);
return (int)USART_ReceiveData(USART1);
}
3.4
使用stdio一定要勾选魔术棒的 Use MicroLIB