/*******************************************************************
TEST ASSIGNMENT ()
FileName : EEPMtest.c
Target : ATmega128
Project : ON CHIP ADC TEST
Author : Hardik Shah
Clock frequency : 16Mhz
Dated : 19th May 2008
Description : a security system with user login. Use internal
EEPROM to store valid passwords (numeric). Let the user enter his password using an
external interrupt. At the start of the program compare a password entered by the user. (Hint: Use appropriate fuse settings)
*******************************************************************/
#include<avr/io.h>
#include<EEPROM_ATM128.h>
#include<stdio.h>
#include<Serial.h>
void NewAccount(void);
void VerifyAccount(void);
int main(void)
{
unsigned char scan_serial;
Serial_Init();
print_string("-----------------System Init------------------");
EEPROM_write(0x5,0x1);
/*
cli(); //clear interrupts
// Enabling external interupt INT5
EICRB = 0xC0;
EIMSK = 0x20;
sei(); //set interrupts
*/
while(1)
{
print_string("\nEnter 1 for New Account");
print_string("\nEnter 2 for Verify Account");
print_string("\n:");
while(1)
{
scan_serial = USART_Receive();
USART_Transmit(scan_serial); //print the received byte
if(scan_serial == '1' ||scan_serial == '2')
break;
else
print_string("\nERROR!!!! Enter the displayed option\n:");
}
switch(scan_serial)
{
case '1':
NewAccount();
break;
case '2':
VerifyAccount();
break;
}
}
/*
while(1)
{
}
*/
}
void NewAccount(void)
{
char accid[8]; //8 bytes
char accpasswd[8]; //8 bytes
char strtmp[50];
char count = 0;
unsigned char E_readid;
int startWriteAddr;
for(count=0;count<=7;count++)
{
accid[count] = '\0';
accpasswd[count] = '\0';
}
print_string("\nEnter the Name of the Account : ");
count=0;
do
{
accid[count] = USART_Receive(); //store the received byte
USART_Transmit(accid[count]); //print the received byte
count++;
}
while(accid[count-1] != '\r' && count < 7);
if(count==7)
while(USART_Receive() != '\r');
else
accid[count-1] = '\0';
print_string("\nEnter the Password : ");
count = 0;
do
{
accpasswd[count] = USART_Receive(); //store the received byte
USART_Transmit('$'); //print the received byte
count++;
}
while(accpasswd[count-1] != '\r' && count < 7);
if(count==7)
while(USART_Receive() != '\r');
else
accpasswd[count-1] = '\0';
///////////////////////////////////////////////////////////////////////////////////////
//void EEPROM_write(unsigned int uiAddress, unsigned char ucData);
//unsigned char EEPROM_read(unsigned int uiAddress);
//read the total no. of users
E_readid = EEPROM_read(0x5);
if(E_readid==0xFF)
{
print_string("\nCANNOT REGISTER MAX FULL MEM");
//break;//users full
}
//assing the address
startWriteAddr = E_readid * 0x10;
#ifdef DEBUG_ON
sprintf(strtmp,"\nSTART ADDR = 0x%04x EEPROM AT 0x5 = %d",startWriteAddr,E_readid);
print_string(strtmp);
#endif
//write to that address id n password
///////
count = 0;
do
{
#ifdef DEBUG_ON
sprintf(strtmp,"\n0x%04x = %c",startWriteAddr,accid[count]);
print_string(strtmp);
#endif
EEPROM_write(startWriteAddr++,accid[count]);
//USART_Transmit(accid[count]); //print the received byte
count++;
}
while(count<=7);
count = 0;
do
{
#ifdef DEBUG_ON
sprintf(strtmp,"\n0x%04x = %c",startWriteAddr,accpasswd[count]);
print_string(strtmp);
#endif
EEPROM_write(startWriteAddr++,accid[count]);
//USART_Transmit(accpasswd[count]); //print the received byte
count++;
}
while(count<=7);
//////
//incremaent n store id
EEPROM_write(0x5,++E_readid);
/*
print_string("\nThe Entered ID is : ");
count = 0;
while(accid[count] != '\0')
{
USART_Transmit(accid[count]); //print the received byte
count++;
}
print_string("\nThe Entered Password is : ");
count = 0;
while(accpasswd[count] != '\0')
{
USART_Transmit(accpasswd[count]); //print the received byte
count++;
}
*/
}
void VerifyAccount(void)
{
char accid[10];
char accpasswd[10];
char strtmp[50];
char count = 0;
int startreadAddr;
char E_ReadByte;
char startid;
char verifyfailed;
unsigned char E_readmaxid;
char gofornext = 0;
for(count=0;count<=7;count++)
{
accid[count] = '\0';
accpasswd[count] = '\0';
}
print_string("\nEnter the Name of the Account : ");
count=0;
do
{
accid[count] = USART_Receive(); //store the received byte
USART_Transmit(accid[count]); //print the received byte
count++;
}
while(accid[count-1] != '\r' && count < 7);
if(count==7)
while(USART_Receive() != '\r');
else
accid[count-1] = '\0';
print_string("\nEnter the Password : ");
count = 0;
do
{
accpasswd[count] = USART_Receive(); //store the received byte
USART_Transmit('$'); //print the received byte
count++;
}
while(accpasswd[count-1] != '\r' && count < 7);
if(count==7)
while(USART_Receive() != '\r');
else
accpasswd[count-1] = '\0';
///////////////////////////////////////////////////////////////////////////////////////
//extract the no. of id's present
//search from start till last id present
//if found the match with the id then go for password checking
//else failed display the message as verification failed
E_readmaxid = EEPROM_read(0x5);
//assing the address
verifyfailed = 0;
for(startid = 1;startid < E_readmaxid;startid++)
{
startreadAddr = startid * 0x10;
count = 0;
do
{
E_ReadByte = EEPROM_read(startreadAddr);
#ifdef DEBUG_ON
sprintf(strtmp,"\nID 0x%04x = %02x accid[%d] = %c",startreadAddr,E_ReadByte,count,accid[count]);
print_string(strtmp);
#endif
if(E_ReadByte == accid[count])
{
startreadAddr++;
count++;
}
else
{
gofornext =1;
break;
}
}
while(count <= 7);
if(gofornext)
{
gofornext = 0;
continue;
}
count = 0;
do
{
E_ReadByte = EEPROM_read(startreadAddr);
#ifdef DEBUG_ON
sprintf(strtmp,"\nPD 0x%04x = %02x accpd[%d] = %c",startreadAddr,E_ReadByte,count,accpasswd[count]);
print_string(strtmp);
#endif
if(E_ReadByte == accpasswd[count])
{
startreadAddr++;
count++;
}
else
{
verifyfailed = 1;
break;
}
}
while(count <= 7);
break;
}
if(startid==E_readmaxid)
{
print_string("\nINVAILD NO RECORDS FOUND");
}
else if(verifyfailed)
{
print_string("\nVERIFYING PASSWORD FAILED");
}
else
{
print_string("\nSUCCESS ON VERIFY");
}
}
/*
ISR(INT5_vect)
{
print_string(" IN ISR5 ");
cli();
}
*/