stm32f103 USART串口简单实现

时间:2021-03-04 22:26:42

(基于奋斗stm32_v5开发板)

1、创建工程并编写C程序

#include"stm32f10x.h"
#include"stdarg.h"

void USART1_Config()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	/* 使能USART1的时钟 */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA , ENABLE);
	
	/*配置USART1 TX,并设置为复用推挽输出模式*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/*配置USART1 RX,并设置为浮空输入模式*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/*配置USART1模式*/
	USART_InitStructure.USART_BaudRate = 57600;                               //波特率设置为57600//
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;               //配置串口传输的字长8位//
	USART_InitStructure.USART_StopBits = USART_StopBits_1;                    //配置停止位为1//
	USART_InitStructure.USART_Parity = USART_Parity_No;                       //不设置奇偶校验位//
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不采用硬件流//
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;						//配置串口为双线全双工通信,将RX、Tx模式都开启//
	USART_Init(USART1, &USART_InitStructure);																	//向寄存器写入配置参数//
	USART_Cmd(USART1, ENABLE);																								//使能USART1外设// 
}

static char *itoa(int value, char *string, int radix)
{
	int i, d;
	int flag = 0;
	char *ptr = string;
	
	/*此实现只适用于十进制数*/
	if(radix != 10)
	{
		*ptr = 0;
		return string;
  }
	
	if(!value)
	{
		*ptr++ = 0x30;
		*ptr = 0;
		return string;
	}
	
	/*如果这是负值,则插入减号*/
	if(value < 0)
	{
		*ptr++ = '-';
		value *= -1;
	}
	for(i = 10000; i > 0; i /= 10)
	{
		d = value / i;
		if (d || flag)
		{
			*ptr++ = (char)(d + 0x30);
			value -= (d * i);
			flag = 1;
		}
	}
	
	/*NULL终止字符串*/
	*ptr = 0;
	return string;
}

void USART1_printf(USART_TypeDef* USARTx, uint8_t *Data,...)
{
	const char *s;
	int d;
	char buf[16];
	va_list ap;
	va_start(ap, Data);
	while (*Data != 0)                                    //判断是否到达字符串结束符//
	{
		if(*Data == 0x5c)                                   //'\'//
		{
			switch( *++Data )
			{
				case 'r':                                       //回车符//
					USART_SendData(USARTx, 0x0d);
					Data ++;
					break;
				
				case 'n':                                       //换行符//
					USART_SendData(USARTx, 0x0a);
					Data ++;
					break;
				
				default:
					Data ++;
					break;
			}
		}
		else if (*Data == '%')
		{
			switch (*++Data)
			{
				case 's':                                        //字符串//
					s = va_arg(ap, const char *);
				for(;*s; s++)
				{
					USART_SendData(USARTx, *s);
					while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
				}
				Data++;
				break;
				
				case 'd':                                       //十进制//
					d = va_arg(ap, int);
					itoa(d, buf , 10);
					for(s = buf;*s;s++)
					{
						USART_SendData(USARTx, *s);
						while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
					}
					Data++;
					break;
					
					default:
						Data++;
						break;
			}
		}
		else USART_SendData(USARTx, *Data++);
		while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
	}
}

int main(void)
{
	/*初始化串口*/
	USART1_Config();
	
	USART1_printf(USART1,"\r\nHello Word\r\n");
	
	while(1);
}

2、连接J-Link,编译,将程序下载进stm32开发板

3、连接串口,打开串口调试助手进行调试

stm32f103 USART串口简单实现

看到打印Hello World,目标完成