|
本帖最后由 cyy998 于 2016-7-24 14:25 編輯
初學(xué)單片機(jī),學(xué)著做一個18B20溫度計(jì),開始在網(wǎng)上找了很多這方面的程序來學(xué)習(xí),但是發(fā)現(xiàn)一個共同的問題,就是在18B20讀取溫度的時候數(shù)碼管都會閃一下,經(jīng)過研究發(fā)現(xiàn)原來是18B20讀取溫度需要的時間是毫秒級的,而數(shù)碼管顯示是實(shí)時掃描的,所以會造成顯示中斷。經(jīng)過幾天的折騰后,把顯示程序改進(jìn)了一下,達(dá)到了預(yù)期效果,現(xiàn)在已經(jīng)看不到一點(diǎn)閃了,現(xiàn)把程序分享出來,跟同學(xué)們交流交流。
先上幾張實(shí)物:
1.jpg (516.32 KB, 下載次數(shù): 104)
下載附件
2016-7-23 23:56 上傳
2.jpg (567.84 KB, 下載次數(shù): 93)
下載附件
2016-7-23 23:56 上傳
3.jpg (738.78 KB, 下載次數(shù): 80)
下載附件
2016-7-23 23:56 上傳
4.jpg (587.78 KB, 下載次數(shù): 87)
下載附件
2016-7-23 23:56 上傳
貼上程序(顯示程序是自己寫的,18B20程序是網(wǎng)上找的現(xiàn)成的)
main.c
- /*
- 程序:18B20數(shù)碼管顯示
- MCU:STC89C52RC
- 晶振:11.0592MHz
- */
- #include<reg52.h>
- #include "delay.h"
- #include "ds18b20.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define duan P2
- #define wei P3
- sbit dp=P2^7; //小數(shù)點(diǎn)
- int temp; //溫度緩存
- //*******************************顯示函數(shù)*****************************
- void Display()
- {
- uchar code table[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};//共陽碼表0~9、負(fù)號、不顯,共12位
- uchar code wf[5]={0xff,0xfe,0xfd,0xfb,0xf7};//位選表(只用到后4位)
- uchar data df[5]; //用來存取溫度各位數(shù)的值(只用到1-4)
- uchar i;
- uint j;
- j=temp;
- if(temp<0)j=-temp; //如果是負(fù)數(shù)轉(zhuǎn)換成正數(shù)
- for(i=4;i>0;i--) //循環(huán)次數(shù)等于溫度值的位數(shù)
- {
- df[i]=j%10; //取出各位數(shù)的值
- j/=10;
- }
- if(temp<0)df[1]=10; //如果溫度是負(fù)數(shù),千位顯示負(fù)號
- if(df[1]==0)df[1]=11; //如果千位是0不顯示空
- for(i=4;i>0;i--) //循環(huán)次數(shù)等于數(shù)碼管的位數(shù)
- {
- delaynms(15); //先延時,跳出循環(huán)后馬上讀取溫度,避免讀溫度時數(shù)碼管閃
- duan=0xff; //先關(guān)段顯
- duan=table[df[i]]; //送出段碼
- if(i==3)dp=0; //顯示小數(shù)點(diǎn)
- wei=wf[i]; //送出位碼
- }
- }
- //*********************************主函數(shù)*********************************
- void main()
- {
- reset(); //18b20初始化
- while(1)
- {
- Display(); //顯示溫度
- temp=get_temp(); //讀取溫度
- }
- }
復(fù)制代碼 18b20.c
- #include <reg52.h>
- #include <intrins.h>
- #include "ds18b20.h"
- #include "delay.h"
- //**************************初始化函數(shù)************************
- void reset()
- {
- uchar x=0;
- DQ = 1;
- delayus(1);
- DQ = 0;
- delayus(50); //延時600us
- DQ = 1;
- delayus(6); //延時80us
- x=DQ;
- delayus(45); //延時420us
- }
- //**************************寫字節(jié)函數(shù)************************
- void write_byte(uchar wByte)
- {
- uchar i;
- for(i=8; i>0; i--)
- {
- DQ = 0;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- DQ = wByte & 0x01;
- delayus(6);
- DQ = 1;
- wByte >>= 1;
- }
- delayus(1);
- }
- //***********************讀字節(jié)函數(shù)************************
- uchar read_byte(void)
- {
- uchar i,rByte;
- for(i=8; i>0; i--)
- {
- DQ = 0;
- _nop_();
- rByte >>= 1;
- DQ = 1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- if(DQ)
- rByte |= 0x80;
- delayus(6);
- }
- return rByte;
- }
- //************************讀取溫度函數(shù)************************
- uint get_temp(void)
- {
- uchar a=0;
- uchar b=0;
- int t=0; //溫度
- uint tt=0;
- reset(); //復(fù)位
- write_byte(0xcc); //忽略ROM
- write_byte(0x44); //發(fā)送溫度轉(zhuǎn)化命令
- reset(); //再次復(fù)位
- write_byte(0xcc); //忽略ROM
- write_byte(0xbe);
- a= read_byte(); //讀溫度低字節(jié)
- b= read_byte(); //讀溫度高字節(jié)
- tt=(b<<8)|a; //合并
- if(b>200)tt=~tt+1; //如果是負(fù)溫度取反,加一
- t=tt*0.625; //轉(zhuǎn)換成十進(jìn)制
- if(b>200)t=~t; //如果是負(fù)溫度轉(zhuǎn)換成負(fù)數(shù)
- return(t); //返回溫度值
- }
復(fù)制代碼 18b20.h
- #ifndef _DS18B20_H_
- #define _DS18B20_H_
- #define uchar unsigned char
- #define uint unsigned int
- sbit DQ = P0^7;
- void reset(void);//復(fù)位
- void write_byte(uchar wByte);//寫字節(jié)
- uchar read_byte(void);//讀字節(jié)
- uint get_temp(void);//讀溫度
- #endif
復(fù)制代碼 delay.c
- #include "delay.h"
- //**************************10us延時函數(shù)************************
- void delayus(uint us)
- {
- while(us--);
- }
- //**************************nms延時函數(shù)************************
- void delaynms(uint n)
- {
- uint a;
- uchar b;
- for(a=n;a>0;a--)
- for(b=110;b>0;b--);
- }
復(fù)制代碼 delay.h
- #ifndef _DELAY_H_
- #define _DELAY_H_
- #define uchar unsigned char
- #define uint unsigned int
- void delayus(uint us);//延時10微秒
- void delaynms(uint n);//延時若干毫秒
- #endif
復(fù)制代碼 最后附上源文件(有仿真)
18B20.rar
(64.82 KB, 下載次數(shù): 85)
2016-7-24 00:05 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|