Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

please rectify the code

Status
Not open for further replies.

thannara123

Advanced Member level 5
Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,602
Helped
122
Reputation
244
Reaction score
116
Trophy points
1,353
Visit site
Activity points
10,625
I am wrting to here a part of my code , when it run works normally at the red colored section

Code:
while(1){  
                  [COLOR=#ff0000]  LCD_clear;
                    password=  InputNumber("Enter Password");//Get the keycode of pressed key
                    if(password==ReadPassFromEEPROM())
                       {
                          LCD_clear;
                          string(" Unlocked");
                          _delay_ms(100);
                          
                          while(1)     {
                         f=GetKeyPressed();
                           while(GetKeyPressed()==255)
                                 {
                               _delay_loop_2(10);
                                }
                            if (f==2){ [/COLOR]                                                                               

                                password==InputNumber("old");    // not getting values 
                                _delay_ms(300);
                                
                                if (password==ReadPassFromEEPROM())                            
                                
                                {  
                                    newpassword=InputNumber("new");
                                    
                                    string("just testing");
                                    WritePassToEEPROM(newpassword);
                                    _delay_ms(300);
                                }                           
                           
                            }
                                
                            }                  
                            }
Logic :
1) If(f==2) it will ask old password
2) Enter the oldpassword
3) check the password is matched with EEPRO
4) if yes ask new password and write to EEPRO
4) else invalid
please help me
 

Code:
uint16_t InputNumber(char *msg)
{
	uint8_t keyCode;
	int x=0;
	int pos=0;
	uint8_t prev_key=0xFF;
	uint8_t k=0xc1;
	uint8_t system_reset = 0;
	int key[4];
	dis_cmd(0x81);
	while(*msg) dis_data(*msg++);
     LINE2;
		while(!system_reset)
		{	
			keyCode=GetKeyPressed(); 							//Get the keycode of pressed key
			if(pos==4|keyCode==2)
			{
				pos==0;
				return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3]));		//save the password in 16 bit integer
				break;
							}
			if(keyCode==prev_key)
			continue;
			else if(keyCode==0xFF)
			{
				prev_key=0xFF;
				continue;
			}
			else
			{
				prev_key=keyCode;
			}


			

switch (keyCode) 									//generating key characeter to display on LCD
{
	case (11):
	string("1");	
	dis_cmd(k++);
	key[x]=1;
	x++;
	pos++;
	break;
	
	
	case (10):
	//dis_cmd(k++);
	string("2");	
	dis_cmd(k++);
	key[x]=2;
	x++;
	pos++;
	break;
	
	case (9):
	string("3");	
	dis_cmd(k++);
	key[x]=3;
	x++;
	pos++;
	break;
	
	case (8):
	string("4");
	dis_cmd(k++);
	key[x]=4;
	x++;
	pos++;
	break;
	
	case (7):
	//dis_cmd(k++);
	string("5");
	dis_cmd(k++);
	key[x]=5;
	x++;
	pos++;
	break;
	
	case (6):
	string("6");
	dis_cmd(k++);
	key[x]=6;
	x++;
	pos++;
	break;
	
	case (5):
	string("7");
	dis_cmd(k++);
	key[x]=7;
	x++;
	pos++;
	break;
	
	case (4):
	string("8");
	dis_cmd(k++);
	key[x]=8;
	x++;
	pos++;
	break;
	
	case (3):
	string("9");
	dis_cmd(k++);
	key[x]=9;
	x++;
	pos++;
	break;
	
	case (2):
	//string("*");
	//dis_cmd(k++);
	key[x]=0;
	x++;
	pos++;
	break;

	case (1):
	string("0");
	dis_cmd(k++);
	key[x]=0;
	x++;
	pos++;
	break;

	case (12):
	system_reset=1;
	break;
	
	//default: ;

}

_delay_loop_2(300);

		}
		
		
	
	}
 

attaching the project file please look it
 

Attachments

  • New folder.rar
    85 KB · Views: 61

defult value is 5050 unlocked
then press * to change password ie (f==2)
 

thanks working but when re-enter old password only three digit can enter why so sir ?
 

No .. problem still..

I think ..
if(pos==4|| keyCode==2)
{ h=0;
pos=0;
x=0;
return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3])); //save the password in 16 bit integer
}

