-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path温度播报.c
398 lines (359 loc) · 5.48 KB
/
温度播报.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define wendushi 88
#define du 87
#define s 10
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit DS=P2^2; //define interface
uint temp,temp1; // variable of temperature
uchar flag1; // sign of the result positive or negative
sbit dula=P2^6;
sbit wela=P2^7;
sbit busy=P1^1;
//sbit jia=P3^4;
//sbit jian=P3^5;
sbit bofang=P3^6;
sbit zhidi=P3^7;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef};
uchar code yinliangjia[]={0xaa,0x14,0x00,'*'};
uchar code yinliangjian[]={0xaa,0x15,0x00,'*'};
uchar code zhidin[]={0xaa,0x07,0x02,0x00,0x09,'*'};
uchar code _1602_1[]=" temperature";
void send_date(uchar *p);
void send_byte(uchar dat);
void init();
void delay1(uint z);
void display(uint temp);
void dsreset(void);
bit tmpreadbit(void);
uchar tmpread(void);
void tmpwritebyte(uchar dat);
void tmpchange(void);
uint tmp();
void display(uint temp);
void zuhe();
void keyscan();
void init_1602();
void write_com(uchar com);
void write_dat(uchar dat);
void display_1602();
void main()
{
// uchar a;
init();
init_1602();
delay1(2000);
while(1)
{
tmpchange();
temp1=tmp();
display_1602();
/* for(a=10;a>0;a--)
{
display(tmp());
} */
keyscan();
}
}
void delay1(uint z) //延时z毫秒
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init() //串口初始化
{
// EA=1;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
// ES=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
EX1=1;
IT1=1;
}
void send_byte(uchar dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void send_date(uchar *p) //发送指令
{
uchar c,i;
i=0;
c=0;
while(p[i]!='*')
{
send_byte(p[i]);
c+=p[i];
i++;
}
send_byte(c); //校验位
delay1(120);
}
void zuhe()
{
uchar n,bai,shi,ge,a;
uchar wd[100];
n=0;
wd[n++]=0xaa;
wd[n++]=0x1b;
wd[n++]=0;
a=temp1/10;
bai=temp1/100;
shi=temp1%100/10;
ge=temp1%10;
wd[n++]=wendushi/10+0x30;
wd[n++]=wendushi%10+0x30;
if(bai!=0)
{
if(temp1>19)
{
wd[n++]=bai/10+0x30;
wd[n++]=bai%10+0x30;
}
wd[n++]=s/10+0x30;
wd[n++]=s%10+0x30;
}
if(shi!=0)
{
wd[n++]=shi/10+0x30;
wd[n++]=shi%10+0x30;
}
if(bai==0&&shi==0)
{
wd[n++]=0x30;
wd[n++]=0x30;
}
wd[n++]=du/10+0x30;
wd[n++]=du%10+0x30;
if(ge!=0)
{
wd[n++]=ge/10+0x30;
wd[n++]=ge%10+0x30;
}
wd[n++]='*';
wd[2]=n-4;
send_date(wd);
}
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
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();
delay(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();
delay(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;
return temp;
}
/*void display(uint temp) //显示程序
{
uchar A1,A2,A2t,A3;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
dula=0;
P0=table[A1]; //显示百位
dula=1;
dula=0;
wela=0;
P0=0x7e;
wela=1;
wela=0;
delay(1);
dula=0;
P0=table1[A2]; //显示十位
dula=1;
dula=0;
wela=0;
P0=0x7d;
wela=1;
wela=0;
delay(1);
P0=table[A3]; //显示个位
dula=1;
dula=0;
P0=0x7b;
wela=1;
wela=0;
delay(1);
} */
void keyscan()
{
/* if(jia==0)
{
delay1(3);
if(jia==0)
{
send_date(yinliangjia);
while(!jia);
}
}
if(jian==0)
{
delay1(3);
if(jian==0)
{
send_date(yinliangjian);
while(!jian);
}
} */
if(bofang==0)
{
delay1(3);
if(bofang==0)
{
zuhe();
while(!bofang);
}
}
if(zhidi==0)
{
delay1(3);
if(zhidi==0)
{
send_date(zhidin);
while(!zhidi);
}
}
}
void write_dat(uchar dat)
{
lcden=0;
lcdrs=1;
P0=dat;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
lcdrs=0;
}
void write_com(uchar com)
{
lcden=0;
lcdrs=0;
P0=com;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
void init_1602()
{
dula=0;
wela=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80+0x00);
}
void display_1602()
{
uchar i=0,bai,shi,ge;
write_com(0x80+0x00);
while(_1602_1[i])
{
write_dat(_1602_1[i++]);
}
write_com(0x80+0x46);
bai=temp/100;
shi=temp%100/10;
ge=temp%10;
write_dat(bai+0x30);
write_dat(shi+0x30);
write_dat('.');
write_dat(ge+0x30);
}