This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

problem in reading and sending data in eeprom using 8051(code given below)

//Program to display String on LCD

//Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)
#include<reg51.h>
#include<intrins.h> //For using [_nop_()]

sbit sda=P1^0;
sbit scl=P1^1;
sbit led=P0^1;
bit ack;
sbit led1=P0^2;
sbit sav=P0^4;
sbit up=P0^5;
sbit down=P0^6;
sfr lcd_data_pin=0xA0;//p2 port
sfr output=0x80;//p0 port
sbit rs=P3^1;

sbit en=P3^0;
unsigned char reead,write;
unsigned int temp,new_value1,new_value,test=0,num=0,i,j;
void init() // to initialize the output pins and Timer0
{ up=down=1;

TMOD=0x01;
TL0=0xf6;
TH0=0xFf;
IE=0x82;
TR0=1;
} void delay(unsigned int count)
{ int i,j; for(i=0;i<count;i++) for(j=0;j<1275;j++);
} void lcd_command(unsigned char comm)
{ lcd_data_pin=comm; en=1; rs=0; delay(1); en=0;
} void lcd_data(unsigned char disp)
{ lcd_data_pin=disp; en=1; rs=1; delay(1); en=0;
} lcd_strng(unsigned char *disp)
{ int x; for(x=0;disp[x]!=0;x++) { lcd_data(disp[x]); }
} void lcd_ini()
{ lcd_command(0x38); // for using 8-bit 2 row LCD delay(5); lcd_command(0x0F); // for display on, cursor blinking delay(5); lcd_command(0x80); delay(5);
} void aknowledge() //acknowledge condition
{ scl=1; _nop_(); _nop_(); scl=0;
} void start() //start condition
{ sda=1; scl=1; _nop_(); //No operation _nop_(); sda=0; scl=0;
} void stop() //stop condition
{ sda=0; scl=1; _nop_(); _nop_(); sda=1; scl=0;
} void send_byte(unsigned int value) //send byte serially
{

unsigned int i; unsigned int send;

send=value; for(i=0;i<8;i++) { sda=send/128; //extracting MSB send=send<<1; //shiftng left scl=1; _nop_(); _nop_(); scl=0; } ack=sda; //reading acknowledge sda=0;
} unsigned char read_byte() //reading from EEPROM serially
{ unsigned int i; sda=1; reead=0; for(i=0;i<8;i++) { reead=reead<<1; scl=1; _nop_(); _nop_(); if(sda==1) reead++; scl=0; } sda=0; return reead; //Returns 8 bit data here
} void save() //save in EEPROM
{ init(); while(1) { if(up==0&&down==1) //check if up pin is pressed { test++; num=test; new_value=num%10; num=num/10; if(test==9) test=0;

} if(up==1&&down==0) //check if down pin is pressed { test--; num=test; new_value=num%10; num=num/10; if(test==0) test=9;

}

start(); send_byte(0xA0); //device address aknowledge(); send_byte(0x00); //word address aknowledge(); send_byte(new_value); //send data aknowledge(); stop(); if(ack==0) { /* new_value1=new_value1+48; */ led1=1; delay(100); led1=0; delay(100); lcd_command(0x86); lcd_data(new_value);

} else led1=1; aknowledge();
}

}

void Read()
{ start(); send_byte(0xA0); aknowledge(); send_byte(0x00); aknowledge(); start(); send_byte(0xA1); //device address aknowledge(); i=read_byte(); aknowledge(); stop(); if(i==new_value) { led=0; delay(100); led=1; delay(100); write=i+48; lcd_command(0xC6); lcd_data(write); } else led=1;

aknowledge();
}

void main()
{ lcd_ini();

lcd_strng("Sent :"); lcd_command(0xC0); lcd_strng("Read :"); temp=0; while(1) { save(); Read(); } }

