Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C18 PIC18F4550 pointer problem

Status
Not open for further replies.

HerbertMunch

New Member
Code:
//Required by Menu system.
//Renders a menu item on chosen medium.
void DisplayMenuItem(MenuItem *item)
{

//	SendCommand(0x01);

	printf(item->text);

	SendString(item->text);
}	

void SendString(const rom char * string)
{
	char* position = string;
	char a;
	while (*position != '\0')
	{
		a = *position;
		SendCharacter(*position++);
	}
	
	
}

Can anyone tell me how to make that method work with the following arrays?
I thought that I finally understood how to access strings, but I was wrong.


Code:
#pragma romdata


//Example heirarchy items.

// **********************************************************************************************
// The following arrays contain menu items for each level of menu heirarchy.
MenuItem menu1Items[] =
						{
						 // TopLevel    , Name     ,   subMenu, handler
							1			,"Menu1-1" , 0		 ,  handler,
							1			,"Menu1-2" , 0		 ,  0
							
						};


//This represents the settings sub menu.
MenuItem menu2Items[] =
						{
						 // TopLevel    , Name     ,   subMenu, handler
							1			,"Menu2-1" , 0 		 ,  0,
							1			,"Menu2-2" , 0		 ,  0,
							1			,"Menu2-3" , 0		 ,  0
							
						};
						
						
//base items. also: menu initialises by cha startup item to baseItems[0]
MenuItem menu0Items[] =
						{
						 // TopLevel    , Name     ,   subMenu, handler
							0           , "Menu1"  ,  &menuLevels[1] , 0,
							0           , "Menu2"  ,  &menuLevels[2] , 0,	
							1           , "Menu3"  ,  0 , 0	
							
						};
						
						
//*********************************************************************************************
//End of item arrays



//The following array is required by menu system. 
//It contains MenuLevel items, which describe a level that contains menu items.
//First item should be the base menu.
MenuLevel menuLevels[] =	{			// start index ,	array size 							  , array
										 0		   	   ,	sizeof(menu0Items) / sizeof (MenuItem), menu0Items,
										 0		   	   ,	sizeof(menu1Items) / sizeof (MenuItem), menu1Items,
										 0		   	   ,	sizeof(menu2Items) / sizeof (MenuItem), menu2Items,
									 
								
							};

When i step through the DisplayMenuItem method, the PrintF succeeds and displays "Menu1" in the uart window. My sendString method doesnt work! I dont know how to access the text, but the printf does!

Here are the struct defs

Code:
#pragma romdata

//represents an individual menu item which belongs to a menu level
typedef struct MenuItem
{	
		//bool. describes whether or not this node is a top level node.
		//i.e when MenuClick() is called on this item, topLevelHandler will be called 
		//and the current item will be stored in history
		unsigned char topLevel;
		//Pointer to an array of characters representing the menu item
		 char *text;
			//A pointer which points to the next level up in the menu heirarchy
		//Actually a MenuLevel pointer.
		near rom *subMenu;
		/*
		Pointer to handler method that deals with the application specifics.
		Paremeter firstRun tells user code whether or not this is the first time
		the method has run (i.e has the user just pressed the button to get here?)
	
		*/
		void (*topLevelHandler)(bool firstRun);

	
}MenuItem;

//represents a level of menu heirarchy.
typedef struct MenuLevel
{

    //The index of the current selected menu on this level
    char currentIndex;
    //The length of the item array specified by *items
    bool itemCount; 
    //Pointer to an array of menu items that represent this level
	MenuItem *items;
}MenuLevel;


When i look in the watch window at the location of the string, it says 31DE. If i try and look at 31DE in the watch window, it just says out of bounds, FFF, etc.

I would be extremely grateful to anyone that can answer this:confused:

Any ideas?

Many thanks:)
Chris
 

Attachments

  • ProgMem.png
    ProgMem.png
    6.9 KB · Views: 161
Last edited:
What happens if you do,

SendString(menu0Item[1]->text);

Or,

MenuItem *Pointer;
*Pointer=menu0Item[1];
SendString(*Pointer->text);

To look at the location of the string do view>program memory.

Mike.
 
Hi Mike, thanks for replying.

Pommie said:
What happens if you do,
SendString(menu0Item[1]->text);
Sendstring skips past the while loop and exits.


Pommie said:
MenuItem *Pointer;
*Pointer=menu0Item[1];
SendString(*Pointer->text);

Exactly the same, just skips past the while loop.

Pommie said:
To look at the location of the string do view>program memory.

Yeah, I included a screenshot of that in my first post;) Why doesnt the watch window show this?

Thanks
 
What happens if you change it to,
Code:
void SendString(rom char * string)
{
	while (*string!=0)
	{
		SendCharacter(*string++);
	}	
}

Just trying to get it in it's simplest form.

Mike.
Edit, I think your char* position needed to be rom char*
 
Last edited:
Nice one mate, that works!

it would appear that the
Code:
while (*position != '\0')

was the problem.

Do you know why this is wrong?

thanks.
 
Just had a look at what the asm is and it appears to do while(*position!='/') !!!

Did you not need to change the "char* position" to "rom char* position"?

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top