Microcontroller › 8051 › 8051 microcontroller programming › codings for the above
codings for the above circuit
#include <reg51.H> //……………….headder file
#include <stdio.h> //……………….headder file
#include <string.h> //……………….headder file
/*………………PIN CONNECTIONS…………………………………………………………….*/
#define LCD_PORT P2 //……………….lcd data port to be connected
#define port0 P0 //……………….Port for keypad connection
#define REL_PORT P1 //……………….relay connection port
sbit LED2=P3^3; //……………….LCD PIN Connection
sbit EN=P3^7; //……………….enable pin for LCD
sbit RW=P3^6; //……………….write pin for LCD
sbit RS=P3^5; //……………….register select pin of LCD
/*………………..Declarations ……………………………………………*/
//………………..DELAYS………………………//
void Delay_in_ms(unsigned char); //……………..milli second delay is to be specifide
void Delay_in_10ms(unsigned char); //……………..10 * number of milli sec
void Delay_in_1s(unsigned char); //……………..number of sec
//………………….LCD………………………//
void lcd_init (void); //………routine to initialise LCD
void write_in (unsigned char ); //………writes data in to LCD as instruction
void write (unsigned char ); //………writes one byte into LCD
void printLcd (unsigned char *); //………prints a line on LCD
void printLcd2 (unsigned char *); //………prints a line on LCD
void clear_display (void); //………clears LCD screen
void set_add (unsigned char ); //………sets the display position
void write_data (unsigned char ); //………writes one byte into LCD as data
//………………..relay………………………//
void RELAY_GO(unsigned char ,unsigned char ); //…..selection of relay through keybord
void relay_set(unsigned char); //…..selected relay ON or OFF
#define RON 1 //……………………………relay pin on state
#define ROFF 0 //……………………………relay pin off state
sbit RELAY1 = P1^0; //……………………………relay one
sbit RELAY2 = P1^1; //……………………………relay two
sbit RELAY3 = P1^2; //……………………………relay three
sbit RELAY4 = P1^3; //……………………………relay four
sbit RELAY5 = P1^4; //……………………………relay five
sbit RELAY6 = P1^5; //……………………………relay six
sbit RELAY7 = P1^6; //……………………………relay seven
sbit RELAY8 = P1^7; //……………………………relay eight
/* ………………password functions…………………*/
char key0 = ‘*’; //……………………..MASK key press
unsigned char key1; //……………………..1 key press
unsigned char key2; //……………………..2 key press
unsigned char key3; //……………………..3 key press
unsigned char key4; //……………………..4 key press
unsigned char key5; //……………………..5 key press
unsigned char key6; //……………………..6 key press
void relese_go(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char);
void lock_go(unsigned char , unsigned char , unsigned char , unsigned char , unsigned char , unsigned char );
/*…………………….4X4 keybord……………………*/
// key select functions
void select(void); //……………………..selection of relay
void locker(); //……………………..lockre rootine
void menu_1(void); //……………………..function choose
void menu_2(void); //……………………..function choose
void stop(void); //……………………..system halt
void sleep(void); //……………………..system standby
unsigned char keyS; //……………………..selected key
unsigned char keypad(void); //……………………..key input
void call_for_column(void); //……………………..key routien work
unsigned char getport0,col,row; //…………………..key routien work
unsigned char keypad_value; //……………………..value of key
unsigned char key[4][4] = { ‘1’,’2′,’3′,’R’,
‘4’,’5′,’6′,’P’,
‘7’,’8′,’9′,’L’,
‘*’,’0′,’#’,’S’};
//………………progam starts………………………………….//
void main()
{
REL_PORT=0x00; //………initialising relay port
lcd_init(); //………initialising lcd port
lock= ROFF;
clear_display(); //………clear th display
printLcd(” Dexter8051 “); //……..FOR PRINTING IN FIRST LINE
printLcd2(” WELCOMES YOU “); //……..FOR PRINTING IN SECAND LINe
Delay_in_1s(2);*/
locker();
}
}
//……………..menu 1 select………………//
void menu_1(void )
{
clear_display();
printLcd (“RELAY :press R “);
keyS= keypad();
if(keyS==’R’)
{
select();
}
else
{
clear_display();
printLcd(“WRONG INPUT”);
Delay_in_1s(1);
menu_1();
}
}
//…………on off select function………….//
void select(void)
{
clear_display();
printLcd(“Enter relay no:”);
key1=keypad();
write_data(key1);
Delay_in_10ms(2);
set_add(0xc0);
printLcd(“PRESS # ON * OFF”);
Delay_in_1s(1);
key2=keypad();
Delay_in_10ms(2);
clear_display();
RELAY_GO(key1,key2);
Delay_in_1s(1);
menu_1();
}
//…………………..relay to be on off select rooine…………//
void RELAY_GO(unsigned char a,unsigned char b)
{
{
if(a==’1’& b==’#’)
{
relay_set(1);// RELAY 1 on //calling relay_set function
}
else if(a==’1’& b==’*’)
{
relay_set(2);// RELAY 1 off //calling relay_set function
}
else if(a==’2’& b==’#’)
{
relay_set(3);// RELAY 2 on //calling relay_set function
}
else if(a==’2’& b==’*’)
{
relay_set(4);// RELAY 2 off //calling relay_set function
}
else if(a==’3’& b==’#’)
{
relay_set(5);// RELAY 3 on //calling relay_set function
}
else if(a==’3’& b==’*’)
{
relay_set(6);// RELAY 3 off //calling relay_set function
}
else if(a==’4’& b==’#’)
{
relay_set(7);// RELAY 4 on //calling relay_set function
}
else if(a==’4’& b==’*’)
{
relay_set(;// RELAY 4 off //calling relay_set function
}
else if(a==’5’& b==’#’)
{
relay_set(9);// RELAY 5 on //calling relay_set function
}
else if(a==’5’&b==’*’)
{
relay_set(10);// RELAY 5 off //calling relay_set function
}
else if(a==’6’&b==’#’)
{
relay_set(11);// RELAY 6 on //calling relay_set function
}
else if(a==’6’& b==’*’)
{
relay_set(12);// RELAY 6 off //calling relay_set function
}
else if(a==’7’& b==’#’)
{
relay_set(13);// RELAY 7 on //calling relay_set function
}
else if(a==’7’& b==’*’)
{
relay_set(14);// RELAY 7 off //calling relay_set function
}
else if(a==’8’& b==’#’)
{
relay_set(15);// RELAY 8 on //calling relay_set function
}
else if(a==’8′ & b==’*’)
{
relay_set(16);// RELAY 8 off //calling relay_set function
}
else
{
clear_display();
printLcd(“WRONG INPUT KEY”);
}
}
}
//…………………LCD Routines…………………..//
//routine to initialise the lcd
void lcd_init(void)
{
write_in(0x38); //to make lcd
Delay_in_ms(1);
write_in(0x0f); //to make lcd to select 5×7 matrix display
Delay_in_ms(1);
write_in(0x01); //to make lcd to clear the display
Delay_in_ms(1);
write_in(0x80); //to make lcd cursor to bigining of first line
Delay_in_ms(1);
}
//routine to write an instruction to LCD
void write_in(unsigned char ch)
{
RS=0; //rs =0 for instr
RW=0;
write(ch); //routine to transfer 8 bits to LCD
}
//routine to transfer 8 bits to LCD module
void write(unsigned char ch)
{
LCD_PORT = ch; //preserves the old value of port
EN = 1; //enable pin high
Delay_in_ms(1);
EN = 0; //enable = 0
}
//clears the entire display area of LCD
void clear_display(void)
{
write_in(0x01); //clears the lcd display
write_in(0x80); //write code to blink at first line first coloum
}
//printing rootin
//routine to display the value in the accumulator in LCD
//routine to display a string on first line
//this routine will display a string stored at address pointed by dptr
void printLcd(unsigned char *str)
{
unsigned char len,i;
len = strlen(str);
set_add(0x80); //sets display position in first line
for(i=0;i<len;i++)
{
write_data(*str); //displays the character on LCD
str++;
}
}
void printLcd2(unsigned char *str)
{
unsigned char len,i;
len = strlen(str);
set_add(0xc0); //sets display position in second line
for(i=0;i<len;i++)
{
write_data(*str); //displays the character on LCD
str++;
}
}
//routine to set the display position
void set_add(unsigned char ch)
{
write_in(ch); //writes it as instruction
}
//routine to write a data to LCD
void write_data(unsigned char ch)
{
RS = 1;
RW=0;
write(ch); //……….transfers 8 bits to lcd module
}
//ms delay in milli secand
void Delay_in_1s(unsigned char ms)//……………..number of secands
{
int t;
t=ms;
{
unsigned int i;
for(i=0;i<t;i++)
{
Delay_in_10ms(10);
}
}
}
void Delay_in_10ms(unsigned char ms)//……………..milli sec * 10
{
int t;
t=ms;
{
unsigned int i;
for(i=0;i<t;i++)
{
Delay_in_ms(100);
}
}
}
void Delay_in_ms(unsigned char ms)//……………….milli sec
{
int t;
t= ms;
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<120;j++);
}
}
//…………..key board processes……………..//
unsigned char keypad()
{
do
{
do
{
port0 = 0x0f;
getport0 = port0;
getport0 = getport0 & 0x0f;
}while(getport0 != 0x0f);
getport0 = port0;
getport0 = getport0 & 0x0f;
}while(getport0 !=0x0f);
while(1)
{
port0 = 0xef;
getport0 = port0;
getport0 = getport0 & 0x0f;
if(getport0 != 0x0f)
{
row = 0;
call_for_column();
break;
}
port0 = 0xdf;
getport0 = port0;
getport0 = getport0 & 0x0f;
if(getport0 != 0x0f)
{
row = 1;
call_for_column();
break;
}
port0 = 0xbf;
getport0 = port0;
getport0 = getport0 & 0x0f;
if(getport0 != 0x0f)
{
row = 2;
call_for_column();
break;
}
port0 = 0x7f;
getport0 = port0;
getport0 = getport0 & 0x0f;
if(getport0 != 0x0f)
{
row = 3;
call_for_column();
break;
}
}
keypad_value = key[row][col];
return keypad_value;
}
void call_for_column()
{
while(1)
{
if(getport0 == 0x07)
{
col = 0;
Delay_in_10ms(3);
break;
}
else if(getport0 == 0x0b)
{
col = 1;
Delay_in_10ms(3);
break;
}
else if(getport0 == 0x0d)
{
col = 2;
Delay_in_10ms(3);
break;
}
else if(getport0 == 0x0e)
{
col = 3;
Delay_in_10ms(3);
break;
}
}
}
//……….relay switching processes……………..//
void relay_set(unsigned char relay)
{
int x;
x= relay;
switch (x)
{
case 1: //………………..RELAY 1 OFF
RELAY1= RON;
printLcd(“RELAY 1 ON “);
break;
case 2: //………………..RELAY 1 0N
RELAY1= ROFF;
printLcd(“RELAY 1 OFF”);
break;
case 3: //………………..RELAY 2 OFF
RELAY2= RON;
printLcd(“RELAY 2 ON “);
break;
case 4: //………………..RELAY 2 0N
RELAY2= ROFF;
printLcd(“RELAY 2 OFF”);
break;
case 5: //…………………RELAY 3 OFF
RELAY3= RON;
printLcd(“RELAY 3 ON “);
break;
case 6: //………………..RELAY 3 0N
RELAY3= ROFF;
printLcd(“RELAY 3 OFF”);
break;
case 7: //………………..RELAY 4 OFF
RELAY4= RON;
printLcd(“RELAY 4 ON “);
break;
case 8: //………………..RELAY 4 0N
RELAY4= ROFF;
printLcd(“RELAY 4 OFF”);
break;
case 9: //………………..RELAY 5 OFF
RELAY5= RON;
printLcd(“RELAY 5 ON “);
break;
case 10: //……………….RELAY 5 0N
RELAY5= ROFF;
printLcd(“RELAY 5 OFF”);
break;
case 11: //……………….RELAY 6 OFF
RELAY6= RON;
printLcd(“RELAY 6 ON “);
break;
case 12: //………………..RELAY 6 0N
RELAY6= ROFF;
printLcd(“RELAY 6 OFF”);
break;
case 13: //………………..RELAY 7 OFF
RELAY7= RON;
printLcd(“RELAY 7 ON “);
break;
case 14: //………………..RELAY 7 0N
RELAY7= ROFF;
printLcd(“RELAY 7 OFF”);
break;
case 15: //………………..RELAY 8 0FF
RELAY8= RON;
printLcd(“RELAY 8 ON “);
break;
case 16: //………………..RELAY 8 0N
RELAY8= ROFF;
printLcd(“RELAY 8 OFF”);
break;
}
}
//…………..locker password code…………….//
void locker(void)
{
clear_display();
printLcd(“ENTER PASSWORD”);
set_add(0xc0);
key1=keypad();
write_data(key0);
key2=keypad();
write_data(key0);
key3=keypad();
write_data(key0);
key4=keypad();
write_data(key0);
key5=keypad();
write_data(key0);
key6=keypad();
write_data(key0);
lock_go(key1,key2,key3,key4,key5,key6);
}
//……………………..lock code ………………………//
void lock_go(unsigned char a, unsigned char b, unsigned char c, unsigned char d, unsigned char e, unsigned char f)
{
if(a==’1′ & b==’2′ & c==’3′ & d==’4′ & e==’5′ & f==’6′)
{
clear_display();
printLcd(“CORRECT PASSWORD”);
printLcd2(“LOCK IS OPEN”);
Delay_in_1s(1);
menu_1();
}
else
{
locker();
}
}