/* Security System Project */ // by /* Shahrukh Saleem Qureshi */ // Software I2C connections sbit Soft_I2C_Scl at RB1_bit; sbit Soft_I2C_Sda at RB0_bit; sbit Soft_I2C_Scl_Direction at TRISB1_bit; sbit Soft_I2C_Sda_Direction at TRISB0_bit; // End Software I2C connections // LCD module connections sbit LCD_RS at RB2_bit; sbit LCD_EN at RB3_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit; sbit LCD_RS_Direction at TRISB2_bit; sbit LCD_EN_Direction at TRISB3_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; // End LCD module connections // Defining PORTS #define OP PORTA.RA2 #define Buzzer PORTA.RA3 #define ok PORTC.RC5 #define b0 PORTC.RC6 #define b1 PORTC.RC0 #define b2 PORTC.RC1 #define b3 PORTC.RC2 #define b4 PORTD.RD0 #define b5 PORTD.RD7 #define b6 PORTD.RD6 #define b7 PORTD.RD5 #define b8 PORTD.RD4 #define b9 PORTC.RC7 #define b10 PORTC.RC4 #define Clk PORTD.RD3 //End Defining Ports // Message & values declarations unsigned char m0[13] = "Initializing"; unsigned char m1[14] = "Initializing."; unsigned char m2[15] = "Initializing.."; unsigned char m3[16] = "Initializing..."; unsigned char m4[17] = "Initializing...."; unsigned char m5[8] = "Welcome"; unsigned char m6[15] = "Enter pincode:"; unsigned char m12[18] = ">Wrong pincode<"; unsigned char m13[17] = "Wrong Alt. code"; unsigned char m14[17] = "Limit reached.."; unsigned char m15[17] = "Enter Alt. code:"; unsigned char m16[18] = "Access Approved."; unsigned char m17[13] = "3 turns left"; unsigned char m18[13] = "2 turns left"; unsigned char m19[12] = "1 turn left"; unsigned char okay[22] = "Press OK "; unsigned char m20[11] = "Buzzer Off"; unsigned char m21[16] = "Enter new code:"; unsigned char str[7] = ""; unsigned char pass[6]; unsigned int i=0,k=0,co,cd=60,backspace=0,newcode=0,cd2=300,alt=0,alterror=0,clksw=0,sys=0,timer0=0,timer1=0; unsigned char count[] = ""; unsigned int check = 0; //End Message & values declarations char seconds, minutes, hours, day, month, year; void ab(); void Read_Time() { Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0xA0); // Address PCF8583, see PCF8583 datasheet Soft_I2C_Write(2); // Start from address 2 Soft_I2C_Start(); // Issue repeated start signal Soft_I2C_Write(0xA1); // Address PCF8583 for reading R/W=1 seconds = Soft_I2C_Read(1); // Read seconds byte minutes = Soft_I2C_Read(1); // Read minutes byte hours = Soft_I2C_Read(1); // Read hours byte day = Soft_I2C_Read(1); // Read year/day byte month = Soft_I2C_Read(0); // Read weekday/month byte Soft_I2C_Stop(); // Issue stop signal } void Transform_Time() { seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F); // Transform hours year = (day & 0xC0) >> 6; // Transform year day = ((day & 0x30) >> 4)*10 + (day & 0x0F); // Transform day month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month } void Display_Time() { Lcd_Chr(1, 6, (day / 10) + 48); // Print tens digit of day variable Lcd_Chr(1, 7, (day % 10) + 48); // Print oness digit of day variable Lcd_Chr(1, 9, (month / 10) + 48); Lcd_Chr(1,10, (month % 10) + 48); Lcd_Chr(1,15, year + 53); // Print year variable (start from year 2010) Lcd_Chr(2, 6, (hours / 10) + 48); Lcd_Chr(2, 7, (hours % 10) + 48); Lcd_Chr(2, 9, (minutes / 10) + 48); Lcd_Chr(2,10, (minutes % 10) + 48); Lcd_Chr(2,12, (seconds / 10) + 48); Lcd_Chr(2,13, (seconds % 10) + 48); } //Buttons method void buttons(); //Main method void main(void) { ADCON1 = 0x0F; CMCON |= 7; //Disable Comparators OSCCON = 0b01111111; //Using internal Oscilator @ 8MHz TRISB = 0x00; //PORTB for output (LCD) LATB = 0xFF; //PORTC for input LATC = 0xFF; //PORTD for input TRISA.RA2 = 0; //RA2 for output TRISA.RA3 = 0; //RA3 for output UCON.USBEN = 0; //Disable usb UCFG.UTRDIS = 1; TRISD = 0xF9; //PORTD output EEPROM_Write(0x01,'1'); EEPROM_Write(0x02,'4'); EEPROM_Write(0x03,'0'); EEPROM_Write(0x04,'5'); EEPROM_Write(0x05,'9'); EEPROM_Write(0x06,'5'); Buzzer=0; OP=0; Lcd_Init(); // Initialize LCD Soft_I2C_Init(); // Initialize RTC I2C IC Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1,1,m0); Delay_ms(400); Lcd_Out(1,1,m1); Delay_ms(400); Lcd_Out(1,1,m2); Delay_ms(400); Lcd_Out(1,1,m3); Delay_ms(400); Lcd_Out(1,1,m4); Delay_ms(400); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,6,m5); Delay_ms(500); label_gotohere1: here1: label_gotohere5: here5: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m6); Delay_ms(400); Lcd_Out(2,1,""); for(;;) { buttons(); if(str[0] == EEPROM_Read(0x01) && str[1] == EEPROM_Read(0x02) && str[2] == EEPROM_Read(0x03) && str[3] == EEPROM_Read(0x04) && str[4] == EEPROM_Read(0x05) && str[5] == EEPROM_Read(0x06) && i==6 && k==0) { Lcd_Out(1,1,okay); } if(ok==1 && str[0] == EEPROM_Read(0x01) && str[1] == EEPROM_Read(0x02) && str[2] == EEPROM_Read(0x03) && str[3] == EEPROM_Read(0x04) && str[4] == EEPROM_Read(0x05) && str[5] == EEPROM_Read(0x06) && i==6 && k==0) { Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m16); Delay_ms(1100); OP = 1;i=0;check=0; Lcd_Out(1,1,"Gate opened for "); //Lcd_Out(1,16,""); Lcd_Out(2,1,"1 min approx."); Delay_ms(48000); OP = 0;str[0] = str[1] = str[2] = str[3] = str[4] = str[5] = '0'; Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"Gate Closed");alt=0; Delay_ms(2500); goto here1; } label_gotohere8: here8: if(Clk==1 && alt==0) { Lcd_Out(1,1,"Date:"); // Prepare and output static text on LCD Lcd_Chr(1,8,'/'); Lcd_Chr(1,11,'/'); Lcd_Out(2,1,"Time:"); Lcd_Chr(2,8,':'); Lcd_Chr(2,11,':'); Lcd_Out(1,12,"201"); // start from year 2010 // Perform initialization for(clksw=0;clksw<=60;clksw++){ // Endless loop Read_Time(); // Read time from RTC(PCF8583) Transform_Time(); // Format date and time Display_Time(); // Prepare and display on LCD } goto here5; } else if(ok==1 && check!=3 && check!=4) { Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m6); Lcd_Out(2,1,m12); Delay_ms(1000);k=1; Lcd_Cmd(_LCD_CLEAR); if(check==0){Lcd_Out(1,1,m17);check++;} else if(check==1){Lcd_Out(1,1,m18);check++;} else if(check==2){Lcd_Out(1,1,m19);check=3;backspace=1;} Delay_ms(1000); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m6); Delay_ms(400); Lcd_Out(2,1,""); i=0; } if(ok==1 && check==3 && k==1) { Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m6); Lcd_Out(2,1,m12);Buzzer=1;i=0; Delay_ms(1000); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m14); Delay_ms(1000); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m15); Lcd_Out(2,1,"");k=2;alt=1; } if(ok==1 && check==3 && str[0] == '1' && str[1] == '9' && str[2] == '8' && str[3] == '4') { Lcd_Cmd(_LCD_CLEAR); Buzzer = 0; Lcd_Out(1,1,m20); Delay_ms(1000);i=0; Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m21); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m21); if(alterror==0){Lcd_Out(2,1,"");} if(alterror==1){Lcd_Out(2,1,"");} check = 4; k = 0; } if(ok==1 && check==4) { EEPROM_Write(0x01,str[0]); EEPROM_Write(0x02,str[1]); EEPROM_Write(0x03,str[2]); EEPROM_Write(0x04,str[3]); EEPROM_Write(0x05,str[4]); EEPROM_Write(0x06,str[5]); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"Saved."); Delay_ms(500);check=0; Lcd_Cmd(_LCD_CLEAR); } if(ok==1 && check==3 && k==2 && alt==1) { Lcd_Out(1,1,m15); Lcd_Out(2,1,""); i=6; Lcd_Out(2,1,m13); Delay_ms(2000); Lcd_Cmd(_Lcd_CLEAR); Lcd_Out(1,1,"Wait for 60 secs"); Lcd_Out(2,1,"Countdown:");alterror=1; goto here2; do { co++; label_gotohere2: here2: for(co=0;co<=60;co++){ IntToStr(cd,count); Lcd_Out(2,10,count);cd=cd-1; Delay_ms(925); } }while(co<=60); cd=60; Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,m15); Lcd_Out(2,1,"");k=2;i=0; } } } //End of main method //Method for buttons void buttons() { if(b0==1 && i!=6) { str[i] = '0';i=i+1; Lcd_Chr_Cp('0'); Delay_ms(250); timer0=0;timer1=0; } else if(b1==1 && i!=6) { str[i] = '1';i=i+1; Lcd_Chr_Cp('1'); Delay_ms(250); timer0=0;timer1=0; } else if(b2==1 && i!=6) { str[i] = '2';i=i+1; Lcd_Chr_Cp('2'); Delay_ms(250); timer0=0;timer1=0; } else if(b3==1 && i!=6) { str[i] = '3';i=i+1; Lcd_Chr_Cp('3'); Delay_ms(250); timer0=0;timer1=0; } else if(b4==1 && i!=6) { str[i] = '4';i=i+1; Lcd_Chr_Cp('4'); Delay_ms(250); timer0=0;timer1=0; } else if(b5==1 && i!=6) { str[i] = '5';i=i+1; Lcd_Chr_Cp('5'); Delay_ms(250); timer0=0;timer1=0; } else if(b6==1 && i!=6) { str[i] = '6';i=i+1; Lcd_Chr_Cp('6'); Delay_ms(250); timer0=0;timer1=0; } else if(b7==1 && i!=6) { str[i] = '7';i=i+1; Lcd_Chr_Cp('7'); Delay_ms(250); timer0=0;timer1=0; } else if(b8==1 && i!=6) { str[i] = '8';i=i+1; Lcd_Chr_Cp('8'); Delay_ms(250); timer0=0;timer1=0; } else if(b9==1 && i!=6) { str[i] = '9';i=i+1; Lcd_Chr_Cp('9'); Delay_ms(250); timer0=0;timer1=0; } else if(b10==1 && i!=6) { i=0; Lcd_Cmd(_LCD_CLEAR); if(backspace==0){ Lcd_Out(1,1,m6); Lcd_Out(2,1,""); } if(backspace==1){ Lcd_Out(1,1,m15); Lcd_Out(2,1,""); } Delay_ms(250); timer0=0;timer1=0; } } void ab(){ }