keyCode=GetKeyPressed();//Get the keycode of pressed key // it makes again pos=1 that may be the reason but how clear
 

Why are you using pos, x , and k. You can just use x. Replace pos and k with x like this
Code:
case (11):
	string("1");	
	key[x]=1;
        dis_cmd(x++);
	break;
and
Code:
 if(x==4|| keyCode==2)

Post the Code for GetKeyPressed() function. Send me the .cof file for your code. I will debug and see.
 

full project code as follows
Code:
                       [COLOR=#FF0000]  Main .c[/COLOR]
   
#include<avr/io.h>
#include<avr/eeprom.h>
#include<util/delay.h>
#include<inttypes.h>
#include "eeprom.h"
#include "lcd.h"
//#include "keypad.h"
#include "user_interface.h"


int main(void)
{
    DDRB=0xFF;
    lcd_init();
        
        uint16_t password;
        uint8_t f;
        uint16_t newpassword;
        if(ReadPassFromEEPROM()==25755)
        {
            //Password is blank so store a default password
            WritePassToEEPROM(5050);
            _delay_ms(400);
        }

        
        LCD_clear;
        LINE1;
        dis_cmd(0x84);
        string("Drive");
      _delay_ms(200);
    
    
    while(1){       
                    LCD_clear;
                    password=  InputNumber("Enter Password");//Get the keycode of pressed key
                    if(password==ReadPassFromEEPROM())
                       {
                          LCD_clear;
                          string(" Unlocked");
                          _delay_ms(100);
                          
                          while(1)     {
                         f=GetKeyPressed();
                           while(GetKeyPressed()==255)
                                 {
                               _delay_loop_2(10);
                                }
                            if (f==2){    
                                
                                
                                password=InputNumber("Old Password");
                                _delay_ms(1000);
                                                                
                                
                                if (password==ReadPassFromEEPROM())
                                                                
                                  {  
                                    newpassword=InputNumber("new");
                                    
                                    string("okok");
                                    WritePassToEEPROM(newpassword);
                                    _delay_ms(300);
                                }            
                                
                                
                            }
                                
                            }                  
                            }                                                                
                    
                  
                          else
                          {
                              LCD_clear;
                              string("Wrong Password");
                              _delay_ms(1000);
                          }
        
          }             
}


Code:
                  [COLOR=#FF0000] interfacing /decoding section file .c[/COLOR]
#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>
#include "keypad.h"
#include "user_interface.h"
#include "lcd.h"

uint16_t InputNumber(char *msg)
{  
    uint8_t keyCode;
    uint8_t x=0;
    uint8_t pos=0;
    uint8_t prev_key=0xFF;
    uint8_t k=0xc1;
    int key[4];
    uint8_t h=0;
    dis_cmd(0x81);
    while(*msg) dis_data(*msg++);
     LINE2;
        while(1)
        {    
                        
             if(h==4)
                 {    pos=0;
                x=0;                                
                return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3]));    //save the password in 16 bit integer
               }    
                    
            keyCode=GetKeyPressed();//Get the keycode of pressed key
                                    
            if(keyCode==prev_key)
            continue;
            else if(keyCode==0xFF)
            {
                prev_key=0xFF;
                continue;
            }
            else
            {
                prev_key=keyCode;
            }    
            
              h++;
switch (keyCode) //generating key characeter to display on LCD
{
    case (11):
    string("1");    
    dis_cmd(k++);
    key[x]=1;
    x++;
    pos++;
    break;
    
    
    case (10):
    //dis_cmd(k++);
    string("2");    
    dis_cmd(k++);
    key[x]=2;
    x++;
    pos++;
    break;
    
    case (9):
    string("3");    
    dis_cmd(k++);
    key[x]=3;
    x++;
    pos++;
    break;
    
    case (8):
    string("4");
    dis_cmd(k++);
    key[x]=4;
    x++;
    pos++;
    break;
    
    case (7):
    //dis_cmd(k++);
    string("5");
    dis_cmd(k++);
    key[x]=5;
    x++;
    pos++;
    break;
    
    case (6):
    string("6");
    dis_cmd(k++);
    key[x]=6;
    x++;
    pos++;
    break;
    
    case (5):
    string("7");
    dis_cmd(k++);
    key[x]=7;
    x++;
    pos++;
    break;
    
    case (4):
    string("8");
    dis_cmd(k++);
    key[x]=8;
    x++;
    pos++;
    break;
    
    case (3):
    string("9");
    dis_cmd(k++);
    key[x]=9;
    x++;
    pos++;
    break;
    
    case (2):
    //string("*");
    //dis_cmd(k++);
    key[x]=0;
    x++;
    pos++;
    break;

    case (1):
    string("0");
    dis_cmd(k++);
    key[x]=0;
    x++;
    pos++;
    break;

    case (12):
    break;
    
    //default: ;

}

_delay_loop_2(300);

        }
        
        
    pos=0;
    x=0;
    }
