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 ...
随机推荐
- SAP 000 客户端初始登录
Solution 在SAP系统DB中删除账号SAP*,SAP系统会自动创建SAP*这个账号,然后初始密码是“PASS”,这样就获得Client 000 SAP*账号. Step by Step 以Or ...
- 只需两步获取任何微信小程序源码
http://baijiahao.baidu.com/s?id=1601969343738344659&wfr=spider&for=pc
- Parallax Mapping
[Parallax Mapping] Parallax mapping belongs to the family of displacement mapping techniques that di ...
- Linux 永久PATH环境变量
在/etc/profile文件中添加变量[对所有用户生效(永久的)] 用vim在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”. 例如:编辑/etc ...
- LibreOJ 6281 数列分块入门5
题目链接:https://loj.ac/problem/6281 参考博客:https://blog.csdn.net/qq_36038511/article/details/79725027 我一开 ...
- c++面向行的输入getline()和get()
来源:c++ primer plus 在c++里当我们输入一个字符串时习惯用cin,但是cin只能读取一段不含空格的字符串,如果我们需要读取一段包含空格的字符串时,就需要用到getline()或get ...
- avoid
avoid 英[əˈvɔɪd] 美[əˈvɔɪd] vt. 避开,避免,预防; [法] 使无效,撤销,废止; [例句]The pilots had to take emergency action t ...
- Codeforces Beta Round #70 (Div. 2)
Codeforces Beta Round #70 (Div. 2) http://codeforces.com/contest/78 A #include<bits/stdc++.h> ...
- 项目打包 TestFlight用法
TestFlight用法 包教包会(iOS APP官方测试工具) https://www.jianshu.com/p/4be185e4069c
- 微信公众号开发(5)---使用开源组件开发公众号OAuth2.0网页授权授权登录
搞清微信公众号授权登录的步骤步骤,我们的开发就完成了一大步 献上github 地址: https://github.com/Wechat-Group/weixin-java-tools/wiki/MP ...