Showing posts with label Embedded C. Show all posts
Showing posts with label Embedded C. Show all posts

Sunday, 7 December 2014

Embedded C code for UART serial communication

#include<reg51.h>
//#include"delay.h"
//#include"74hc595.h"

#ifndef _74hc595_h_
#define _74hc595_h_
sbit ds=P3^0;	//connect to serial input pin of 74hc595
sbit clk=P3^1;	//connect to clock input
sbit _mr=P3^2;	//connect to reset input
#define rst_74hc595_mr=0;_mr=1;	//to reset a device
//routine to convert 8bit parellel to 1bit serial msb first
void write_74hc595(unsigned char dat)
{
unsigned char i;
	for(i=0;i<=7;i++)
	{
		clk=0;
		ds=(dat&(0x80>>1))?1:0;
		clk=1;
	}
//extra clock for moving from shift register to storage register
	clk=0;
	clk=1;
//rst_74hc595 //reset shift register(optional)
}



void main()
{
unsigned int i;
for(i=0;i<=255;i++)
{
write_74hc595(i);
//delay_ms(500);
}
while(1);
}
Read More

Saturday, 6 December 2014

#include<reg51.h>
code char str[]="welcome to vector";
data char t[20] _at_ 0x40;
xdata char t1[20] _at_ 0x1000;
main()
{
unsigned char i;
for(i=0;str[i];i++)
{
t[i]=t1[i]=str[i];
}
while(1);
}
Read More

Embedded C code for switching of LED

#include<reg51.h>
//sfr P2=0x0a0;
sbit sw1=P2^0;
sbit sw2=P2^1;
sbit sw3=P2^2;
sbit sw4=P2^3;
sbit led1=P2^4;
sbit led2=P2^5;
sbit led3=P2^6;
sbit led4=P2^7;

