STM32F103 USART serial port simple implementation

Publisher:SereneWhisperLatest update time:2018-12-17 Source: eefocusKeywords:stm32f103  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

(Based on the stm32_v5 development board)


1. Create a project and write a C program


#include"stm32f10x.h"

#include"stdarg.h"

 

void USART1_Config()

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;


/* Enable USART1 clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA , ENABLE);


/*Configure USART1 TX and set it to multiplexed push-pull output mode*/

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);


/*Configure USART1 RX and set it to floating input mode*/

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);


/*Configure USART1 mode*/

USART_InitStructure.USART_BaudRate = 57600; //Set the baud rate to 57600//

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Configure the word length of serial port transmission to 8 bits//

USART_InitStructure.USART_StopBits = USART_StopBits_1; //Configure the stop bit to 1//

USART_InitStructure.USART_Parity = USART_Parity_No; //Do not set the parity bit //

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //Do not use hardware flow//

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Configure the serial port to two-wire full-duplex communication, turn on both RX and Tx modes //

USART_Init(USART1, &USART_InitStructure); //Write configuration parameters to registers//

USART_Cmd(USART1, ENABLE); //Enable USART1 peripheral// 

}

 

static char *itoa(int value, char *string, int radix)

{

int i, d;

int flag = 0;

char *ptr = string;


/*This implementation only works for decimal numbers*/

if(radix != 10)

{

*ptr = 0;

return string;

  }


if(!value)

{

*ptr++ = 0x30;

*ptr = 0;

return string;

}


/*If this is a negative value, insert a minus sign*/

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 terminated string*/

*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) //Judge whether the string terminator has been reached//

{

if(*Data == 0x5c)                                   //'\'//

{

switch( *++Data )

{

case 'r': //Carriage return character//

USART_SendData(USARTx, 0x0d);

Data ++;

break;


case 'n': //line break//

USART_SendData(USARTx, 0x0a);

Data ++;

break;


default:

Data ++;

break;

}

}

else if (*Data == '%')

{

switch (*++Data)

{

case 's': // string //

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': //decimal//

d = va_arg(ap, int);

drown(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)

{

/*Initialize the serial port*/

USART1_Config();


USART1_printf(USART1,"\r\nHello Word\r\n");


while(1);

}


2. Connect J-Link, compile, and download the program into the stm32 development board


3. Connect the serial port and open the serial port debugging assistant for debugging



See Hello World printed, the goal is completed

Keywords:stm32f103  USART Reference address:STM32F103 USART serial port simple implementation

Previous article:STM32 serial port register library function configuration method
Next article:STM32F103 series practical universal synchronous asynchronous receiver and transmitter (USART)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号