code does not work as expected i suspect a bug

General discussion on mikroC.
Post Reply
Author
Message
gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

code does not work as expected i suspect a bug

#1 Post by gambrose » 31 May 2005 12:55

This code does not work as expected i suspect a bug.

Code: Select all

char frequency[4];

void Send_Frequency()
{
     Soft_UART_Write(frequency[3]);
     Soft_UART_Write(frequency[2]);
     Soft_UART_Write(frequency[1]);
     Soft_UART_Write(frequency[0]);

     Soft_UART_Write(0x0A);
}

void main()
{
    Soft_UART_Init(PORTA, 4, 0, 4800);

    while(1==1)
    {
       frequency[0] = 4;
       frequency[1] = 3;
       frequency[2] = 2;
       frequency[3] = 1;

       Send_Frequency();

       Delay_ms(5000);
    }
}
Received: 0x01 0x01 0x01 0x01 0x0A ??? :o

But if i put this:

Code: Select all

void Send_Frequency()
{
     char temp;

     temp = frequency[3];
     Soft_UART_Write(temp);
     temp = frequency[2];
     Soft_UART_Write(temp);
     temp = frequency[1];
     Soft_UART_Write(temp);
     temp = frequency[0];
     Soft_UART_Write(temp);

     Soft_UART_Write(0x0A);
}
It does work

Received: 0x01 0x02 0x03 0x04 0x0A :)

P.S. can we have direct addressing of arrays accessed using constant indexes.
Graham Ambrose

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: code does not work as expected i suspect a bug

#2 Post by pizon » 01 Jun 2005 13:35

Thanks for the tip, we'll check this out.
gambrose wrote:P.S. can we have direct addressing of arrays accessed using constant indexes.
It will be done in a next couple of months, in the meantime you can use structures as a workaround (for smaller arrays), since structures use direct acces to variables. This means that

Code: Select all

struct quasi_array {
  unsigned short a1, a2, a3, a4;
} my_quasi_array;
...
my_quasi_array.a1 = 4;
...
will be translated as

Code: Select all

MOVLW    4
MOVWF    _my_quasi_array_a1
This won't solve all the problems, but will 'ease the pain' until direct access to array variables gets implemented.
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#3 Post by gambrose » 01 Jun 2005 14:37

Thanks for the tip :)
Graham Ambrose

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#4 Post by gambrose » 01 Jun 2005 15:59

Are structures allowed as local types?

Code: Select all

struct quasi_array {
  unsigned short a1, a2, a3, a4;
} my_quasi_array;

void main()
{
    my_quasi_array.a1 = 4;
}
works fine.
But

Code: Select all

void main()
{
    struct quasi_array {
      unsigned short a1, a2, a3, a4;
    } my_quasi_array;

    my_quasi_array.a1 = 4;
}
does not compile?
Graham Ambrose

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#5 Post by pizon » 01 Jun 2005 16:22

Yes, that should (but does not) compile properly. However, you can use:

Code: Select all

struct quasi_array {
  unsigned short a1, a2, a3, a4;
};

void main()
{
    struct quasi_array my_quasi_array;

    my_quasi_array.a1 = 4;
}
and get the same results: The quasi_array structure will be defined globally, but you will have only one object of this kind (my_quasi_array), which is declared (and exists) locally. And since mikroC handles the tag scope in somewhat specific manner (take a look in mikroC Help under "Scope and Visibility" topic), this way is (should be) exactly the same thing as the one you gave.
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#6 Post by gambrose » 01 Jun 2005 16:25

Thanks :)
Graham Ambrose

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#7 Post by pizon » 01 Jun 2005 16:41

If (when) you happen to be in somewhat sadistic mood, you can even try something like this:

Code: Select all

struct quasi_array {                            // define your structure
  unsigned short a1, a2, a3, a4;
};

void main() {
  struct quasi_array my_quasi_array;            // create one object locally

  PORTB = 0;
  TRISB = 0;

  my_quasi_array.a1 = 4;
  {                                             // open the next namespace
    struct quasi_array my_quasi_array;          // this is a different object,
    my_quasi_array.a1 = 5;                      //   but can be accessed only
    PORTB = my_quasi_array.a1;                  //   within these braces...
    Delay_ms(1000);
  }
  PORTB = my_quasi_array.a1;                    // ...as you can see.
}//~!
And they say that Sam Peckinpah had a twisted mind... :twisted:
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#8 Post by gambrose » 01 Jun 2005 16:50

:shock:
Now your just showing off!! :wink:

I am impressed though.

Useful for things like loops where you can declare your index next to the loop instead of at the top of a function.
Graham Ambrose

Post Reply

Return to “mikroC General”