các anh xem hộ em chương trình của em sai ở đâu mà nó không đọc đươc nhiệt độ từ ds18b20.em dùng thạch anh 12M.
#include <REGX52.H>
#include <string.h>
#include <stdio.h>
sfr LCDdata=0xA0;
sbit BF=P2^7;
sbit RS=P1^0;
sbit RW=P1^1;
sbit EN=P1^2;
sbit DQ=P3^7;
void delay(unsigned int time)
{
while(time--);
}
void wait(void)
{
unsigned char n=0;
EN=1;
RS=0;
RW=1;
LCDdata=0xFF;
while(BF) {n++;if(n>100) break;}
EN=0;
RW=0;
}
void LCDcontrol(unsigned char x)
{
EN=1;
RS=0;
RW=0;
LCDdata=x;
EN=0;
wait();
}
void LCDwrite(unsigned char c)
{
EN=1;
RS=1;
RW=0;
LCDdata=c;
EN=0;
wait();
}
void LCDinit(void)
{
LCDcontrol(0x30);
LCDcontrol(0x30);
LCDcontrol(0x30);
LCDcontrol(0x38);
LCDcontrol(0x0C);
LCDcontrol(0x06);
LCDcontrol(0x01);
}
void LCDgotoxy(unsigned char cot,unsigned char hang)
{
char d;
if(hang==0) LCDcontrol(0x80);
else LCDcontrol(0xC0);
for(d=0;d<cot;d++)
{
LCDcontrol(0x14);
}
}
void LCDclear()
{
LCDcontrol(0x01);
}
void LCDputs(unsigned char *s)
{
unsigned char len;
len=strlen(s);
while(len!=0)
{
LCDwrite(*s);
s++;
len--;
}
}
//-------------------------------------------------
// Ham khoi tao DS18b20
//-------------------------------------------------
void Init_DS18B20()
{
unsigned char x=0;
DQ = 1;
delay(8);
DQ = 0;
delay(80);
DQ = 1;
delay(14);
delay(20);
x=DQ;
}
//=DOC MOT BYTE
unsigned char ds18b20_readbyte(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; //CHO DUONG DQ XUONG MUC THAP
dat>>=1;
DQ = 1; //CHO DUONG DQ LEN MUC CAO
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//========================
//=CHUONG TRINH GHI 1BYTE
void ds18b20_writebyte(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
}
//-------------------------------------------------
// Ham chuyen doi va tra ve gia tri nhiet do do duoc
//-------------------------------------------------
float ds18b20_readtemp(void)
{
float temp;
unsigned char a,b;
Init_DS18B20; // Khoi tao DS18b20
ds18b20_writebyte(0xCC) ; // Cho phep VDK truy cap thang den cac lenh bo nho cua DS18b20
ds18b20_writebyte(0x44) ; // Khoi dong qua trinh do va chuyen doi nhiet do ra so nhi phan
Init_DS18B20 ;
ds18b20_writebyte(0xCC) ;
ds18b20_writebyte(0xBE) ; // Cho phep doc du lieu tu bo nho DS18b20 ra ngoai
a = ds18b20_readbyte();
tempfloat)(a&0x0f))/16; // Lay phan thuc cua gia tri nhiet do
b = ds18b20_readbyte();
a a&0xf0)>>4)|((b&0x0f)<<4) ; // Lay phan nguyen cua gia tri nhiet do
temp=temp+a;
return temp; // Tra ve gia tri nhiet do
}
void main()
{
float tep;
unsigned char Lcd_Buff[15];
LCDinit();
LCDgotoxy(4,0);
LCDputs("WELCOME");
delay(40000);
LCDclear();
while(1)
{
LCDgotoxy(0,0);
LCDputs(" ");
LCDgotoxy(0,0);
LCDputs("cam bien nhiet");
tep = ds18b20_readtemp();
sprintf(Lcd_Buff,"TEMP: %2.2f^C",tep);
LCDgotoxy(0,1);
LCDputs(Lcd_Buff);
delay(300);
}
}
em mô phỏng nó cứ hiện nhiệt độ là cố định TEMP:255.94^C.
các anh giúp em với.
#include <REGX52.H>
#include <string.h>
#include <stdio.h>
sfr LCDdata=0xA0;
sbit BF=P2^7;
sbit RS=P1^0;
sbit RW=P1^1;
sbit EN=P1^2;
sbit DQ=P3^7;
void delay(unsigned int time)
{
while(time--);
}
void wait(void)
{
unsigned char n=0;
EN=1;
RS=0;
RW=1;
LCDdata=0xFF;
while(BF) {n++;if(n>100) break;}
EN=0;
RW=0;
}
void LCDcontrol(unsigned char x)
{
EN=1;
RS=0;
RW=0;
LCDdata=x;
EN=0;
wait();
}
void LCDwrite(unsigned char c)
{
EN=1;
RS=1;
RW=0;
LCDdata=c;
EN=0;
wait();
}
void LCDinit(void)
{
LCDcontrol(0x30);
LCDcontrol(0x30);
LCDcontrol(0x30);
LCDcontrol(0x38);
LCDcontrol(0x0C);
LCDcontrol(0x06);
LCDcontrol(0x01);
}
void LCDgotoxy(unsigned char cot,unsigned char hang)
{
char d;
if(hang==0) LCDcontrol(0x80);
else LCDcontrol(0xC0);
for(d=0;d<cot;d++)
{
LCDcontrol(0x14);
}
}
void LCDclear()
{
LCDcontrol(0x01);
}
void LCDputs(unsigned char *s)
{
unsigned char len;
len=strlen(s);
while(len!=0)
{
LCDwrite(*s);
s++;
len--;
}
}
//-------------------------------------------------
// Ham khoi tao DS18b20
//-------------------------------------------------
void Init_DS18B20()
{
unsigned char x=0;
DQ = 1;
delay(8);
DQ = 0;
delay(80);
DQ = 1;
delay(14);
delay(20);
x=DQ;
}
//=DOC MOT BYTE
unsigned char ds18b20_readbyte(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; //CHO DUONG DQ XUONG MUC THAP
dat>>=1;
DQ = 1; //CHO DUONG DQ LEN MUC CAO
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//========================
//=CHUONG TRINH GHI 1BYTE
void ds18b20_writebyte(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
}
//-------------------------------------------------
// Ham chuyen doi va tra ve gia tri nhiet do do duoc
//-------------------------------------------------
float ds18b20_readtemp(void)
{
float temp;
unsigned char a,b;
Init_DS18B20; // Khoi tao DS18b20
ds18b20_writebyte(0xCC) ; // Cho phep VDK truy cap thang den cac lenh bo nho cua DS18b20
ds18b20_writebyte(0x44) ; // Khoi dong qua trinh do va chuyen doi nhiet do ra so nhi phan
Init_DS18B20 ;
ds18b20_writebyte(0xCC) ;
ds18b20_writebyte(0xBE) ; // Cho phep doc du lieu tu bo nho DS18b20 ra ngoai
a = ds18b20_readbyte();
tempfloat)(a&0x0f))/16; // Lay phan thuc cua gia tri nhiet do
b = ds18b20_readbyte();
a a&0xf0)>>4)|((b&0x0f)<<4) ; // Lay phan nguyen cua gia tri nhiet do
temp=temp+a;
return temp; // Tra ve gia tri nhiet do
}
void main()
{
float tep;
unsigned char Lcd_Buff[15];
LCDinit();
LCDgotoxy(4,0);
LCDputs("WELCOME");
delay(40000);
LCDclear();
while(1)
{
LCDgotoxy(0,0);
LCDputs(" ");
LCDgotoxy(0,0);
LCDputs("cam bien nhiet");
tep = ds18b20_readtemp();
sprintf(Lcd_Buff,"TEMP: %2.2f^C",tep);
LCDgotoxy(0,1);
LCDputs(Lcd_Buff);
delay(300);
}
}
em mô phỏng nó cứ hiện nhiệt độ là cố định TEMP:255.94^C.
các anh giúp em với.
Comment