8051 code of RFID Based Ticketing System

RFID System MicroController (keil) code 8051
*/ 

#include <reg51.h>
#include <string.h>
#define dataport P1 //Port for LCD display
#define keyp P3  // Port for keypad
#define port P2 //control signlas for LCD and Buzzer
#define port0 P0

sbit rs = port^1;//register select
sbit rw = port^0;//read/write
sbit en = port^2;//enable
sbit buzzer = port0^0; //buzzer port
sbit red = port0^2;  //port for Red LED
sbit green=port0^1;  //port for Green LED
unsigned char card[15];
unsigned char c[15];
char READ_SWITCHES(void);
char get_key(void);

sbit mtrop=P0^4;  //port for opening gate (moves motor anticlockwise)
sbit mtrcl=P0^3;   //port for closing gate(moves motor clockwise)
sbit ir=P0^6;         //port connected to IR circuit
sbit RowA = keyp^0;     //RowA
sbit RowB = keyp^1;     //RowB
sbit RowC = keyp^2;     //RowC
sbit RowD = keyp^3;     //RowD

sbit C1   = keyp^4;     //Column1
sbit C2   = keyp^5;     //Column2
sbit C3   = keyp^6;     //Column3
sbit C4   = keyp^7;     //Column 4


void delay(unsigned int time){
   unsigned int i;
   for(i=0;i<time;i++);
 }
void send(unsigned char d)
{
while(!TI);
TI=0;
SBUF=d;
}
unsigned char read()
{
while(!RI);
RI=0;
return SBUF;
}
void lcd_cmd(unsigned char item) //Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
en=1;
delay(1);
en=0;
return;
}
void lcd_data(unsigned char item) //Funtion to send data on LCD
{
//send(item);
dataport = item;
rs= 1;
rw=0;
en=1;
delay(1);
en=0;
return;
}
void lcd_data_string(unsigned char *str) // Function to send string on LCD
{
int i=0;
while(str[i]!='\0')
{
  lcd_data(str[i]);
  i++;
  delay(20000);
}
return;
}

void lcd(unsigned char str[15],int r,int p,int c)  // Funtion to Initialize,write,clear LCD
{
  //row1 0x80
  //row2 0xc0
lcd_cmd(0x38);//8bit data bus
lcd_cmd(0x0f);//turn display on,display cursor, blink cursor
//delay(sec);

//---lcd_cmd(0x01);//clear
if(c==1)
lcd_cmd(0x01);
//delay(sec);
if(r==1)
lcd_cmd(0x80+p); //row1 pos
else if(r==2)
lcd_cmd(0xc0+p); //row2 pos

//delay(sec);
lcd_data_string(str);
}
char get_key(void)           //get key from user
{
char key = 'n';              //assume no key pressed

while(key=='n')              //wait untill a key is pressed
key = READ_SWITCHES();   //scan the keys again and again
/* buzzer=1;
delay(20);
buzzer=0;  */
return key;                  //when key pressed then return its value
}

void serial_init()
{
// P0=0x00;                      // P0 and P2 as output ports
    P2=0x00;
TMOD = 0x20;
SCON = 0x50;
    TH1  = 0xFD;
    TL1  = 0xFD;
    TR1  = 1;
}
char READ_SWITCHES(void)
{
RowA = 0; RowB = 1; RowC = 1; RowD = 1;  //Test Row A

if (C1 == 0) { delay(10000); while (C1==0); return '*'; }
if (C2 == 0) { delay(10000); while (C2==0); return '*'; }
if (C3 == 0) { delay(10000); while (C3==0); return '*'; }
if (C4 == 0) { delay(10000); while (C4==0); return '*'; }

RowA = 1; RowB = 0; RowC = 1; RowD = 1;  //Test Row B

if (C1 == 0) { delay(10000); while (C1==0); return '*'; }
if (C2 == 0) { delay(10000); while (C2==0); return '*'; }
if (C3 == 0) { delay(10000); while (C3==0); return '*'; }
if (C4 == 0) { delay(10000); while (C4==0); return '*'; }

RowA = 1; RowB = 1; RowC = 0; RowD = 1;  //Test Row C

if (C1 == 0) { delay(10000); while (C1==0); return '*'; }
if (C2 == 0) { delay(10000); while (C2==0); return '6'; }
if (C3 == 0) { delay(10000); while (C3==0); return '5'; }
if (C4 == 0) { delay(10000); while (C4==0); return '4'; }

RowA = 1; RowB = 1; RowC = 1; RowD = 0;  //Test Row D

if (C1 == 0) { delay(10000); while (C1==0); return '*'; }
if (C2 == 0) { delay(10000); while (C2==0); return '3'; }
if (C3 == 0) { delay(10000); while (C3==0); return '2'; }
if (C4 == 0) { delay(10000); while (C4==0); return '1'; }

return 'n';            // Means no key has been pressed
}
void display(char a,int r,int p) //Display functon for LCD
{
switch(a)
{
  case '1':lcd("1",r,p,0);
    break;
  case '2':lcd("2",r,p,0);
    break;
  case '3':lcd("3",r,p,0);
    break;
  case '4':lcd("4",r,p,0);
    break;
  case '5':lcd("5",r,p,0);
    break;
  case '6':lcd("6",r,p,0);
    break;
  case '7':lcd("7",r,p,0);
    break;
  case '8':lcd("8",r,p,0);
    break;
  case '9':lcd("9",r,p,0);
    break;
  case '0':lcd("0",r,p,0);
    break;

}
}
main()
{
char key;
unsigned char i;
ir=0;  // Initialize IR circuit to 0

   serial_init();
   TI=1;
RI=1;
while(1)
{
 lcd(" Welcome to",1,0,1);
 lcd("Ticketing System",2,0,0);

    lcd(" Name:",1,0,1);
for(i=0;i<12;i++) //reading name
   {
    while(RI==0);
    card[i]=SBUF;
    RI=0;
   }

if(card[1]=='#')   //if invalid user
{
red=0;   //redLED ON
    buzzer=0;   //Buzzer ON
    delay(40000);
    red=1;   //redLED OFF
    buzzer=1;   //Buzzer OFF
lcd(" Invalid User",1,0,1);
continue;
}
lcd(card,1,6,0); //if valid user
lcd("Dest:",2,0,0);
    key = get_key(); 
    display(key,2,7);
buzzer=0;
delay(2000);
buzzer=1;
        send(key);


   
for(i=0;i<4;i++)  //response to enough or low balance
   {
    while(RI==0);
    c[i]=SBUF;
    RI=0;
    }

if(c[1]=='@')   //if low balance
{
 
   red=0;    //glow red LED
   buzzer=0;   //start buzzer
   delay(4000);
   red=1;    //stopGlowing
   buzzer=1;   //stop buzzer
   lcd("Low Balance",2,0,0);
}
else    //enough balance then display balance
{
  green=0;   //GreenLED ON
   delay(4000);
   delay(4000);
   delay(4000);
  green=1;   //Green LED OFF
  mtrop = 0;   //open gate by moving motor anticlockwise
  delay(4000);
delay(3200);
     mtrop = 1;  //stop moving
lcd("Bal: ",2,0,0);
   lcd(c,2,5,0);
  while(ir==0);  //Wait till IR Circuit Sends 1
  mtrcl=0;  //close gate by moving motor clockwise
 delay(4000);
  mtrcl=1;  //stop moving
  ir=0;  //Initialize IR back to 0
 
}

         }   

}


THANK YOU FOR VISITING..!!
PLEASE COMMENT

No comments:

Post a Comment