serial -1

#include <reg52.h>
#include <stdio.h>
#define uchar unsigned char
sbit LED = P2^2;
uchar receive;
uchar sdata[11]={13,10,'l','e','d',58,111,'0','0',13,10};

void main(void)
{
EA = 1; //允许总中断(如不使用中断,可用//屏蔽)
ES = 1; //允许UART串口的中断

TMOD = 0x20; //定时器T/C1工作方式2
SCON = 0x50; //串口工作方式1,允许串口接收(SCON = 0x40 时禁止串口接收)
TH1 = 0xF3; //定时器初值高8位设置 //12MHZ晶振,波特率为4800 0xf3
TL1 = 0xF3; //定时器初值低8位设置 //11.0592MHZ晶振,波特率为4800 0xf4 9600 0xfa 19200 0xfd
PCON = 0x80; //波特率倍频(屏蔽本句波特率为2400)
TR1 = 1; //定时器启动
LED=1;
while(1);

}

void tranData() interrupt 4
{
uchar i;
if(RI)
{
RI=0;
receive=SBUF;
if(receive==0x31)
{
LED=0;
// ES=0;
sdata[8]=0x00;
sdata[7]='n';
for(i=0;i<=10;i++)
{
SBUF=sdata[i];
while(!TI);
TI=0;
}
// ES=1;
}
else if(receive==0x30)
{
LED=1;
// ES=0;
sdata[8]='f';
sdata[7]='f';
for(i=0;i<=10;i++)
{
SBUF=sdata[i];
while(!TI);
TI=0;
}
// ES=1;
}
}
}


/* while(!TI);的意思是等待串口发送完成,
当串口发送未完成时:
TI值为0,(!TI)值为1,;号前面无语句,故一直在此循环
当串口发送完成时:
TI值为1,(!TI)值为0,while(!TI)不满足循环,退出,继续执行下一条*/

 

 

2-输入特定字符, 发送一组数据

 

#include<reg52.h>

#define uint unsigned int
#define uchar unsigned char

sbit DQ=P3^7; // 接DS18B20的数据端
uchar datain;
/***********************************
函数:DelayMs(uint z)
----------------------
说明:毫秒级的延时
参数:z 代表要延时的毫秒数
返回值:无
***********************************/
void DelayMs(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}


/***********************************
void ReSet(void)
------------------
说明:复位启动DS18B20
参数:无
返回值:无
***********************************/
void ReSet(void)
{
uint i;
DQ=0;
i=100;
while(i--);
DQ=1;
i=4;
while(i--);
while(DQ);
while(~DQ);
i=4;
while(i--);
}

 

/***********************************
uchar ReadByte(void)
------------------
说明:读取DS18B20的一个字节
参数:无
返回值:返回读取到的字节
***********************************/
uchar ReadByte(void)
{
uchar i,j,b,dat=0;
for(j=0;j<8;j++)
{
DQ=0;
i++;
DQ=1;
i=3; // 延时15us
while(--i);
b=DQ;
i=10;
while(i--);
dat=(b<<7)|(dat>>1);
}
return(dat);
}


/************************************************
void WriteByte(uchar b)
------------------
说明:写数据的一个字节,满足写1和写0的时隙要求
参数:b代表要写入到DS18B20的内容
返回值:无
************************************************/
void WriteByte(uchar b)
{
uint i;
uchar j;
bit btmp;
for(j=0;j<8;j++)
{
btmp=b&0x01;
b=b>>1; // 取下一位(由低位向高位)
if(btmp)
{
DQ=0;
i++;
i++;
DQ=1;
i=10;
while(i--); // 整个写1时隙不低于60us
}
else
{
DQ=0;
i=10;
while(i--); // 保持低在60us到120us之间
DQ=1;
i++;
i++;
}
}
}

 


