//头文件
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
//---------------------
sbit f1=P1^1;
sbit f2=P1^2;
sbit f3=P1^3;
sbit f4=P1^4;
sbit fa=P3^0;
sbit fa1=P3^1;
//共阳极数码管段码
uchar code xian[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,};
//传感器的数据线
sbit DQ=P1^0;
uchar x1,x2,x3;
uint wendu=0;
uint wendux=0;
//延时函数
void delay3(unsigned int i)
{
while(i--);
}
//初始化
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay3(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay3(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay3(10);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay3(5);
}
//读一个字节
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay3(5);
}
return(dat);
}
//写一个字节
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay3(5);
DQ = 1;
dat>>=1;
}
delay3(5);
}
//读取温度
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
delay3(1000);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar(); //低位
b=ReadOneChar(); //高位
b<<=8;
t=a+b;
return(t);
}
//数据处理
void read()
{
uint y;uint x;uint x0;
y=ReadTemperature();
x=y>>4;
x0=y & 0x000F;
//分解小数个位十位百位
x1=x%1000/100;//百位
x2=x%100/10;//十位
x3=x%10;//个位
wendu=x1*100+x2*10+x3;
}
void main()
{
read(); delay3(20000);//读取温度
read(); delay3(20000);//读取温度
read(); delay3(20000);//读取温度
read(); delay3(20000);//读取温度
read(); delay3(20000);//读取温度
while(1)
{
//读取温度-------------------------------------
read();
//显示温度
P2=xian[wendu%100/10];
P3=xian[wendu%10];
delay3(20000);
wendux=wendu;
//-----------------------
if(wendux<20){f1=1;f2=1;f3=0;f4=1;fa=0;fa1=1;}
if((wendux>30)&&(wendux<40)){f1=1;f2=0;f3=0;f4=1;fa=1;,fa1=1;}
if((wendux>50)&&(wendux<60)){f1=0;f2=0;f3=0;f4=1;fa1=1;,fa0=1;}
if(wendu>60){f1=0;f2=0;f3=0;f4=0;fa=1;fa1=1;}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
test -14-温度 -+继电器风扇_51单片机DS18B20仿真_
共21个文件
bak:5个
workspace:2个
pdsbak:2个
5星 · 超过95%的资源 6 下载量 158 浏览量
2021-09-29
11:24:06
上传
评论
收藏 141KB RAR 举报
温馨提示
基于proteus7.8的仿真,程序是用keil编译,采用DS18B20为传感器,可以控制风扇的转动
资源详情
资源评论
资源推荐
收起资源包目录
test -14-温度 -+继电器风扇.rar (21个子文件)
test -14-温度 -+继电器风扇
test
lamp.opt.bak 1KB
lamp.c 2KB
仿真.DSN 162KB
lamp.M51 9KB
lamp_Opt.Bak 1KB
仿真.pdsprj 26KB
仿真.pdsprj.PC-20180316WPNF.Administrator.workspace 328B
lamp_Uv2.Bak 2KB
lamp.hex 2KB
Backup Of 仿真.pdsbak 26KB
lamp.lnp 25B
lamp.uvopt 65KB
lamp.uvproj 13KB
仿真.PWI 1KB
lamp.Uv2.bak 2KB
lamp.LST 5KB
Last Loaded 仿真.pdsbak 26KB
仿真.pdsprj.Shinelon-PC.Administrator.workspace 1KB
lamp_uvopt.bak 65KB
Last Loaded 仿真.DBK 162KB
lamp.plg 188B
共 21 条
- 1
弓弢
- 粉丝: 42
- 资源: 4021
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1