|
是這樣的,在DS18B20上琢磨了幾天,但還是沒成功
具體就是讀取溫度失敗,總為一個數值,沒有變化,通過keil仿真,時序沒錯,步驟沒錯,但到proteus上仿真就是只有一個數值,怎么改ds18b20的數值都不變,因此求助各位幫忙看下是哪里出了問題。keil和proteus仿真均是使用89C52單片機以及12MHZ晶振。
具體代碼如下:我也會附上文件,方便各位大佬檢查
單片機源程序如下:
- #include<reg52.h>
- #ifndef uchar
- #define uchar unsigned char
- #endif
- #ifndef uint
- #define uint unsigned int
- #endif
- sbit DSPORT =P2^0;
- #define LCD1602_DATAPINS P0
- sbit LCD1602_E=P1^1;
- sbit LCD1602_RS=P1^0;
- uint temp = 0;
- uchar yanwu = 0;
- uchar yushe_temp=50;
- uchar yushe_yanwu=50;
- void check_temp();
- void Delay1ms();
- void Delay_DS18B20(int num);
- void Ds18b20Init();
- void Ds18b20WriteByte(uchar dat);
- uchar Ds18b20ReadByte();
- uint Ds18b20ReadTemp();
- void LcdWriteCom(uchar com);
- void LcdWriteData(uchar dat) ;
- void LcdInit();
- void Display_LCD1602(yushe_temp,yushe_yanwu,temp,yanwu);
- uchar code Init1[]="SET T:00 E:000";
- uchar code Init2[]="NOW T:00.0 E:000";
- void Delay1ms(uint c)
- {
- uchar a,b;
- for (; c>0; c--)
- {
- for (b=199;b>0;b--)
- {
- for(a=1;a>0;a--);
- }
- }
- }
- void LcdWriteCom(uchar com)
- {
- LCD1602_E = 0;
- LCD1602_RS = 0;
-
- LCD1602_DATAPINS = com;
- Delay1ms(1);
- LCD1602_E = 1;
- Delay1ms(5);
- LCD1602_E = 0;
- }
- void LcdWriteData(uchar dat)
- {
- LCD1602_E = 0;
- LCD1602_RS = 1;
- LCD1602_DATAPINS = dat;
- Delay1ms(1);
- LCD1602_E = 1;
- Delay1ms(5);
- LCD1602_E = 0;
- }
-
- void LcdInit()
- {
- uchar i = 0;
- LcdWriteCom(0x38);
- LcdWriteCom(0x0c);
- LcdWriteCom(0x06);
- LcdWriteCom(0x01);
-
- LcdWriteCom(0x80);
- for(i=0;i<16;i++)
- {
- LcdWriteData(Init1[i]);
- }
- LcdWriteCom(0x80+0x40);
- for(i=0;i<16;i++)
- {
- LcdWriteData(Init2[i]);
- }
- }
- void Display_LCD1602(yushe_temp,yushe_yanwu,temp,yanwu)
- {
- LcdWriteCom(0x80+6);
- LcdWriteData(0x30+yushe_temp/10);
- LcdWriteData(0x30+yushe_temp%10);
- LcdWriteCom(0x80+13);
- LcdWriteData(0x30+yushe_yanwu/100);
- LcdWriteData(0x30+yushe_yanwu%100/10);
- LcdWriteData(0x30+yushe_yanwu%10);
- LcdWriteCom(0x80+0x40+6);
- LcdWriteData(0x30+temp/100);
- LcdWriteData(0x30+temp%100/10);
- LcdWriteData('.');
- LcdWriteData(0x30+temp%10);
- LcdWriteCom(0x80+0x40+13);
- LcdWriteData(0x30+yanwu/100);
- LcdWriteData(0x30+yanwu%100/10);
- LcdWriteData(0x30+yanwu%10);
- }
- void Delay_DS18B20(int num)
- {
- while(num--);
- }
- void check_temp()
- {
- temp = Ds18b20ReadTemp();
- if(temp<0) temp = 0;
- if(temp>999) temp = 999;
- }
- void Ds18b20Init()
- {
- uchar x = 0;
- DSPORT = 1;
- Delay_DS18B20(8);
- DSPORT = 0;
- Delay_DS18B20(80);
- DSPORT = 1;
- Delay_DS18B20(34);
- x = DSPORT;
- Delay_DS18B20(20);
- }
- void Ds18b20WriteByte(uchar dat)
- {
- uchar i=0;
- for(i=8; i>0; i--)
- {
- DSPORT = 0;
- DSPORT = dat&0X01;
- Delay_DS18B20(5);
- DSPORT = 1;
- dat>>=1;
- }
- }
- uchar Ds18b20ReadByte()
- {
- uchar i=0;
- uchar byte=0;
- for (i=8; i>0; i--)
- {
- DSPORT = 0;
- byte>>=1;
- DSPORT = 1;
- if(DSPORT)
- byte|=0X80;
- Delay_DS18B20(4);
- }
- return (byte);
- }
- uint Ds18b20ReadTemp()
- {
- uchar tml=0;
- uchar tmh=0;
- uint t=0;
- float tt=0;
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc);
- Ds18b20WriteByte(0x44);
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc);
- Ds18b20WriteByte(0x44);
- tml = Ds18b20ReadByte();
- tmh = Ds18b20ReadByte();
- t = tmh;
- t <<= 8;
- t |= tml;
- tt = t*0.0625;
- t = tt*10+0.5;
- return t;
- }
- void main()
- {
- check_temp();
- LcdInit();
- while(1)
- {
- check_temp();
- Display_LCD1602(yushe_temp,yushe_yanwu,temp,yanwu);
- }
- }
復制代碼 由于有些是移植別人的代碼,yushe_temp,yushe_yanwu,yanwu這幾個變量是我畢業設計的其他要求,可以忽略,重點就是這個temp顯示不了當前溫度值。想破頭都想不到是怎么回事,求助各位,幫忙分析下,指出錯誤點。
|
-
-
實驗.rar
2020-3-14 15:55 上傳
點擊文件名下載附件
34.92 KB, 下載次數: 15
附件為DS18B20+LCD1602顯示實驗
|