蓝桥杯篇---IAP15F2K61S2串口

embedded/2025/2/22 22:42:37/
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="markdown_views prism-atom-one-dark">cap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">

class="toc">

文章目录

  • 前言
  • 简介
  • 串口通信的基本参数
    • 1.波特率
    • 2.数据位
    • 3.停止位
    • 4.校验位
  • 串口相关寄存器
    • 1.SCON
    • 2.SBUF
    • 3.PCON
    • 4.TMOD
    • 5.TH1/TL1
  • 串口使用步骤
    • 1.配置波特率
    • 2.配置串口模式
    • 3.使能串口中断
    • 4.发送数据
    • 5.接收数据
    • 6.处理中断
  • 示例代码:串口发送与接收
  • 示例代码:串口接收数据并回显
  • 注意事项
    • 1.波特率设置
    • 2.中断优先级
    • 3.数据缓冲区
    • 4.错误处理
  • 总结


前言

本文仅仅简单介绍了IAP15F2K61S2的串口的使用。


简介

IAP15F2K61S2 是一款基于8051内核的class="tags" href="/DanPianJi.html" title=单片机>单片机࿰c;内置一个全双工的串行通信接口(UART)c;支持异步串行通信。串口通信是class="tags" href="/DanPianJi.html" title=单片机>单片机与外部设备(如PC、传感器、其他class="tags" href="/DanPianJi.html" title=单片机>单片机等)进行数据交换的常用方式。

串口通信的基本参数

1.波特率

波特率:数据传输速率࿰c;常见值有9600、19200、38400、57600、115200等。

2.数据位

数据位:每帧数据的位数࿰c;通常为8位

3.停止位

停止位:每帧数据结束的标志位࿰c;通常为1位

4.校验位

校验位:用于错误检测࿰c;可以是无校验、奇校验或偶校验

串口相关寄存器

1.SCON

SCON(串口控制寄存器):配置串口的工作模式和状态

2.SBUF

SBUF(串口数据缓冲器):用于发送和接收数据。

3.PCON

PCON(电源控制寄存器):包含波特率加倍控制位(SMOD)。

4.TMOD

TMOD(定时器模式寄存器):用于配置定时器以生成波特率

5.TH1/TL1

TH1/TL1:定时器1的初值寄存器࿰c;用于波特率生成

串口使用步骤

1.配置波特率

配置波特率:通过定时器1设置波特率。

2.配置串口模式

配置串口模式:设置 SCON 寄存器

3.使能串口中断

使能串口中断:如果需要中断方式接收数据c;需配置 IE 寄存器。

4.发送数据

发送数据:将数据写入 SBUF

5.接收数据

接收数据:从 SBUF 读取数据

6.处理中断

处理中断:在中断服务函数中处理接收或发送完成事件

示例代码:串口发送与接收

以下代码展示了如何使用串口在 IAP15F2K61S2 上实现数据的发送与接收。

