蓝桥杯之单片机设计与开发(7)——串口通信原理与实现

蓝桥杯之单片机设计与开发(7)——串口通信原理与实现

问题

在运行编译的时候,解决的几个问题。

  1. 烧录代码进入单片机,单片机串口不能立马显示。
    解决方法有两个其一:重新上电;其二:在串口初始化后加300ms延时
    其中可以通过ISP自带的功能一键生成300ms的延时函数。
  2. 2.在ISP中。波特率需要选择准确/否则出错/
  3. 延时函数编译存在问题
    遇到了一个有趣的错误,在这里记录一下:如果遇到main.c(32): error C264: intrinsic ‘nop’: declaration/activation error这个错误,可以通过引入intrins.h库解决。
    备注:
    nop’: missing function-prototype
    xxxxxxx.C(25): warning C206: ‘nop’: missing function-prototype
    xxxxxxx.C(25): error C264: intrinsic ‘nop’: declaration/activation error
    解决方法: 添加包含文件 #include
    ‘memset’: requires ANSI-style prototype
    xxxxxxx.C(79): error C267: ‘memset’: requires ANSI-style prototype
    解决方法: 添加包含文件 #include
    ‘sprintf’: requires ANSI-style prototype
    xxxxxxx.C(79): error C267: ‘sprintf’: requires ANSI-style prototype
    解决方法: 添加包含文件 #include
#include "reg52.h"
#include "intrins.h"

sfr AUXR = 0x8e;

unsigned char urdat;

void SendByte(unsigned char dat);

void InitUart()
{
	TMOD = 0x20;  //定时器计数器工作方式寄存器
	TH1 = 0xfd;
	TL1 = 0xfd;
	TR1 = 1;
	
	SCON = 0x50;  //串行口控制寄存器
	AUXR = 0x00;
	
	ES = 1;
	EA = 1;
}

void ServiceUart() interrupt 4
{
	if(RI == 1)
	{
		RI = 0;
		urdat = SBUF; 
		SendByte(urdat+1);
	}
}

void SendByte(unsigned char dat)
{
	SBUF = dat;
	while(TI == 0);
	TI = 0;
}

void Delay300ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 13;
	j = 156;
	k = 83;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void main()
{
	InitUart();
	Delay300ms();
	SendByte(0x5a);
	SendByte(0xa5);
	while(1);
}

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弱冠而立

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值