0

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?

C K
  • 344
  • 2
  • 13
  • Although it should not be the issue, I would change `unsigned char* xdata enter_byte_count_items[3] = ...` into `unsigned char xdata * xdata enter_byte_count_items[3] = ...` so that the compiler knows the pointed data (not only the pointers in `enter_byte_count_items`) is in `xdata` too, and generates more efficient code. Also, for the same reason, change `void menu_init(unsigned char** menu_array, ...` into `void menu_init(unsigned char xdata * xdata * menu_array, ...` or something similar... – BillyJoe Mar 31 '17 at 14:44
  • @mbjoe, good tip. Will try it – C K Mar 31 '17 at 14:45
  • Just to be on the safe side, could you share `write_string_to_line` code? – user58697 Mar 31 '17 at 20:19
  • @user58697 there it is – C K Apr 01 '17 at 06:58
  • You should put a few statements in the form `len=sizeof(...)` and verify with the debugger the size of yours arrays. Probably your compiler is using ptr3 by default (16 bit pointer with added byte for xdata, idata, pdata selector), but this would let the option _"Keep variables in order"_ superfluous. Probably the main problem is the declaration `menu_init(unsigned char** menu_array ...`. I can post a more complete answer (comments are so short)... what compiler are you using? And no - putting all the things in xdata is **not** the most efficient way. – linuxfan says Reinstate Monica Apr 01 '17 at 09:11
  • @linuxfan i am using keil c51. I think i have found something, i am calling the functions that print the statements using function pointers. It seems to be triggering the problem, i will answer if i find a solution – C K Apr 01 '17 at 09:13
  • @linuxfan, okay, it isnt function pointer related, tested – C K Apr 01 '17 at 09:25
  • Ok, use the mbjoe suggestion, and confirm size and content of the arrays, as seen by the function menu_init(). – linuxfan says Reinstate Monica Apr 01 '17 at 09:28

0 Answers0