không biết em viết code sai ở đâu mà không hiển thị nhiệt độ được với ds1620. các bác xem jup em. xem sai ở chỗ nào nhé. e đang làm đồ án cần lắm. các bác sửa jup e nhé. thank các bác nhiều.
/************************************************** *********/
/************ chuong trinh hien thi nhiet do
su dung at89c51
ds1620
************************************************** ***********/
#include <REGX51.H>
unsigned char code7seg[10]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
sbit ds1620_dq = P3^0;
sbit ds1620_clk = P3^1;
sbit ds1620_rst = P3^2;
#define read_temp 0xAA // read temp command
#define start_convert 0xEE // bat dau bien doi
#define write_config 0x0C // ghi cau hinh
/*********************************************/
// ham tre
void delay(long time)
{
unsigned char n;
for(n=0;n<time;n++)
{};
}
void tre()
{
unsigned int x;
for(x=0;x<33000;x++)
{};
}
/**********************************************/
// ghi ds1620
void write_ds1620_bit(unsigned char b)
{
ds1620_dq = b; // gui bit
ds1620_clk = 0; // clk = 0
ds1620_clk = 1; // clk = 1
ds1620_dq = 1;
}
/**********************************************/
// doc ds1620
unsigned char read_ds1620_bit()
{
unsigned char b;
ds1620_clk=0;
b=ds1620_dq;
ds1620_clk=1;
return(b);
}
/**********************************************/
// ham ghi giao thuc, lenh vao ds1620
void write_to_ds1620(unsigned char ds1620_function,unsigned char ds1620_data,unsigned char bit_count)
{
unsigned char i,this_bit;
ds1620_rst=1;
for(i=0;i<8;i++)
{
this_bit=ds1620_function>>i;
this_bit=this_bit & 0x01;
write_ds1620_bit(this_bit);
}
for(i=0;i<bit_count;i++)
{
this_bit=ds1620_data>>i;
this_bit=this_bit & 0x01;
write_ds1620_bit(this_bit);
}
ds1620_rst=0;
}
/************************************************/
// ham doc nhiet do tu ds1620
unsigned int read_from_ds1620(unsigned char ds1620_function,unsigned char bit_count)
{
unsigned char i,this_bit;
unsigned char ds1620_data;
ds1620_data=0;
ds1620_rst=1;
for(i=0;i<8;i++)
{
this_bit=ds1620_function>>i;
this_bit=this_bit & 0x01;
write_ds1620_bit(this_bit);
}
for(i=0;i<bit_count;i++)
{
ds1620_data=ds1620_data | read_ds1620_bit() <<i;
}
ds1620_rst=0;
return(ds1620_data);
}
/************************************************/
// ghi cau hinh cho ds1620
void configure_ds1620()
{
write_to_ds1620(write_config,2,8);
tre();
}
/***********************************************/
// ham bat dau chuyen doi nhiet do
void start_temp_convert()
{
write_to_ds1620(start_convert,0,0);
}
/************************************************/
// ham doc nhiet do
unsigned char read_temperature()
{
unsigned int t;
t=read_from_ds1620(read_temp,9);
return(t);
}
/************************************************/
// ham hien thi nhiet do len led 7 thanh
void display_temperature(unsigned char x)
{
P2=0x01;
P0=code7seg[(x%10)];
delay(100);
P2=0x02;
P0=code7seg[(x/10)];
delay(100);
}
/*************************************************/
// ham chinh
void main(void)
{
unsigned int temp;
cofigure_ds1620();
start_temp_convert();
while(1)
{
temp=read_temperature();
display_temperature(temp);
tre();
}
}
/************ chuong trinh hien thi nhiet do
su dung at89c51
ds1620
************************************************** ***********/
#include <REGX51.H>
unsigned char code7seg[10]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
sbit ds1620_dq = P3^0;
sbit ds1620_clk = P3^1;
sbit ds1620_rst = P3^2;
#define read_temp 0xAA // read temp command
#define start_convert 0xEE // bat dau bien doi
#define write_config 0x0C // ghi cau hinh
/*********************************************/
// ham tre
void delay(long time)
{
unsigned char n;
for(n=0;n<time;n++)
{};
}
void tre()
{
unsigned int x;
for(x=0;x<33000;x++)
{};
}
/**********************************************/
// ghi ds1620
void write_ds1620_bit(unsigned char b)
{
ds1620_dq = b; // gui bit
ds1620_clk = 0; // clk = 0
ds1620_clk = 1; // clk = 1
ds1620_dq = 1;
}
/**********************************************/
// doc ds1620
unsigned char read_ds1620_bit()
{
unsigned char b;
ds1620_clk=0;
b=ds1620_dq;
ds1620_clk=1;
return(b);
}
/**********************************************/
// ham ghi giao thuc, lenh vao ds1620
void write_to_ds1620(unsigned char ds1620_function,unsigned char ds1620_data,unsigned char bit_count)
{
unsigned char i,this_bit;
ds1620_rst=1;
for(i=0;i<8;i++)
{
this_bit=ds1620_function>>i;
this_bit=this_bit & 0x01;
write_ds1620_bit(this_bit);
}
for(i=0;i<bit_count;i++)
{
this_bit=ds1620_data>>i;
this_bit=this_bit & 0x01;
write_ds1620_bit(this_bit);
}
ds1620_rst=0;
}
/************************************************/
// ham doc nhiet do tu ds1620
unsigned int read_from_ds1620(unsigned char ds1620_function,unsigned char bit_count)
{
unsigned char i,this_bit;
unsigned char ds1620_data;
ds1620_data=0;
ds1620_rst=1;
for(i=0;i<8;i++)
{
this_bit=ds1620_function>>i;
this_bit=this_bit & 0x01;
write_ds1620_bit(this_bit);
}
for(i=0;i<bit_count;i++)
{
ds1620_data=ds1620_data | read_ds1620_bit() <<i;
}
ds1620_rst=0;
return(ds1620_data);
}
/************************************************/
// ghi cau hinh cho ds1620
void configure_ds1620()
{
write_to_ds1620(write_config,2,8);
tre();
}
/***********************************************/
// ham bat dau chuyen doi nhiet do
void start_temp_convert()
{
write_to_ds1620(start_convert,0,0);
}
/************************************************/
// ham doc nhiet do
unsigned char read_temperature()
{
unsigned int t;
t=read_from_ds1620(read_temp,9);
return(t);
}
/************************************************/
// ham hien thi nhiet do len led 7 thanh
void display_temperature(unsigned char x)
{
P2=0x01;
P0=code7seg[(x%10)];
delay(100);
P2=0x02;
P0=code7seg[(x/10)];
delay(100);
}
/*************************************************/
// ham chinh
void main(void)
{
unsigned int temp;
cofigure_ds1620();
start_temp_convert();
while(1)
{
temp=read_temperature();
display_temperature(temp);
tre();
}
}
Comment