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 SBUF 8051

Hello everybody.

I'm french, so sorry for my english.

First, thanks for all gentlemens who will help me.

I follow a formation about programmation microcontroleur 8051's family.

First I have studied the microcontroleur himself, after I have made a lot of programs in assembleur with Keil, and no problem.

After I have learn the langage C, and I have made a lot of program on my computer for the computer, and now I try to made the same previous program that I have created in assembleur but in langage C, always with Keil, and I have a problem.

The problem is that the hyperterminal diplay :

"Serial number : 1’˜ž¤ª"

instead of display "Serial number : 111111".

The problem is when the program call the fonction VERIFTI(), the veriable unite is lost!!!!
Why ?????

Do you have an idea?

My microcontroleur is an NXP P89V51RD2BN

Please look at my program:


#include <REG51.H>

void VERIFRI()reentrant;
void VERIFTI()reentrant;

data unsigned char cent;
data unsigned char diz;
data unsigned char unite;


void Led_chenillard()
{



SM0=0;                          //UART en mode 1
SM1=1;                          //UART en mode 1
SM2=0;                          //Gestion multiprocesseur inactive
REN=1;                          //Reception active

TMOD=0x20;                      //Config Timer 1 en mode 2 et mode temporisateur
TH1=255;                        //56000 Bauds
TR1=1;                          //Active le timer 1

TI=1;



SBUF=0x0C;                  //Efface écran
VERIFTI();                      //Vérifie si l'envoie au PC est terminé


SBUF='S';
VERIFTI();

SBUF='e';
VERIFTI();

SBUF='r';
VERIFTI();

SBUF='i';
VERIFTI();

SBUF='a';
VERIFTI();

SBUF='lk';
VERIFTI();

SBUF=' ';
VERIFTI();

SBUF='n';
VERIFTI();

SBUF='u';
VERIFTI();

SBUF='m';
VERIFTI();

SBUF='b';
VERIFTI();

SBUF='e';
VERIFTI();

SBUF='r';
VERIFTI();

SBUF=' ';
VERIFTI();

SBUF=':';
VERIFTI();

SBUF=' ';
VERIFTI();

unite='1';

SBUF=unite;
VERIFTI();

SBUF=unite;
VERIFTI();

SBUF=unite;
VERIFTI();

SBUF=unite;
VERIFTI();

SBUF=unite;
VERIFTI();

SBUF=unite;
VERIFTI();

FIN:
goto FIN;

}



void VERIFRI()reentrant
{
        DEBUT:
        if (RI==0)
        {
        goto DEBUT;
        }

        else
        {
        RI=0;
        }
}

void VERIFTI()reentrant
{
        DEBUT:
        if (TI==0)
        {
        goto DEBUT;
        }

        else
        {
        TI=0;
        }
}

