1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

error embedded c code

Discussion in '8051/8951' started by bhavyam1234, Apr 9, 2014.

  1. bhavyam1234

    bhavyam1234 New Member

    Joined:
    Apr 9, 2014
    Messages:
    1
    Likes:
    0
    In the embedded c code below it is written for password checking. This code does not give syntax error but for every correct password also is displays "pass is wrong".

    Code (c):

    #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];
        int 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 a moderator: Apr 10, 2014
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    If you need help Please repost your code with CODE tags It's the little gear icon in the toolbar..
     
  3. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Write a debug code that displays the current password stored in memory.. check if that is correct.
     

Share This Page