蓝桥杯之单片机设计与开发(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);
}

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

你可能感兴趣的:(蓝桥杯)