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

Problems with GLOBAL VARIABLES

HI!,
I have a big problem when i define global variables, because the execution programs is doing extranges things.

I have 2 task. READ and WRITE

void Read (void) _task_ READ _priority_ 0 {


        unsigned int ReceivedMessage;

        while(1){

           ReceivedMessage=_getkey();
           os_send_message (MAILBOX, ReceivedMessage, WAIT_FOREVER);

        }
}
void Write (void) _task_ WRITE _priority_ 1 {

               unsigned int Message[6];
               while(1){

                  P2=0x01;
                  for(i=0;i<6;i++){

                        os_wait (K_MBX + MAILBOX, WAIT_FOREVER, &Message[i]);
                        P2<<=1;

                  }
                  /* ACCIONS */

but i need 3 variables for to save the results of the ACCIONS for share with others task.
At the top i declared the variables:

int ax,bx,cx


it is ok??
Maybe can do it others way??

Thanks

Parents
  • First off: Yes, you can declare your three global variables as you suggested. But you say you have a problem - but no description about that the problem is. What happens and what did you expect should happen?

    Note that this is an 8-bit processor, variables larger than 8 bits may be only partially updated if you eccess them from both interrupt handlers and normal code, or from two different tasks.

    Another thing: Why you you have the internal loop in the Write task, to wait for 6 mails? Shouldn't you process the mails one-by-one when you get them, or look into a different message model where you get one mail with all information? Is it six separate threads that should send one mail each before you start processing or? If you have multiple tasks that sends mails, you can't know in which order they will be received, so the index into the Message array can't be used to distinguish the mail source or mail type.

Reply
  • First off: Yes, you can declare your three global variables as you suggested. But you say you have a problem - but no description about that the problem is. What happens and what did you expect should happen?

    Note that this is an 8-bit processor, variables larger than 8 bits may be only partially updated if you eccess them from both interrupt handlers and normal code, or from two different tasks.

    Another thing: Why you you have the internal loop in the Write task, to wait for 6 mails? Shouldn't you process the mails one-by-one when you get them, or look into a different message model where you get one mail with all information? Is it six separate threads that should send one mail each before you start processing or? If you have multiple tasks that sends mails, you can't know in which order they will be received, so the index into the Message array can't be used to distinguish the mail source or mail type.

Children
  • HI,
    First; Thanks very much for your quickly answer!

    Explain my code:

    #include <reg936.h>
    #include <stdio.h>
    #include <rtx51.h>
    
    extern TXRX_getByte();
    extern TXRX_putByte(char x);
    
    #define READ            0          /* Numero de tarea */
    #define WRITE           1
    
    /* RTX constantes */
    
    #define RTX_DONE                0
    #define WAIT_FOREVER    0xFF
    
    /* Mailboxes */
    
    #define MAILBOX         0               /* Buzon donde almacenamos las variables recibidas via serie */
    
    /* Variables globales para acciones del PID  */
    
    int ax,bx,cx;           /* Variables para construir el entero de 2 bytes  */
    int ay,by,cy;
    
    /*************************************/
    /* TASK 0, Lee del puerto serie      */
    /*************************************/
    
    void Read (void) _task_ READ _priority_ 0 {
    
    
            unsigned int ReceivedMessage;
    
            while(1){
    
                            ReceivedMessage= TXRX_getByte();
                            os_send_message (MAILBOX, ReceivedMessage, WAIT_FOREVER);
    
            }
    }
    
    /******************************************/
    /* TASK 1, Escribe en el puerto serie     */
    /******************************************/
    
    void Write (void) _task_ WRITE _priority_ 1 {
    
            unsigned int Message[6];
            unsigned int i;
            unsigned char RTXCompletion;                    /* Para verificar que la tarea se crea satisfactoriamente */
    
    
            RTXCompletion = os_create_task (READ);
    
            if (RTX_DONE == RTXCompletion){                         /* Si vale '0' -> tarea creada   */
    
                    while(1){
    
                      P2=0x01;
                      for(i=0;i<6;i++){
    
                            os_wait (K_MBX + MAILBOX, WAIT_FOREVER, &Message[i]);
                            P2<<=1;
    
                      }
    
                      /* Construimos el entero de 2 bytes */
                      /*
                      ax = ((Message[1] << 8) | (Message[0]));
              bx = ((Message[3] << 8) | (Message[2]));
                      cx = ((Message[5] << 8) | (Message[4]));                */
    
                      os_create_task(A);
                      os_create_task(B);
                      os_create_task(C);
    
    
                      /*TXRX_putByte(ax);
                      TXRX_putByte(ax>>8);
                      TXRX_putByte(bx);
                      TXRX_putByte(bx>>8);
                      TXRX_putByte(cx);
                      TXRX_putByte(cx>>8); */
    
    
    
                    }
    
            }else {
                    printf(" Fallo en la creación de tarea");               /* Si vale '-1' -> tarea no creada   */
                while(1);           /* Bucle infinito   */
            }
    }
    
    /*********************************************/
    /*        MAIN, Inicio del programa          */
    /*********************************************/
    
    void main (void){
    
            P2M1 = 0;                                          /* P2 output modo 1, LEDs               */
            P1M1 = 0;                                          /* P1 output modo 1 -> COM1 activado    */
    
            BRGCON = 0x00;                             /* Para evitar que BRGEN este activo cuando inicializamos el baud rate  */
            SCON   = 0x52;                 /* initialize UART  (Modo 1: 8 bits, enable Tx)                         */
            BRGR0  = 0xF0;                 /* 9600 baud, 8 bits, no paridad, 1 bit de stop                         */
            BRGR1  = 0x02;
            BRGCON = 0x03;                             /* Activamos BRGEN  */
    
    
            os_start_system (WRITE);
    }
    

    I received from serial port 6 variables, with it, i need built 3 variables type int (ax,bx,cx)
    this var are used in 3 task that executed the same function and later send this var for serial port.

    what u do think about this???