Ai cho em code mach này với. E muốn khi ấn nút On/ Off thì led hiển thị nhiệt độ. Ấn cái nữa thì tắt. Đồng thời khi ấn On sau đó an Start thì Led van cấp của em báo sáng. Ai làm dc giúp e với.
.
còn đây là code DB18S20.
#include <REGX51.H>
#define scan_seg P1
#define dt_seg P2
sbit DQ = P3^6; // chan ket noi voi DS1820
unsigned char data_seg[13] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 ,0x9C,0xC6,0xBF }; // Ma led 7 thanh
#include <stdio.h>
void delay_18B20(unsigned int i);
void Init_18B20(void) ; // khoi tao DS1820
unsigned char ReadOneChar(void); // doc mot byte trong DS1820
void WriteOneChar(unsigned char dat); // Ghi byte vao DS1820
void ReadTemperature(void); // Doc gia tri nhiet do ket qu cuoi cung luu o temp
unsigned char data_rs232,temp,temp_1,k;
//*********************// chuong trinh cho DS18B20
void delay_18B20(unsigned int i)
{
while(i--);
}
void Init_18B20(void)
{
unsigned char x=0;
DQ = 1;
delay_18B20(8);
DQ = 0;
delay_18B20(80);
DQ = 1;
delay_18B20(14);
x=DQ;
delay_18B20(10);
}
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat |= 0x80;
delay_18B20(4);
}
return(dat);
}
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;
}
}
void ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
Init_18B20();
WriteOneChar(0xCC); // Bo qua ROM
WriteOneChar(0x44);
delay_18B20(100); // this message is very important
Init_18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE); //Doc nhiet do
delay_18B20(100);
a=ReadOneChar(); //Read temp low value
b=ReadOneChar(); //Read temp high value
tempb*256+a)>>4); //gia tri nhiet do
}
void delay_us(unsigned int a)
{
unsigned int d;
for (d=0;d<a;d++);
}
void hienthitemp(void)
{
ReadTemperature();
//scan_seg=0x00;
scan_seg=0x01;
dt_seg=data_seg[temp/10];
delay_us(120);
// scan_seg=0x00;
scan_seg=0x02;
dt_seg=data_seg[temp%10];
delay_us(120);
//scan_seg=0x00;
scan_seg=0x04;
dt_seg=data_seg[10];
delay_us(120);
// scan_seg=0x00;
scan_seg=0x08;
dt_seg=data_seg[11];
delay_us(120);
}
// chuong trinh chinh
void main(void)
{
while(1)
{
hienthitemp();
}
}
.
còn đây là code DB18S20.
#include <REGX51.H>
#define scan_seg P1
#define dt_seg P2
sbit DQ = P3^6; // chan ket noi voi DS1820
unsigned char data_seg[13] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 ,0x9C,0xC6,0xBF }; // Ma led 7 thanh
#include <stdio.h>
void delay_18B20(unsigned int i);
void Init_18B20(void) ; // khoi tao DS1820
unsigned char ReadOneChar(void); // doc mot byte trong DS1820
void WriteOneChar(unsigned char dat); // Ghi byte vao DS1820
void ReadTemperature(void); // Doc gia tri nhiet do ket qu cuoi cung luu o temp
unsigned char data_rs232,temp,temp_1,k;
//*********************// chuong trinh cho DS18B20
void delay_18B20(unsigned int i)
{
while(i--);
}
void Init_18B20(void)
{
unsigned char x=0;
DQ = 1;
delay_18B20(8);
DQ = 0;
delay_18B20(80);
DQ = 1;
delay_18B20(14);
x=DQ;
delay_18B20(10);
}
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat |= 0x80;
delay_18B20(4);
}
return(dat);
}
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;
}
}
void ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
Init_18B20();
WriteOneChar(0xCC); // Bo qua ROM
WriteOneChar(0x44);
delay_18B20(100); // this message is very important
Init_18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE); //Doc nhiet do
delay_18B20(100);
a=ReadOneChar(); //Read temp low value
b=ReadOneChar(); //Read temp high value
tempb*256+a)>>4); //gia tri nhiet do
}
void delay_us(unsigned int a)
{
unsigned int d;
for (d=0;d<a;d++);
}
void hienthitemp(void)
{
ReadTemperature();
//scan_seg=0x00;
scan_seg=0x01;
dt_seg=data_seg[temp/10];
delay_us(120);
// scan_seg=0x00;
scan_seg=0x02;
dt_seg=data_seg[temp%10];
delay_us(120);
//scan_seg=0x00;
scan_seg=0x04;
dt_seg=data_seg[10];
delay_us(120);
// scan_seg=0x00;
scan_seg=0x08;
dt_seg=data_seg[11];
delay_us(120);
}
// chuong trinh chinh
void main(void)
{
while(1)
{
hienthitemp();
}
}