Parents
  • sir. very sorry with the bad messages. here is very good. please kindest help now.

    //Program to display String on LCD
    
    //Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)
    #include<reg51.h>
    #include<intrins.h> //For using [_nop_()]
    
    sbit sda=P1^0;
    sbit scl=P1^1;
    sbit led=P0^1;
    bit ack;
    sbit led1=P0^2;
    sbit sav=P0^4;
    sbit up=P0^5;
    sbit down=P0^6;
    sfr lcd_data_pin=0xA0;//p2 port
    sfr output=0x80;//p0 port
    sbit rs=P3^1;
    
    sbit en=P3^0;
    unsigned char reead,
    write;
    unsigned int temp,
    new_value1,
    new_value,
    test=0,
    num=0,
    i,
    j;
    
    void init() // to initialize the output pins and Timer0
    {
     up=down=1;
    
    TMOD=0x01;
    TL0=0xf6;
    TH0=0xFf;
    IE=0x82;
    TR0=1;
    }
     void delay(unsigned int count)
    {
     int i,
    j;
    for(i=0;i<count;i++)
    for(j=0;j<1275;j++);
    }
    void lcd_command(unsigned char comm)
    {
    lcd_data_pin=comm;
    en=1;
    rs=0;
    delay(1);
    en=0;
    }
    void lcd_data(unsigned char disp)
    {
    lcd_data_pin=disp;
    en=1;
    rs=1;
    delay(1);
    en=0;
    }
    lcd_strng(unsigned char *disp)
    {
    int x;
    for(x=0;disp[x]!=0;x++)
    {
    lcd_data(disp[x])
    ;
    }
    }
    void lcd_ini()
    {
    lcd_command(0x38); // for using 8-bit 2 row LCD
    delay(5);
    lcd_command(0x0F); // for display on, cursor blinking
    delay(5);
    lcd_command(0x80);
    delay(5);
    }
    void aknowledge() //acknowledge condition
    {
    scl=1;
    _nop_();
    _nop_();
    scl=0;
    }
    void start() //start condition
    {
    sda=1;
    scl=1;
    _nop_(); //No operation
    _nop_();
    sda=0;
    scl=0;
    }
    void stop() //stop condition
    {
    sda=0;
    scl=1;
    _nop_();
    _nop_();
    sda=1;
    scl=0;
    }
    void send_byte(unsigned int value) //send byte serially
    {
    unsigned int i;
    unsigned int send;
    
    send=value;
    for(i=0;i<8;i++)
    {
    sda=send/128; //extracting MSB
    send=send<<1; //shiftng left
    scl=1;
    _nop_();
    _nop_();
    scl=0;
    }
    ack=sda; //reading acknowledge
    sda=0;
    }
    unsigned char read_byte() //reading from EEPROM serially
    {
    unsigned int i;
    sda=1;
    reead=0;
    for(i=0;i<8;i++)
    {
    reead=reead<<1;
    scl=1;
    _nop_();
    _nop_();
    if(sda==1)
    reead++;
    scl=0;
    }
    sda=0;
    return reead; //Returns 8 bit data here
    }
    void save() //save in EEPROM
    {
    init();
    while(1)
    {
    if(up==0&&down==1) //check if up pin is pressed
    {
    test++;
    num=test;
    new_value=num%10;
    num=num/10;
    if(test==9)
    test=0;
    }
    if(up==1&&down==0) //
    check if down pin is pressed
    {
    test--;
    num=test;
    new_value=num%10;
    num=num/10;
    if(test==0)
    test=9;
    }
    start();
    send_byte(0xA0);
    //device address
    aknowledge();
    send_byte(0x00); //word
             address
    aknowledge();
    send_byte(new_value); //send data
    aknowledge();
    stop();
    if(ack==0)
    { /*
    new_value1=new_value1+48; */
    led1=1;
    delay(100);
    led1=0;
    delay(100);
    lcd_command(0x86);
    lcd_data(new_value);
    }
    else
    led1=1;
    aknowledge();
    }
    }
    void Read()
    {
    start();
    send_byte(0xA0);
    aknowledge();
    send_byte(0x00);
    aknowledge();
    start();
    send_byte(0xA1); //
    device address
    aknowledge();
    i=read_byte();
    aknowledge();
    stop();
    if(i==new_value)
    {
    led=0;
    delay(100);
    led=1;
    delay(100);
    write=i
    +48;
    lcd_command(0xC6);
    lcd_data(write);
    }
    else
    led
    =
    1;
    aknowledge();
    }
    
    void main()
    {
    lcd_ini();
    
    lcd_strng("Sent :")
    ;
    lcd_command(0xC0)
    ;
    lcd_strng("Read :")
    ;
    temp=0
    ;
    while
    (1)
    {
    save()
    ;
    Read()
    ;
    }
    }
    
    
    

