#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000UL
uint8_t column[2] = {0,64};
const char password[4] = {'9','9','8','9'};
char entered_password[4] = {0};
uint8_t currentindex = 0;
const char keymap[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
#define row1 PD0
#define row2 PD1
#define row3 PD2
#define row4 PD3
#define col1 PD4
#define col2 PD5
#define col3 PD6
#define col4 PD7
#define rs PC0
#define en PC1
#define D4 PC2
#define D5 PC3
#define D6 PC4
#define D7 PC5
void ports_init(void)
{
DDRD &= ~((1 << row1) | (1 << row2) | (1 << row3) | (1 << row4));
PORTD |= (1 << row1) | (1 << row2) | (1 << row3) | (1 << row4);
DDRD |= (1 << col1) | (1 << col2) | (1 << col3) | (1 << col4);
PORTD |= (1 << col1) | (1 << col2) | (1 << col3) | (1 << col4);
lcd_init();
timer_init();
send_string("enter password =");
set_cursor(0,1);
}
void timer_init(void)
{
DDRB = (1 << PB1);
TCCR1A |= (1 << COM1A1) | (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << WGM12) | (1 << CS11);
ICR1 = 39999;
}
void set_servo_position(uint16_t position)
{
if(position < 1000) position = 1000;
if(position > 2000) position = 2000;
OCR1A = position * 2;
}
char scan_keypad(void)
{
for(uint8_t col = 0;col < 4;col++)
{
PORTD &= ~(1 << (col1 + col));
for(uint8_t row=0;row < 4;row++)
{
if(!(PIND & (1 << (row1 + row))))
{
_delay_ms(20);
while(!(PIND & (1 << (row1 + row))));
PORTD |= (1 << (col1 + col));
return keymap[row][col];
}
}
PORTD |= (1 << (col1 + col));
}
return '\0';
}
void lcd_clear(void)
{
send_command(0x01);
_delay_ms(2);
}
uint8_t checkpassword(void)
{
for(uint8_t i=0;i<4;i++)
{
if(entered_password[i] != password[i])
{
return 0;
}
}
return 1;
}
void lock_open(void)
{
servo_open();
lcd_clear();
send_string("access granted");
set_cursor(0,1);
send_string("lock open");
_delay_ms(3000);
lcd_clear();
send_string("enter password = ");
set_cursor(0,1);
}
void servo_open(void)
{
set_servo_position(2000);
_delay_ms(5000);
set_servo_position(1000);
}
void wrongpassword(void)
{
lcd_clear();
send_string("access denied");
set_cursor(0,1);
send_string("please try again");
_delay_ms(3000);
lcd_clear();
send_string("enter password = ");
set_cursor(0,1);
}
void lcd_init(void)
{
DDRC |= (1 << rs) | (1 << en) | (1 << D4) | (1 << D5) | (1 << D6) | (1 << D7);
_delay_ms(20);
send_command(0x33);
send_command(0x32);
send_command(0x28);
send_command(0x0E);
send_command(0x06);
send_command(0x01);
_delay_ms(2);
}
void send_command(unsigned char command)
{
PORTC = ((PORTC & 0x03) | ((command & 0xF0) >> 2));
PORTC &= ~(1 << rs);
PORTC |= (1 << en);
_delay_ms(2);
PORTC &= ~(1 << en);
_delay_ms(200);
PORTC = ((PORTC & 0x03) | (command & 0x0F) << 2);
PORTC |= (1 << en);
_delay_ms(2);
PORTC &= ~(1 << en);
_delay_ms(2);
}
void send_data(unsigned char data)
{
PORTC = ((PORTC & 0x03) | ((data & 0xF0) >> 2));
PORTC |= (1 << rs);
PORTC |= (1 << en);
_delay_ms(2);
PORTC &= ~(1 << en);
_delay_ms(200);
PORTC = ((PORTC & 0x03) | (data & 0x0F) << 2);
PORTC |= (1 << en);
_delay_ms(2);
PORTC &= ~(1 << en);
_delay_ms(2);
}
void set_cursor(uint8_t x,uint8_t y)
{
send_command(0x80 + x + column[y]);
}
void send_string(char *string)
{
while(*string != '\0')
{
send_data(*string++);
}
}
int main(void)
{
ports_init();
set_servo_position(1000);
while(1)
{
char key = scan_keypad();
if(key != '\0')
{
if(key == '#')
{
if(currentindex == 4)
{
if(checkpassword())
{
lock_open();
}
else
{
set_servo_position(1000);
wrongpassword();
}
}
else
{
set_servo_position(1000);
wrongpassword();
}
currentindex = 0;
}
else if(key == '*')
{
currentindex = 0;
set_cursor(0,1);
send_string(" ");
set_cursor(0,1);
_delay_ms(200);
}
else if(currentindex < 4)
{
entered_password[currentindex++] = key;
send_data('*');
}
}
}
return 0;
}