当前位置:网站首页>STM8S105K4T6------Serial port sending and receiving
STM8S105K4T6------Serial port sending and receiving
2022-08-04 02:35:00 【kick the player】
The last article talked about how to light up the boardLED,这次来讲,stm8s105k4t6的串口发送数据
不会点亮LEDlights can be seen—>stm8点亮LED
Serial port sending part
步骤: 在点亮LEDAdd a light to the baseuart.c和uart.h文件 See my picture below for the code,I explain the code later

记得要#include“uart.h”Header file and initialization serial port configuration function

main.c就几行代码,Just copy me ,下面放uart.c和uart.h的代码

Here is the initialization of the serial port pins

UART.C代码
#include "usart.h"
void Usart2_Config(void)
{
//Send and receive sockets are initialized
GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART2, ENABLE);
UART2_Init((uint32_t)115200, UART2_WORDLENGTH_8D, UART2_STOPBITS_1,UART2_PARITY_NO,UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE);
UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE);//开启接收中断
UART2_Cmd(ENABLE);
}
void Usart2_SendByte(u8 Byte)
{
UART2_SendData8(Byte);
while((UART2->SR & 0x80) == 0x00)
{
}
}
void Usart2_SendHalfWord(uint16_t ch)
{
uint8_t temp_h, temp_l;
/* 取出高八位 */
temp_h = (ch&0XFF00)>>8;
/* 取出低八位 */
temp_l = ch&0XFF;
/* 发送高八位 */
Usart2_SendByte(temp_h); /* 发送低八位 */
Usart2_SendByte(temp_l);
}
void Usart2_SendByteArr(u8* Buff, u16 Size)
{
while(Size--)
{
Usart2_SendByte(*Buff++);
}
}
void Usart2_SendHalfWordArr(u16* Buff, u16 Size)
{
while(Size--)
{
Usart2_SendHalfWord(*Buff++);
}
}
void Usart2_SendString(char *str)
{
unsigned int k=0;
Delay_us(100);
do
{
Usart2_SendByte(*(str + k));
k++;
}while(*(str + k)!='\0');
Delay_us(100);
}
这几个函数,It is to encapsulate the sent bytes,数组,字符串的函数,这样封装后,The code can directly call the function to realize the sending function
UART.H代码
#ifndef __USART_H
#define __USART_H
#include "stm8s_conf.h"
#define UART2_FRAME_LENGHT 30
//Interrupt buffer serial port data length
extern u8 Uart2_Buff[UART2_FRAME_LENGHT], Uart2_RecNum;
void Usart2_Config(void);
void Usart2_SendHalfWord(uint16_t ch);
void Usart2_SendByte(u8 Byte);
void Usart2_SendByteArr(u8* Buff, u16 Size);
void Usart2_SendHalfWordArr(u16* Buff, u16 Size);
void Usart2_SendString(char *str);
#endif

Finally, the serial port can send data,I have this because of the poor contact of the Dupont wire, Only when there is signal interference
串口接收部分
Because the function to be implemented here is:What is received by the serial port,We will send something out
只需要在main.cIt is enough to add the code to turn on the total interrupt
This is done in the interrupt code
This will make hair1,LED亮,否则灭
经过测试,都是没问题的,You should also try it now
边栏推荐
- How to drop all tables under database in MySQL
- Big guys, it takes a long time to read mysql3 million single tables, what parameters can be discounted, or is there any way to hurry up
- C program compilation and predefined detailed explanation
- 香港服务器有哪些常用的型号
- Ant - the design of the Select component using a custom icon (suffixIcon attribute) suffixes, click on the custom ICONS have no reaction, will not display the drop-down menu
- Intranet penetration - application
- 云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
- Flutter3.0线程——四步教你如何全方位了解(事件队列)
- C语言力扣第54题之螺旋矩阵。模拟旋转
- 实例035:设置输出颜色
猜你喜欢

MySQL高级-读写分离-分库分表

Simple record of Flink principle flow chart

Qt中对象树的机制介绍以及底层实现,各种结果分析:(以及自己写容易犯错的点)

小甲鱼汇编笔记

In a more general sense, calculating the displacement distance and assumptions

Flutter3.0线程——四步教你如何全方位了解(事件队列)

Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).

Countdown to 2 days, the "New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" will kick off soon

sqoop ETL tool

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
随机推荐
keytool命令
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
DDTL: Domain Transfer Learning at a Distance
Security First: Tools You Need to Know to Implement DevSecOps Best Practices
织梦内核电动伸缩门卷闸门门业公司网站模板 带手机版【站长亲测】
Parquet encoding
Rongyun "Audio and Video Architecture Practice" technical session [complete PPT included]
Small Turtle Compilation Notes
实例040:逆序列表
Priority_queue element as a pointer, the overloaded operators
一文看懂推荐系统:召回04:离散特征处理,one-hot编码和embedding特征嵌入
activiti流程执行过程中,数据库表的使用关系
单片机C语言->的用法,和意思
融云「音视频架构实践」技术专场【内含完整PPT】
Zabbix set up email alert + enterprise WeChat alert
C语言力扣第54题之螺旋矩阵。模拟旋转
flinkcdc 消费 mysql binlog 没有 sqltype=delete 的数据是什么原
Use of lombok annotation @RequiredArgsConstructor
html select tag assignment database query result
SAP SD module foreground operation