Program to provide 1msec Delay
Delay.h
void Delayus(unsigned int);
void Delayms(unsigned int);
void Delayms(unsigned int m)
{
unsigned int i,j;
for(i=0;i<m;i++)
for(j=0;j<1275;j++);
}
DS18b20 Temparature Sensor
Ds18b20.h
sbit DQ = P3^5; // connect with DS1820 Data pin
void DelayMs(unsigned int);
bit ResetDS1820(void);
void DelayUs(int);
bit ReadBit(void);
void WriteBit(bit);
unsigned char ReadByte(void);
void WriteByte(unsigned char);
int ReadTemp(void);
void Delayus(int);
void DelayUs(int us)
{
int i;
for (i=0; i<us; i++);
}
//----------------------------------------
// Reset DS1820
//----------------------------------------
bit ResetDS1820(void)
{
bit presence;
DQ = 0; //pull DQ line low
DelayUs(29); // leave it low for about 490us
DQ = 1; // allow line to return high
DelayUs(3); // wait for presence 55 uS
presence = DQ; // get presence signal
DelayUs(25); // wait for end of timeslot 316 uS
return(presence); // presence signal returned
} // 0=presence, 1 = no part
//-----------------------------------------
// Read one bit from DS1820
//-----------------------------------------
bit ReadBit(void)
{
unsigned char i=0;
DQ = 0; // pull DQ low to start timeslot
DQ=1;
for (i=0; i<3; i++); // delay 17 us from start of timeslot
return(DQ); // return value of DQ line
}
//-----------------------------------------
// Write one bit to DS1820
//-----------------------------------------
void WriteBit(bit Dbit)
{
unsigned char i=0;
DQ=0;
DQ = Dbit ? 1:0;
DelayUs(5); // delay about 39 uS
DQ = 1;
}
//-----------------------------------------
// Read 1 byte from DS1820
//-----------------------------------------
unsigned char ReadByte(void)
{
unsigned char i;
unsigned char Din = 0;
for (i=0;i<8;i++)
{
Din|=ReadBit()? 0x01<<i:Din;
DelayUs(6);
}
return(Din);
}
//-----------------------------------------
// Write 1 byte
//-----------------------------------------
void WriteByte(unsigned char Dout)
{
unsigned char i;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
WriteBit((bit)(Dout & 0x1)); // write bit in temp into
Dout = Dout >> 1;
}
DelayUs(5);
}
//-----------------------------------------
// Read temperature
//-----------------------------------------
int ReadTemp(void)
{
unsigned char n,buff[2];
int temp;
EA=0; // disable all interrupt
ResetDS1820();
WriteByte(0xcc); // skip ROM
WriteByte(0x44); // perform temperature conversion
while (ReadByte()==0xff); // wait for conversion complete
ResetDS1820();
WriteByte(0xcc); // skip ROM
WriteByte(0xbe); // read the result
for (n=0; n<2; n++) // read 9 bytes but, use only one byte
{
buff[n]=ReadByte(); // read DS1820
}
EA=1;
temp=buff[1];
temp=temp<<8;
temp=temp|buff[0];
return(temp);
}
Program to roll a string on LCD
LCD.h
#define lcd P0 //LCD Data lines connected to Port 0
sbit rs=P2^0; //RS bit connected to Port 2.0
sbit rw=P2^1; //RW bit connected to Port 2.1
sbit en=P2^2; //En bit connected to Port 2.2
void init_lcd(void);
void writecmd(unsigned char);
void lcd_char(unsigned char);
void lcd_str(unsigned char*,unsigned char);
void lcd_int(char);
void Delayus(int);
//----------------------------------------
// Initlise LCD
//----------------------------------------
void init_lcd()
{
writecmd(0x38);
writecmd(0x0c);
writecmd(0x06);
writecmd(0x01);
}
//----------------------------------------
// Writing command to LCD
//----------------------------------------
void writecmd(unsigned char c)
{
lcd=c;
rs=rw=0;
en=1;
Delayus(400);
en=0;
}
//----------------------------------------
// Writing Char on LCD
//----------------------------------------
void lcd_char(unsigned char d)
{
lcd=d;
rs=1;
rw=0;
en=1;
Delayus(400);
en=0;
}
//----------------------------------------
// Writing String
//----------------------------------------
void lcd_str(unsigned char *s,unsigned char l)
{
writecmd(l);
while(*s)
lcd_char(*s++);
}
//----------------------------------------
// Writing Integer
//----------------------------------------
void lcd_int(char i)
{
char d=0,t=0,h=0;
if(i)
{
d=(i%10)+48;
i=i/10;
if(i)
{
t=(i%10)+48;
i=i/10;
if(i)
h=i+48;
}
}
if(h)
lcd_char(h);
if(t)
lcd_char(t);
if(d)
lcd_char(d);
}
Serial Data Transmission
Serial.h
void init_serial(void);
void ser_char(unsigned char);
void ser_str(unsigned char *);
void ser_int(char);
//----------------------------------------
// Initlise Serial communication
//----------------------------------------
void init_serial()
{
TMOD=0x20;
TH1=-3;
SCON=0x40;
}
//----------------------------------------
// Sending Character
//----------------------------------------
void ser_char(unsigned char c)
{
TR1=1;
SBUF=c;
while(!TI);
TR1=0;
TI=0;
}
//----------------------------------------
// Sending String
//----------------------------------------
void ser_str(unsigned char *s)
{
while(*s)
ser_char(*s++);
}
//----------------------------------------
// Sending integer
//----------------------------------------
void ser_int(char i)
{
char d=0,t=0,h=0;
if(i)
{
d=(i%10)+48;
i=i/10;
if(i)
{
t=(i%10)+48;
i=i/10;
if(i)
h=i+48;
}
}
if(h)
ser_char(h);
if(t)
ser_char(t);
if(d)
ser_char(d);
}
Main Program To Calculate Temparature by Using 1-wire Protocol
Main.c
#include<reg51.h>
#include"lcd.h"
#include"serial.h"
#include"ds18b20.h"
void Delayms(unsigned int);
main()
{
int temp;
char tp,tpd,n=0;
init_lcd();
init_serial();
lcd_str("Temperature:",0x80);
//ser_str("Temperature: ");
while(1)
{
n++;
temp=ReadTemp();
tp = temp>>4;
tpd=temp&0x08?0x35:0x30;
writecmd(0xc0);
lcd_int(tp);
lcd_char('.');
lcd_char(tpd);
lcd_str(" Degree C",0xc4);
ser_int(n);
ser_str(" Temperature= ");
ser_int(tp);
ser_char('.');
ser_char(tpd);
ser_str(" Degree C\r\n ");
Delayms(200);
}
}
void Delayms(unsigned int m)
{
unsigned int i,j;
for(i=0;i<m;i++)
for(j=0;j<1275;j++);
}
Read More