Sie sind auf Seite 1von 38

1) Keboard interfacing 3x4

#include<reg51.h>

#include"lcddisplay.h"

sbit C4 = P1^0; // Connecting keypad to Port 1

sbit C3 = P1^1;

sbit C2 = P1^2;

sbit C1 = P1^3;

sbit R1 = P1^4;

sbit R2 = P1^5;

sbit R3 = P1^6;

void msdelay(unsigned int time) // Function for creating delay in milliseconds.

unsigned i,j ;

for(i=0;i<time;i++)

for(j=0;j<1275;j++);

void row_finder1() //Function for finding the row for column 1

R1=R2=R3=1;

C1=C2=C3=C4=0;

if(R1==0)

lcddata('*');
if(R2==0)

lcddata('0');

if(R3==0)

lcddata('#');

void row_finder2() //Function for finding the row for column 2

R1=R2=R3=1;

C1=C2=C3=C4=0;

if(R1==0)

lcddata('7');

if(R2==0)

lcddata('8');

if(R3==0)

lcddata('9');

void row_finder3() //Function for finding the row for column 3

R1=R2=R3=1;

C1=C2=C3=C4=0;

if(R1==0)

lcddata('4');

if(R2==0)

lcddata('5');

if(R3==0)
lcddata('6');

void row_finder4() //Function for finding the row for column 4

R1=R2=R3=1;

C1=C2=C3=C4=0;

if(R1==0)

lcddata('1');

if(R2==0)

lcddata('2');

if(R3==0)

lcddata('3');

void main()

lcd_init();

while(1)

msdelay(30);

C1=C2=C3=C4=1;

R1=R2=R3=0;

if(C1==0)

row_finder1();

else if(C2==0)

row_finder2();
else if(C3==0)

row_finder3();

else if(C4==0)

row_finder4();

2) Using Relay turn ON and turn OFF the 230v lamp.

#include<reg52.h>

sbit s1=P1^0;
sbit s2=P1^1;

sbit relay=P1^2;

unsigned int x,y;


void delay(unsigned int value);

void main()
{

relay=1;
delay(10);
relay=0;

while(1)
{
if(s1==0)
{
relay=1;
delay(100);

}
else if(s2==0)
{
relay=0;
delay(100);
}

}
}
void delay(unsigned int value)
{
unsigned int x,y;
for(x=0;x<value;x++)
for(y=0;y<1275;y++);
}

3) Bidirection control of DC motor

#include<reg52.h>

sbit m1=P1^0;
sbit m2=P1^1;

sbit s=P1^2;
sbit s1=P1^3;

unsigned int x,y;


void delay(unsigned int value);

void main()
{

m1=1;
m2=0;
delay(50);
m1=0;
m2=0;

while(1)
{
if(s==0)
{
m1=1;
m2=0;
delay(1000);
m1=0;
m2=0;
delay(300);
}
else if(s1==0)
{
m1=0;
m2=1;
delay(1000);
m1=0;
m2=0;
delay(300);
}

}
}
void delay(unsigned int value)
{
unsigned int x,y;
for(x=0;x<value;x++)
for(y=0;y<1275;y++);
}
4) Led turn on and trun off
#include<reg52.h>

sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit led5=P1^4;
sbit led6=P1^5;
sbit led7=P1^6;
sbit led8=P1^7;
sbit s=P3^0;

unsigned int x,y;


void delay(unsigned int value);

void main()
{

led1=0;
led2=0;
led3=0;
led4=0;
led5=0;
led6=0;
led7=0;
led8=0;

while(1)
{
if(s==0)
{
led1=1;
led2=1;
led3=1;
led4=1;
led5=1;
led6=1;
led7=1;
led8=1;
delay(100);
led1=0;
led2=0;
led3=0;
led4=0;
led5=0;
led6=0;
led7=0;
led8=0;
delay(300);
}

}
}
void delay(unsigned int value)
{
unsigned int x,y;
for(x=0;x<value;x++)
for(y=0;y<1275;y++);
}

5)RTC interface

#include<reg51.h>

#include<intrins.h>

#include"lcddisplay.h"

sbit SCL=P1^2;

sbit SDA=P1^1;

sbit enter = P3^0;

sbit dec = P3^1;

sbit inc = P3^2;

sbit as = P3^3;

sbit end = P0^7;

sbit mot=P1^0;

void start(void);

void write(unsigned char,unsigned char);


unsigned char read(unsigned char);

void ptos(unsigned char );

void stop(void);

void settime(void);

