C51 - Pointer related bug

108 Views Asked by At

EDIT: It seems like the problem went away when I checked the "Keep variables in order" box in the C51 optimization menu. I still don't know what caused the problem & and if it is a permanent fix. Does anybody have an idea what is going on?

I am trying to build a graphical menu in C51. I am using a menu_init() function, to bring the text on a 4-line LCD screen. The function is defined as:

void menu_init(unsigned char** menu_array, unsigned char menu_length)
{   
unsigned char i, menu_max_index;
write_command(CLEAR_DISPLAY);
if (menu_length < 4)            {menu_max_index = menu_length;}
else                            {menu_max_index = 4;}


 for (i = 0; i < menu_max_index; i++)
 {
     write_string_to_line(i+1, (menu_array[i]));
 }

}

void write_string_to_line (unsigned char line, unsigned char* lcd_string)
{
    unsigned char i = 0;

    gotoxy(0, line);
    while(lcd_string[i] != '\0')
    {
        EN = 0;
        RS = 1;
        EN = 1;
        P0 = lcd_string[i];
        i++;
        EN = 0;
        delay(DELAY_COUNT);
    }   
}

In the bug occurrence, menu_array is:

unsigned char* xdata enter_byte_count_items[3] = {enter_byte_count_text, enter_byte_count_text2, enter_byte_count_number};
//For the unfamiliar, xdata is the segment in the 8051 memory, which the variable is located in

The initializers of the array are the following texts:

unsigned char xdata enter_byte_count_text[20] = " Enter byte count of";
unsigned char xdata enter_byte_count_text2[20] = " PGN: ";
unsigned char xdata enter_byte_count_number[20] = " Bytes";

with these just near it in the code, which are the strings for another menu:

unsigned char xdata enter_pgn_text[20] = " Enter PGN Number";
unsigned char xdata enter_pgn_number[20] = " 64000" ;

I expected this code to print the strings in order. However, this isn't the case, the output is like this:

 Enter byte count of
 x:
 Bytes PGN Number

And debugger verifies this result:enter image description here

I can't understand what is going on... I believe it is something about pointer decaying stuff, but I can't pinpoint the problem. Where did I do wrong?

0

There are 0 best solutions below