NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Populating an Array
July 04, 2011 by Ralphxyz |
I am trying to populate line_display[] with 255 elements holding 0 - 255.
This code compiles but nothing happens with my LEDs connected to PORTD. Of course since I am using PORTD I have no Terminal or LCD for output testing. I have to figure out how to use a AVR-gcc debugger but until then I need help from my friends :-) Ralph |
---|---|
July 04, 2011 by Ralphxyz |
In conjunction with my above question about populating the line_display[] array why doesn't this program count correctly?
This counts to 15 correctly and then jumps all over the place. I would "think" it would just keep incrementing by 1. Not using an array increments correctly:
Ralph |
July 04, 2011 by esoderberg |
Ralph, I think I can see what you're trying to do. Try this code; I verified that it runs and lights up LEDs on PORTD in sequence counting in binary up to 1 byte.
|
July 04, 2011 by Ralphxyz |
Thanks esoderberg, that works but I need to use an array. This is for my Water Curtain project and so far the best way I can see to do it is using an array. I need to be able to reference the different array elements to build my letters and symbols. I even tried populating the array elements individually:
But that doesn't work either. So I guess my question is how does one initialize and populate an array? Ralph |
July 04, 2011 by Noter |
Ralph, 0 to 255 is 256 elements so start by defining your array as [256]. Then use a 16 bit integer to index the array so it can be > 255. Otherwise the code looks ok but check for warnings and fix any that you find. If it still doesn't work, post the whole program and I'll give it a go to see if it works for me. Otherwise we have to assume things like you configured PORTD for output, etc.
|
July 04, 2011 by esoderberg |
Ralph, An oddity I noticed in my posted code, I set PORTC as output, with DDRC |= 0xFF;, when I was intending to set PORTD. What is odd is that ran just fine with LEDs attached to PORTD. As to your real question IRT arrays, I'm sure with Noter on it you'll be set. Eric |
July 04, 2011 by Ralphxyz |
Thanks Paul I thought I would need the 16 bit integer. With that fix and initializing the array I have it working!! Here is the working code in case anyone would like 8 blinking leds using PORTD.
I even have all of my warnings fixed!! Thanks Noter and esoderberg, Ralph |
July 04, 2011 by Ralphxyz |
Well that code almost works. I left it running for 10 minutes and all of a sudden the leds went blank for 30 seconds and then came back on with random displays. It is no longer incrementing correctly, darn. So I still need help. It worked for 10 minutes correctly counting up to all eight leds lit and then starting over at 1 now it is completely random. HELP Ralph |
July 04, 2011 by Ralphxyz |
I moved the array population code out of the while(1) loop. I can see where that would cause a problem but it still counts to 255 and then goes to random displays. Here is the corrected code segment but it still does not work!
Ralph |
July 04, 2011 by Noter |
Here it is with a couple of minor changes. No need to OR the data since every pin on PORTD is set everytime. Likewise to turn them off just set PORTD to zero. I reduced the delay counts to make it fast so I could see the whole array in a shorter time. Then I let it run for a while and it was still fine. If you still get random looking patterns with this code then I suspect you have a wire or two misplaced.
|
July 05, 2011 by Ralphxyz |
[quote] "If you still get random looking patterns with this code then I suspect you have a wire or two misplaced." [/quote] Huh who me. Actually it is running fine, thank you once again. At the moment I am planning on being able to generate my letters and symbols for my Water Curtain from the array instead of hardcoding each letter and symbol in a lookup table. Ralph |
July 05, 2011 by Ralphxyz |
Paul your code prompts more questions:
I think I am hijacking my own thread but You have #define F_CPU 14745600 commented out! Why, what does this mean to my program? Am I using a internal oscillator? Ralph |
July 05, 2011 by Noter |
I just forgot to put it back before posting. I set the F_CPU in my makefile and pass it via the compile command line so to avoid a warning when I build I comment out the line in the source file. No, it doesn't change anything as far as which clock source is being used and probably makes no difference at all with the nerdkit delay code. Mostly it only makes a difference if you use F_CPU to calculate values to setup timers or bit rates. The only way to use the internal clock is to set the fuses appropriately. |
July 16, 2011 by BobaMosfet |
Instead of commenting out F_CPU, make the compiler tell the difference:
This way, if it isn't defined, it uses your define, otherwise, it relies on was was already defined in a previous header file. BM |
July 19, 2011 by Ralphxyz |
Ok so why do all 8 of the leds flash? I just added an array, to Noter's code, to get a sequence of leds lit but all leds light.
Ralph |
July 19, 2011 by bretm |
The "standard" Nerdkits makefile doesn't include array initialization constants in the .hex file. It uses "-j" parameter in one place to specify which binary sections of the file are included so the ".data" section, usually included by default, is omitted. If this is what's going on in your project's makefile then the array could contain all 0xFF's which is the typical "blank" value in .hex files, which would make all the LEDs light. |
July 19, 2011 by Noter |
Maybe it's working but a 20ms blink is so fast all the leds appear to be on all the time? |
July 19, 2011 by Ralphxyz |
I have the .data in the Makefile:
But when I compile I get this:
I slowed the flash rate down and still get the same all on/off! If I use the line_display[] array:
If I initialize the array uint16_t line_display[255] = {0}; the leds are off all of the time. If I do not initialize the line_display[] array then I get a seemingly random pattern of leds lighting! If I hard code PORTD = 3; then the value is displayed! This really should not be so hard I could easily do this in C# on a Windows PC. I'll try using a Struct and PROGMEM but it seems the benefit of using a Struct is that you essentially make the Struct a Type and you can reuse it. I would have to have dedicated Structs for each configuration which seems contrary to what I have read about using Structs. Of course I am back to trying to use PORTD without the LCD or serial port so I can only debug by the action of the leds. I can not get 8 pins using PORTB or PORTC probable I could use both PORTS but that would be pushing my programing abilities. Ralph |
July 19, 2011 by Noter |
When I don't have the .data in the makefile, I get 0xFF for all the values. Then when I put in the .data, it works fine. So just to be sure it is not an initalization issue try initializing after you declare the array like so - this should work no matter about the .data in the makefile.
|
July 19, 2011 by Noter |
By the way, I think your missing a -j in the makefile. This is what works for me:
|
July 19, 2011 by Noter |
Some time ago I wrote these macros for port/pin manipulation and I've used them quite a bit since. With these you can assign whatever free pins you have available to control your solonoids which will allow you to keep your display connected. You will really need the display working as you progress and your program becomes more and more complex. The following sample is based on your test program and demonstrates how to use the macros. It will need a few tweaks for your environment (F_CPU, etc.) but otherwise it's ready to go. Look it over and see what you think.
|
July 20, 2011 by Ralphxyz |
Thanks Paul, I have seen your macros before but never could comprehend them. I am actually starting to understand them this time. I am trying to just get something working in order to establish the feasibility of what I am trying to do with my Water Curtain, so at the moment I do not care about the LCD or other aspects I just need to be able to see something predictable happen to the LEDS (water flow). I have already spent a lot of time but do not know if it will actually work or what I might have to do to make it work! If your -j correction to the make file does not get the arrays working I am just going to put all of the configurations inline. This works:
and on and on but it works and I get my test patterns so now I can test my solenoids and entire setup. I have my four components (slantLeft, slantRight, Bar and Up) each repeated (full configuration) 10 times. I think these configurations will give me all the views I need to prove the feasibility of my concept. It takes up:
So I have lots of room for more configurations as I am using an ATmega328p micro. It actually looks very impressive on the LEDs so now I can try with water. I'll also setup my strobe light with manual speed control to see if I can get it to match the water drip. Hopefully I'll be posting a video so you all can see what you have all been working on :-) Ralph |
Please log in to post a reply.
Did you know that two resistors can be used to make a voltage divider? Learn more...
|