Microcontroller › 8051 › CODE FOR CAR PARKING SYSTEM › 6.1 MAIN
6.1 MAIN PROGRAM
#include<reg51.h>
#include “liftcontrol.h”
#include “liftapp.h”
#include “lcd.h”
#include “delay.h”
#include “visitor.h”
#include “display.h”
#include “rtc.h”
sbit sw0 =P0^0;
sbit sw1 =P0^1;
sbit sw2 =P0^2;
sbit sw3 =P0^3;
int main()
{
unsigned char time[]=” : : “; //buff for storing time
unsigned char date[]=” / / “; //buff for storing date
unsigned char time_date[]={0x30,0x15,0x03,0x08,0x06,0x10};//(min,hr,day,date,month,yr)
unsigned char lftup,lftdown;
unsigned char engn,exgn,enfrst,exfrst;
engn =1;
exgn =1;
enfrst =1;
exfrst =1;
lftup =1;
lftdown =1;
init_rtc(&time_date);
while(1)
{
IE =0X85;
IT0 =1;
IT1 =1;
clear();
Read_RTC_Time(&time);
clear();
lcd_pnt(time);
MSdelay(1000);
Read_RTC_Date(&date);
clear();
lcd_pnt(date);
MSdelay(1000);
clear();
visitordisplay();
MSdelay(1000);
clear();
lcd_pnt(“PRESS SWITCH “);
MSdelay(1000);
clear();
engn =sw0;
exgn =sw1;
enfrst =sw2;
exfrst =sw3;
displayled(engn,exgn,enfrst,exfrst);
clear();
lcd_pnt(“USE LIFT”);
MSdelay(1000);
lftup =sw0;
lftdown =sw1;
liftcontrol(lftup,lftdown);
}
return 0;
}
6.2 MODULE FOR THE OPERATION OF RTC
/*rtc.c*/
#include<reg51.h>
#include<stdio.h>
#include <stdlib.h>
#include”rtc.h”
#include”i2c.h”
unsigned char RTC_ARR[7];
void init_rtc(unsigned char *val)
{
ReadRTC(&RTC_ARR[0]);
RTC_ARR[0] = RTC_ARR[0] & 0x7F; // enable oscillator (bit 7=0)
RTC_ARR[1] = val[0]; // minute = 59
RTC_ARR[2] = val[1]; // hour = 05 ,24-hour mode(bit 6=0)
RTC_ARR[3] = val[2]; // Day = 1 or sunday
RTC_ARR[4] = val[3]; // Date = 30
RTC_ARR[5] = val[4]; // month = August
RTC_ARR[6] = val[5]; // year = 05 or 2005
WriteRTC(&RTC_ARR[0]); // Set RTC
}
void Read_RTC_Time(unsigned char *time)
{
ReadRTC(&RTC_ARR[0]);
time[0]=bcd_acii1(RTC_ARR[2]); //geting MSB and LSB and storing it in
time[1]=bcd_acii2(RTC_ARR[2]); // date buffer
time[3]=bcd_acii1(RTC_ARR[1]);
time[4]=bcd_acii2(RTC_ARR[1]);
time[6]=bcd_acii1(RTC_ARR[0]);
time[7]=bcd_acii2(RTC_ARR[0]);
}
void Read_RTC_Date(unsigned char *date)
{
date[0]=bcd_acii1(RTC_ARR[4]); //geting MSB and LSB and storing it in
date[1]=bcd_acii2(RTC_ARR[4]); // date buffer
date[3]=bcd_acii1(RTC_ARR[5]);
date[4]=bcd_acii2(RTC_ARR[5]);
date[6]=bcd_acii1(RTC_ARR[6]);
date[7]=bcd_acii2(RTC_ARR[6]);
}
unsigned char bcd_acii1(unsigned char val)
{
unsigned char lc[]={‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’};
unsigned char r,temp,tfr;
r= val;
temp=(r/0x10); //taking MSb
tfr=(lc[temp]);
return(tfr); //returning obtaind MSB
}
unsigned char bcd_acii2(unsigned char dat)
{
unsigned char lc[]={‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’};
unsigned char val1,temp1,tfr1;
val1=dat; //geting the LSB
temp1=val1%0x10;
tfr1=(lc[temp1]); //returning LSB
return(tfr1);
}
/*i2c.c*/
#include<reg51.h>
#include<stdio.h>
#include”intrins.h”
#include”I2C.h”
#define ACK 1
#define NO_ACK 0
#define SLAVE 0xD0
#define WRITE 0x00
#define READ 0x01
#define ERR_ACK 0x01
unsigned char i;
sbit SCL = P3^7; // connect to SCL pin (Clock)
sbit SDA = P3^6; // connect to SDA pin (Data)
void Start(void) //start SDA and SCL
{
SDA = 1;
SCL = 1;
_nop_();_nop_();
SDA = 0;
_nop_();_nop_();
SCL = 0;
_nop_();_nop_();
}
void Stop(void) //stop bit for SDA
{
SDA = 0;
_nop_();_nop_();
SCL = 1;
_nop_();_nop_();
SDA = 1;
}
void WriteI2C(unsigned char Data) //passing write code to device
{
for (i=0;i<8;i++)
{
SDA = (Data & 0x80) ? 1:0;
SCL=1;SCL=0;
Data<<=1;
}
SCL = 1;
_nop_();_nop_();
SCL = 0;
}
unsigned char ReadI2C(bit ACK_Bit) //readiong ACk from rtc or eeprom
{
unsigned char Data=0;
SDA = 1;
for (i=0;i<8;i++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
_nop_();
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
_nop_();_nop_();
SCL = 1;
_nop_();_nop_();
SCL = 0;
return Data;
}
void ReadRTC(unsigned char * buff) //reading data from RTC
{
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
Start();
WriteI2C(0xD1);
*(buff+0)=ReadI2C(ACK); // Second and incrimenting
*(buff+1)=ReadI2C(ACK); // Minute
*(buff+2)=ReadI2C(ACK); // hour
*(buff+3)=ReadI2C(ACK); // Day
*(buff+4)=ReadI2C(ACK); // date
*(buff+5)=ReadI2C(ACK); // month
*(buff+6)=ReadI2C(NO_ACK); // year
Stop();
}
void WriteRTC(unsigned char *buff) //writing data to RTC
{
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(*(buff+0)); //writing sec and inc
WriteI2C(*(buff+1)); //wirting min and inc
WriteI2C(*(buff+2)); //wirting hr and inc
WriteI2C(*(buff+3)); //wirting day and inc
WriteI2C(*(buff+4)); //wirting date and inc
WriteI2C(*(buff+5)); //wirting month and inc
WriteI2C(*(buff+6)); //wirting yr
Stop();
}
6.3 MODULE FOR VISITOR COUNTER
/*visitor.c*/
#include<reg51.h>
#include “visitor.h”
#include “lcd.h”
#include “delay.h”
unsigned int m,k,l,i;
extern unsigned int count=20;
unsigned char visitor[12] ={“TOTAL AVAL “};
unsigned char number[] ={‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’};
void visitordisplay()
{
k =count/10;
l =count%10;
clear();
for(i=0;i<12;i++)
{
lcd_pntc(visitor);
MSdelay(250);
}
lcd_pntc(number[k]);
MSdelay(250);
lcd_pntc(number[l]);
MSdelay(500);
return;
}
void enter (void) interrupt 0
{
EA =0;
count–;
if(count<0)
{
k =count/10;
l =count%10;
clear();
for(i=0;i<12;i++)
{
lcd_pntc(visitor);
MSdelay(250);
}
lcd_pntc(number[k]);
MSdelay(250);
lcd_pntc(number[l]);
MSdelay(500);
MSdelay(250);
}
EA=1;
}
void exit (void) interrupt 2
{
EA =0;
count++;
if(count>20)
{
k =count/10;
l =count%10;
clear();
for(i=0;i<12;i++)
{
lcd_pntc(visitor);
MSdelay(250);
}
lcd_pntc(number[k]);
MSdelay(250);
lcd_pntc(number[l]);
MSdelay(500);
MSdelay(250);
}
EA =1;
}
6.4 MODULE FOR LCD DISPLAY
/*lcd.c*/
#include<reg51.h>
#include<intrins.h>
#include”lcd.h”
sfr ldata=0x90; //difineng port1 for data
sbit rs=P2^0; //seting rs at p2.0
sbit rw=P2^1; //seting rw at p2.1
sbit en=P2^2; //seting en at p2.2
extern int fst;
extern int gn;
extern unsigned int m;
void MSdelay(unsigned int item);
void lcdcmmd(unsigned char mybyte)
{
ldata=mybyte;
rs=0;
rw=0;
en=1;
MSdelay(1);
en=0;
return;
}
void lcd_pnt(unsigned char *Ldat)
{
unsigned char *tmp;
tmp=Ldat;
while(*tmp!=’