• AVR Freaks

Hot!PIC18F4550 - Display 16X4 and menu with more than 4 items

Author
danbakana
Junior Member
  • Total Posts : 93
  • Reward points : 0
  • Joined: 2012/08/27 05:21:28
  • Location: 0
  • Status: offline
2019/07/23 10:42:59 (permalink)
0

PIC18F4550 - Display 16X4 and menu with more than 4 items

I need to print a list of itens of a menu in a 16x4 LCD display using a PIC18F4550. I already did it using MPLABX and XC8 compiler, however, I need to migrate it to mikroC enviroment. But, It is not working. The characters are messy in the display. I put here a slice of code, because the code is big to put it here. But, can someone find a mistake in this code that I wrote here ? Sorry for the lack of comments in the code, but it were in a other language and the variables too, I translated the variables to make the code more readable here.
 
EDITED 
The code works in the following whay: I have a stack to store the menus, the menu structure has a field that points to a list of itens that has the line text.
 
I guess the problem is in the pointer of menu to list Of Items. This way as below. 
 
stackMenus->stack[stackMenus->top]->itens[0]->text
 
 
It is in the  function menu_draw(Tstack *stackMenus). If I put a string directly, the line will be written correctly. But, if I use the code above, it doesn't. 
 
When I do:
 
stackMenus->stack[stackMenus->top]->title

 
it works fine, the title is written.
 
I didn't put the entire code because it is too big. I used a lot of pointers in this passage, so I'm afraid that this is causing the problem, so I put the code related with this passage here. Sorry for the incomplete information.
 
 
Solution until now
 
I solve the problem !! At least until now... When I did
const TitenMenu listaItensMenuPrincipal[]


Is was not working , So, I did:
TitenMenu listaItensMenuPrincipal[]


And it works fine. I don't know why it happened, but, I will keep it this way. But, does someone have any idea of why it happens ?
 
Here is the code:
 
(...)

Tstack stackMenus;
stackMenus.top = 0;
error = menu_stack(&stackMenus, &mainMenu);
changeMenu = TRUE;
typeActualMenu = eSubmenu;
menu_handleMenus(&stackMenus);

(...)

Terro menu_stack( Tstack *s, PTmenu iten ){
Terro error = SUCCESS;
if(s->top == SIZE_MAX_STACK_MENUS - 1){
return ERROR_STACK_MENUS_FULL;
}
s->stack[++s->top] = iten;
return error;
}

(...)

Terro menu_handleMenus(Tstack *stackMenus){
(...)
menu_draw(stackMenus);
(...)
}

(...)

void menu_draw(Tpilha * pilhaMenus){
(...)
 
Lcd_16x4_Out(1,1, stackMenus->stack[stackMenus->top]->title);
Lcd_16x4_Out(2,2, stackMenus->stack[stackMenus->top]->itens[0]->text);
Lcd_16x4_Out(3,2, stackMenus->stack[stackMenus->top]->itens[1]->text);
Lcd_16x4_Out(4,2, stackMenus->stack[stackMenus->top]->itens[2]->text);
(...)
}

//This is a 16X4 LCD adequacy code of the original library of MicroC
void Lcd_16x4_Out(char row, char column, char *text){
switch(row){
case 0x01:
LCD_CMD(128 + column - 1);
break;
case 0x02:
LCD_CMD(192 + column - 1);
break;
case 0x03:
LCD_CMD(144 + column - 1);
break;
case 0x04:
LCD_CMD(208 + column - 1);
break;
}
Lcd_Out_Cp(text);

}


These are the List of Itens:
const TitenMenu listaItensMenuPrincipal[] = {
{0, eSubmenu, "Iten1", NULL},
{1, eSubmenu, "Iten2", NULL},
{2, eSubmenu, "Iten3", NULL},
{3, eSubmenu, "Iten4", NULL},
};

Tmenu mainMenu = {
menu_numberOfItens(mainMenuItensList),
"Main Menu",
(PTitenMenu)mainMenuItensList,
NULL
};


These are the structures and types:

struct Sstack {
PTmenu stack[SIZE_MAX_STACK_MENUS];
Tint8 top;
};

typedef struct Sstack Tstack;