Keypad.h file

Code:
/*
 * keypad.h
 *
 * Created: 12/19/2012 10:49:06 PM
 *  Author: Gelectorn
 */ 


#ifndef KEYPAD_H_
#define KEYPAD_H_

#define KEYPAD_PORT PORTD
#define KEYPAD_DDR   DDRD
#define KEYPAD_PIN   PIND

uint8_t GetKeyPressed()
{
  uint8_t r,c;
  KEYPAD_PORT|= 0X0F;
  for(c=0;c<3;c++)
     {
        KEYPAD_DDR&=~(0X7F);
        KEYPAD_DDR|=(0X40>>c);
        for(r=0;r<4;r++)
           {
            if(!(KEYPAD_PIN & (0X08>>r)))
              {
                return (r*3+c);
              }
           }
     }
      return 0XFF;//Indicate No key pressed
}      



#endif /* KEYPAD_H_ */
 

It displayes "Old Password" and the user has to enter the old password. Why is it necessary. Already the user has entered the old password and it is checked with the value of the eeprom and "Unlocked" has been displayed. Why the old password needs to be entered a second time?

You have used another variable h, so you have to clear it.

Code:
 if(h==4)
          {    pos=0;
               x=0;
               k=0;
               h=0;                                
               return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3]));    //save the password in 16 bit integer
          }
 
Last edited:

sir that just for privacy . thanks for advise i plan to avoid that ..

h==0; is the variable to check whether entered 4 times key pressed .(just changed variable pos to h - just for trying)
 

It is happening because of the code
Code:
 if(pos==4|| keyCode==2)
When you enter 5050 pos will be 4 and you have to clear it. input will be the value corresponding to 0 in 5050. so pos will be 1 because case for key value 0 will be executed. So, you have to clear input when you clear x, k, h, and pos.

Post the latest project files. I am getting nubere if I press 5. The keypad is giving wrong values.
 
Last edited:
sorry for the latency ..
I am attaching the eeprom and hex file for the proteus .please check it

- - - Updated - - -

sir if you can just check the code

my problem is it will run password= InputNumber("Enter Password"); first time correctly and ask password ,if enter correct passowrd , shown unlokced. there is no problem.

but when i press * that keycod is 2 (if(pos==2)) call the above said same function ie,password=InputNumber("Old Password"); here araise a problem i can onle enter the 3 digit (there is need to enter 4 digit ) if you can please help me thanks

i am submitting my code here

Code:
                                   main file
#include<avr/io.h>
#include<avr/eeprom.h>
#include<util/delay.h>
#include<inttypes.h>
#include "eeprom.h"
#include "lcd.h"
#include "user_interface.h"


int main(void)
{
    DDRB=0xFF;
    lcd_init();
        
        uint16_t password;
        uint8_t f;
        uint16_t newpassword;
        if(ReadPassFromEEPROM()==25755)
        {
            //Password is blank so store a default password
            WritePassToEEPROM(5050);
            _delay_ms(400);
        }
        
        LCD_clear;
        LINE1;
        dis_cmd(0x84);
        string("Drive");
      _delay_ms(200);    
    
    while(1){      
                    LCD_clear;
                    password=  InputNumber("Enter Password");//Get the keycode of pressed key
                    if(password==ReadPassFromEEPROM())
                       {
                          LCD_clear;
                          string(" Unlocked");
                          _delay_ms(100);
                          
                          while(1)     {
                         f=GetKeyPressed();
                           while(GetKeyPressed()==255)
                                 {
                               _delay_loop_2(10);
                                }
                            if (f==2){    
                                                                
                                password=InputNumber("Old Password");
                                _delay_ms(1000);
                                                                                        
                                if (password==ReadPassFromEEPROM())
                                                        
                                  {  
                                    newpassword=InputNumber("new");
                                    
                                    string("okok");
                                    WritePassToEEPROM(newpassword);
                                    _delay_ms(300);
                                }            
                                
                                
                            }
                                
                            }                  
                            }                                                                
                    
                  
                          else
                          {
                              LCD_clear;
                              string("Wrong Password");
                              _delay_ms(1000);
                          }
        
          }            
}




