Вы находитесь на странице: 1из 7

//Controller used is PIC18F4520

//Crystal OScillator frequency = 16MHz


//LCD in 8 bit mode data lines on Port D and control lines on C5,C6,C7
//Touch panel connected to C0-C3 and analog input to AN0(y direction) and AN1(x
direction)
//LED bank connected to pins AN2-AN7---one for each picture on the screen
//Two stepper motors connected to higher and lower nibble of PORT B
#include<stdio.h>
#include<stdlib.h>
#include<p18f4520.h>
#include"config.h"
#include"delays.h"
#include"xlcd.h"
#include"adc.h"
#include"config.h"
#include"pconfig.h"
#include"EEP.h"
#define _XTAL_FREQ 16000000
int xconvert(); //Function prototype declarations
int yconvert();
unsigned char correctpassword();
void changepassword();
void msdelay(unsigned int);
void aster();
int xresult=0,yresult=0;
int yconvert() //Function to convert y axis input
{
int result;
TRISCbits.TRISC0=0;
TRISCbits.TRISC1=1;
TRISCbits.TRISC2=0;
TRISCbits.TRISC3=1;
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 0;
PORTCbits.RC2 = 1;
PORTCbits.RC3 = 0;
OpenADC( ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_4_TAD,ADC_CH0 & ADC_INT_OFF & ADC_VR
EFPLUS_VDD & ADC_VREFMINUS_VSS,13);
ADCON1=0b00001101;
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result = ReadADC(); // Read result
CloseADC(); // Disable A/D converter
return(result);
}
int xconvert() //Function to convert x axis input
{
int result;
TRISCbits.TRISC0=1;
TRISCbits.TRISC1=0;
TRISCbits.TRISC2=1;
TRISCbits.TRISC3=0;
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 1;
PORTCbits.RC2 = 0;
PORTCbits.RC3 = 0;
OpenADC( ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_4_TAD,ADC_CH1 & ADC_INT_OFF & ADC_VR
EFPLUS_VDD & ADC_VREFMINUS_VSS,13);
ADCON1=0b00001101;
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result = ReadADC(); // Read result
CloseADC(); // Disable A/D converter
return(result);
}
void aster()
{
while(BusyXLCD());
WriteCmdXLCD(SHIFT_DISP_RIGHT);
while(BusyXLCD());
putrsXLCD("*");
}
unsigned char correctpassword() //Function to check
{ //validity of password entered
unsigned char q[4],p[4],i;
unsigned int j=0x00;
for(i=0;i<4;i++)
{
Busy_eep();
q[i]=Read_b_eep(j);
j=j+0x01;
}
for(i=0;i<4;i++)
{
xresult=xconvert();
yresult=yconvert();
while(xresult<50&&yresult<50)
{
xresult=xconvert();
yresult=yconvert();
while(BusyXLCD());
putrsXLCD("WAITING...");
}
while(BusyXLCD());
WriteCmdXLCD(0xC0);
while(BusyXLCD());
WriteCmdXLCD(BLINK_ON);
TRISAbits.TRISA2=0;
TRISAbits.TRISA3=0;
TRISAbits.TRISA4=0;
TRISAbits.TRISA5=0;
TRISAbits.TRISA6=0;
TRISAbits.TRISA7=0;
PORTAbits.RA2=1;
PORTAbits.RA3=1;
PORTAbits.RA4=1;
PORTAbits.RA5=1;
PORTAbits.RA6=1;
PORTAbits.RA7=1;
if(xresult<500)
{
if(yresult>0&&yresult<200)
{
p[i]=0x00;
aster();
PORTAbits.RA2=0;
}
if(yresult>200&&yresult<400)
{
p[i]=0x01;
aster();
PORTAbits.RA3=0;
}
if(yresult>400&&yresult<600)
{
p[i]=0x02;
aster();
PORTAbits.RA4=0;
}
}
else
{
if(yresult>0&&yresult<200)
{
p[i]=0x10;
aster();
PORTAbits.RA5=0;
}
if(yresult>200&&yresult<400)
{
p[i]=0x11;
aster();
PORTAbits.RA6=0;
}
if(yresult>400&&yresult<600)
{
p[i]=0x12;
aster();
PORTAbits.RA7=0;
}
} //End of else
} //End of for loop
for(i=0;i<4;i++)
{
if(p[i]!=q[i])
return 0;
}//End of function
return 1;
}
void changepassword() //Function to change password
{
unsigned char m,i;
unsigned char z[4];
unsigned int j=0;
while(BusyXLCD());
putrsXLCD("OLD PASSWORD:");
m=correctpassword();
if(m==1)
{
while(BusyXLCD());
putrsXLCD("NEW PASSWORD:");
for(i=0;i<4;i++)
{
while(xresult<50&&yresult<50)
{
xresult=xconvert();
yresult=yconvert();
while(BusyXLCD());
putrsXLCD("WAITING...");
}
while(BusyXLCD());
WriteCmdXLCD(0xC0);
while(BusyXLCD());
WriteCmdXLCD(BLINK_ON);
TRISAbits.TRISA2=0;
TRISAbits.TRISA3=0;
TRISAbits.TRISA4=0;
TRISAbits.TRISA5=0;
TRISAbits.TRISA6=0;
TRISAbits.TRISA7=0;
PORTAbits.RA2=1;
PORTAbits.RA3=1;
PORTAbits.RA4=1;
PORTAbits.RA5=1;
PORTAbits.RA6=1;
PORTAbits.RA7=1;
if(xresult<500)
{
if(yresult>0&&yresult<200)
{
z[i]=0x00;
aster();
PORTAbits.RA2=0;
}
if(yresult>200&&yresult<400)
{
z[i]=0x01;
aster();
PORTAbits.RA3=0;
}
if(yresult>400&&yresult<600)
{
z[i]=0x02;
aster();
PORTAbits.RA4=0;
}
}
else
{
if(yresult>0&&yresult<200)
{
z[i]=0x10;
aster();
PORTAbits.RA5=0;
}
if(yresult>200&&yresult<400)
{
z[i]=0x11;
aster();
PORTAbits.RA6=0;
}
if(yresult>400&&yresult<600)
{
z[i]=0x12;
aster();
PORTAbits.RA7=0;
}
}
} //End of for loop
for(i=0;i<4;i++)
{
Busy_eep();
Write_b_eep(j,z[i]);
j=j+0x01;
}
}
} //End of function
void DelayFor18TCY( void )
{
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
}
void DelayPORXLCD (void)
{
Delay1KTCYx(60); // Delay of 15ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (15ms * 16MHz) / 4
// Cycles = 60,000
return;
}
void DelayXLCD (void)
{
Delay1KTCYx(20); // Delay of 5ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (5ms * 16MHz) / 4
// Cycles = 20,000
return;
}
void main(void)
{
unsigned char i,m;
OpenXLCD( EIGHT_BIT & LINES_5X7 );
while(BusyXLCD());
putrsXLCD("ENTER PASSWORD");
yresult=yconvert();
if(yresult>600&&yresult<800)
changepassword();
m=correctpassword();
if(m==1)
{
while(BusyXLCD());
putrsXLCD("CORRECT PASSWORD");
//stepper motor sequence
yresult=yconvert();
while(yresult<800)
{
yresult=yconvert();
}
//reverse stepper motor sequence
while(1)
{
}
}
else
{
while(BusyXLCD());
putrsXLCD("WRONG PASSWORD");
while(BusyXLCD());
WriteCmdXLCD(0xC0);
while(BusyXLCD());
putrsXLCD("WAIT 15 SECONDS");
for(i=0;i<15;i++)
{
msdelay(1000);
}
while(1);
}
}//End of main
void msdelay(unsigned int mil)
{
unsigned int t,y;
for(t=0;t<mil;t++);
for(y=0;y<135;y++);
}

Вам также может понравиться