NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » TypeCast problem

August 15, 2012
by eminthepooh
eminthepooh's Avatar

I declare this 2D array outside my main loop as a global variable (that doesn't change), along with 2 other variables

static uint16_t Matrix1[4][3]={ {1,1,1},{2,2,2},{3,3,3},{4,4,4} };
uint16_t PlantRow;
uint16_t runTime1 = 0;

later on, I refer to it as such:

PlantRow=Plant;             //Plant is the output of an int type function, it can be 1,2,3, or 4. I set it to 1 for debug.
runTime1 = Matrix1[PlantRow][0];

Later on when I try to use that runTime in delay, it messes up:

delay_ms(runTime);

Keeps seeing it as -1, It even sees Matrix1[1][0] as -1 (as outputted to the LCD via lcd_write_int16() )

What's going on? and how do I fix it?

Thank you,

August 15, 2012
by pcbolt
pcbolt's Avatar

eminthepooh -

The way the makefiles are set up, the AVR compiler has trouble with initialized arrays, even ones simpler than the 2-dimensional one you're using. I'd wager this might work:

static uint16_t Matrix1[4][3];
Matrix1[0][0] = 1;
Matrix1[0][1] = 1;
Matrix1[0][2] = 1;
Matrix1[1][0] = 2;
Matrix1[1][1] = 2;
Matrix1[1][2] = 2;
Matrix1[2][0] = 3;
Matrix1[2][1] = 3;
Matrix1[2][2] = 3;
Matrix1[3][0] = 4;
Matrix1[3][1] = 4;
Matrix1[3][2] = 4;

There is a forum thread about this HERE where it shows a modification you can use to change the makefile to operate as expected...along with a detailed explanation. Pretty sure it is not a type cast problem.

August 17, 2012
by eminthepooh
eminthepooh's Avatar

Somehow got it working with specifying PROGMEM in my initialization. guess it was a compiler problem. Thanks pcbolt

Post a Reply

Please log in to post a reply.

Did you know that a microcontroller can measure an RC time constant? Learn more...