I have problem in embedded c coding for digital password lock it gives the message "p

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhavya1234
    New Member
    • Apr 2014
    • 3

    #1

    I have problem in embedded c coding for digital password lock it gives the message "p

    Code:
    #include <AT89X51.H> 
    #define EE_SCL P1_0 
    #define EE_SDA P1_1 
    #define LCD_E P1_7 
    #define LCD_RW P1_6 
    #define LCD_RS P1_5 
    #define LCD_DATA P3 
    #define buzz P0_3 
    #define led P1_2 
    void cmnwrt(int); 
    void datawrt(char); 
    char keypad_refresh(); 
    void init(); 
    void delay(unsigned i); 
    void get_sntnc(char *); 
    void print_msg(char*,char); 
    void EE_write(unsigned int, char); 
    char EE_read(unsigned int); 
    void EE_strt(); 
    void EE_stop(); 
    char EE_shin(); 
    void EE_shout(char); 
    //---------------------------------------- 
    //---------------------------------------- 
    int main(){ 
    char tmp[17],i,j; 
    init(); 
    cmnwrt(0x80); 
    led=1; 
    while(1){ 
    if(EE_read(0)!='p'||EE_read(1)!='='){ 
    np: 
    cmnwrt(1); 
    print_msg("Enter new pass:\0",0x80); 
    get_sntnc(tmp); 
    EE_write(0,'p'); 
    EE_write(1,'='); 
    for(i=0;tmp[i];i++) 
    EE_write(i+2,tmp[i]); 
    EE_write(i+2,0); 
    } 
    rpa: 
    cmnwrt(1); 
    print_msg("Enter the pass:\0",0x80); 
    get_sntnc(tmp); 
    for(i=0,j=0;;i++){ 
    if(tmp[i]!=EE_read(i+2)){ 
    j=-1; 
    break; 
    } 
    if(!tmp[i]) 
    break; 
    } 
    cmnwrt(1); 
    if(j==-1) 
    { 
    print_msg("Pass is wrong!\0",0x80); 
    led=1; 
    buzz=1; 
    delay(5000); 
    buzz=0; 
    goto rpa; 
    } 
    else 
    { 
    print_msg("Pass is right!\0",0x80); 
    led=0; 
    buzz=0; 
    delay(5000); 
    led=1; 
    } 
    cmnwrt(1); 
    print_msg("*=change pass\0",0x80); 
    print_msg("#=back\0",0xc0); 
    ka: 
    do 
    i=keypad_refresh(); 
    while(i==-1); 
    if(i==0x3a) 
    goto np; 
    else if(i==0x3c) 
    goto rpa; 
    else 
    goto ka; 
    } 
    } 
    //------------------------------------ 
    //------------------------------------ 
    void print_msg(char *msg, char line){ 
    char i; 
    cmnwrt(line); 
    for(i=0;*(msg+i);i++) 
    datawrt(*(msg+i)); 
    } 
    //------------------------------------ 
    //------------------------------------ 
    char keypad_refresh(){ 
    char tmp,row,cl; 
    P2 = 0xf0; 
    if(!P2_4) 
    row = 0; 
    else if(!P2_5) 
    row = 1; 
    else if(!P2_6) 
    row = 2; 
    else if(!P2_7) 
    row = 3; 
    else 
    return -1; 
    P2 = 0xff; 
    P2 = 0x0f; 
    if(!P2_2) 
    cl = 0; 
    else if(!P2_1) 
    cl = 1; 
    else if(!P2_0) 
    cl = 2; 
    else 
    return -1; 
    tmp = row*3+cl+1; 
    if(tmp == 11) 
    tmp = 0; 
    return tmp|0x30; 
    } 
    //-------------------------------------------------- 
    //-------------------------------------------------- 
    void get_sntnc(char *str) { 
    char i,j,z; 
    for(i=0;i<17;i++) 
    *(str+i)=0; 
    i=0; 
    while(1){ 
    cmnwrt(0xc0); 
    for(z=0;z<16;z++) 
    if(*(str+z)) 
    datawrt(*(str+z)); 
    else 
    datawrt(' '); 
    delay(70); 
    do 
    j = keypad_refresh(); 
    while(j==-1); 
    if(j==0x3c){ 
    *(str+i)=0; 
    break; 
    }
    else if(j==0x3a){ 
    if(i>0){ 
    i--; 
    *(str+i)=0; 
    } 
    }
    else{ 
    if(i<=15){ 
    *(str+i)=j; 
    i++; 
    } 
    } 
    } 
    } 
    //-------------------------------------------------- 
    //-------------------------------------------------- 
    void init() { 
    int i; 
    P0=P1=P2=P3=0; 
    //initialize LCD 
    LCD_RS = 0; 
    LCD_RW = 0; 
    delay(30); 
    cmnwrt(0x3f); 
    delay(20); 
    cmnwrt(0x3f); 
    delay(10); 
    cmnwrt(0x3f); 
    delay(5); 
    cmnwrt(0x38); 
    cmnwrt(0x01); 
    cmnwrt(0x0c); 
    //initialize EEPROM 
    EE_SDA = 1; 
    for(i=0;i<10;i++) { 
    EE_SCL = 1; 
    i=i; //delay 
    EE_SCL = 0; 
    } 
    } 
    //-------------------------------------------------- 
    //-------------------------------------------------- 
    void delay(unsigned i) { 
    TMOD = 1; 
    for (;i>0;i--) { 
    TH0 = 0xFc; //0xFFFF - 0xFc17 = 1000 (decimal) -> 1ms for 12MH crystal 
    TL0 = 0x17; 
    TR0 = 1; 
    while(!TF0); 
    TR0 = 0; 
    TF0 = 0; 
    } 
    } 
    //-------------------------------------------------- 
    //-------------------------------------------------- 
    void cmnwrt(int cmn) { 
    LCD_DATA = cmn; 
    LCD_RS = 0; 
    LCD_RW = 0;
    LCD_E = 1; 
    LCD_E = 0; 
    delay(2); 
    } 
    void datawrt(char ch) { 
    LCD_DATA = ch; 
    LCD_RS = 1; 
    LCD_RW = 0; 
    LCD_E = 1; 
    LCD_E = 0; 
    delay(2); 
    } 
    //--------------------------------------------------- 
    //--------------------------------------------------- 
    //Write into EEPROM 
    void EE_write(unsigned int addr, char dta){ 
    char chtmp; 
    CY = 0; 
    EE_strt(); 
    EE_shout(0xA0); 
    chtmp=addr>>8; 
    EE_shout(chtmp); 
    chtmp=addr; 
    EE_shout(chtmp); 
    EE_shout(dta); 
    EE_stop(); 
    delay(13); 
    } 
    //Reading from EEPROM 
    char EE_read (unsigned int addr) { 
    char chtmp; 
    CY=0; 
    EE_strt(); 
    EE_shout(0xA0); 
    chtmp=addr>>8; 
    EE_shout(chtmp); 
    chtmp=addr; 
    EE_shout(chtmp); 
    EE_strt(); 
    EE_shout(0xA1); 
    chtmp=EE_shin(); 
    EE_stop(); 
    return chtmp; 
    } 
    //Start 
    void EE_strt() { 
    EE_SDA=1; 
    EE_SCL=1; 
    EE_SDA=0; 
    EE_SCL=0; 
    } 
    //Stop 
    void EE_stop() { 
    EE_SCL=0; 
    EE_SDA=0; 
    EE_SCL=1; 
    EE_SDA=1; 
    } 
    //Shift out 
    void EE_shout(char sho) { 
    unsigned char i,j; 
    for(j=0,i=0;i<8;i++) { 
    j/=2; 
    if(!j) 
    j=128; 
    if(sho&j) 
    CY=1; 
    else
    CY=0; 
    EE_SDA=CY; 
    EE_SCL=1; 
    j=j; //delay 
    EE_SCL=0; 
    } 
    EE_SDA=1; 
    EE_SCL=1; 
    j=j; //delay 
    CY=EE_SDA; 
    EE_SCL=0; 
    } 
    //Shift in 
    char EE_shin() { 
    char i,shi = 0; 
    EE_SDA = 1; 
    for(i=0;i<8;i++) { 
    CY=0; 
    shi<<=1; 
    EE_SCL=1; 
    i=i; 
    CY=EE_SDA; 
    EE_SCL=0; 
    if(CY) 
    shi++; 
    } 
    EE_SDA=1; 
    EE_SCL=1; 
    i=i; //delay 
    EE_SCL=0; 
    return shi; 
    }
    Last edited by weaknessforcats; Apr 7 '14, 05:35 AM. Reason: added code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    May be you could explain a little further. The error occurs when the code runs, when it links, or when it compiles?

    What is the code supposed to do? There are no comments whatever.

    I suggest that you step through this code using your debugger. Since you know what you are looking for, and I don't, you doing the debugging will get results instead of me.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Also, what is the error message?
      (Only the first letter appears in the title of your post.)

      Comment

      • bhavya1234
        New Member
        • Apr 2014
        • 3

        #4
        there are no syntax errors.
        The program stores the newly entered password and must display the message" pass is right" when the password again entered is correct but the program always displays "pass is worng" message only.

        Comment

        • bhavya1234
          New Member
          • Apr 2014
          • 3

          #5
          After much debugging only i have posted here.
          code must check for the password with the previously entered But the code only displays "pass is wrong" for every correct password also.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Your code to make the decision if the password is right or wrong is:

            Code:
            if (j == -1)...
            -1 is an int an j is a char.

            Start by making j an int.

            Comment

            Working...