unsigned char COUNT,dat,add,hr,min,x,binbyte,B1,B2,B3,entt=0,astt=0;

unsigned int i;

void delay1(unsigned int itime);

unsigned char time[7],temptime[7],alarmcheck,day=1,alarmcount=0,type,ii=0;

bit pm=0,pm1,dayselect;

void daydisplay(unsigned char);

void main()

unsigned char z,x,addrs;

enter=inc=dec=end=1;

mot=0;

dayselect=0;
lcd_init();

delay(100);

lcd_init();

delay(100);

msgdisplay(" welcome");

delay(500);

lcdcmd(0x01);

delay(10);

msgdisplay(" REAL TIME ");

lcdcmd(0xc0);

msgdisplay("CLOCK SYSTEM");

delay(1000);

z=read(0);

if(z==0x80)

start: lcdcmd(0x01);

type=6;

msgdisplay(" SET THE TIME ");

settime();

write(0,0);

delay(10);

write(1,temptime[1]);

delay(10);
write(2,temptime[0]);

delay(10);

for(add=4;add<7;add++)

write(add,temptime[add-1]);

delay(10);

dayselect=1;

delay(10);

delay(10);

lcdcmd(0x01);

timedisp:

lcdcmd(0x01);

msgdisplay("Dt:");

while(1)

lcdcmd(0x04);

for(add=0;add<7;add++)
{

time[add]=read(add);

z=time[add];

if(add==0)

lcdcmd(0x04);

lcdcmd(0xC9);

if(add==4)

lcdcmd(0x06);

lcdcmd(0x84);

if(add==2)

z=time[2]&(0x60);

if(z==0x40)

pm1=0;

else

pm1=1;

z=time[2]&0x1f;

B1=z&0x0f;

B2=(z&0xf0)>>4;
if(add<3)

lcddata(B1+48);

lcddata(B2+48);

if(add>3)

lcddata(B2+48);

lcddata(B1+48);

if(add<2)

lcddata(':');

if((add>3)&&(add<6))

lcddata('/');

delay(10);

lcdcmd(0xca);

if(pm1)

msgdisplay("pm");

else

msgdisplay("am");
if(dayselect==1)

lcdcmd(0x1);

msgdisplay("SELECT DAY: SUN");

while(enter==1)

lcdcmd(0x8d);

lcdcmd(0xe);

if(inc==0)

while(inc==0);

if(day<8)

day=day+1;

daydisplay(day);

if(dec==0)

while(dec==0);

if(day>1)

day=day-1;

daydisplay(day);

write(3,day);
while(enter==0);

dayselect=0;

lcdcmd(0x0c);

goto timedisp;

lcdcmd(0x8d);

daydisplay(time[3]);

addrs=0x20;

alarmcount=0;

for(x=0;x<15;x++)

z=read(addrs);

delay(10);

addrs=addrs+1;

if(time[2]==z)

z=read(addrs);

if((time[1]==z)&&((time[0]==0x00) || (time[0]==0x01)))

alarmcount=x+1;;
goto xyz;

addrs=addrs+1;

xyz:

if(as==0)

while(as==0);

addrs=0x20;

lcdcmd(0x01);

type=3; // to enter upto time only (no date)

for(x=0;x<2;x++)

if(x==0)

msgdisplay("ENTR ON TIME:");

if(x==1)

msgdisplay("ENTR OFF TIME:");

lcddata(x/10+48);

lcddata(x%10+48);
settime();

write(addrs,temptime[0]);

delay(10);

addrs=addrs+1;

write(addrs,temptime[1]);

addrs=addrs+1;

delay(10);

lcdcmd(0xca);

lcddata('-');

msgdisplay(">");

while(enter==1);

if(dec==0)

for(ii=x+1;ii<15;ii++)

write(addrs,0);

delay(10);

addrs=addrs+1;

write(addrs,0);

addrs=addrs+1;

delay(10);

x=15;

}
if(x==15)

lcdcmd(0xce);

lcddata(' ');

lcddata(' ');

goto timedisp;

if(enter==0)

while(enter==0);

goto start;

if(alarmcount==2)

mot=0;

}
if(alarmcount==1)

mot=1;

else;

void settime(void)

unsigned char keycount=0,h,g,d1,d2,cmd,uplimit;

lcdcmd(0xC0);

if(type==6)

msgdisplay("hh:mmAM dd/mm/yr");

else

msgdisplay("hh:mmAM ");

lcdcmd(0x0e); //cursor blinking


while(keycount<type) // to select alarm or current time

h=0;

if(keycount==0)

cmd=0xc0;

uplimit=12;

else