Reply
  • sir. very sorry with the bad messages. here is very good. please kindest help now.

    //Program to display String on LCD
    
    //Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)
    #include<reg51.h>
    #include<intrins.h> //For using [_nop_()]
    
    sbit sda=P1^0;
    sbit scl=P1^1;
    sbit led=P0^1;
    bit ack;
    sbit led1=P0^2;
    sbit sav=P0^4;
    sbit up=P0^5;
    sbit down=P0^6;
    sfr lcd_data_pin=0xA0;//p2 port
    sfr output=0x80;//p0 port
    sbit rs=P3^1;
    
    sbit en=P3^0;
    unsigned char reead,
    write;
    unsigned int temp,
    new_value1,
    new_value,
    test=0,
    num=0,
    i,
    j;
    
    void init() // to initialize the output pins and Timer0
    {
     up=down=1;
    
    TMOD=0x01;
    TL0=0xf6;
    TH0=0xFf;
    IE=0x82;
    TR0=1;
    }
     void delay(unsigned int count)
    {
     int i,
    j;
    for(i=0;i<count;i++)
    for(j=0;j<1275;j++);
    }
    void lcd_command(unsigned char comm)
    {
    lcd_data_pin=comm;
    en=1;
    rs=0;
    delay(1);
    en=0;
    }
    void lcd_data(unsigned char disp)
    {
    lcd_data_pin=disp;
    en=1;
    rs=1;
    delay(1);
    en=0;
    }
    lcd_strng(unsigned char *disp)
    {
    int x;
    for(x=0;disp[x]!=0;x++)
    {
    lcd_data(disp[x])
    ;
    }
    }
    void lcd_ini()
    {
    lcd_command(0x38); // for using 8-bit 2 row LCD
    delay(5);
    lcd_command(0x0F); // for display on, cursor blinking
    delay(5);
    lcd_command(0x80);
    delay(5);
    }
    void aknowledge() //acknowledge condition
    {
    scl=1;
    _nop_();
    _nop_();
    scl=0;
    }
    void start() //start condition
    {
    sda=1;
    scl=1;
    _nop_(); //No operation
    _nop_();
    sda=0;
    scl=0;
    }
    void stop() //stop condition
    {
    sda=0;
    scl=1;
    _nop_();
    _nop_();
    sda=1;
    scl=0;
    }
    void send_byte(unsigned int value) //send byte serially
    {
    unsigned int i;
    unsigned int send;
    
    send=value;
    for(i=0;i<8;i++)
    {
    sda=send/128; //extracting MSB
    send=send<<1; //shiftng left
    scl=1;
    _nop_();
    _nop_();
    scl=0;
    }
    ack=sda; //reading acknowledge
    sda=0;
    }
    unsigned char read_byte() //reading from EEPROM serially
    {
    unsigned int i;
    sda=1;
    reead=0;
    for(i=0;i<8;i++)
    {
    reead=reead<<1;
    scl=1;
    _nop_();
    _nop_();
    if(sda==1)
    reead++;
    scl=0;
    }
    sda=0;
    return reead; //Returns 8 bit data here
    }
    void save() //save in EEPROM
    {
    init();
    while(1)
    {
    if(up==0&&down==1) //check if up pin is pressed
    {
    test++;
    num=test;
    new_value=num%10;
    num=num/10;
    if(test==9)
    test=0;
    }
    if(up==1&&down==0) //
    check if down pin is pressed
    {
    test--;
    num=test;
    new_value=num%10;
    num=num/10;
    if(test==0)
    test=9;
    }
    start();
    send_byte(0xA0);
    //device address
    aknowledge();
    send_byte(0x00); //word
             address
    aknowledge();
    send_byte(new_value); //send data
    aknowledge();
    stop();
    if(ack==0)
    { /*
    new_value1=new_value1+48; */
    led1=1;
    delay(100);
    led1=0;
    delay(100);
    lcd_command(0x86);
    lcd_data(new_value);
    }
    else
    led1=1;
    aknowledge();
    }
    }
    void Read()
    {
    start();
    send_byte(0xA0);
    aknowledge();
    send_byte(0x00);
    aknowledge();
    start();
    send_byte(0xA1); //
    device address
    aknowledge();
    i=read_byte();
    aknowledge();
    stop();
    if(i==new_value)
    {
    led=0;
    delay(100);
    led=1;
    delay(100);
    write=i
    +48;
    lcd_command(0xC6);
    lcd_data(write);
    }
    else
    led
    =
    1;
    aknowledge();
    }
    
    void main()
    {
    lcd_ini();
    
    lcd_strng("Sent :")
    ;
    lcd_command(0xC0)
    ;
    lcd_strng("Read :")
    ;
    temp=0
    ;
    while
    (1)
    {
    save()
    ;
    Read()
    ;
    }
    }
    
    
    

