Friday, December 20, 2013

Project Log - Project Code Name Password

Brief:



Basically this is a basic password lock system. It asks for password and then if password is correct it will do the appropriate action. In this case the appropriate action is driving the motor. L293D motor driving IC is used for driving a single motor.

So in short type the password if password is correct run the motor.



Features:



  1. Says hello and opens a scrollable menu. 
  2. Menu can be scrolled using keys 8 and 2.  
  3. After reaching down to last item the action to key 8 is disabled.
  4. Similarly while reaching from bottom to up when the first item is reached the 2 key action becomes disabled.
  5. Will asks for password 3 times. If all three times password is entered incorrect the buzzer will start.
  6. The menu item is stored as text in program memory.
  7. The password is stored in eeprom.
  8. The password can be any 4 digit. Password can be changed and it will again be stored in eeprom.
  9. By storing password in eeprom the changed password will be retained even after the power to the controller is turned off.
  10. The keys of the keypad are quite responsive and fast. 

Limitations:


  1. Only 2 menus right now, enter password and change password.
  2. It is not designed to come out of menu item enter password. One will hasve to turn of the controller or reset it to go to main menu.
  3. Usees PORTB 8 data bit lcd interface can be used as four.
  4. Uses PORTD for interfacing keypad hence disabling any scope for serial communication.
  5. Uses PORTA for control interfacing of 16x2 character LCD.
  6. Uses letter D for hitting enter.
  7. Multiple character from same key not enabled.

Components:


  1. Any avr development board
  2. 4x4 keypad.
  3. LCD
  4. L293D motor driving IC.
  5. Motor and a prefer separate supply for Motor and for controller. Use a smps unit for development purposes.



Modules:

The project has been divide into separate independent modules so one can attach or detach each module depending on one’s final control element.

  1. Timer module having function relating to hardware timer. For creating hardware timer delay.
  2. Display Module having all basic control functions required to interface a character LCD.
  3. Keypad Module handles interfacing of keypad.
  4. EEPROM Module handles writing and reading from the eeprom.
  5. Menu Module creates and control the menu.
  6. Main Modules contains the whole application logic and functions.


All the files and the complete source of the project can be downloaded from my open source project link provide here. 


All the files are well documented with comments describing each function. This post will only show major lines from each files and will not go into detail of how and why. For the future I will try to write a separate post for implementation of each module and why and how certain techniques were used. Right now I am just logging all my old work so I can be descriptive in future.

Timer Module


Function Name
Description
void Delay1Sec(void);

Returns: void
Parameter: void
Description: Creates a hardware delay of 1 sec using timers and counters.
void DelayXSec(unsigned int number);

Returns: void
Parameter: Unsigned int number (Number of Seconds)
Description: Creates a hardware delay of X secs using timers and counters, the x is the number provided by the user.
void Delay1mSec(void);
Returns: void
Parameter: void
Description: Creates a hardware delay of 1 millisecond using timers and counters.
void DelayXmSec(unsigned int number);
Returns: void
Parameter: Unsigned int number (Number of milliseconds)
Description: Creates a hardware delay of X milliseconds using timers and counters, the x is the number provided by the user.

Keypad Module

Macro Name
Macro Purpose
#define KEY_PRT PORTD
Port at which output of keypad to be passed
#define KEY_DDR DDRD
DDR register for that port. Will define which pins to be considered as input and which as output
#define KEY_PIN PIND
Pin for port at which input from the columns of the keypad will be taken.

Function Name
Function Description
void KEYPAD_Init(void);
Returns: void
Parameter: void
Description: Initializes Keypad port and DDR register.
unsigned char GetKeyPressed(void);
Returns: unsigned char (char of the key pressed)
Parameter: void
Description: Returns the character of the key pressed.

Display Modules

Macro Name
Macro Purpose
#define LCD_DATA_PORT PORTB
Port at which data output to LCD will be passed
#define LCD_DATA_DDR  DDRB
DDR Register for DATA port determining the direction of data.
#define LCD_DATA_PIN PINB
Pin port for reading data from the buffer of the LCD
#define LCD_CMD_PORT PORTA
Port at which command to LCD will be passed.
#define LCD_CMD_DDR DDRA
DDR register for COMMAND port determining the direction of the command.
#define LCD_CMD_PIN PINA
Pin port for reading command from command register from command buffer of LCD.
#define LCD_RS 0
LCD Register select pin.
#define LCD_RW 1
LCD read-write pin
#define LCD_EN 2
LCD Enable Pin

Function Name
Function Description
void LCD_Init(void);
Returns: void
Parameter: void
Description: Initializes LCD port and DDR register.
void LCD_GotoXY(unsigned char row, unsigned char col);
Returns: void
Parameter: Unsigned char row and unsigned character column.(meaning x and y)
Description: Jumps to the row and column as provided by the user.
void LCD_Write_Command(unsigned char command);
Returns: void
Parameter: unsigned char command(command in form of hexadecimal or in form of character)
Description: Writes the command to the LCD
void LCD_Write_Data(unsigned char data);
Returns: void
Parameter: unsigned char data (writes the ascii data directly to the LCD buffer hence displaying it on LCD)
Description: Writes data to the LCD like text or string.
void LCD_Clear(void);
Returns: void
Parameter: void
Description: Clears LCD
void LCD_Print(char *str);
Returns: void
Parameter: char * str
Description: Prints the passed string to the LCD.
void LCD_Write_Char(char c);
Returns: void
Parameter: char c
Description: Prints the passed character to the LCD.

EEPROM Module

Function Name
Function Description
void write_eeprom(unsigned char *text)
Returns: void
Parameter: unsigned char *text(text to be written to eeprom)
Description:Writes text to eeprom.
void read_eeprom(unsigned char *text)
Returns: void
Parameter: unsigned char *text(variable to which text is to be written)
Description: Read the eeprom and writes the read text on the provided variable.

Menu Module

Program Memory Strings
Description
static const char menuitem_1_line_1[] PROGMEM=  "1) - Enter ";
Initialize Items to be written on program memory
static const char common_line_2[] PROGMEM=                 "     Password";
Initialize Items to be written on program memory
static const char menuitem_2_line_1[] PROGMEM=      "2) - Change ";
Initialize Items to be written on program memory
PGM_P string_table[] PROGMEM =
{
                menuitem_1_line_1,
                common_line_2,
                menuitem_2_line_1
};
Making strings in program memory accessible by using string table.
strcpy_P(buffer, (PGM_P)pgm_read_word(&(string_table[0])));
Using of string table by use of string copy function as shown. Here buffer is a variable with size equivalent to the number of columns in the character LCD.

Function Name
Function Description
int create_and_show_menu(void);
Returns: int(1 or 2 depending on the choice of menu item)
Parameter: void
Description: Returns integer value 1 if menu item 1 is selected and 2 if menu item 2 is selected.

Main Module

Function Name
Function Description
void sayHello(void);
Returns: void
Parameter: void
Description: Says hello on LCD. Can be used as a test function to check connection of LCD.
void enter_password_handler(void);
Returns: void
Parameter: void
Description: Called when enter password menu is selected. Does whatever comes after hitting enter password menu item.
void change_password_handler(void);
Returns: void
Parameter: void
Description: Called when change password menu is selected. Does whatever comes after hitting change password menu item.
void system_error_handler(void);
Returns: void
Parameter: void
Description: Called when non of the though value is arised.
void main(void);
Returns: void
Parameter: void
Description: Main function.
MCUCSR = (1<<JTD);
Statemen to disable JTAG

Demonstration Video