main()
{
while(1)
{
led1=sw1;
led2=sw2;
led3=sw3;
led4=sw4;
}
Read More

Embedded C code for LCD display


#include<reg51.h>
//#include"lcd8.h"

#define lcd_data P0
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void write_lcd(unsigned char dat)
{
lcd_data=dat;
RW=0;
EN=1;
EN=0;
//delay_ms(2);
}

void cmd_lcd(unsigned char cmd)
{
RS=0;
write_lcd(cmd);
}

void disp_lcd(unsigned char c)
{
RS=1;
write_lcd(c);
}

void init_lcd(void)
{
cmd_lcd(0x01);
cmd_lcd(0x02);
cmd_lcd(0x06);
cmd_lcd(0x0e);
cmd_lcd(0x38);
cmd_lcd(0x80);
}

void str_lcd(char *s)
{
while(*s)
disp_lcd(*s++);
}

void int_lcd(unsigned int n)
{
char a[5]={0},i=0;
if(n==0)
{
disp_lcd('0');
return;
}

else
{
while(n>0)
{
a[i++]=((n%10)+48);
n=n/10;
}
for(--i;i>=0;i--)
{
disp_lcd(a[i]);
}
}
}

void float_lcd(float f)
{
int i;
unsigned char j;
i=f;
int_lcd(i);
disp_lcd('.');
for(j=0;j<3;j++)
{
f=(f-i)*10;
i=f;
int_lcd(i);
}
}


main()
{
init_lcd();
disp_lcd('v');
str_lcd("hello");
cmd_lcd(0xc0);
int_lcd(1234);
float_lcd(56.34);
while(1);
{
}
}
Read More

Embedded C code for 3x3 keypad matrix

#ifdef _keypad_h_
#define _keypad_h_
sbit r0=p3^0;sbit c0=p3^4;
sbit r1=p3^1;sbit c1=p3^5;
sbit r2=p3^2;sbit c2=p3^6;
sbit r3=p3^3;sbit c3=p3^7;
code unsigned char keypad_lut[4][4]=
{
1,2,3,4,
5,6,7,8,
9,10,11,12,
13,143,15,16
};
bit colscan()
{
return(c0&c1&c2&c3);
}
unsigned char keyscan()
{
unsigned char row,col;
c0=c1=c2=c3=1;
r0=r1=r2=r3=0;
while(colscan());
r0=0;
r1=r2=r3=1;
if(!colscan())
{
row=0;goto colcheck;
}

r1=0;
r0=r2=r3=1;
if(!colscan())
{
row=1; goto colcheck;
}

r2=0;
r0=r2=r3=1;
if(!colscan())
{
row=2; goto colcheck;
}

r3=0;
r0=r1=r3=1;
if(!colscan())
{
row=3; goto colcheck;
}

colcheck:
if(c0==0)
col=0;
else if(c1==0)
col=1;
else if(c2==0)
col=2;
else if(c3==0)
col=3;
return keypad_lut[row][col];
}
#endif
Read More
sbit scl=P3^0;
sbit sda=P3^1;
void i2c_start(void);
void i2c_write(unsigned char dat);
void i2c_ack(void);
void i2c_stop(void);
unsigned char i2c_read(void);
void i2c_noack(void);
void i2c_device_write(unsigned char sa, unsigned char addr,unsigned char dat);
unsigned char i2c_device_read(unsigned char sa,unsigned char dat);
///////////////////////////////////////////////////////
void i2c_start(void)
{
scl=1;
sda=1;
sda=0;
}

void i2c_write(unsigned char dat)
{
unsigned char i;
	for(i=0;i<=7;i++)
	{
	scl=0;
	sda=(dat&(0x80>>i))?1:0;
	scl=1;
	}
}

void i2c_ack(void)
{
scl=0;
sda=1;
scl=1;
scl=0;
}

void i2c_stop(void)
{
scl=0;
sda=0;
scl=1;
sda=1;
}

void i2c_device_write(unsigned char sa,unsigned char addr,unsigned char dat)
{
i2c_start();
i2c_write(sa);
i2c_ack();
i2c_write(addr);
i2c_ack();
i2c_write(dat);
i2c_ack();
i2c_stop();
//delay_ms(9);
}

unsigned char i2c_read(void)
{
unsigned char buff=0x00,i;
sda=1;
	for(i=0;i<=7;i++)
	{
	scl=1;
	if(sda)
	buff=buff|(0x80>>i);
	scl=0;
	}
	return buff;
}

void i2c_noack()
{
scl=0;
sda=1;
scl=1;
}

unsigned char i2c_device_read(unsigned char sa,unsigned char addr)
{
unsigned char buff;
i2c_start();
i2c_write(sa);
i2c_ack();
i2c_write(addr);
i2c_ack();
i2c_start();
i2c_write(sa|1);
i2c_ack();
buff=i2c_read();
i2c_noack();
i2c_stop();
return buff;
}

i2cdriver.c
#include<reg51.h>
#include"i2c.h"
char t _at_ 0x30;
main()
{
i2c_device_write(0xa0,0x00,'U');
t=i2c_device_read(0xa0,0x00);
while(1);
}
Read More

Embedded C program for Graphical LCD(GLCD) in 8051

#define glcd P2
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

sbit RST=P2^3;
sbit CS1=P2^4;
sbit CS2=P2^5;
#define PAGE_0 0xb8		//first page address
#define PAGE_7 0xbf		//last page address
#define COL_ADDR 0x40	//base address of 64 columns

#define NONE 0
#define CHIP1 1
#define CHIP2 2
#define BOTH 3

//primary drives proto types
void InitGLCD(void);
void CmdGLCD(unsigned char,unsigned char);
void DispGLCD(unsigned char);
void WriteGLCD(unsigned char);

void ChipSel(unsigned char);

//secondary driver proto types
void ClrScreen();
void ClrPage(unsigned char,unsigned char);
void ShowDigit(unsigned char);
void ShowAlphabet(unsigned char);

//api proto types
void Puts_GLCD(char *);
void Puti_GLCD(unsigned int);
void Putf_GLCD(float f);
void ShowImage(void);

/////////////////////////////////////////
void InitGLCD(void)
{
RST=0;
RST=1;		//high clock for reset lcd controllers
CmdGLCD(0x3e,BOTH);		//to turn it OFF
CmdGLCD(0x3f,BOTH);		//to display ON
CmdGLCD(0xb8,BOTH);		//page '0' base address
CmdGLCD(0x40,BOTH);		//column base address
CmdGLCD(0xc0,BOTH);		//take cursor to top line of refresh RAM
}
///////////////////////////////////////
void CmdGLCD(unsigned char cmd,unsigned char chip)
{
ChipSel(chip);
RS=0;
WriteGLCD(ch);
}
////////////////////////////////////////
void DispGLCD(unsigned char ch)
{
RS=1;
WriteGLCD(ch);
}
//////////////////////////////////////
void WriteGLCD(unsigned char val)
{
RW=0;
GLCD=val;
EN=1;
EN=0;
}
////////////////////////////////////////
void ChipSel(unsigned char cs)
{
switch(cs)
{
case 0:CS1=0;CS2=0; return;
case 1:CS1=1;CS2=0; return;
case 2:CS1=0;CS2=1; return;
case 3:CS1=1;CS2=1; return;
}
}
////////////////////////////////////////
void ClrScreen()
{
unsigned char i,y;
for(i=PAGE_0;i<=PAGE_7;i++)
{
CmdGLCD(i,BOTH); //select pages
CmdGLCD(COL_ADDR,BOTH);
for(y=0;y<64;y++)
DispGLCD(0x00);		//clear data in GLCD
}
}
///////////////////////////////////////
void ClrPage(unsigned char page,unsigned char chip)
{
unsigned char y;
CmdGLCD(PAGE_0+page,chip);
CmdGLCD(COL_ADDR,chip);
for(y=0;y<64;y++)
DispGLCD(0x00);		//clear data in GLCD
}
///////////////////////////////////////
code unsigned char digit_arr[10][4]=
{
	0xff,0x81,0x81,0xff,		//0
	0xff,0x00,0x00,0x00,		//1
	0xf9,0x89,0x89,0x8f,		//2
	0x89,0x89,0x89,0xff,		//3
	0x0f,0x08,0x08,0xff,		//4
	0x8f,0x89,0x89,0xf9,		//5
	0xff,0x89,0x89,0xf9,		//6
	0x01,0x01,0x01,0xff,		//7
	0xff,0x89,0x89,0xff,		//8
	0x9f,0x91,0x91,0xff,		//9
};
//////////////////////////////////////
void ShowDigit(unsigned char dig)
{
unsigned char i;
for(i=0;i<4;i++)
{
DispGLCD(digit_arr[dig][i]);
}
DispGLCD(0x00);		//space between each digit
}
//////////////////////////////////////
void Puti_GLCD(unsigned int n)
{
char a[5]={0},i=0;
if(n==0)
{
ShowDigit(0);
return;
}
while(n)
{
a[i++]=(n%10);
n=n/10;
}
for(--i;i>=0;i--)
{
ShowDigit(a[i]);
}
}
///////////////////////////////////

code unsigned char alpha_arr[26][4]=
{
	0xfe,0x09,0x09,0xfe,	//A
	0xff,0x89,0x89,0xf6,	//B
	0xff,0x81,0x81,0x81		//C
};
///////////////////////////////////
void ShowAlphabet(char c)
{
unsigned char i;
for(i=0;i<4;i++)
{
DispGLCD(alpha_arr[c-'A'][i]);
}
DispGLCD(0x00);		//to provide spacing
}
////////////////////////////////////
void Puts_GLCD(char *s)
{
while(*s)
ShowAlphabet(*s++);
}
///////////////////////////////////
void Putf_GLCD(float f)
{
int i;
unsigned char j;
i=f;
Puti_GLCD(i);
DispGLCD(0x80);
for(j=0;j<3;j++)
{
f=(f-i)*10;
i=f;
Puti_GLCD(i);
}
}
///////////////////////////////

/*void ShowImage()
{
unsigned char i,j;
for(i=0;i<8;i++)
{
CmdGLCD(0xb8+i,BOTH);
CmdGLCD(0x40,BOTH);
ChipSel(1);
for(j=0;j<128;j++)
{
if(j==64)
{
ChipSel(2);
}
DispGLCD(arr[i][j]);
}
}
}*/

Glcdmain.c
#include<reg51.h>
//#include"glcd.h"
//#include"delay.h"
void main(void)
{
unsigned char i;
InitGLCD();
CmdGLCD(PAGE_0,BOTH);
CmdGLCD(COL_ADDR,BOTH);
Puti_GLCD(1234);
ShowAlphabet('A');
ShowDigit('5');
Puts_GLCD("HAI");
while(1);
}
Read More

Embedded C Program to provide 1msec time delay

void delay_ms(unsigned int dly)
{
unsigned int i;
for(;dly>0;dly--)
{
for(i=122;i>0;i--);
}
}
main()
{
delay_ms(1);
while(1);
}
Read More

Embedded C program for LCD by using bit bang method

#define TEST 2
#include<reg51.h>
//#include"delay.h"
#if TEST==1	//using bit banging method to write
#include"74hc595.h"
#endif
#if TEST==2	//using UARTMode_0 for write
#include"uartmode_0.h"
#include"utility.h"
#endif
//#include"lcd8.h"

////////////////lcd8/////////

#define lcd_data P0
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void write_lcd(unsigned char c)
{
#if TEST==1
write_74hc595(c);
#endif;
#if TEST==2
rev_bits2(c);
uart_tx(x);
#endif
RW=0;
EN=1;
EN=0;
//delay_ms(2);
}
void cmd_lcd(unsigned char cmd)
{
RS=0;
write_lcd(cmd);
}

void disp_lcd(unsigned char c)
{
RS=1;
write_lcd(c);
}

void init_lcd(void)
{
cmd_lcd(0x01);
cmd_lcd(0x02);
cmd_lcd(0x06);
cmd_lcd(0x0e);
cmd_lcd(0x38);
cmd_lcd(0x80);
}

void str_lcd(char *s)
{
while(*s)
disp_lcd(*s++);
}

void int_lcd(unsigned int n)
{
char a[5]={0},i=0;
if(n==0)
{
disp_lcd('0');
return;
}

else
{
while(n>0)
{
a[i++]=((n%10)+48);
n=n/10;
}
for(--i;i>=0;i--)
{
disp_lcd(a[i]);
}
}
}

void float_lcd(float f)
{
int i;
unsigned char j;
i=f;
int_lcd(i);
disp_lcd('.');
for(j=0;j<3;j++)
{
f=(f-i)*10;
i=f;
int_lcd(i);
}
}
///////////////////////bit banging and uart mode0 method to write//////////
main()
{
#if TEST==2
init_uart();
#endif
init_lcd();
disp_lcd('B');
cmd_lcd(0xc0);
str_lcd("serial lcd");
while(1);
}



74hc595.h
#ifndef _74hc595_h_
#define _74hc595_h_
sbit ds=p3^0;	//connect to serial input pin of 74hc595
sbit clk=p3^1;	//connect to clock input
sbit _mr=p3^2;	//connect to reset input
#define rst_74hc595_mr=0;_mr=1;	//to reset a device
//routine to convert 8bit parellel to 1bit serial msb first
void write_74hc595(unsigned char dat)
{
unsigned char i;
	for(i=0;i<=7;i++)
	{
		clk=0;
		ds=(dat&(0x80>>1))?1:0;
		clk=1;
	`}
//extra clock for moving from shift register to storage register
	clk=0;
	clk=1;
//rst_74hc595 //reset shift register(optional)
}
#endif



delay.h
#ifndef _delay_h_
#define _delay_h_
void delay_ms(unsigned int dly)
{
unsigned int i;
for(;dly>0;dly--)
{
for(i=122;i>0;i--);
}
}
main()
{
delay_ms(1);
while(1);
}

#endif

Read More
#include<reg51.h>
//unsigned int dly;
#define mydata P1
void delay(unsigned char n);
void main(void)
{
while(1)
{
mydata=0xff;
mydata=0x00;
delay(200);
}
}

void delay(unsigned char n)
{
unsigned char i;
for(i=n;i>0;i--)
while(1);
}
Read More

Basic LED program in 8051 micro controller

#include<reg51.h>
void delay(unsigned int n)
{
unsigned int i,j;
for(i=n;i>0;i--)
{
for(j=0;j<1275;j++);
}
}
main()
{
while(1)
{
P1=0x00;
delay(1000);
P1=0xff;

}
}
Read More

Embedded C Program for 4x4 matrix keypad

#include<reg51.h>
//#include<keypad.h>
sfr p1=0x80;
#ifdef _keypad_h_
#define _keypad_h_
sbit r0=p3^0;sbit c0=p3^4;
sbit r1=p3^1;sbit c1=p3^5;
sbit r2=p3^2;sbit c2=p3^6;
sbit r3=p3^3;sbit c3=p3^7;
code unsigned char keypad_lut[4][4]=
{
1,2,3,4,
5,6,7,8,
9,10,11,12,
13,143,15,16
};
bit colscan()
{
return(c0&c1&c2&c3);
}
unsigned char keyscan()
{
unsigned char row,col;
c0=c1=c2=c3=1;
r0=r1=r2=r3=0;
while(colscan());
r0=0;
r1=r2=r3=1;
if(!colscan())
{
row=0;goto colcheck;
}

r1=0;
r0=r2=r3=1;
if(!colscan())
{
row=1; goto colcheck;
}

r2=0;
r0=r2=r3=1;
if(!colscan())
{
row=2; goto colcheck;
}

r3=0;
r0=r1=r3=1;
if(!colscan())
{
row=3; goto colcheck;
}

colcheck:
if(c0==0)
col=0;
else if(c1==0)
col=1;
else if(c2==0)
col=2;
else if(c3==0)
col=3;
return keypad_lut[row][col];
}
#endif



main()
{
//while(1)
{
keyscan();
while(!colscan());
}
}
Read More

4Mux Segment LED

#include<reg51.h>
//#include"delay.h"
//#include"seg.h"

void delay_ms(unsigned int dly)
{
unsigned int i;
for(;dly>0;dly--)
{
for(i=122;i>0;i--);
}
}

#define segs_4_mux P0
sbit sel1=P2^0;
sbit sel2=P2^1;
sbit sel3=P2^2;
sbit sel4=P2^3;
unsigned char dp1=0x80,dp2=0x80,dp3=0x80,dp4=0x80;
code unsigned char seg_lut[]=
{
	0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10
};

void disp_4_mux_segs(unsigned int n)
{
segs_4_mux=seg_lut[n/1000]|dp1;
sel1=0;
//;delay_ms(10);
sel1=1;

segs_4_mux=seg_lut[(n/100)%10]|dp2;
sel2=0;
//;delay_ms(10);
sel2=1;

segs_4_mux=seg_lut[(n%100)/10]|dp3;
sel3=0;
//;delay_ms(10);
sel3=1;

segs_4_mux=seg_lut[n%10]|dp4;
sel4=0;
//;delay_ms(10);
sel4=1;
}
//#endif
void main(void)
{
while(1)
disp_4_mux_segs(1234);
}
Read More

Wednesday, 9 April 2014

1-wire protocol project to read temparature

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