Children
  • I wonder why my code looks like:

    void main(void) {
        init();
        for (;;) {
            led_1_on();
            delay_ms(500);
            led_1_off();
            delay_ms(500);
        }
    }
    


    and your code looks like:

    void main(void) {
    init();
    for (;;) {
    led_1_on();
    delay_ms(500);
    led_1_off();
    delay_ms(500);
    }
    }
    

    Did you post with tabs? Did you, in that case, see any setting to specify what tab settings
    your post was expected to have? Didn't you see something strange in the preview, before you
    pressed Post?

    Even more interesting - how come your code looks like:

    led
    =
    1;
    aknowledge();
    }
    


    What web site did you find that recommended having an assign split over three lines?

    or:

    lcd_strng("Sent :")
    ;
    lcd_command(0xC0)
    ;
    lcd_strng("Read :")
    ;
    


    Since when was it made popular to have the ';' statement termination on a separate line?

    Is this how the source code looks in your own editor? Assigns over three lines, and the
    semicolon sometimes on separate lines?

    I don't believe you. Because your latest post contains the following:

    send_byte(0x00); //word
             address
    aknowledge();
    

    Notice that your // comment got split to two lines - the compiler can't accept that "address"
    word. So you have once more reposted code with already broken indentations, despite me
    specifically saying that it is a bad idea. You have, once more, posted code that can't be
    compiled.

  • here is very good.

    You have to be kidding. You've now copy-pasted your code into this forum four times. So you've had four preview opportunities, by minimum. And you still can't be bothered to actually look at the preview to check if it matches what your code is supposed to look like, before you hit "Post"?

    Just in case you really don't get it: no, that that is not "very good". That is, at best, a tiny bit less bad than your previous 3 attempts.

    Keep in mind that it's you who expects help for free here --- so the least you can do to earn it would be to take care how you present your case. So far you've failed spectacularly at that, four times in a row. Time to get your act together.

  • sir. you complain. and complain. and complain again. how can it be easier. can you help. you just pretend. yes. YES. can anyone good give help?

  • And the answer is that whatever we do, we can't even get you to understand that you can't just
    take the broken code from the first post and repost - that the formatting have been forever lost.

    If we can't even get you to understand that you must go all the way back to the code available in
    the editor, then it isn't likely that we can be able to discuss actual source code with you
    either. So why should we even try?

  • sir. you do not understand. sir i ask you not to reply. is there a good person who will answer. my chief always answered well here but he says you always fight his sense and you were jealous because he was so good. i now understand him.

  • you complain. and complain. and complain again.

    Because you get it wrong. And get it wrong. And get it wrong again.

    can you help.

    You're doing everything you possibly could to make sure nobody would want to help you. You've been told so a couple of times, and yet you persist behaving like a 5-year-old.

    I'm done with you. Good bye.

  • instead of stamping your little feet in the floor and crying "help me" why not post properly formatted code and HOPE you have not pissed everybody off by your "I post crap and want you to spend your spare time to decipher it".

    Erik

  • Is this what it's supposed to look like?

    I spent 2 minutes to reformat this - because I wanted an answer to the question (maybe)... I wasted more time reading the argument (shame on BOTH of you - stupid poster didn't bother to take the 2 minutes to reformat correctly, and stupid arrogant answer wasted all our time chastising someone - instead of taking 2 minutes to reformat. --- when they should have remembered, that the information on the forums is not only for the original asker, but for anyone searching for answers to the same questions. for shame and a sad statement.

    and if the tabs don't look right, sorry they look right in the preview for me.

    //Program to display String on LCD
    
    //Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)
    #include<reg51.h>
    #include<intrins.h> //For using [_nop_()]
    
    sbit sda=P1^0;
    sbit scl=P1^1;
    sbit led=P0^1;
    bit ack;
    sbit led1=P0^2;
    sbit sav=P0^4;
    sbit up=P0^5;
    sbit down=P0^6;
    sfr lcd_data_pin=0xA0;//p2 port
    sfr output=0x80;//p0 port
    sbit rs=P3^1;
    
    sbit en=P3^0;
    unsigned char reead, write;
    unsigned int temp, new_value1, new_value, test=0, num=0, i, j;
    
    void init() // to initialize the output pins and Timer0
    {
            up=down=1;
    
            TMOD=0x01;
            TL0=0xf6;
            TH0=0xFf;
            IE=0x82;
            TR0=1;
    }
    void delay(unsigned int count)
    {
            int i, j;
            for(i=0;i<count;i++)
                    for(j=0;j<1275;j++);
    }
    void lcd_command(unsigned char comm)
    {
            lcd_data_pin=comm;
            en=1;
            rs=0;
            delay(1);
            en=0;
    }
    void lcd_data(unsigned char disp)
    {
            lcd_data_pin=disp;
            en=1;
            rs=1;
            delay(1);
            en=0;
    }
    lcd_strng(unsigned char *disp)
    {
            int x;
            for(x=0;disp[x]!=0;x++)
            {
                    lcd_data(disp[x]);
            }
    }
    void lcd_ini()
    {
            lcd_command(0x38); // for using 8-bit 2 row LCD
            delay(5);
            lcd_command(0x0F); // for display on, cursor blinking
            delay(5);
            lcd_command(0x80);
            delay(5);
    }
    void aknowledge() //acknowledge condition
    {
            scl=1;
            _nop_();
            _nop_();
            scl=0;
    }
    void start() //start condition
    {
            sda=1;
            scl=1;
            _nop_(); //No operation
            _nop_();
            sda=0;
            scl=0;
    }
    void stop() //stop condition
    {
            sda=0;
            scl=1;
            _nop_();
            _nop_();
            sda=1;
            scl=0;
    }
    void send_byte(unsigned int value) //send byte serially
    {
            unsigned int i;
            unsigned int send;
    
            send=value;
            for(i=0;i<8;i++)
            {
                    sda=send/128; //extracting MSB
                    send=send<<1; //shiftng left
                    scl=1;
                    _nop_();
                    _nop_();
                    scl=0;
            }
            ack=sda; //reading acknowledge
            sda=0;
    }
    unsigned char read_byte() //reading from EEPROM serially
    {
            unsigned int i;
            sda=1;
            reead=0;
            for(i=0;i<8;i++)
            {
                    reead=reead<<1;
                    scl=1;
                    _nop_();
                    _nop_();
                    if(sda==1)
                            reead++;
                    scl=0;
            }
            sda=0;
            return reead; //Returns 8 bit data here
    }
    void save() //save in EEPROM
    {
            init();
            while(1)
            {
                    if(up==0&&down==1) //check if up pin is pressed
                    {
                            test++;
                            num=test;
                            new_value=num%10;
                            num=num/10;
                            if(test==9)
                                    test=0;
                    }
                    if(up==1&&down==0) //
                            check if down pin is pressed
                    {
                            test--;
                            num=test;
                            new_value=num%10;
                            num=num/10;
                            if(test==0)
                                    test=9;
                    }
                    start();
                    send_byte(0xA0);
                    //device address
                    aknowledge();
                    send_byte(0x00); //word
                    address
                            aknowledge();
                    send_byte(new_value); //send data
                    aknowledge();
                    stop();
                    if(ack==0)
                    { /*
                            new_value1=new_value1+48; */
                            led1=1;
                            delay(100);
                            led1=0;
                            delay(100);
                            lcd_command(0x86);
                            lcd_data(new_value);
                    }
                    else
                            led1=1;
                    aknowledge();
            }
    }
    void Read()
    {
            start();
            send_byte(0xA0);
            aknowledge();
            send_byte(0x00);
            aknowledge();
            start();
            send_byte(0xA1); //
            device address
                    aknowledge();
            i=read_byte();
            aknowledge();
            stop();
            if(i==new_value)
            {
                    led=0;
                    delay(100);
                    led=1;
                    delay(100);
                    write=i
                            +48;
                    lcd_command(0xC6);
                    lcd_data(write);
            }
            else
                    led = 1;
            aknowledge();
    }
    
    void main()
    {
            lcd_ini();
    
            lcd_strng("Sent :");
            lcd_command(0xC0);
            lcd_strng("Read :");
            temp=0;
            while (1)
            {
                    save();
                    Read();
            }
    }