<code class="prism language-c">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><reg52.h>class="token macro property">class="token directive-hash">#class="token directive keyword">define class="token macro-name">uchar class="token expression">class="token keyword">unsigned class="token keyword">char
class="token macro property">class="token directive-hash">#class="token directive keyword">define class="token macro-name">uint class="token expression">class="token keyword">unsigned class="token keyword">intclass="token keyword">void class="token function">UART_Initclass="token punctuation">(class="token punctuation">) class="token punctuation">{SCON class="token operator">= class="token number">0x50class="token punctuation">;  class="token comment">// 串口模式1࿰c;8位数据࿰c;1位停止位TMOD class="token operator">|= class="token number">0x20class="token punctuation">; class="token comment">// 定时器1࿰c;模式2(8位自动重装)TH1 class="token operator">= class="token number">0xFDclass="token punctuation">;   class="token comment">// 波特率9600TL1 class="token operator">= class="token number">0xFDclass="token punctuation">;ES class="token operator">= class="token number">1class="token punctuation">;       class="token comment">// 使能串口中断EA class="token operator">= class="token number">1class="token punctuation">;       class="token comment">// 使能总中断TR1 class="token operator">= class="token number">1class="token punctuation">;      class="token comment">// 启动定时器1
class="token punctuation">}class="token keyword">void class="token function">UART_SendByteclass="token punctuation">(uchar datclass="token punctuation">) class="token punctuation">{SBUF class="token operator">= datclass="token punctuation">;   class="token comment">// 将数据写入SBUFclass="token keyword">while class="token punctuation">(class="token operator">!TIclass="token punctuation">)class="token punctuation">;  class="token comment">// 等待发送完成TI class="token operator">= class="token number">0class="token punctuation">;       class="token comment">// 清除发送中断标志
class="token punctuation">}class="token keyword">void class="token function">UART_SendStringclass="token punctuation">(class="token keyword">char class="token operator">*strclass="token punctuation">) class="token punctuation">{class="token keyword">while class="token punctuation">(class="token operator">*strclass="token punctuation">) class="token punctuation">{class="token function">UART_SendByteclass="token punctuation">(class="token operator">*strclass="token operator">++class="token punctuation">)class="token punctuation">;  class="token comment">// 逐个发送字符class="token punctuation">}
class="token punctuation">}class="token keyword">void class="token function">UART_ISRclass="token punctuation">(class="token punctuation">) interrupt class="token number">4 class="token punctuation">{class="token keyword">if class="token punctuation">(RIclass="token punctuation">) class="token punctuation">{     class="token comment">// 如果接收中断标志置位RI class="token operator">= class="token number">0class="token punctuation">;   class="token comment">// 清除接收中断标志P1 class="token operator">= SBUFclass="token punctuation">; class="token comment">// 将接收到的数据输出到P1口class="token punctuation">}
class="token punctuation">}class="token keyword">void class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{class="token function">UART_Initclass="token punctuation">(class="token punctuation">)class="token punctuation">;  class="token comment">// 初始化串口class="token function">UART_SendStringclass="token punctuation">(class="token string">"Hello, World!\n"class="token punctuation">)class="token punctuation">;  class="token comment">// 发送字符串class="token keyword">while class="token punctuation">(class="token number">1class="token punctuation">)class="token punctuation">;    class="token comment">// 主循环࿰c;等待中断
class="token punctuation">}
代码说明
UART_Init:
设置串口为模式class="token number">1class="token number">8位数据࿰c;class="token number">1位停止位)。
配置定时器class="token number">1为模式class="token number">2class="token number">8位自动重装)࿰c;设置波特率为class="token number">9600。
使能串口中断和总中断。
启动定时器class="token number">1。UART_SendByte:
将数据写入 SBUF࿰c;启动发送。
等待发送完成(TI class="token operator">= class="token number">1)࿰c;并清除发送中断标志。UART_SendString:
逐个发送字符串中的字符。UART_ISR:
串口中断服务函数࿰c;接收数据并将其输出到P1口。main:
初始化串口࿰c;发送字符串 class="token string">"Hello, World!"c;进入主循环等待中断。
code>

示例代码:串口接收数据并回显

以下代码展示了如何使用串口在 IAP15F2K61S2 上实现数据接收并回显。