if(keycount==1)

cmd=0xc3;

uplimit=59;

else

if(keycount==2)

cmd=0xc5;

uplimit=1;

else

if(keycount==3)

cmd=0xc8;

uplimit=31;
}

else

if(keycount==4)

cmd=0xcb;

uplimit=12;

else

if(keycount==5)

cmd=0xce;

uplimit=99;

else;

entt=0;

astt=0;

while(enter==1 && as==1)

lcdcmd(cmd);

while((inc==1)&&(dec==1)&&(enter==1)&&(as==1)); //wait till any switch is pressed

delay(60);

if(inc==0)

delay(30);
while(inc==0);

if(h==uplimit)

h=0;

h=h+1;

if(dec==0)

delay(30);

while(dec==0);

if(h)

h=h-1;

else

h=uplimit;

if(keycount==2)

if(h)

msgdisplay("pm");
pm=1;

else

msgdisplay("am");

pm=0;

else

g=h;

d1=g/10;

d2=g%10;

lcddata(d1+48);

lcddata(d2+48);

g=(d1<<4)|(d2%10);

temptime[keycount]=g;

if(enter==0)
{

entt=1;

if(as==0)

astt=1;

if(enter==0)

delay(50);

while(enter==0);

if(as==0)

delay(50);

while(as==0);

if(keycount==2)
lcddata(' ');

if(entt==1)

keycount=keycount+1;

if((astt==1) && (keycount >0))

keycount=keycount-1;

lcdcmd(0xcf);

lcddata(keycount+48);

while(enter==0);

while(as==0);

if(pm)

temptime[0]= temptime[0]|0x60;

else

temptime[0]=temptime[0]|0x40;

lcdcmd(0x01); // clear the lcd

lcdcmd(0x0c); //curser blink off;

}
// ####################################### DATA READING FUNCTION
###################################

void write(unsigned char add,unsigned char dat)

start();

ptos(0Xd0); //device addr in write mode//

ptos(add); //byte addr//

ptos(dat); //data//

stop();

//%%%%%%%%%%%%%%%%%%%% READING FUNCTION


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

unsigned char read(unsigned char add)

unsigned char v,i;

start();

ptos(0Xd0); //device addr in write mode//

ptos(add); //byte addr//

_nop_();

start();

ptos(0Xd1); //device addr in read mode//


v=0;

SDA=1;

for(i=0;i<=7;i++)

SCL=0;

_nop_();

_nop_();

SCL=1;

v=v|SDA;

if(i<=6)

v=v<<1;

SCL=0;

delay1(100);

stop();

return(v);

void daydisplay(unsigned char day)

if(day==1)

msgdisplay("SUN");
else

if(day==2)

msgdisplay("MON");

else

if(day==3)

msgdisplay("TUE");

else

if(day==4)

msgdisplay("WED");

else

if(day==5)

msgdisplay("THU");

else

if(day==6)

msgdisplay("FRI");

else

if(day==7)

msgdisplay("SAT");

else;

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ START FUNCTION


$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
void start(void)

SCL=1;

SDA=1;

_nop_();

_nop_();

SDA=0;

SCL=0;

//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa DATA SENDING TO EEPRAM IN READ MODE


AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

void ptos(unsigned char a)

unsigned char i,c;

for(i=0;i<=7;i++)

c=a&128;

if(c==0)

SDA=0;
else

SDA=1;

SCL=1;

_nop_();

_nop_();

SCL=0;

a=a<<1;

SDA=1;

_nop_();

_nop_();

SCL=1;

_nop_();

_nop_();

SCL=0;

//SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS STOP FUNCTION


SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
void stop(void)

SDA=0;

SCL=1;

_nop_();

_nop_();

SDA=1;

SCL=0;

// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV DELAY FUNCTION


VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

//*********************DEFINEATION OF LCD COMMAND IN FOUR BIT MODE


*************************

void delay1(unsigned int itime)

unsigned int i,j;

for(i=0;i<itime;i++)

for(j=0;j<3;j++);

5) ADC based fan control


#include<reg52.h>
#include<lcddisplay.h>
#include<adc.h>

sbit m22=P1^4;
sbit m21=P1^5;
sbit m12=P1^6;
sbit m11=P1^7;

