当前位置:网站首页>STM8S105K4T6------串口发送和接收
STM8S105K4T6------串口发送和接收
2022-08-04 02:20:00 【挨踢玩家】
上篇文章讲了如何点亮板载LED,这次来讲,stm8s105k4t6的串口发送数据
不会点亮LED灯可以看—>stm8点亮LED
串口发送部分
步骤: 在点亮LED灯基础上增加一个uart.c和uart.h文件 代码看我下面的图,我后面讲解代码
记得要#include“uart.h”头文件和初始化串口配置函数
main.c就几行代码,照抄我都就行了 ,下面放uart.c和uart.h的代码
这里是是对串口引脚的初始化
UART.C代码
#include "usart.h"
void Usart2_Config(void)
{
//发送接收引角口初始化
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);
}
这几个函数,就是封装发送字节,数组,字符串的函数,这样封装后,代码直接调用函数就能够实现发送的功能
UART.H代码
#ifndef __USART_H
#define __USART_H
#include "stm8s_conf.h"
#define UART2_FRAME_LENGHT 30
//中断缓存串口数据长度
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
最后串口就能实现发送数据啦,我这是因为杜邦线接触不良, 有信号干扰才这样
串口接收部分
因为这里要实现的功能是:串口接收到什么,我们就发什么出去
只需要在main.c里面加上开启总中断的代码就行了
在中断代码里面这样写就行了
这样就能实现发1,LED亮,否则灭
经过测试,都是没问题的,你也赶紧试试吧
边栏推荐
- Web APIs BOM- 操作浏览器:swiper 插件
- FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
- 实例036:算素数
- 小程序:扫码打开参数解析
- 脚手架内容详解分析
- 共n级台阶,每次可以上1级或2级台阶,有多少种上法?
- JS 保姆级贴心,从零教你手写实现一个防抖debounce方法
- Flink jdbc connector 源码改造sink之 clickhouse多节点轮询写与性能分析
- 织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
- Zabbix set up email alert + enterprise WeChat alert
猜你喜欢
Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).
Utilities of Ruineng Micrometer Chip RN2026
董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
Example 040: Reverse List
html select tag assignment database query result
There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?
第08章 索引的创建与设计原则【2.索引及调优篇】【MySQL高级】
Web APIs BOM- 操作浏览器:swiper 插件
MallBook联合人民交通出版社,推动驾培领域新发展,开启驾培智慧交易新生态
idea中diagram使用
随机推荐
ant-design的Select组件采用自定义后缀图标(suffixIcon属性)时,点击该自定义图标没有反应,不会展示下拉菜单的问题
什么是SVN(Subversion)?
2022年T电梯修理考题及答案
持续投入商品研发,叮咚买菜赢在了供应链投入上
多线程 之 JUC 学习篇章一 创建多线程的步骤
2022年茶艺师(中级)考试试题模拟考试平台操作
Qt中对象树的机制介绍以及底层实现,各种结果分析:(以及自己写容易犯错的点)
html select tag assignment database query result
Simple record of Flink principle flow chart
[Original] Start the XPS/OXPS reader that comes with Windows 10
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
脚手架内容详解分析
In the season of going overseas, the localization of Internet tips for going overseas
2022G1工业锅炉司炉考试练习题及模拟考试
【学习笔记之菜Dog学C】动态内存管理
关联接口测试
实例037:排序
sql有关问题,小时粒度,找到前一个小时内的数据