18. Jiangke University stm32 video study notes - USART serial port transmission & serial port transmission and reception

Table of contents

1. USART serial port sending 

1. Circuit diagram

2. The transplant method of printf function

3、serial.c

4、main.c

5. Solve the problem of writing Chinese characters directly, and the compiler reports an error

2. USART serial port sending and receiving

1. Query realization

2. Interrupt implementation 

(1) Code added in Serial.c

(2) Called in the main function

(3) Ideas

(4) Complete Serial.c code

(5)mian.c


1. USART serial port sending 

1. Circuit diagram

 To cross-connect, so RX connects to TX

2. The transplant method of printf function

Before using printf, open the project option first

In serial.c, add #include <stdio.h>, and rewrite the fputc function in the file

int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}

fputc is the bottom layer of the printf function. When printf prints, it continuously calls the fputc function to print one by one. Now that the fputc function is redirected to the serial port, printf will naturally output to the serial port.

Include the header file in the serial.h file

#include <stdio.h>

This method can only have one printf, if it is redirected to serial port 1, then serial port 2 cannot be used

If multiple serial ports want to use printf, use sprintf, sprintf can output formatted characters into a string

3、serial.c

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	

	GPIO_InitTypeDef GPIO_InitStructure;
	//TX引脚是USART外设控制的输出引脚,所以要选复用推挽输出
	//RX引脚是USART外设数据输入脚,所以要选择输入模式
	//一根线只能有一个输出,可以有多个输入,故输入脚,外设和GPIO都可以用
	//一般RX配置是浮空输入或者上拉输入,因为串口波形空闲状态是高电平,故不选择下拉输入
	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_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;//9600波特率
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件流控制
	USART_InitStructure.USART_Mode = USART_Mode_Tx;
	USART_InitStructure.USART_Parity = USART_Parity_No;//无校验
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//1位停止位
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位字长
	USART_Init(USART1, &USART_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
}

void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);//发送数据
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Array[i]);
	}
}

void Serial_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);
	}
}

uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;
	while (Y --)
	{
		Result *= X;
	}
	return Result;
}

//传输12345,相当于12345/10000%10=1,12345/1000%10=2
void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
	}
}

int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);//改发送给串口
	return ch;
}

//用来接收后面的可变参数列表
void Serial_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg, format);
	vsprintf(String, format, arg);
	va_end(arg);
	Serial_SendString(String);
}

4、main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"

int main(void)
{
	OLED_Init();
	
	Serial_Init();
	
	Serial_SendByte(0x41);//发送字节
	
	uint8_t MyArray[] = {0x42, 0x43, 0x44, 0x45};
	Serial_SendArray(MyArray, 4);//发送数组
	
	Serial_SendString("\r\nNum1=");//发送字符串
	
	Serial_SendNumber(111, 3);//发送n位数字
	
	printf("\r\nNum2=%d", 222);
	
	char String[100];
	sprintf(String, "\r\nNum3=%d", 333);
	Serial_SendString(String);
	
	Serial_Printf("\r\nNum4=%d", 444);
	Serial_Printf("\r\n");
	
	while (1)
	{
		
	}
}

5. Solve the problem of writing Chinese characters directly, and the compiler reports an error

--no-multibyte-chars 

Serial_Printf("Hello, world"); Chinese characters can be displayed on the display

2. USART serial port sending and receiving

1. Query realization

Query process: continuously judge the RXNE flag in the main function, if it is set to 1, it means that the data has been received

Then call ReceiveData to read the value of the DR register

	while(1)
	{
		if (USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == RET )
		{
            //DR完成读操作后会自动清零,故需要手动清0
			RxData = USART_ReceiveData(USART1);
			OLED_ShowHexNum(1 , 1 , RxData , 2);
		}
	}

In the serial port debugging assistant, send AF, and AF will be displayed on the display screen of the microcontroller. 

2. Interrupt implementation 

(1) Code added in Serial.c

     uint8_t Serial_RxData;
     uint8_t Serial_RxFlag;
     
    在初始化的函数中:
	//写入中断的代码
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启RXNE标志位到中断的输出
	
	//以下是配置NVIC
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//先分组
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1, ENABLE);//使能

    
uint8_t Serial_GetRxFlag(void)//读后自动清除的功能
{
	if (Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}


uint8_t Serial_GetRxData(void)
{
	return Serial_RxData;
}

void USART1_IRQHandler(void)
{
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)//判断标志位
	{
		//在中断中对数据进行转存
		Serial_RxData = USART_ReceiveData(USART1);//先读取模块的变量里
		Serial_RxFlag = 1;
		//如果读取DR,就自动清除,如果没有读取DR,就要手动清除
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);//清除标志位
	}
}

(2) Called in the main function

	while (1)
	{
		if (Serial_GetRxFlag() == 1)
		{
			RxData = Serial_GetRxData();
			Serial_SendByte(RxData);//把接收到的数据回传给电脑
			OLED_ShowHexNum(1, 8, RxData, 2);//显示在显示屏上
		}
	}

(3) Ideas

When RXNE=1, that is, when the computer serial port assistant has data to be transmitted to the development board, it enters the interrupt to
obtain the data on the computer, and manually clears it, encapsulates the obtained data with a function, calls
the function in the main function, and The acquired values ​​are displayed on the computer and in the display 

(4) Complete Serial.c code

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

uint8_t Serial_RxData;
uint8_t Serial_RxFlag;

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	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);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//接收+发送
	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);//开启RXNE标志位到中断的输出
	
	//以下是配置NVIC
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//先分组
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1, ENABLE);//使能
}

void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Array[i]);
	}
}

void Serial_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);
	}
}

uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;
	while (Y --)
	{
		Result *= X;
	}
	return Result;
}

void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
	}
}

int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}

void Serial_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg, format);
	vsprintf(String, format, arg);
	va_end(arg);
	Serial_SendString(String);
}


uint8_t Serial_GetRxFlag(void)//读后自动清除的功能
{
	if (Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}


uint8_t Serial_GetRxData(void)
{
	return Serial_RxData;
}

void USART1_IRQHandler(void)
{
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)//判断标志位
	{
		//在中断中对数据进行转存
		Serial_RxData = USART_ReceiveData(USART1);//先读取模块的变量里
		Serial_RxFlag = 1;
		//如果读取DR,就自动清除,如果没有读取DR,就要手动清除
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);//清除标志位
	}
}

(5)mian.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"

uint8_t RxData;

int main(void)
{
	OLED_Init();
	OLED_ShowString(1, 1, "RxData:");
	Serial_Init();

	while (1)
	{
		if (Serial_GetRxFlag() == 1)
		{
			RxData = Serial_GetRxData();
			Serial_SendByte(RxData);//把接收到的数据回传给电脑
			OLED_ShowHexNum(1, 8, RxData, 2);//显示在显示屏上
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_45981798/article/details/129432421