ds18b20温度程序,求大神指导,哪里错了?要求只是显示整...
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar table1[]="tempature: ";
uchar temp0[]={0x00,0x00};
uint display[]={0,0,0,0}; //???è??êy′?′¢??
uchar display_buffer[]={"temp: "}; //??ê??o3???
//μ?μ?μ????è??êyêy?μ
//sbit dula=P2^6;
//sbit wela=P2^7;
sbit lcden=P3^4;
sbit rs=P3^5;
sbit rw=P3^7;
sbit dq=P2^2;
uchar num,a=0,b=0;
int temp;
float f_temp;
void delay_ms(uint z)
{
uchar i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com)
{
rs=0;
P0=com;
delay_ms(5);
lcden=1;
delay_ms(5);
lcden=0;
}
void write_data(uchar date)
{
rs=1;
P0=date;
delay_ms(5);
lcden=1;
delay_ms(5);
lcden=0;
}
void init()
{
//dula=0;
//wela=0;
lcden=0;
rw=0;
write_com(0x38);
write_com(0x0f);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
// void dereset() ìì?é3ìDò
//{
// uint i;
// dq=1;
// delay_ms(1);
// dq=0;
// i=90;
// while(i>0)i--;
// dq=1;
// delay_ms(1);
//
//
//}
void delay(uint x)
{
while(--x);
}
void dereset(void)
{
char status=0;
dq=1;
delay(8);
dq=0;
delay(100);
dq=1;
delay(20);
status=dq;
delay(30);
dq=1;
//return status;
}
//bit tempreadbit(void) //óD′í
//{
// uint i;
// bit dat;
// dq=0;i++;
// dq=1;i++;i++;
// dat=dq;
// i=8;while(i>0);i--;
// return(dat);
//}
//uchar tempread(void)
//{
// uchar i,j,dat;
// dat=0;
// for(i=1;i<=8;i++)
// {
// j=tempreadbit();
// dat=(j<<7)|(dat>>1);
// }
// return(dat);
//}
void tempwritebyte(uchar wdata)
{
uchar i=0;
for(i=8;i>0;i--)
{
dq=0;
delay(15);
dq=wdata&0x01;
delay(45);
dq=1;
wdata>>=1;
}
delay(8);
}
uchar tempread(void)//?áè?DSD??¢×??ú
{
uchar i,dat=0;
//dq=1;
delay(1);
for(i=0;i<8;i++)
{
dq=0;
dat>>=1;
dq=1;
delay(15);
if(dq)
dat|=0x80;
delay(30);
dq=1;
}
return (dat);
}
int read(void)//???è?áè?oˉêy
{ //if(dereset!=1)
//{
//int temp;
dereset();
tempwritebyte(0xcc);
tempwritebyte(0x44);//???ˉ???è×a??
dereset();
tempwritebyte(0xcc);
tempwritebyte(0xbe);//?áè????è??′??÷
delay(50);
a=tempread();
b=tempread();
temp=((a>>4)|((b&0x07)<<4));
return (temp);
// }
}
void display1()
{ int num,current;
current=read();
// current=(a>>4)|(b<<4)&0x3f;
// current=((a>>4)|((b&0x07)<<4));
//current=((a&0xf0)>>4)|((b&0x07)<<4);
// f_temp=current*0.0625;
// current=f_temp*10+0.5;
// display[4]=current/1000;
display[3]=current/100;//°????èμ?°ù·???′?′¢μ???êy′?′¢??
display[2]=current%100/10;//°????èμ?ê?·???′?′¢μ???êy′?′¢??
display[1]=current%10;//°????èμ?????′?′¢μ???êy′?′¢??
display_buffer[8]=display[1]+'0';//?ù°?′?′¢??μ??úèY·¢?íμ??o′???£?×?±???ê?
display_buffer[7]=display[2]+'0';
display_buffer[6]=display[3]+'0';
// display_buffer[5]=display[4]+'0';
if(display[6]==0)
display_buffer[6]=' ';
write_com(0x80);
for(num=0;num<10;num++)
{
write_data(table1[num]);
delay_ms(1);
}
write_com(0x80+0x40);
for(num=0;num<10;num++)
{
write_data(display_buffer[num]);
delay_ms();
}
}
void main()
{
init();
while(1)
{
read();
delay_ms(10);
display1();
}
}
#define uchar unsigned char
#define uint unsigned int
uchar table1[]="tempature: ";
uchar temp0[]={0x00,0x00};
uint display[]={0,0,0,0}; //???è??êy′?′¢??
uchar display_buffer[]={"temp: "}; //??ê??o3???
//μ?μ?μ????è??êyêy?μ
//sbit dula=P2^6;
//sbit wela=P2^7;
sbit lcden=P3^4;
sbit rs=P3^5;
sbit rw=P3^7;
sbit dq=P2^2;
uchar num,a=0,b=0;
int temp;
float f_temp;
void delay_ms(uint z)
{
uchar i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com)
{
rs=0;
P0=com;
delay_ms(5);
lcden=1;
delay_ms(5);
lcden=0;
}
void write_data(uchar date)
{
rs=1;
P0=date;
delay_ms(5);
lcden=1;
delay_ms(5);
lcden=0;
}
void init()
{
//dula=0;
//wela=0;
lcden=0;
rw=0;
write_com(0x38);
write_com(0x0f);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
// void dereset() ìì?é3ìDò
//{
// uint i;
// dq=1;
// delay_ms(1);
// dq=0;
// i=90;
// while(i>0)i--;
// dq=1;
// delay_ms(1);
//
//
//}
void delay(uint x)
{
while(--x);
}
void dereset(void)
{
char status=0;
dq=1;
delay(8);
dq=0;
delay(100);
dq=1;
delay(20);
status=dq;
delay(30);
dq=1;
//return status;
}
//bit tempreadbit(void) //óD′í
//{
// uint i;
// bit dat;
// dq=0;i++;
// dq=1;i++;i++;
// dat=dq;
// i=8;while(i>0);i--;
// return(dat);
//}
//uchar tempread(void)
//{
// uchar i,j,dat;
// dat=0;
// for(i=1;i<=8;i++)
// {
// j=tempreadbit();
// dat=(j<<7)|(dat>>1);
// }
// return(dat);
//}
void tempwritebyte(uchar wdata)
{
uchar i=0;
for(i=8;i>0;i--)
{
dq=0;
delay(15);
dq=wdata&0x01;
delay(45);
dq=1;
wdata>>=1;
}
delay(8);
}
uchar tempread(void)//?áè?DSD??¢×??ú
{
uchar i,dat=0;
//dq=1;
delay(1);
for(i=0;i<8;i++)
{
dq=0;
dat>>=1;
dq=1;
delay(15);
if(dq)
dat|=0x80;
delay(30);
dq=1;
}
return (dat);
}
int read(void)//???è?áè?oˉêy
{ //if(dereset!=1)
//{
//int temp;
dereset();
tempwritebyte(0xcc);
tempwritebyte(0x44);//???ˉ???è×a??
dereset();
tempwritebyte(0xcc);
tempwritebyte(0xbe);//?áè????è??′??÷
delay(50);
a=tempread();
b=tempread();
temp=((a>>4)|((b&0x07)<<4));
return (temp);
// }
}
void display1()
{ int num,current;
current=read();
// current=(a>>4)|(b<<4)&0x3f;
// current=((a>>4)|((b&0x07)<<4));
//current=((a&0xf0)>>4)|((b&0x07)<<4);
// f_temp=current*0.0625;
// current=f_temp*10+0.5;
// display[4]=current/1000;
display[3]=current/100;//°????èμ?°ù·???′?′¢μ???êy′?′¢??
display[2]=current%100/10;//°????èμ?ê?·???′?′¢μ???êy′?′¢??
display[1]=current%10;//°????èμ?????′?′¢μ???êy′?′¢??
display_buffer[8]=display[1]+'0';//?ù°?′?′¢??μ??úèY·¢?íμ??o′???£?×?±???ê?
display_buffer[7]=display[2]+'0';
display_buffer[6]=display[3]+'0';
// display_buffer[5]=display[4]+'0';
if(display[6]==0)
display_buffer[6]=' ';
write_com(0x80);
for(num=0;num<10;num++)
{
write_data(table1[num]);
delay_ms(1);
}
write_com(0x80+0x40);
for(num=0;num<10;num++)
{
write_data(display_buffer[num]);
delay_ms();
}
}
void main()
{
init();
while(1)
{
read();
delay_ms(10);
display1();
}
}
调出来了,是时序没有弄对。
积分太难挣了,下载资料都难