当前位置:网站首页>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

边栏推荐
- pytest-fixture
- An article explaining the publisher subscriber model and the observer model
- Introduction to ieda right click source file menu
- Mnasnet learning notes
- Communication protocol -- Classification and characteristics Introduction
- C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
- js 找出两个数组中的重复元素
- Densenet network paper learning notes
- 第03章_用户与权限管理
- servlet【初识】
猜你喜欢

xxl-job使用指南

Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
![[machine learning] vectorized computing -- a must on the way of machine learning](/img/3f/d672bb254f845ea705b3a0ca10ee19.png)
[machine learning] vectorized computing -- a must on the way of machine learning

Redis高效点赞与取消功能
![[exsi] transfer files between hosts](/img/c3/128b72aca6e030b2d4be2b6bddbc43.png)
[exsi] transfer files between hosts

Restcloud ETL practice data row column conversion

Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example

最好用的信任关系自动化脚本(shell)

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

VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
随机推荐
Golang多图生成gif
别再说不会解决 “跨域“ 问题啦
EtherCAT简介
How to verify whether the contents of two files are the same
伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
[PR # 5 A] two way running (state pressure DP)
Mouse over effect III
[applet project development -- JD mall] uni app commodity classification page (first)
JS to find duplicate elements in two arrays
Druid monitoring statistics source
Elk elegant management server log
【小程序项目开发-- 京东商城】uni-app之分类导航区域
STM32——一线协议之DS18B20温度采样
实战 ELK 优雅管理服务器日志
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
第03章_用户与权限管理
【日常训练】1175. 质数排列
几行事务代码,让我赔了16万
Mysql知识点
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)