void main()
{
unsigned int x,y;
lcd_init();

m11=m12=m21=m22=0;

lcdcmd(0x01);
msgdisplay(" WELCOME");
delay(50);
lcdcmd(0x01);
msgdisplay("MEMS CONTROLLED");
lcdcmd(0xc0);
msgdisplay(" WHEEL CHAIR");
delay(200);

lcdcmd(0x01);
msgdisplay("X: Y:");

x=(adc_convert(0));
y=(adc_convert(1));

while(1)
{

x=(adc_convert(0));

lcdcmd(0x82);
lcddata((x/1000)%10+48);
lcddata((x/100)%10+48);
lcddata((x/10)%10+48);
lcddata((x%10)+48);

y=(adc_convert(1));
lcdcmd(0x8a);
lcddata((y/1000)%10+48);
lcddata((y/100)%10+48);
lcddata((y/10)%10+48);
lcddata((y%10)+48);

if(x<1200)
{
m11=1;
m12=0;
m21=1;
m22=0;
}
else if(x>1500)
{
m11=0;
m12=1;
m21=0;
m22=1;
}
else if(y<1200)
{
m11=1;
m12=0;
m21=0;
m22=0;
}
else if(y>1500)
{
m11=0;
m12=0;
m21=1;
m22=0;
}
else
{
m11=0;
m12=0;
m21=0;
m22=0;
}

HEADER FILE OF ADC


sbit CLK = P1^3;
sbit DOUT= P1^2;
sbit DIN = P1^1;
sbit CS = P1^0;

float adc_convert (unsigned char v);


unsigned char byte_write_read(unsigned char);
void select_channel(void);
unsigned char channel,A,C;
int result,result1,result2;
void integer_lcd(int);
void delay_ms(int);

float adc_convert (unsigned char v)


{
unsigned char byte0,byte1,byte2;
int i;
int val;
channel=v;
CLK=0;
CS=1;
CS=0;
select_channel();
byte0=byte_write_read(A);
byte1=byte_write_read(C);
byte2=byte_write_read(0x00);
CS=1;
i=(byte1&0x0f);
i=((i<<8)| byte2);
val=((i));
return val;
}

///////////////////////////////////////////////////////////////////////////////////////////
unsigned char byte_write_read (unsigned char a)
{
unsigned char c=0,mask=0x80;
do
{
CLK=1;
if(a&mask)
DIN=1;
else
DIN=0;
CLK=0;
if(DOUT==1)
c|=mask;
mask>>=1;
}
while(mask>0);
return c;
}
////////////////////////////////////////////////////////////////////////

void delay_ms(int cnt)


{
int i;
while(cnt--)
for(i=0;i<200;i++);
}

void select_channel (void)


{
unsigned char x;
x=(channel*4)+0x60;
A = x>>4;
C = x<<4;

}
HEADER FILE OF LCD DISPLAY

#define lcd_data P2
#define lcd_cont() ((lcd_en=1),(delay(3)),(lcd_en=0))

sbit lcd_rs = P2^0; // Here we are using LCD in four bit mode that's why LCD's
Data pins and control
sbit lcd_en = P2^1;

void lcd_init(void);
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void msgdisplay(unsigned char b[]);
void delay(unsigned int value);

void lcd_init(void)
{
lcdcmd(0x02);
lcdcmd(0x02);
lcdcmd(0x02);
lcdcmd(0x28);
lcdcmd(0x28);
lcdcmd(0x28);
lcdcmd(0x0c);
lcdcmd(0x06);
lcdcmd(0x01);

void lcdcmd(unsigned char value) // LCD COMMAND


{

lcd_data=value&(0xf0);
lcd_rs=0;
lcd_cont();
lcd_data=((value<<4)&(0xf0));
lcd_rs=0;
lcd_cont();
}

void lcddata(unsigned char value)

lcd_data=value&(0xf0);
lcd_rs=1;
lcd_cont();
delay(3);
lcd_data=((value<<4)&(0xf0));
lcd_rs=1;
lcd_cont();
delay(3);
}

void msgdisplay(unsigned char b[])


{
unsigned char s,count=0;
for(s=0;b[s]!='\0';s++)
{
lcddata(b[s]);
}
}

void delay(unsigned int value)

{
unsigned int x,y;
for(x=0;x<value;x++)
for(y=0;y<1275;y++);
}
7) Dot matrix led interface
#include<reg52.h>

unsigned int Col[5]= {0xfe,0xfd,0xfb,0xf7,0xef};


unsigned int Row[5]={0xfe,0x11,0x11,0x11,0xfe};
void delay (unsigned int x)
{
unsigned int k,m;
for (k = 0;k<x;k++)
{
for(m=0;m<500;m++);
}

}
void main()

unsigned int d;
while(1)
{

for(d=0;d<5;d++)

P1=Col[d];
P3=Row[d];
delay(1);
}

}
}