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 |
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.
|
---|---|
September 27, 2012 by Noter |
When you say it doesn't work, does that mean the display remains blank? |
September 27, 2012 by pcbolt |
@ 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....
There is a better way and THIS THREAD may help you. |
September 27, 2012 by fixedPoint |
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 |
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. |
Please log in to post a reply.
Did you know that signed numbers need to be sign-extended when chaging variable sizes? Learn more...
|