Code:
            interface/keypad convertor
#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>
#include "keypad.h"
#include "user_interface.h"
#include "lcd.h"

uint16_t InputNumber(char *msg)
{  
    uint8_t keyCode;
    uint8_t x=0;
    uint8_t pos=0;
    uint8_t prev_key=0xFF;
    uint8_t k=0xc1;
    int key[4];
    uint8_t h=0,l=0;
    dis_cmd(0x81);
    while(*msg) dis_data(*msg++);
     LINE2;
        while(1)
        {    
            
             if(h==4)
            {    pos=0;
                x=0;
                keyCode=0;
                k=0x80;
                _delay_ms(100);
                return(((1000*key[0])+(100*key[1])+(10*key[2])+key[3]));    //save the password in 16 bit integer
                h=0;
                break;
            }
            keyCode=GetKeyPressed();//Get the keycode of pressed key
            
            if(keyCode==prev_key)
            continue;
            else if(keyCode==0xFF)
            {
                prev_key=0xFF;
                continue;
            }
            else
            {
                prev_key=keyCode;
            }
                        
                          
                h++;    
            
            
              
            
switch (keyCode) //generating key characeter to display on LCD
{
    case (11):
    string("1");    
    dis_cmd(k++);
    key[x]=1;
    x++;
    pos++;
    break;
    
    
    case (10):
    //dis_cmd(k++);
    string("2");    
    dis_cmd(k++);
    key[x]=2;
    x++;
    pos++;
    break;
    
    case (9):
    string("3");    
    dis_cmd(k++);
    key[x]=3;
    x++;
    pos++;
    break;
    
    case (8):
    string("4");
    dis_cmd(k++);
    key[x]=4;
    x++;
    pos++;
    break;
    
    case (7):
    //dis_cmd(k++);
    string("5");
    dis_cmd(k++);
    key[x]=5;
    x++;
    pos++;
    break;
    
    case (6):
    string("6");
    dis_cmd(k++);
    key[x]=6;
    x++;
    pos++;
    break;
    
    case (5):
    string("7");
    dis_cmd(k++);
    key[x]=7;
    x++;
    pos++;
    break;
    
    case (4):
    string("8");
    dis_cmd(k++);
    key[x]=8;
    x++;
    pos++;
    break;
    
    case (3):
    string("9");
    dis_cmd(k++);
    key[x]=9;
    x++;
    pos++;
    break;
    
    case (2):
    //string("*");
    //dis_cmd(k++);
    //key[x]=0;
    //x++;
    //pos++;
    break;

    case (1):
    string("0");
    dis_cmd(k++);
    key[x]=0;
    x++;
    pos++;
    break;

    case (12):
    break;
    
    //default: ;

}

_delay_loop_2(300);

        }
        
        
    
    }

Code:
/*
 * keypad.h
 *
 * Created: 12/19/2012 10:49:06 PM
 *  Author: Gelectorn
 */


#ifndef KEYPAD_H_
#define KEYPAD_H_

#define KEYPAD_PORT PORTD
#define KEYPAD_DDR   DDRD
#define KEYPAD_PIN   PIND



uint8_t GetKeyPressed()
{
  uint8_t r,c;
  KEYPAD_PORT|= 0X0F;
  for(c=0;c<3;c++)
     {
        KEYPAD_DDR&=~(0X7F);
        KEYPAD_DDR|=(0X40>>c);
        for(r=0;r<4;r++)
           {
            if(!(KEYPAD_PIN & (0X08>>r)))
              {
                return (r*3+c);
              }
           }
     }
      return 0XFF;//Indicate No key pressed
}      



#endif /* KEYPAD_H_ */
 

Attachments

  • Debug.rar
    34.2 KB · Views: 63

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top