Parents Reply Children
  • The variable unite is use only at line :

    D:0008H SYMBOL UNITE?040

    but I note that there is a ?040. Why ?

    It's so easy in assembleur why is it so difficult in C ? However, the C language is a high level language.

    The problem, I think, is that the variable "unite" is not stock at the good place, and I think that there is, somewhere in Keil, a place to define this, because in assembleur we define this emplacement, for example (MOV 33h, #01Fh), bot not in C, and I don't know how Keil define this automatically.

    When we create a software on a PC, windows define automatically the place where variables are stock in the RAM, but maybe with microcontroleur it works diferently.

    Are you OK with me ?

  • I solved part of my problem.
    Instead of declaring my variables at the begining of the program, I declare it in the fonction.

    Before :

    
    #include <REG51.H>
    
    void VERIFRI();
    void VERIFTI();
    
    data unsigned char cent;
    data unsigned char diz;
    data unsigned char unite;
    
    
    void Led_chenillard()
    {
    
    
    
    SM0=0;                          //UART en mode 1
    
    

    And now :

    
    #include <REG51.H>
    void verifri();
    void verifti();
    
    void led_chenillard()
    {
    
    data unsigned char unite;
    data unsigned char diz;
    data unsigned char cent;
    
    
    SM0=0;                          //UART en mode 1
    SM1=1;                          //UART en mode 1
    SM2=0;                          //Gestion multiprocesseur inactive
    REN=1;                          //Reception active
    
    TMOD=0x20;                      //Config Timer 1 en mode 2 et mode temporisateur
    TH1=255;                        //56000 Bauds
    TR1=1;                          //Active le timer 1
    
    TI=1;
    
    
    
    SBUF=0x0C;                  //Efface écran
    verifti();                      //Vérifie si l'envoie au PC est terminé
    
    
    SBUF=0x0C;                  //Efface écran
    verifti();                      //Vérifie si l'envoie au PC est terminé
    
    SBUF='S';
    verifti();
    
    SBUF='e';
    verifti();
    
    SBUF='r';
    verifti();
    
    SBUF='i';
    verifti();
    
    SBUF='a';
    verifti();
    
    SBUF='l';
    verifti();
    
    SBUF=' ';
    verifti();
    
    SBUF='n';
    verifti();
    
    SBUF='u';
    verifti();
    
    SBUF='m';
    verifti();
    
    SBUF='b';
    verifti();
    
    SBUF='e';
    verifti();
    
    SBUF='r';
    verifti();
    
    SBUF=' ';
    verifti();
    
    SBUF=':';
    verifti();
    
    SBUF=' ';
    verifti();
    
    
    unite='1';
    
    P2='1';
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    P1=unite;
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    while (1);
    
    }
    
    
    
    void verifri()
    {
            while (RI==0);
            RI=0;
    }
    
    void verifti()
    {
            while (TI==0);
            TI=0;
    }
    
    
    

    So, with this program the hyperterminal of the PC display "Serial number : 111111".

    BUT now if I swap the variables at the time of the statement, it does not work anymore. The PC display "Serial number : çiEEöf.

    
    #include <REG51.H>
    void verifri();
    void verifti();
    
    void led_chenillard()
    {
    
    data unsigned char diz;
    data unsigned char cent;
    data unsigned char unite;             //unite changed place
    
    

    Why ????

  • because whatever writes over, now overwrites 'cent'

    you DO have a 'wild' write to the position that is unite/cent FIND IT.

    is there somewhere in the rest of your code a write to an absolute address?

  • Thank you very much for your dedication.

    I don't understand "you DO have a 'wild' write to the position that is unite/cent FIND IT."

    is there somewhere in the rest of your code a write to an absolute address? :

    All of my code is here. As you can see I declare 3 variables at the beginig o "unite, "diz", and "cent", but actually only "unite" is used. When I have solved my problem I can go further, and use the other variable.

    My complete code is :

    
    #include <REG51.H>
    void verifri();
    void verifti();
    
    void led_chenillard()
    {
    
    data unsigned char diz;
    data unsigned char cent;
    data unsigned char unite;
    
    
    SM0=0;                          //UART en mode 1
    SM1=1;                          //UART en mode 1
    SM2=0;                          //Gestion multiprocesseur inactive
    REN=1;                          //Reception active
    
    TMOD=0x20;                      //Config Timer 1 en mode 2 et mode temporisateur
    TH1=255;                        //56000 Bauds
    TR1=1;                          //Active le timer 1
    
    TI=1;
    
    
    
    SBUF=0x0C;                  //Efface écran
    verifti();                      //Vérifie si l'envoie au PC est terminé
    
    
    SBUF=0x0C;                  //Efface écran
    verifti();                      //Vérifie si l'envoie au PC est terminé
    
    SBUF='S';
    verifti();
    
    SBUF='e';
    verifti();
    
    SBUF='r';
    verifti();
    
    SBUF='i';
    verifti();
    
    SBUF='a';
    verifti();
    
    SBUF='l';
    verifti();
    
    SBUF=' ';
    verifti();
    
    SBUF='n';
    verifti();
    
    SBUF='u';
    verifti();
    
    SBUF='m';
    verifti();
    
    SBUF='b';
    verifti();
    
    SBUF='e';
    verifti();
    
    SBUF='r';
    verifti();
    
    SBUF=' ';
    verifti();
    
    SBUF=':';
    verifti();
    
    SBUF=' ';
    verifti();
    
    
    unite='1';
    
    P2='1';
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    P1=unite;
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    while (1);
    
    }
    
    
    
    void verifri()
    {
            while (RI==0);
            RI=0;
    }
    
    void verifti()
    {
            while (TI==0);
            TI=0;
    }
    
    

  • Your complete code???

    Where is main()?

  • Ok, I have changed void led_chenillard() for void main(), but the problem is the same, and in addition I have a new warning :

    Warning L1 : unresolved external symbol
    Symbol : ?C_STARTUP
    Module : Led chenillard.obj (LED_CHENILLARD)

    
    #include <REG51.H>
    void verifri();
    void verifti();
    
    void main()
    {
    
    data unsigned char diz;
    data unsigned char cent;
    data unsigned char unite;
    
    
    SM0=0;                          //UART en mode 1
    SM1=1;                          //UART en mode 1
    SM2=0;                          //Gestion multiprocesseur inactive
    REN=1;                          //Reception active
    
    TMOD=0x20;                      //Config Timer 1 en mode 2 et mode temporisateur
    TH1=255;                        //56000 Bauds
    TR1=1;                          //Active le timer 1
    
    TI=1;
    
    
    
    SBUF=0x0C;                  //Efface écran
    verifti();                      //Vérifie si l'envoie au PC est terminé
    
    
    SBUF=0x0C;                  //Efface écran
    verifti();                      //Vérifie si l'envoie au PC est terminé
    
    SBUF='S';
    verifti();
    
    SBUF='e';
    verifti();
    
    SBUF='r';
    verifti();
    
    SBUF='i';
    verifti();
    
    SBUF='a';
    verifti();
    
    SBUF='l';
    verifti();
    
    SBUF=' ';
    verifti();
    
    SBUF='n';
    verifti();
    
    SBUF='u';
    verifti();
    
    SBUF='m';
    verifti();
    
    SBUF='b';
    verifti();
    
    SBUF='e';
    verifti();
    
    SBUF='r';
    verifti();
    
    SBUF=' ';
    verifti();
    
    SBUF=':';
    verifti();
    
    SBUF=' ';
    verifti();
    
    
    unite='1';
    
    P2='1';
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    P1=unite;
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    SBUF=unite;
    verifti();
    
    while (1);
    
    }
    
    
    
    void verifri()
    {
            while (RI==0);
            RI=0;
    }
    
    void verifti()
    {
            while (TI==0);
            TI=0;
    }
    
    

  • Sorry - I've lost track of what the actual problem is!

    Warning L1 : unresolved external symbol
    Symbol : ?C_STARTUP

    You're not using the SRC directive (or GUI equivalent) to convert all your 'C' to assembly, are you...?

    www.keil.com/.../search.asp

  • You are probably right. Thanks you for your help.