typedef Tuint8 TidMenu;
typedef TidMenu *PTidMenu;
typedef unsigned char TmodoItenMenu;


struct SitenMenu {
TidMenu id;
TmodoItenMenu type;
char *text;
void *submenu;
};

typedef struct SitenMenu TitenMenu;
typedef TitenMenu *PTitenMenu;

struct Shandlers{

Terror (* create)(void);
Terror (* selection)(TidMenu);
void (* destroy)(void);
};

typedef struct Shandlers Thandlers;
typedef Thandlers * PThandlers;


struct Smenu {
unsigned char numberOfItens;
// Title
char *title;
PTitenMenu itens;
PThandlers handlers;
};

typedef struct Smenu Tmenu;
typedef Tmenu *PTmenu;

 
post edited by danbakana - 2019/07/24 08:11:28
#1

18 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 1187
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/23 11:01:19 (permalink)
    +2 (2)
    Very few people are going to bother to hunt through code that is incomplete and may not even be the code that is actually causing you problem.
    Post a minimum complete example that demonstrates the problem.
    Describe what "messy in the display" means.
     
    ...and in English it's spelt "items". grin: grin
    #2
    katela
    Super Member
    • Total Posts : 1317
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/23 12:03:03 (permalink)
    +2 (2)
    If the xc8 version compiles why not post it in the mikroC forum? They also have a forum and you might get better assistance than here.
    As said above, with those pieces of code and your lack of explanation of what is "not working" it will be difficult to get assistance in any forum.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #3
    P Lameijn
    Super Member
    • Total Posts : 1967
    • Reward points : 0
    • Joined: 2004/01/22 18:30:23
    • Location: The Netherlands
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/23 12:27:21 (permalink)
    +2 (2)
    What type of display are you using?...
    (i think not a 16x4 but graphical display?)
     
    A compilable example would help.
    Indeed few people are willing to dig into a loose pile of code...

    Regards,
    Peter
    #4
    danbakana
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2012/08/27 05:21:28
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 04:30:20 (permalink)
    0
    pcbbc
    Very few people are going to bother to hunt through code that is incomplete and may not even be the code that is actually causing you problem.
    Post a minimum complete example that demonstrates the problem.
    Describe what "messy in the display" means.
     
    ...and in English it's spelt "items". grin: grin


    Yes, you're right. 
     
    I guess the problem is in the pointer of menu to list Of Items. This way as below. 
     
    stackMenus->stack[stackMenus->top]->itens[0]->text
     
    It is in the  function menu_draw(Tstack *stackMenus). If I put a string directly, the line will be written correctly. But, if I use the code above, it doesn't. I didn't put the entire code because it is too big. I used a lot of pointers in this passage, so I'm afraid that this is causing the problem, so I put the code related with this passage. Sorry for the incomplete information.
    #5
    danbakana
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2012/08/27 05:21:28
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 04:35:21 (permalink)
    0
    P Lameijn
    What type of display are you using?...
    (i think not a 16x4 but graphical display?)
     
    A compilable example would help.
    Indeed few people are willing to dig into a loose pile of code...




    It is not graphical, it is a 16x4 display.
    #6
    pcbbc
    Super Member
    • Total Posts : 1187
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 05:56:45 (permalink)
    +2 (2)
    danbakanastackMenus->stack[stackMenus->top]->itens[0]->text

    Most likely stackMenus->top is out of range and you are just accessing garbage outside of the bounds of the stack array.
    #7
    danbakana
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2012/08/27 05:21:28
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 06:17:23 (permalink)
    0
    pcbbc
    danbakanastackMenus->stack[stackMenus->top]->itens[0]->text

    Most likely stackMenus->top is out of range and you are just accessing garbage outside of the bounds of the stack array.



    I'm suspecting this too. But, just for me to be sure, what exactcly  do you mean by "out of range"  ? Do you have any idea of how to avoid it ?
    #8
    Jim Nickerson
    User 452
    • Total Posts : 6117
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 06:23:22 (permalink)
    0
    Smile: Smile
    #9
    danbakana
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2012/08/27 05:21:28
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 06:38:11 (permalink)
    0
    pcbbc
    danbakanastackMenus->stack[stackMenus->top]->itens[0]->text

    Most likely stackMenus->top is out of range and you are just accessing garbage outside of the bounds of the stack array.




    When I put the title, as below, it is printed correctly. 
     
    stackMenus->stack[stackMenus->top]->title
     
    So, the problem occour when I out
     
    stackMenus->stack[stackMenus->top]->itens[0]->text
    post edited by danbakana - 2019/07/24 06:40:04
    #10
    danbakana
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2012/08/27 05:21:28
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 07:13:56 (permalink)
    0
    I solve the problem !! At least until now... When I did
     
    const TitenMenu listaItensMenuPrincipal[]
    Is was not working , So, I did:
     
    TitenMenu listaItensMenuPrincipal[]
     
    And it works fine. I don't know why it happened, but, I will keep it this way. Does someone have any idea of why it happens ?
    post edited by danbakana - 2019/07/24 07:57:20
    #11
    ric
    Super Member
    • Total Posts : 23160
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 13:44:50 (permalink)
    +1 (1)
    You have moved the table from ROM to RAM, so uninitialised values will be zero instead of 0xFF.
    It sounds like you still have bad code, just you're getting different rubbish data now.
     
    Please do NOT change the first post after you have received answers. It gets very confusing when the post being replied to has changed.
     
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    pcbbc
    Super Member
    • Total Posts : 1187
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/24 14:51:52 (permalink)
    +2 (2)
    ...and additionally to what ric said moving to RAM a whole load of data that is constant, and so could just as easily be left in ROM, is very wasteful.

    This code...
    stackMenus.top = 0;
    error = menu_stack(&stackMenus, &mainMenu);
    ...
    Terro menu_stack( Tstack *s, PTmenu iten ){
    ...
    s->stack[++s->top] = iten;


    Is inserting the pointer to mainMenu into stack entry index 1 because you are using pre-increment [++s->top] instead of post-increment [s->top++] operator.
    Is that really what you intended? To leave the entry at index 0 of the stack uninitialised?
    #13
    danbakana
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2012/08/27 05:21:28
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/25 05:02:50 (permalink)
    0
    ric
    You have moved the table from ROM to RAM, so uninitialised values will be zero instead of 0xFF.
    It sounds like you still have bad code, just you're getting different rubbish data now.
     
    Please do NOT change the first post after you have received answers. It gets very confusing when the post being replied to has changed.



    Thank you, Ric. Constants are stored in flash, so I did not understand why I had uninitialised values, or garbage, when I put the list of intems as a const vector. And now thinking about, removing the const designator,  willmove the table from ROM to RAM,  as you said, and it is really not a good thing, because RAM of this microcontroller is very limited in size. So, do you have any suggestion ?
    #14
    danbakana
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2012/08/27 05:21:28
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/25 05:32:00 (permalink)
    0
    pcbbc
    ...and additionally to what ric said moving to RAM a whole load of data that is constant, and so could just as easily be left in ROM, is very wasteful.

    This code...
    stackMenus.top = 0;
    error = menu_stack(&stackMenus, &mainMenu);
    ...
    Terro menu_stack( Tstack *s, PTmenu iten ){
    ...
    s->stack[++s->top] = iten;


    Is inserting the pointer to mainMenu into stack entry index 1 because you are using pre-increment [++s->top] instead of post-increment [s->top++] operator.
    Is that really what you intended? To leave the entry at index 0 of the stack uninitialised?



    Yes, you're right, is very wasteful, but ,what can I do instead ?
    About the pre-increment instead of post-increment, is not a problem, because in my code I consider the stack empty when top <= 0. So, mainMenu will be in position of index 1.
     
    #15
    ric
    Super Member
    • Total Posts : 23160
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/25 05:42:23 (permalink)
    +1 (1)
    danbakana
    ...
    Thank you, Ric. Constants are stored in flash, so I did not understand why I had uninitialised values, or garbage, when I put the list of intems as a const vector.
    ...

    I haven't gone through your logic in detail, but I suspect you are out by one somewhere, and therefore accessing one item past the end of your initialised data in ROM, so it's probably accessing an unused bit of ROM, which will contain the erased value of 0xFF in each byte.
     What happens if you place an extra entry full of zeros at the end of your table?

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #16
    danbakana
    Junior Member
    • Total Posts : 93
    • Reward points : 0
    • Joined: 2012/08/27 05:21:28
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/25 07:05:23 (permalink)
    0
    ric
    danbakana
    ...
    Thank you, Ric. Constants are stored in flash, so I did not understand why I had uninitialised values, or garbage, when I put the list of intems as a const vector.
    ...

    I haven't gone through your logic in detail, but I suspect you are out by one somewhere, and therefore accessing one item past the end of your initialised data in ROM, so it's probably accessing an unused bit of ROM, which will contain the erased value of 0xFF in each byte.
     What happens if you place an extra entry full of zeros at the end of your table?




    I did it, an extra entry full of zeros at the end of the table, but if I put const, nothing works. I was wondering if the problem is the fact that that mainMenu is not const and the list of items is const.
     
    const TitenMenu mainMenuItensList[] = {
    {0, eSubmenu, "Iten1", NULL},
    {1, eSubmenu, "Iten2", NULL},
    {2, eSubmenu, "Iten3", NULL},
    {3, eSubmenu, "Iten4", NULL},
    };
    Tmenu mainMenu = {
    menu_numberOfItens(mainMenuItensList),
    "Main Menu",
    (PTitenMenu)mainMenuItensList,
    NULL
    };
     
    The problem is that when I do mainMenu const,:
     
    const Tmenu mainMenu = {
    menu_numberOfItens(mainMenuItensList),
    "Main Menu",
    (PTitenMenu)mainMenuItensList,
    NULL
    };
     
    The compiler complains about this line:
     
    error = menu_stack(&stackMenus, &mainMenu);
     
    It says that it is a "Illegal pointer conversion". And I'm trying to eliminate this error , but until now I couldn't
     
     
    post edited by danbakana - 2019/07/25 07:32:51
    #17
    mlp
    boots too small
    • Total Posts : 776
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/29 08:59:23 (permalink)
    +2 (2)
    danbakana
    I was wondering if the problem is the fact that that mainMenu is not const and the list of items is const.

    That is probably it.
     
    const TitenMenu mainMenuItensList[] = {
    {0, eSubmenu, "Iten1", NULL},
    {1, eSubmenu, "Iten2", NULL},
    {2, eSubmenu, "Iten3", NULL},
    {3, eSubmenu, "Iten4", NULL},
    };
    Tmenu mainMenu = {
    menu_numberOfItens(mainMenuItensList),
    "Main Menu",
    (PTitenMenu)mainMenuItensList,

    This cast is a problem.
    A cast should almost never appear in any code you write. There is usually a non-cast way to achieve the result you really need.
    A cast says to the compiler "I know better than you what types are involved and should be involved here, and I know that a conversion you would not automatically do is the correct way forward". Most programmers do not in fact know these things.
     
    I am 100% sure that taking a pointer-to-Flash and converting it to a pointer-to-RAM is not going to have a good outcome for you.
     

    Mark (this opinion available for hire)
    #18
    oliverb
    Super Member
    • Total Posts : 204
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: PIC18F4550 - Display 16X4 and menu with more than 4 items 2019/07/30 02:03:56 (permalink)
    0
    Long shot here (haven't read everything) but there is a REALLY IMPORTANT DIFFERENCE between XC8 and mikroC pointer handling.
     
    XC8 tries to implement compliant pointer handling, where there is only one memory space and a "const" pointer can point to RAM or ROM. A non-const pointer can be pointed to ROM but if the compiler spots it it will spam warnings. How it does this I don't know, I presume each time you read from/dereference a pointer it has to check which space it points to and use either table read or IND accordingly?
     
    Last time I looked MikroC implemented two pointer types, one for RAM and one for ROM. Attempt to point to the wrong memory space and you get garbage. This means some things you can do in XC8 will fail in MikroC.
     
    Funnily enough I implemented a scrolling menu in MikroBasic and later converted it to XC8. Line by line BASIC to C is tedious but doable. I looked into automating it but "BACON" appeared too locked to one specific BASIC.
     
    #19
    Jump to:
    © 2019 APG vNext Commercial Version 4.5