/************************************************
uint ReadTemp(void)
------------------
说明:读取温度值
参数:无
返回值:返回读取到的温度
************************************************/
uint ReadTemp(void)
{
uchar TempLow,TempHig; // 温度值低位、高位字节
float tt;
uint temp;
ReSet(); // 产生复位脉冲,初始化DS18B20
WriteByte(0xcc); // skip rom 命令
WriteByte(0x44); // convert T 命令
ReSet();
WriteByte(0xcc); // skip rom 命令
WriteByte(0xbe); // read 温度命令
TempLow=ReadByte(); // 温度值低位字节(其中低4位为二进制的"小数"部分)
TempHig=ReadByte(); // 高位值高位字节(其中高5位为符号位)
temp=TempHig;
temp<<=8;
temp=temp|TempLow;
tt=temp*0.0625;
temp=tt*10+0.5;
return (temp);
}

 

/***********************************
函数:void send(uint dat)
---------------------------
说明:将测得的距离通过串口发送出去
参数:dat是测得的距离
返回值:无
***********************************/
void Send(uint dat)
{
// SBUF=0xaa; //
// while(!TI);
// TI=0;
SBUF=(dat/1000)+ 48; // 发送 千 位
while(!TI);
TI=0;
SBUF=(dat%1000/100)+ 48; // 发送 百 位
while(!TI);
TI=0;
SBUF=(dat%100/10)+48; // 发送 十 位
while(!TI);
TI=0;
SBUF=0x2E; // 发送 点 位
while(!TI);
TI=0;
SBUF=(dat%10)+48; // 发送 位
while(!TI);
TI=0;
SBUF=0x0A; // 发送换行
while(!TI);
TI=0;
}

void tempTran() interrupt 4
{
uint temp; // 用来保存读取到的温度值
if(RI)
{
RI=0;
datain=SBUF;
if(datain==0x31)
{
temp=ReadTemp();
Send(temp);
DelayMs(200);
}
}
}

/***********************************
函数:void InitUart()
----------------------
说明:对串口进行初始化
参数:无
返回值:无
***********************************/
void InitUart()
{
SCON = 0x50; //串口工作方式1,允许串口接收(SCON = 0x40 时禁止串口接收)
PCON = 0x80; //波特率倍频(屏蔽本句波特率为2400)
TMOD = 0x20; //定时器T/C1工作方式2
TH1 = 0xF3; //定时器初值高8位设置
TL1 = 0xF3; //定时器初值低8位设置
EA = 1; //允许总中断(如不使用中断,可用//屏蔽)
ES = 1; //允许UART串口的中断
TR1 = 1; //定时器启动
}

 

/***********************
函数:void main(void)
----------------------
说明:主函数
参数:无
返回值:无
***********************/
void main()
{
InitUart();
while(1);
}

 

转载于:https://www.cnblogs.com/https/p/9247627.html

Serial is a modern terminal emulator designed to make working with servers, network equipment, and embedded hardware easier for serialengineers and system administrators. For system administrators, Serial supports the all-important break sequence required when working with routers and switches. Serial also includes a full-featured terminal emulator, allowing you to work with the screen-based menus found in network equipment and Unix programs including the text editors vi and emacs. Makers and engineers will appreciate Serial’s built-in support for embedded devices including the Arduino, Raspberry Pi, BeagleBone, and thousands of others. Serial includes several useful features for working with these devices including the ability to reset an Arduino, and the ability to connect to a BeagleBone over USB directly or through the on-board header. Finally, Serial supports several of the most common USB to serial adapters WITHOUT requiring drivers, often saving you the hassle of finding and installing additional software. Features: Full-featured xterm, linux, and ANSI/VT100 terminal emulation Works with USB to serial adapters and devices that use USB to serial chips internally UTF-8 character set support No kernel drivers required for many of the most common devices Bluetooth serial support USB Communication Device Class (CDC) support Break support required for routers, switches and other network equipment Line-buffered send mode Text pacing options XMODEM and YMODEM file transfer support WHAT’S NEW Version 1.3.9: Added support for macOS 10.14 (Mojave) Added the ability to customize the session log save path Added CP437 and CP850 character set support Added support for additional devices Other bug fixes and improvements REQUIREMENTS OS X 10.7 or later, 64-bit processor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值