?? 18b20_1602.lst
字號:
226 1 bit b = 0;
227 1
228 1 DO = 0; //產生讀時隙
229 1 i++; //維持低電平至少1us
230 1
231 1 DO = 1; //1us以上后拉高
232 1 Delayus(2); //延時8us,DO下降沿15內ds18b20輸出的數據才有效
233 1
234 1
235 1 b = DO; //讀取數據
236 1 Delayus(40); //每個讀時隙至少持續(xù)60us
237 1
238 1 return(b);
239 1 }
240
C51 COMPILER V8.12 18B20_1602 07/30/2008 10:40:15 PAGE 5
241 /*讀取一個字節(jié)*/
242 uchar Readbyte()
243 {
244 1 uchar byte_read = 0;
245 1 uchar i, j;
246 1
247 1 for(i=0; i<8; i++)
248 1 {
249 2 j = Readbit();
250 2 byte_read = (j<<i) | byte_read; //低位讀起
251 2 }
252 1
253 1 return(byte_read);
254 1 }
255
256 /*寫一個字節(jié)*/
257 void Writebyte(uchar byte_to_write)
258 {
259 1 uchar i = 0;
260 1 uchar j = 0;
261 1 bit write_bit = 0;
262 1
263 1 for(j=0; j<8; j++)
264 1 {
265 2 write_bit = (byte_to_write & 0x01);
266 2 if(write_bit == 1) //寫1
267 2 {
268 3 DO = 0; //產生寫時隙
269 3 Delayus(3); //延時15us
270 3
271 3 DO = 1; //寫1
272 3 Delayus(40); //延時,寫時隙不得低于60us
273 3 }
274 2 else
275 2 {
276 3 DO = 0; //產生寫時隙
277 3 Delayus(50); //延時,保持低約60us~120us
278 3 DO = 1;
279 3 i++;
280 3 }
281 2 byte_to_write = byte_to_write >> 1;
282 2 }
283 1 }
284
285
286 /*啟動溫度轉換*/
287 void StartConvert()
288 {
289 1 Resetpaulse(); // 發(fā)出復位脈沖,每次操作都從復位開始
290 1 Delay(1);
291 1 EA = 0;
292 1 Writebyte(0xcc); //skip room命令
293 1 Writebyte(0x44); //啟動溫度轉換命令
294 1 EA = 1;
295 1 }
296
297 /*讀取溫度值*/
298 void ReadTempreture()
299 {
300 1 EA = 0;
301 1 Resetpaulse(); // 發(fā)出復位脈沖,每次操作都從復位開始
302 1 Delay(1);
C51 COMPILER V8.12 18B20_1602 07/30/2008 10:40:15 PAGE 6
303 1 Writebyte(0xcc); //skip room命令
304 1 Writebyte(0xbe); //讀取暫存器命令
305 1 temp_l = Readbyte(); //存儲溫度低字節(jié)值 (整數部分低四位和小數部分)
306 1 temp_h = Readbyte(); //存儲溫度高字節(jié)值 (其中高五位為符號位)
307 1 EA = 1;
308 1 }
309
310 /*數據處理程序*/
311 void Digital_process()
312 {
313 1 uchar total = 0;
314 1 uchar low = 0;
315 1 uint dicimal = 0;
316 1
317 1 tempsign = (temp_h >> 7) & 0x01; //得出符號位
318 1 total = ((temp_h << 4)&0xf0) | ((temp_l >> 4)&0x0f); //取整數位
319 1 low = temp_l & 0x0f; //取小數位
320 1
321 1 if(tempsign == 0)
322 1 {
323 2 temp_integer[0] = total / 100 + '0'; //計算百、十、個位
324 2 temp_integer[1] = (total%100)/10 + '0';
325 2 temp_integer[2] = (total%100)%10 + '0';
326 2 temp_integer[3] = '\0';
327 2 if(temp_integer[0] == '0')
328 2 {
329 3 if(temp_integer[1] != '0')
330 3 {
331 4 temp_integer[0] = '\0'; //百位零消隱
332 4
333 4 }
334 3 else if(temp_integer[1] == '0')
335 3 {
336 4 temp_integer[0] = '\0'; //百位,十位零都消隱
337 4 temp_integer[1] = '\0';
338 4 }
339 3 }
340 2 dicimal = low * 625; //計算小數
341 2 temp_dicimal[0] = dicimal / 1000 + '0'; //十分位
342 2 temp_dicimal[1] = dicimal % 1000 /100 + '0'; //百分位
343 2 temp_dicimal[2] = dicimal % 100 / 10 + '0'; //千分位
344 2 temp_dicimal[3] = dicimal % 10 + '0'; //萬分位
345 2 temp_dicimal[4] = '\0'; //數組加一個空字符(好像系統(tǒng)也會自動加上的?)
346 2 }
347 1
348 1 else if(tempsign == 1) //負數處理
349 1 {
350 2 if(low == 0x00) //負數要取反加一再乘以0.0625就是實際溫度值了,我這里沒有設那么多int型變量,
351 2 {
352 3 total = ~total + 1; //所以就用了這么一個計算方法
353 3 low &= 0x0f;
354 3 } /*具體一點講,小樹低四位為全零時取反加一要有進位,此時只要整數位取反加一即可,
355 2 小數位不用理會,其余情況整數位取反,小數位取反加一*/
356 2 else
357 2 {
358 3 total = ~total ;
359 3 low = (~low) + 1;
360 3 low &= 0x0f; //注意高四位要變成零
361 3 }
362 2 temp_integer[1] = (total%100)/10 + '0'; //計算十、個位
363 2 temp_integer[2] = (total%100)%10 + '0';
364 2 temp_integer[3] = '\0';
C51 COMPILER V8.12 18B20_1602 07/30/2008 10:40:15 PAGE 7
365 2
366 2
367 2 if(temp_integer[1] == '0')
368 2 {
369 3 temp_integer[1] = '\0';
370 3 }
371 2 dicimal = low * 625;
372 2 temp_dicimal[0] = dicimal / 1000 + '0';
373 2 temp_dicimal[1] = dicimal % 1000 /100 + '0';
374 2 temp_dicimal[2] = dicimal % 100 / 10 + '0';
375 2 temp_dicimal[3] = dicimal % 10 + '0';
376 2 temp_dicimal[4] = '\0';
377 2 }
378 1
379 1
380 1 }
381
382
383
384 void main()
385 {
386 1 bit palse = 0;
387 1 Initial_LCD();
388 1
389 1 GotoXY(0,0);
390 1 Print("CHECKING...",12);
391 1 Delay(3000);
392 1
393 1
394 1 palse = Resetpaulse(); //檢測DS18B20是否響應
395 1 if(palse)
396 1 {
397 2 Initial_LCD();
398 2 GotoXY(0,0);
399 2 Print("DS18B20 OK",11);
400 2 }
401 1 else
402 1 {
403 2 Initial_LCD();
404 2 GotoXY(0,0);
405 2 Print("DS18B20 ERROR",13);
406 2 while(1);
407 2
408 2 }
409 1
410 1 do{
411 2 Delay(1);
412 2 StartConvert();
413 2 Delay(1020);
414 2 ReadTempreture();
415 2 Digital_process();
416 2
417 2 if(tempsign == 0) //顯示正值溫度
418 2 {
419 3 GotoXY(0,1);
420 3 Print("TEMP:",5);
421 3 GotoXY(5,1);
422 3 Print(temp_integer,3);
423 3 GotoXY(8,1);
424 3 Print(".",1);
425 3 GotoXY(9,1);
426 3 Print(temp_dicimal,4);
C51 COMPILER V8.12 18B20_1602 07/30/2008 10:40:15 PAGE 8
427 3 }
428 2 else //顯示負值溫度
429 2 {
430 3 GotoXY(0,1);
431 3 Print("TEMP:",5);
432 3 GotoXY(5,1);
433 3 Print("-",1);
434 3 GotoXY(6,1);
435 3 Print(temp_integer + 1,2);
436 3 GotoXY(8,1);
437 3 Print(".",1);
438 3 GotoXY(9,1);
439 3 Print(temp_dicimal,4);
440 3 }
441 2 }
442 1 while(1);
443 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 991 ----
CONSTANT SIZE = 47 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 12 3
IDATA SIZE = ---- ----
BIT SIZE = ---- 5
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -