Thread: Need help

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If your friend is using a different version of Proteus, and your code works fine for them, then that really points to your simulator being broke.

    What's stopping you using the same version?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    May 2016
    Posts
    11
    nothing stopping me i'm using an earlier one that supports using on win. 10. also i didn't believe it's because of the version but i will give it a try. Thank you.

  3. #18
    Registered User
    Join Date
    May 2016
    Posts
    11
    i thought i have to share that i solved the problem. Unfortunately the UART pins had to be attached with actual wires, i was using a label for every pin which is working for everything but not the UART . thank you again salem i really appreciate your time. this is what i have added to the code so far.

    Door lock

    user interface micro-controller code
    Code:
     
    
    #include <avr/io.h>
    #include "BASICTYPES.h"
    #include <util/delay.h>
    #include "keypad.h"
    #include "LCD.h"
    #include "UART.h"
    
    
    #define CORRECT_PASSWORD  0X01
    #define WRONG_PASWORD     0X02
    #define newsetsucces   0x03
    #define reset              1
    #define open               2
    
    
    
    
    void send_password(void);
    void receive_status(void);
    void mode_rst_opn(void);
    
    
    u8 k=0,no_digits=0,ctr=0,cond,passw[4],dec;
    u8 flag;
    
    
    int main(void)
    {
        keypad_init();
        LCD_init();
        UART_Init();
        _delay_ms(100);
        
        
        while(1)
        {  
            LCD_command(0x01);
            LCD_print("  Enter password");
            send_password();
            receive_status();
            
            
           
        }
    }
    
    
    void send_password(void)
    {
        
        LCD_goto(9,2);
        no_digits=0; 
        while (no_digits!=4)
        { 
            k=keypad_read();
            _delay_ms(200);
            while (k!=0)
            {
                 passw[no_digits]=k;
                 LCD_data(k);
                 k=0;
                 no_digits++;    
        
            }
            
        }
        for (ctr=4;ctr>0;ctr--)
        {
            UART_SendByte(passw[4-ctr]);
            _delay_ms(100);
            
        }    
    }
    
    
    void receive_status(void)
    {
        cond=UART_ReciveData();
        switch (cond)
        {
            case CORRECT_PASSWORD :
            {
                LCD_command(0x01);
                LCD_goto(0,2);
                LCD_print(" Correct password");
                _delay_ms(500);
                 mode_rst_opn();
            }
            break;
            
            case WRONG_PASWORD :
            {
               LCD_command(0x01);
                LCD_goto(0,2);
                LCD_print(" Wrong password");
                _delay_ms(1000);
            }
            break;
        }
    }
    
    
    void mode_rst_opn(void)
    { 
        u8 user_deci=0,rst_status;
        LCD_command(0x01);
        LCD_print(" ON to open");
        LCD_goto(0,2);
        LCD_print(" '=' to reset");
        user_deci=keypad_read();
        _delay_ms(100);
        while (!user_deci);
        if (user_deci=='C')
        { 
            UART_SendByte(open);
            LCD_command(0x01);
            LCD_print(" door is open ");
            _delay_ms(1000);
            user_deci=0;
        }
        else if (user_deci=='=')
        {   user_deci=0;
            UART_SendByte(reset);
            LCD_command(0x01);
            LCD_print("please enter  ");
            LCD_goto(0,2);
            LCD_print(" new pass  ");
            send_password();        
            LCD_command(0x01);
            LCD_print("enter password ");
            LCD_goto(0,2);
            LCD_print(" again ");
            send_password();
            rst_status=UART_ReciveData();
            if (rst_status==newsetsucces)
            {
                LCD_command(0x01);
                LCD_print("password matched");
                _delay_ms(1000);
            }
            else
            {
                LCD_command(0x01);
                LCD_print("not matched pass");
                _delay_ms(1000);
                
             }
        }
    
    
    }
    inside room micro-controller
    Code:
    
    #include <avr/io.h>
    #include "UART.h"
    #include "BASICTYPES.h"
    #include "EEPROM_Driver.h"
    #include <util/delay.h>
    
    
    
    
    #define CORRECT_PASSWORD  0X01
    #define WRONG_PASWORD     0X02
    #define newsetsucces   0x03
    #define setagain    0x04
    #define reset              1
    #define open               2
    
    
    void mode_rst_opn(void);
    void stor_pass_eeprom(void);
    void compare_passes(void);
    u8 cmpr_new_pass(void);
    
    
    u8 password[4]="5555";
    u8 flag,gflag;
    
    
    int main(void)
    {
        UART_Init();
        
    if (gflag==0)
    {
        stor_pass_eeprom();
        gflag=1;
    }
       while ( 1 )
       {
            flag=0;
            
         compare_passes();
         if(flag==5)
         {
             mode_rst_opn();
         }
         
       }
    }
    
    
    
    
    u8 cmpr_new_pass(void)
    {
         u8 frst_pass[4], scndpass[4],dec,z;
          
         for (z=0;z<4;z++)
         {
             frst_pass[z]=UART_ReciveData();
             _delay_ms(50);
         } 
         for (z=0;z<4;z++)
         {
              scndpass[z]=UART_ReciveData();
              _delay_ms(50);
         }
         if ((frst_pass[0]==scndpass[0])&&(frst_pass[1]==scndpass[1])&&(frst_pass[2]==scndpass[2])&&(frst_pass[3]==scndpass[3]))
         {
               dec=1;
              for (z=0;z<4;z++)
              {
                  password[z]=frst_pass[z];
              }
             
              stor_pass_eeprom();
          }
          else
          {
                dec=2;
          }
          
          return dec;
        
    }
    
    
    
    
    void stor_pass_eeprom(void)
    { 
       u8 ctr;
       for (ctr=0;ctr<4;ctr++)
        {
          EEPROM_Write(ctr,password[ctr]);    
        }
    }
    
    
     
     
     void mode_rst_opn(void)
     {
         u8 mod,cmpr_status;
         mod=UART_ReciveData();
         if (mod == open)
         {
             
             
         }
         else if(mod==reset)
         {
            cmpr_status=cmpr_new_pass();
             if (cmpr_status==1)
             {
                 UART_SendByte(newsetsucces);
             }
             else if (cmpr_status==2)
             {
                 UART_SendByte(setagain);
             }
             
         }
         
         
         
         
     }
     
     
    void compare_passes(void)
    {
      u8 rx_pass[4],z,k=0;
        
        for (z=0;z<4;z++)
        {
            
             rx_pass[z]=UART_ReciveData();
             _delay_ms(50);
        } 
        
        for (z=0;z<4;z++)
        {
        
             password[z]=EEPROM_Read(z);
             _delay_ms(50);
         }
            
            
        
        
        
            if((password[0]==rx_pass[0])&&(password[1]==rx_pass[1])&&(password[2]==rx_pass[2])&&(password[3]==rx_pass[3])) k=1;    
        
        
               if (k==1)
                  {
                        UART_SendByte(CORRECT_PASSWORD);
                        flag=5;
                  }
        
                else if (k==0)
                   {
                           UART_SendByte(WRONG_PASWORD);
                           flag=4;
                   }
                   
        
    }
    Need help-project-jpg
    i'm still stuck with having a default password 5555 every time you turn on the device. you can change the pass when your using the device but if turned off returns back to 5555.

  4. #19
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Ahmed El-Wakil View Post
    i'm still stuck with having a default password 5555 every time you turn on the device. you can change the pass when your using the device but if turned off returns back to 5555.
    Carefully step through the second program by hand.

    Line 28: "main" starts
    Line 32: "gflag" is checked - this value is initialized to zero, and not updated up this point, so this check will always be true. Therefore...
    Line 34: "password" is written to EEPROM. Since it has not been updated, the initialized value of "5555" is written to memory.

    So basically, every time the program starts, memory is overwritten with the default password value.

    This assumes that "gflag" is not updated in the "UART_Init()" function. Since a UART initialization function should not be updating an unrelated variable, this is likely a correct assumption.

  5. #20
    Registered User
    Join Date
    May 2016
    Posts
    11
    This assumes that "gflag" is not updated in the "UART_Init()" function. Since a UART initialization function should not be updating an unrelated variable, this is likely a correct assumption.
    i'm sorry English is not my first lang. so i didn't really get what you mean please explain if possible. Also the "UART_Init()" is not updating the gflag it doesn't even know it's exist . it was a desperate try in order to store the default password just once and then use but it didn't work. i think because the gflag starts as zero too because it a global variable.

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Ahmed El-Wakil View Post
    i think because the gflag starts as zero too because it a global variable.
    Correct. Therefore, every time the program is executed, "gflag" is initialized to zero, and as a result, the first "if()" overwrites whatever was saved as "password" in the EEPROM.

    You need to figure out a way to determine if a password has been previously saved or not. (Hint: you can use another EEPROM byte to represent a "data has been saved" flag.)

    If a password has been previously saved, load that value and update the "password" array. Otherwise, you can just work with the default value that the "password" array is initialized with.

  7. #22
    Registered User
    Join Date
    May 2016
    Posts
    11
    i used this but it didn't work. isn't this what you meant?. i think that the eeprom itself is not keeping it's value. btw i removed the initialization of the password[4] array and the only case that stores the array values in eeprom is when i reset the password
    Code:
    if (EEPROM_Read(4)!=1)
    {
    	EEPROM_Write(0,'5');
    	EEPROM_Write(1,'5');
    	EEPROM_Write(2,'5');
    	EEPROM_Write(3,'5');
    	EEPROM_Write(4,1);
    }

  8. #23
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I was actually talking more along the lines of:

    Code:
    [address + 0] --> Password byte 0
    [address + 1] --> Password byte 1
    [address + 2] --> Password byte 2
    [address + 3] --> Password byte 3
    [address + 4] --> Set to a specific value (e.g. 0x01) when a password is saved
    Code:
    initialize password[0:3] to default value
    
    if [address + 4] == 0x01
        load [address + 0:3] into password[0:3]
    
    Quote Originally Posted by Ahmed El-Wakil View Post
    i think that the eeprom itself is not keeping it's value.
    I cannot answer to this, as you have not provided the implementation of the EEPROM functions.

Popular pages Recent additions subscribe to a feed

Tags for this Thread