51单片机+DS18B20+数码管显示+超温提示+Proteus仿真

51单片机+DS18B20+数码管显示+超温提示+Proteus仿真


  • Proteus仿真
    在这里插入图片描述

实例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

sbit we1 = P3^0;
sbit we2 = P3^1;
sbit we3 = P3^2;
sbit we4 = P3^3;
sbit DS = P1^0;

sbit D1 = P3^6;
sbit D2 = P3^7;

uchar qian=0,bai=0,shi=0,ge=0;
uint temp; // variable of temperature

void delay2(uchar x); //x\*0.14MS
void dsreset(void); //send reset and initialization command
bit tmpreadbit(void); //read a bit
uchar tmpread(void); //read a byte date
void tmpwritebyte(uchar dat); //write a byte to ds18b20
void tmpchange(void); //DS18B20 begin change
uint tmp(); //get the temperature
void fenli(uint temp); //分离温度函数
void display(uchar qian,uchar bai,uchar shi,uchar ge);//显示函数
//共阳数码管
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x7f};

void main()
{
int wendu;
we1=1;
we2=1;
we3=1;
we4=1;

while(1)
{
tmpchange();
wendu = tmp();
if(wendu>1000){D1=1;D2=0;}

if(wendu<1000){D2=1;D1=0;}
fenli(wendu);

we1=1;
P2=table[qian];
delay2(20);
we1=0;

we2=1;
P2=table[bai];
delay2(20);
we2=0;

we3=1;
P2=table[shi] & 0x7f;
delay2(20);
we3=0;

we4=1;
P2=table[ge];
delay2(20);
we4=0;
}
}

void fenli(uint temp)
{
qian=temp/1000;

bai=temp/100%10; //百

shi=temp%100/10; //十

ge=temp%10; //个
}

void delay2(uchar x) //x\*0.14MS
{
unsigned char i;
while(x--)
{
for (i = 0; i<13; i++) {}
}
}

void dsreset(void) //send reset and initialization command
{ //发送复位和初始化命令
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}

bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}

uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}

void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}

}
}

void tmpchange(void) //DS18B20 begin change
{
dsreset();
delay2(1);
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}

uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay2(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread(); //低字节
b=tmpread(); //高字节
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp\*0.0625;
temp=tt\*10+0.5; //0.5四舍五入作
return temp;
}


仿真资源和源码下载

1
2
3
链接:https://pan.baidu.com/s/1D06qQwHdc3DU1SnLiaCUYg 
提取码:kvn1