This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

bad structure declaration?

i'm trying to create an array of structures but when i make more than a handful my robot doesn't work.

so in main.h i have

typedef struct SPid
{
  int dState;      	 // Last position input
  int iState;      	 // Integrator state
  int iMax, iMin;    // Maximum and minimum allowable integrator state
  int desired_value; // the desired value the PID should converge upon


  float 	iGain,    	// integral gain
        	pGain,    	// proportional gain
         	dGain;     	// derivative gain
}SPid;

extern SPid vel_Gains[];

and then in main.c
#include "MAIN.H"
struct SPid vel_Gains[5];

it barely works for vel_Gains[2], (printf looks glitchy but readable) and then vel_Gains[1] works for sure...

i'm using the small memory model btw...

is there a size limitation on global variables?

thanks,
fred

Parents
  • There's not a size limit on globals, so much as a size limit on the internal RAM of an 8051. (128 bytes).

    In the "small" data model, any variables without an explicit memory type qualifier are assumed to go into the "data" space, which is the internal RAM. Your struct is about 22 bytes, so five of them is 110 bytes, and you're almost out of room.

    Try declaring them "xdata". Or just change to the large memory model.

    A number of programmers recommend explicit memory qualifers for all declarations, rather than relying on the memory model to get things right. If you ever switch memory models, having explicit declarations for everything will continue to work. Without them, you'll be hunting down problem locations in your code where you let a variable default.

Reply
  • There's not a size limit on globals, so much as a size limit on the internal RAM of an 8051. (128 bytes).

    In the "small" data model, any variables without an explicit memory type qualifier are assumed to go into the "data" space, which is the internal RAM. Your struct is about 22 bytes, so five of them is 110 bytes, and you're almost out of room.

    Try declaring them "xdata". Or just change to the large memory model.

    A number of programmers recommend explicit memory qualifers for all declarations, rather than relying on the memory model to get things right. If you ever switch memory models, having explicit declarations for everything will continue to work. Without them, you'll be hunting down problem locations in your code where you let a variable default.

Children
  • thanks for your reply,

    i added

    struct SPider xhuge vel_Gains;
    

    and it didn't work... i don't think my compiler has xdata....

    do i need to modify my START167.A66 file to tell it where the ram is? (i believe i set the RAM location in keil)

  • could someone explain what this means?

    "If you want the data to reside in xdata, then you'll need to be sure your startup code executes the code in INIT.A51 to actually copy the initializer values from ROM to the xdata RAM. "

  • could someone explain what this means?

    Variables live in RAM, so they can vary. RAM does not retain its values when you turn the power off. If you initialize global variables with constants in C:

    char myArray[] = { 1, 2, 3};

    the (RAM) variable myArray has to begin with the constants 1, 2, and 3. But those constants have to be stored in some sort of ROM, rather than RAM, to survive loss of power. That is, they are stored somewhere, in ROM, in addition to the actual variable myArray. Before main() executes, some piece of code has to copy any such constant initializers from ROM to the proper locations in RAM. This job is done in the Keil toolchain by the INIT.A51 code.

    You may not need this particular feature, so use of INIT.A51 is optional. If you do not link it into your code, but use static initializers anyway, your variables will not be initialized the way you think they will.


    Have you read this thread:

    http://www.keil.com/forum/docs/thread4241.asp

    And the app note it mentions?