当前位置:网站首页>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
边栏推荐
- 小程序+新零售,玩转行业新玩法!
- 实例037:排序
- 大佬们,读取mysql300万单表要很长时间,有什么参数可以优惠,或者有什么办法可以快点
- 2022广东省安全员A证第三批(主要负责人)考试题库及模拟考试
- Detailed analysis of scaffolding content
- uni-app 从零开始-基础模版(一)
- 小程序:扫码打开参数解析
- 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
- 实例035:设置输出颜色
- Snake game bug analysis and function expansion
猜你喜欢

2022广东省安全员A证第三批(主要负责人)考试题库及模拟考试

Flink原理流程图简单记录
编写 BOLL 心得体会

实例041:类的方法与变量

Detailed analysis of scaffolding content

Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain

Example 041: Methods and variables of a class

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

实例040:逆序列表

共n级台阶,每次可以上1级或2级台阶,有多少种上法?
随机推荐
sql有关问题,小时粒度,找到前一个小时内的数据
Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)
[Playwright Test Tutorial] 5 minutes to get started
In a more general sense, calculating the displacement distance and assumptions
共n级台阶,每次可以上1级或2级台阶,有多少种上法?
云开发旅游打卡广场微信小程序源码(含视频教程)
There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?
2022G1工业锅炉司炉考试练习题及模拟考试
FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
香港服务器有哪些常用的型号
html select tag assignment database query result
cdh6.x 集成spark-sql
sqoop ETL tool
Flutter3.0线程——四步教你如何全方位了解(事件队列)
C program compilation and predefined detailed explanation
C程序编译和预定义详解
esp8266-01s刷固件步骤
架构实战营模块三作业
什么是SVN(Subversion)?
STM32-遥感数据处理