<code class="prism language-c">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><reg52.h>class="token macro property">class="token directive-hash">#class="token directive keyword">define class="token macro-name">uchar class="token expression">class="token keyword">unsigned class="token keyword">char
class="token macro property">class="token directive-hash">#class="token directive keyword">define class="token macro-name">uint class="token expression">class="token keyword">unsigned class="token keyword">intclass="token keyword">void class="token function">UART_Initclass="token punctuation">(class="token punctuation">) class="token punctuation">{SCON class="token operator">= class="token number">0x50class="token punctuation">;  class="token comment">// 串口模式1࿰c;8位数据࿰c;1位停止位TMOD class="token operator">|= class="token number">0x20class="token punctuation">; class="token comment">// 定时器1࿰c;模式2(8位自动重装)TH1 class="token operator">= class="token number">0xFDclass="token punctuation">;   class="token comment">// 波特率9600TL1 class="token operator">= class="token number">0xFDclass="token punctuation">;ES class="token operator">= class="token number">1class="token punctuation">;       class="token comment">// 使能串口中断EA class="token operator">= class="token number">1class="token punctuation">;       class="token comment">// 使能总中断TR1 class="token operator">= class="token number">1class="token punctuation">;      class="token comment">// 启动定时器1
class="token punctuation">}class="token keyword">void class="token function">UART_SendByteclass="token punctuation">(uchar datclass="token punctuation">) class="token punctuation">{SBUF class="token operator">= datclass="token punctuation">;   class="token comment">// 将数据写入SBUFclass="token keyword">while class="token punctuation">(class="token operator">!TIclass="token punctuation">)class="token punctuation">;  class="token comment">// 等待发送完成TI class="token operator">= class="token number">0class="token punctuation">;       class="token comment">// 清除发送中断标志
class="token punctuation">}class="token keyword">void class="token function">UART_ISRclass="token punctuation">(class="token punctuation">) interrupt class="token number">4 class="token punctuation">{class="token keyword">if class="token punctuation">(RIclass="token punctuation">) class="token punctuation">{     class="token comment">// 如果接收中断标志置位RI class="token operator">= class="token number">0class="token punctuation">;   class="token comment">// 清除接收中断标志class="token function">UART_SendByteclass="token punctuation">(SBUFclass="token punctuation">)class="token punctuation">;  class="token comment">// 将接收到的数据回显class="token punctuation">}
class="token punctuation">}class="token keyword">void class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{class="token function">UART_Initclass="token punctuation">(class="token punctuation">)class="token punctuation">;  class="token comment">// 初始化串口class="token keyword">while class="token punctuation">(class="token number">1class="token punctuation">)class="token punctuation">;    class="token comment">// 主循环࿰c;等待中断
class="token punctuation">}
代码说明
UART_Init:
初始化串口࿰c;设置波特率为class="token number">9600。UART_SendByte:
发送一个字节的数据。UART_ISR:
串口中断服务函数࿰c;接收数据并回显。main:
初始化串口࿰c;进入主循环等待中断。
code>

注意事项

1.波特率设置

波特率设置:波特率需要与通信设备一致࿰c;否则会导致通信失败。

2.中断优先级

中断优先级:如果有多个中断࿰c;需合理设置中断优先级

3.数据缓冲区

数据缓冲区:在实际应用中࿰c;可能需要使用缓冲区来存储接收到的数据

4.错误处理

错误处理:在实际应用中࿰c;需考虑**通信错误(如帧错误、溢出错误)**的处理。


总结

通过以上代码和说明࿰c;你可以在 IAP15F2K61S2 上实现串口通信功能࿰c;包括数据发送、接收和回显。实际开发中可以根据需求扩展功能࿰c;如协议解析、数据校验等。


http://www.ppmy.cn/embedded/164137.html

相关文章

SQL FIRST() 函数详解

SQL FIRST() 函数详解 在SQL中&#xff0c;FIRST() 函数是一个用于处理查询结果的聚合函数。它通常与 GROUP BY 子句结合使用&#xff0c;用于返回每个分组中的第一个记录。本文将详细解释 FIRST() 函数的用法、参数、返回值以及与它的关联函数。 1. 函数概述 FIRST() 函数的…

前端笔试面试资源汇总

好的&#xff0c;我现在需要帮助用户找到热门实用的前端笔试面试贴。首先&#xff0c;回顾之前的对话&#xff0c;用户已经询问了常见的前端算法题目&#xff0c;现在他们想要更广泛的资源&#xff0c;可能包括面试题、面经、学习资料等。用户可能正在准备前端面试&#xff0c;…

机器学习_19 集成学习知识点总结

集成学习&#xff08;Ensemble Learning&#xff09;是一种强大的机器学习范式&#xff0c;通过组合多个模型的预测结果来提高整体性能和泛化能力。它在分类、回归和特征选择等任务中表现出色&#xff0c;广泛应用于各种实际问题。今天&#xff0c;我们就来深入探讨集成学习的原…

深度学习的力量:精准肿瘤检测从此不再遥远

目录 引言 一、医学图像分析的挑战与深度学习的优势 1.1 医学图像分析的挑战 1.2 深度学习的优势 二、肿瘤检测的深度学习模型设计 2.1 卷积神经网络&#xff08;CNN&#xff09;的基本原理 2.2 网络架构设计 2.3 模型训练 三、肿瘤检测中的挑战与解决方案 3.1 数据不…

【git】提交修改、回撤、回滚、Tag 操作讲解,与reset (--soft、--mixed、--hard) 的区别

Git 提交修改、回撤、回滚、Tag 操作详解 1. git commit --amend -m "message" 作用&#xff1a;修改最近一次提交的信息或内容。 适用场景&#xff1a; 提交后发现 commit message 写错了。提交后发现 少 add 了文件&#xff0c;想直接加进上一次提交。 示例 1&…

CSS定位全解析:position属性详解与应用场景

在网页布局中&#xff0c;CSS定位是实现元素精准控制的关键技术之一。通过position属性&#xff0c;我们可以将元素放置在页面的任何位置&#xff0c;并控制其相对于其他元素的行为。本文将深入解析position属性的各个取值及其应用场景&#xff0c;帮助你掌握CSS定位的精髓。 …

深度学习-122-大语言模型LLM之基于langchian自定义国内联网查询工具并创建智能代理

文章目录 1 访问百度1.1 百度检索1.2 库baidusearch2 自定义工具@tool装饰器3 使用工具创建智能代理3.1 语言模型与工具结合3.2 方式一create_react_agent3.3 方式二create_tool_calling_agent3.4 给代理添加记忆4 参考附录1 访问百度 1.1 百度检索 from bs4 import Beautifu…

责任链模式解析FilterChain

分析&#xff1a; 过滤器链的调用过程 A过滤器调用chain.doFilter此时会进入到下一个过滤器 B过滤器继续调用chain.doFilter会继续进入下一个过滤器 当所有过滤器都执行完成后&#xff0c;会进入目标方法。 既然chain.doFilter能进入下一个过滤器&#xff0c;那本质上就是方法的…