Cảm biến nhiệt độ AT89C2051 + DS18S20
Board mạch (kích thước 7.3 x 4.1mm)
Bố trí linh kiện
Sơ đồ nguyên lý
Mã nguồn các bạn mở bằng Keil C
//===========================================
//=CHUONG TRINH HIEN THI NHIET DO TU 00 --> 99 [HIEN THI 2 SO THAP PHAN]=
#include <REG2051.H>
char disp[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char disp_dot[11]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x3f};
sbit DQ = P3^7; //KHAI BAO CHAN CAM BIEN
sbit A1=P3^2; //KHAI BAO CHO LED-7 [1]
sbit A2=P3^3; //KHAI BAO CHO LED-7 [2]
sbit A3=P3^4; //KHAI BAO CHO LED-7 [3]
sbit A4=P3^5; //KHAI BAO CHO LED-7 [4]
//=CHUONG TRINH DELAY
void delay_18B20(unsigned int i)
{
while(i--);
}
//========================
//=KHOI TAO CAM BIEN
void Init_DS18B20()
{
unsigned char x=0;
DQ = 1;
delay_18B20(8);
DQ = 0;
delay_18B20(80);
DQ = 1;
delay_18B20(14);
x=DQ;
delay_18B20(20);
}
//=DOC MOT BYTE
ReadOneChar(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_18B20(4);
}
return(dat);
}
//========================
//=CHUONG TRINH GHI 1BYTE
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay_18B20(5);
DQ = 1;
dat>>=1;
}
}
//========================
//=CHUONG TRINH DOC NHIET DO TU CAM BIEN
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
delay_18B20(100);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
a=ReadOneChar();
b=ReadOneChar();
t = (b*256+a)*25;
return( t >> 2 );
}
//========================
//=CHUONG TRINH CHINH=====
main()
{
unsigned int tmp;
unsigned char counter;
while(1)
{
if(counter-- == 0)
{
tmp = ReadTemperature();
counter = 20;
}
A4 = 0;P1 = 0xc6;delay_18B20(500);A4 = 1;
A3 = 0;P1 = 0x9c;delay_18B20(500);A3 = 1;
A2 = 0;P1 = disp[tmp/100%10];delay_18B20(500);A2 = 1;
A1 = 0;P1 = disp[tmp/1000%10];delay_18B20(500);A1 = 1;
}
}
Download: http://www.mediafire.com/?q42xm5rdhvn7igm
No comments:
Post a Comment