#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);
}

serial -1的更多相关文章

  1. python serial 获取所有的串口名称

    http://blog.csdn.net/qq61394323/article/details/44619511 #!/usr/bin/env python # -*- coding: utf-8 - ...

  2. Serial Port Programming on Linux(转载)

    This is a tutorial on how to program the Serial Ports on your Linux box.Serial Ports are nice little ...

  3. Serial Port Programming using Win32 API(转载)

    In this tutorial we will learn How to communicate with an external device like a microcontroller boa ...

  4. Serial Communication Protocol Design Hints And Reference

    前面转载的几篇文章详细介绍了UART.RS-232和RS-485的相关内容,可以知道,串口通信的双方在硬件层面需要约定如波特率.数据位.校验位和停止位等属性,才可以正常收发数据.实际项目中使用串口通信 ...

  5. RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)

    前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview ...

  6. RS-232 vs. TTL Serial Communication(转载)

    RS-232串口一度像现在的USB接口一样,是PC的标准接口,用来连接打印机.Modem和其他一些外设.后来逐渐被USB接口所取代,现在PC上已经看不到它的身影了.开发调试时如果用到串口,一般都是用U ...

  7. select/poll/epoll on serial port

    In this article, I will use three asynchronous conferencing--select, poll and epoll on serial port t ...

  8. 编写ros串口节点,使用官方serial包

    参考http://www.roswiki.com/read.php?tid=557&fid=39 1.通过sudo apt-get install ros-<distro>-ser ...

  9. 利用 Serial Over Lan(SOL)搭建 XEN 的调试信息输出环境

    如有转载,请注明出处与本文连接,谢谢! 修改XEN的源码实现额外的功能,需要有一个调试环境来得到XEN的调试信息(有关源码编译并安装 XEN 请阅读我以前的博文:在CentOS下源码安装 Xen并搭建 ...

  10. C语言dsPIC / PIC24 serial bootloader和C#语言bootloader PC端串口通信程序

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新dsPIC/PIC2 ...

随机推荐

  1. springboot-day02-整合mybatis与简单使用

    前言:该文章是紧接上一篇文章springboot-day01-引入如何读取配置文件以及helloWorld 1.springboot整合mybatis 1.添加jar包依赖 <?xml vers ...

  2. Executors与ThreadPoolExecutor

    最近阿里发布的 Java开发手册中强制线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗 ...

  3. Manifest File

    [Manifest File] on every build, webpack generates some webpack runtime code, which helps webpack do ...

  4. iOS8不能通过itms-services协议下载安装app

    问题:iOS包通过itms-services协议下载app无反应   使用  itms-services://?action=download-manifest&url=[ipa下载链接] 下 ...

  5. GreenDao在列中的单词之间自动加_

    1.第一种情况,原字段(属性.列)是 驼峰式命名法 @Entitypublic class Employee { @Id(autoincrement = true) private Long id; ...

  6. springboot1 缓存前端

    @Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter { public void addResourceHan ...

  7. java集合: ArrayList源码浅析

    ArrayList 是一个动态数组,线程不安全 ,允许元素为null. ArrayList的数据结构是数组,查询比较方便. ArrayList类的接口 public class ArrayList&l ...

  8. windows2008 r2 不能启用网络发现解决方法

    1.出现的问题: 在“网络和共享中心”-“网络发现”不论如何,“启用”网络发现功能,系统都会自动重置为关闭状态. 2.解决方法: 运行中输入 services.msc-->在里边找到下边上个服务 ...

  9. 两条线段求交点+叉积求面积 poj 1408

    题目链接:https://vjudge.net/problem/POJ-1408 题目是叫我们求出所有四边形里最大的那个的面积. 思路:因为这里只给了我们正方形四条边上的点,所以我们要先计算横竖线段两 ...

  10. RocketMq顺序消费

    部分内容出处   https://www.jianshu.com/p/453c6e7ff81c rocketmq内部有4个默认的队里,在发送消息时,同一组的消息需要按照顺序,发送到相应的mq中,同一组 ...