当前位置:网站首页>Hal library operation STM32 serial port
Hal library operation STM32 serial port
2022-07-01 03:07:00 【two thousand and twenty-one point zero nine】
This blog knowledge comes from Mr. Wei Dongshan's 7 Internet of things course .
One 、cubeMX Generate engineering framework
First select the serial port from the left 1, Then select asynchronous communication .

Two 、 analysis program
Here's the picture ,cubeMX The serial port initialization function is automatically generated .

3、 ... and 、 Programming
After the above initialization , You can use HAL Library provides the “ HAL_UART_Transmit()” Send data from serial port , Use “ HAL_UART_Receive()” receive data , But it is inconvenient to use this way , You need to handle the data types yourself . I'm learning C Language , Usually use printf Format and print the data , It's more convenient . therefore , Here you need to redirect the print function , In the use of printf Called when the “ HAL_UART_Transmit()” Print .
printf and scanf Will call... Separately “ fputc()” and “ fgetc()”, So here we override these two functions , Use HAL The function provided realizes sending and receiving data .
Put the following code into usart.c in , Remember to add header file #include <stdio.h>.
printf and scanf Will call... Separately “ fputc()” and “ fgetc()”, So here we override these two functions , Use HAL The function provided realizes sending and receiving data .
usart.c
#pragma import(__use_no_semihosting)
struct __FILE
{
int a;
};
FILE __stdout;
FILE __stdin;
void _sys_exit(int x)
{
}
/*****************************************************
*function: Write character file function
*param1: Output characters
*param2: The file pointer
*return: The output character of ASCII code
******************************************************/
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 10);
return ch;
}
/*****************************************************
*function: Read character file function
*param1: The file pointer
*return: Read characters ASCII code
******************************************************/
int fgetc(FILE *f)
{
uint8_t ch = 0;
HAL_UART_Receive(&huart1, (uint8_t*)&ch, 1, 10);
return (int)ch;
}Write the following code in the main function
#include <stdio.h>
printf(" Baiwen Technology www.100ask.net\n\r");
printf("UART experiment \n\r");
printf("test char = %c,%c\n\r", 'H', 'c');
printf("test string1 = %s\n\r", "www.100ask.net");
printf("test string2 = %s\n\r", " Shenzhen Baiwen Technology Co., Ltd ");
printf("test decimal1 number = %d\n\r", 123456);
printf("test decimal2 number = %d\n\r", -123456);
printf("test hex1 number = 0x%x\n\r", 0x123456);
printf("test hex2 number = 0x%08x\n\r", 0x123456);
printf("test float = %.5f\n\r", 3.1415);
printf("test double = %.10lf\n\r", 3.141592653);Four 、 Effect validation
open MobaXterm Follow these steps


The final result

边栏推荐
- Share Creators萌芽人才培养计划来了!
- 手把手带你了解一块电路板,从设计到制作(干货)
- So easy 将程序部署到服务器
- 伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
- A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
- Cloud native annual technology inventory is released! Ride the wind and waves at the right time
- Introduction to ieda right click source file menu
- [QT] add knowledge supplement of third-party database
- Completely solve the lost connection to MySQL server at 'reading initial communication packet
- Network address translation (NAT) technology
猜你喜欢

最新接口自动化面试题

Network address translation (NAT) technology

Stop saying that you can't solve the "cross domain" problem
![[small program project development -- Jingdong Mall] the home page commodity floor of uni app](/img/80/20bed20a6ab91e82ad6800b11f2caa.png)
[small program project development -- Jingdong Mall] the home page commodity floor of uni app

MySQL index --01--- design principle of index

【小程序项目开发-- 京东商城】uni-app之首页商品楼层

Redis高效点赞与取消功能

Complete training and verification of a neural network based on pytorch

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)
随机推荐
Mouse over effect 9
CX5120控制汇川IS620N伺服报错E15解决方案
Redis高效点赞与取消功能
Dart training and sphygmomanometer inflation pump power control DPC
Mouse over effect 10
Mysql知识点
Latest interface automation interview questions
Redis tutorial
Catch 222222
JS to find duplicate elements in two arrays
mybati sql 语句打印
Pytest -- plug-in writing
【Qt】添加第三方库的知识补充
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
EtherCAT原理概述
PCB defect detection based on OpenCV and image subtraction
Mouse over effect VI
【日常训练】1175. 质数排列
The best learning method in the world: Feynman learning method