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 » indexing an array with a variable

September 27, 2012
by fixedPoint
fixedPoint's Avatar

Hi everyone, I'm having a strange programming problem. I have a feeling that the answer is something trivial that I'm missing, but I can't find it. I have program what uses shift registers to display numbers on a seven segment display. I store the bit pattern for each digit in an array (sevenSeg in the code), then index that array with the digit I want printed.

Everything works great when I index the array with a constant number (i.e. sevenSeg[4]). But if I index the array with an int variable assigned to 4, it doesn't work (ie. uint8_t i=4; sevenSeg[i]). The code is below. Please ignore that it doesn't really make sense as I stripped most things out that are not needed to demonstrate the problem. The code below doesn't work, but if I replace the sevenSeg[i] with sevenSeg[6] (or any other number) it works great and will show the number that I indexed the array with.

const uint16_t sevenSeg[11] = {
    0x0208,   // 0
    0x070e,   // 1
    0x080a,   // 2
    0x0604,   // 3
    0x0702,   // 4
    0x0601,   // 5
    0x0201,   // 6
    0x070c,   // 7
    0x0200,   // 8
    0x0600,   // 9
    0x0f0f    // BLANK
};

#define BLANK 10

int main() {
    // start up the seven segment
    shift_init();

    uint8_t i;
    uint8_t data[2] = {BLANK,BLANK};
    for (i=0; i<10; i++) {
        data[0] = sevenSeg[i] >> 8;
        data[1] = sevenSeg[i];
        data[0] = data[0] | 0x10;
        shift_out(data, 2);    
        delay_ms(2000);
    }

    while(1){
    }

    return 0;
}
September 27, 2012
by Noter
Noter's Avatar

When you say it doesn't work, does that mean the display remains blank?

September 27, 2012
by pcbolt
pcbolt's Avatar

@ fixedPoint

I've had some troubles with initializing arrays before, but not what you are describing. The "hacky" workaround I used was to specifically assign the array values one at a time like this....

sevenSeg[0] = 0x0208;   // 0
sevenSeg[1] = 0x070e;   // 1
sevenSeg[2] = 0x080a;   // 1
// etc

There is a better way and THIS THREAD may help you.

September 27, 2012
by fixedPoint
fixedPoint's Avatar

Noter, yes, I mean that the display remains blank.

I did a little more debugging. If I create another uint8_t variable and either initialize it to a number or set it to a number later on, I can use that variable to index the sevenSeg array. It is only when the indexing variable gets set via the for loop that the code fails. I tried changing the for loop to a while loop and manually incrementing the indexing variable, but that also did not work.

September 27, 2012
by fixedPoint
fixedPoint's Avatar

Thanks pcbolt. The makefile modification to have avr-objcopy copy the data section fixed the problem. Now I need to read through that thread carefully and try to figure out what exactly is going on.

Post a Reply

Please log in to post a reply.

Did you know that you can see each keypress on a computer keyboard on an oscilloscope? Learn more...