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);
}
serial -1的更多相关文章
- python serial 获取所有的串口名称
http://blog.csdn.net/qq61394323/article/details/44619511 #!/usr/bin/env python # -*- coding: utf-8 - ...
- 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 ...
- Serial Port Programming using Win32 API(转载)
In this tutorial we will learn How to communicate with an external device like a microcontroller boa ...
- Serial Communication Protocol Design Hints And Reference
前面转载的几篇文章详细介绍了UART.RS-232和RS-485的相关内容,可以知道,串口通信的双方在硬件层面需要约定如波特率.数据位.校验位和停止位等属性,才可以正常收发数据.实际项目中使用串口通信 ...
- RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)
前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview ...
- RS-232 vs. TTL Serial Communication(转载)
RS-232串口一度像现在的USB接口一样,是PC的标准接口,用来连接打印机.Modem和其他一些外设.后来逐渐被USB接口所取代,现在PC上已经看不到它的身影了.开发调试时如果用到串口,一般都是用U ...
- select/poll/epoll on serial port
In this article, I will use three asynchronous conferencing--select, poll and epoll on serial port t ...
- 编写ros串口节点,使用官方serial包
参考http://www.roswiki.com/read.php?tid=557&fid=39 1.通过sudo apt-get install ros-<distro>-ser ...
- 利用 Serial Over Lan(SOL)搭建 XEN 的调试信息输出环境
如有转载,请注明出处与本文连接,谢谢! 修改XEN的源码实现额外的功能,需要有一个调试环境来得到XEN的调试信息(有关源码编译并安装 XEN 请阅读我以前的博文:在CentOS下源码安装 Xen并搭建 ...
- C语言dsPIC / PIC24 serial bootloader和C#语言bootloader PC端串口通信程序
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新dsPIC/PIC2 ...
随机推荐
- 七个不可错过的React组件库与开发框架
React是如今最火爆的前端技术,而React最棒的一个特点就是有大量功能丰富的组件库和开发框架可用.从按钮到卷轴到工具条,应有尽有,而且这些组件可以各行其是,也可以组装成复杂的UI,你也可以把UI分 ...
- 外购半成品报SHORT问题(非验货客户)
外购半成品报SHORT问题(验货客户)https://www.cnblogs.com/Snowfun/p/8660646.html 下面看非验货客户: 1.检查采购类型是否为F(SAP_MARC),为 ...
- Pandas基本功能之算术运算、排序和排名
算术运算和数据对齐 Series和DataFrame中行运算和列运算有种特征叫做广播 在将对象相加时,如果存在不同的索引对,则结果的索引就是该索引对的并集.自动的数据对齐操作在不重叠的索引处引入了NA ...
- python--第五天总结
装饰器-- @ 重命名原函数,返回函数对象 是一个函数,至少两层执行函数,被装饰的函数作为参数----------------------------------------------------1 ...
- 数据库与ORM
一.数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库. <1> sqlite django默认使用sqlite的数据库,默认自带 ...
- as3.0加载swf并控制
私人QQ 280841609 var myload:Loader=new Loader(); var url:URLRequest=new URLRequest("1.swf"); ...
- poj 1789 prime
链接:Truck History - POJ 1789 - Virtual Judge https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...
- 善于利用python中的os模块
作为一个程序猿,平时善于利用python中的os模块进行路径等操作,会省去很多麻烦,下面总结一下我平时经常用到的方法: import os os.getcwd() # 获取当前文件所在的目录 os.p ...
- laravel5.6上传图片及显示
借鉴大神博客:https://blog.csdn.net/tony_110/article/details/80105099文档:http://laravelacademy.org/post/8965 ...
- wiper
wiper - 必应词典 美['waɪpər]英['waɪpə(r)] n.手绢儿:搌布:揩布:(汽车风挡上的)雨刷器 网络刮水器:雨刮器:拭雨器 变形复数:wipers: