代码如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit DQ = P1^0; //DS18B20数据口
- uchar FLAG=1; //正负温度标志
- unsigned char TMPH,TMPL;
- //这三个引脚参考资料
- sbit E=P2^2; //1602使能引脚
- sbit RW=P2^1; //1602读写引脚
- sbit RS=P2^0; //1602数据/命令选择引脚
- void delay_1()
- {
- int i,j;
- for(i=0; i<=10; i++)
- for(j=0; j<=2; j++)
- ;
- }
- // 1602命令函数
- void enable(uchar del)
- {
- P0 = del;
- RS = 0;
- RW = 0;
- E = 0;
- delay_1();
- E = 1;
- delay_1();
- }
- void write(uchar del)
- {
- P0 =del;
- RS = 1;
- RW = 0;
- E = 0;
- delay_1();
- E = 1;
- delay_1();
- }
- // 1602初始化,请参考1602的资料
- void L1602_init(void)
- {
- enable(0x01);
- enable(0x38);
- enable(0x0c);
- enable(0x06);
- enable(0xd0);
- }
- //改变液晶中某位的值,如果要让第一行,第五个字符显示"b" ,调用该函数如下 L1602_char(1,5,'b')
- void L1602_char(uchar hang,uchar lie,char sign)
- {
- uchar a;
- if(hang == 1) a = 0x80;
- if(hang == 2) a = 0xc0;
- a = a + lie - 1;
- enable(a);
- write(sign);
- }
- //改变液晶中某位的值,如果要让第一行,第五个字符开始显示"ab cd ef" ,调用该函数如下L1602_string(1,5,"ab cd ef;")
- void L1602_string(uchar hang,uchar lie,uchar *p)
- {
- uchar a;
- if(hang == 1) a = 0x80;
- if(hang == 2) a = 0xc0;
- a = a + lie - 1;
- enable(a);
- while(1)
- {
- if(*p == '\0') break;
- write(*p);
- p++;
- }
- }
- void delay(uint N)
- {
- int i;
- for(i=0; i<N; i++)
- ;
- }
- Init_Ds18b20()
- {
- bit Status_Ds18b20;
- DQ=1;
- DQ=0;
- delay(250);
- DQ=1;
- delay(20);
- if(!DQ)
- Status_Ds18b20=0;
- else
- Status_Ds18b20=1;
- delay(250);
- DQ=1;
- return Status_Ds18b20;
- }
- uchar Read_Ds18b20()
- {
- uchar i=0,dat=0;
- for(i=0;i<8;i++)
- {
- DQ=1;
- DQ=0;
- dat>>=1;
- DQ=1;
- if(DQ)
- dat|=0x80;
- DQ=1;
- delay(25);
- }
- return dat;
- }
- void Witie_Ds18b20(uchar dat)
- {
- uchar i=0;
- for(i=0;i<8;i++)
- {
- DQ=1;
- dat>>=1;
- DQ=0;
- DQ=CY;
- delay(25);
- DQ=1;
- }
- }
- void chuli()
- {
- uint temp;
- float tem;
- Init_Ds18b20(); //复位
- Witie_Ds18b20(0xcc); //写跳过ROM命令
- Witie_Ds18b20(0x44); //开启温度转换
- Init_Ds18b20();
- Witie_Ds18b20(0xcc);
- Witie_Ds18b20(0xbe); //读暂存器
- TMPL = Read_Ds18b20();
- TMPH = Read_Ds18b20();
- temp = TMPH;
- temp <<= 8;
- temp = temp | TMPL;
- if(TMPH>=8)
- {
- temp=~temp+1;
- FLAG=1;
- }
- else FLAG=0;
- tem=temp*0.0625;
- temp=tem*100;
- if((temp/10000)==0) //当高位为0时不显示0
- L1602_char(2,5,' ');
- else
- L1602_char(2,5,temp/10000 + 48);
- if((temp/10000)==0&&(temp/1000%10)==0) //当高位为0时不显示0
- L1602_char(2,6,' ');
- else
- L1602_char(2,6,temp/1000%10 + 48);
- L1602_char(2,7,temp/100%10 + 48);
- L1602_char(2,8,0x2e);
- L1602_char(2,9,temp/10%10 + 48);
- L1602_char(2,10,temp%10 + 48);
- L1602_char(2,11,0xdf); //温度符号C前的圈
- if(FLAG==1)
- L1602_char(2,4,0x2d); //输出-号
- else
- L1602_char(2,4,0x2b); //输出+号
- }
- void main()
- {
- // float tt;
- // uint temp;
- P2 = 0x00;
- L1602_init();
- L1602_string(1,1," DS18B20 TEST ");
- L1602_string(2,1," C");
- while(1)
- {
- chuli();
- }
- }
复制代码
硬件没问题,应该就是程序问题,求大佬解答
|