#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);
char keypad();
void data1(char);
void display(const char*p)
{
while (*p!='\0')
{
data1(*p);
p++;
}
}
int main(void)
{char d;
int i=0,f=0;
char a[]="1234",S[30]; // predefined password is 1234
DDRA=0xff;
DDRB=0xff;
DDRC=0xf0;
cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);
while(1)
{i=0;
while(i<4)
{
d=keypad();
if (d!='z')
{
S[i]=d;
data(S[i]);
i++;
}
}
cmd(0x01);
cmd(0x80);
for (i=0;i<4;i++)
if(S[i]!=a[i])
f=1;
if(f==1)
display("access denied");
else
display("access granted");
_delay_ms(5000);
cmd(0x01);
cmd(0x80);
}
}
char keypad()
{
int e;
PORTC=0x7F;
e=PINC&0x0F;
switch (e)
{
case 0x07:
return '1';
break;
case 0x0B:
return '2';
break;
case 0x0D:
return '3';
break;
case 0x0E:
return '4';
break;
}
PORTC=0xbf;
e=PINC&0x0f;
switch (e)
{
case 0x07:
return '5';
break;
case 0x0b:
return '6';
break;
case 0x0d:
return '7';
break;
case 0x0e:
return '8';
break;
}
PORTC=0xdf;
e=PINC&0x0f;
switch (e)
{
case 0x07:
return '9';
break;
case 0x0b:
return '0';
break;
case 0x0d:
return 'a';
break;
case 0x0e:
return 'b';
break;
}
PORTC=0xef;
e=PINC&0x0f;
switch (e)
{
case 0x07:
return 'c';
break;
case 0x0b:
return 'd';
break;
case 0x0d:
return 'e';
break;
case 0x0e:
return 'f';
break;
default:
return 'z';
break;
}
}
void cmd(int a)
{
PORTB=a;
PORTA=0x00;
PORTA=0x02;
_delay_ms(200);
PORTA=0x00;
}
void data(char s)
{
PORTB=s;
PORTA=0x03;
_delay_ms(1000);
PORTA=0x01;
}
void data1(char s)
{
PORTB=s;
PORTA=0x03;
_delay_ms(100);
PORTA=0x01;
}
No comments:
Post a Comment