Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
acurseoffear
PostPosted: Mar 11, 2010 - 07:27 AM
Newbie


Joined: Feb 24, 2010
Posts: 4


Hello all,
I spent last night's 5 hours trying to figure out why my array literal is not initializing to the literal values given (nasty words didn't help). All the values are 255 instead it seems.

sample showing off my woes:
code should just blink some lights on PORTB

Code:

#define F_CPU 8000000UL

#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>

uint8_t array[] = {42, 86, 125, 220}; //meaningful literals

int main()
{
   uint8_t i;
   //static uint8_t array[] = {42, 86, 125, 220}; //test 1
   
   //array[0] = 42; array[1] = 86; array[2] = 125; array[3] = 220; //test 2 --hack--
   DDRB = 0xff;
   
   while(1)
   {
      for(i = 4; i--;)
      {
         _delay_ms(250);
         PORTB = array[i]; // =(
         //PORTB = i; //test 3
      }
   }
}



tried:
- putting the array inside and static didn't help
- doing the initializing myself works (but is messy, worse in my real program)
- putting the variable i to the port works, so i haven't broken the leds yet

close to the same problem, but for exmem


Any insight would be awesome.

env:
stk500
atmega8515
winavr 20100110
(avr-gcc -mmcu=atmega8515 -I. -Os -Wall -std=gnu99 main.c)
win7 x64

thanks
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Mar 11, 2010 - 08:39 AM
Raving lunatic


Joined: Feb 12, 2005
Posts: 7302
Location: Cratfield, England

What makes you think that the program does not work?

It simulates perfectly. loading 0xDC, 0x7D, 0x56, 0x2a ... into PORTB.

The STK500 leds are active-low. So 0xDC will light LED0, LED1, LED5.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
acurseoffear
PostPosted: Mar 11, 2010 - 09:32 AM
Newbie


Joined: Feb 24, 2010
Posts: 4


In studio it worked for me too. Course this isn't the first time studio didn't match hardware for me; always skeptical now.

I'll have some time to test this program on an atmega16/stk500 today to rules out the 8515, but I can't see any reason for it to be the problem.
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Mar 11, 2010 - 10:38 AM
Raving lunatic


Joined: Feb 12, 2005
Posts: 7302
Location: Cratfield, England

acurseoffear wrote:
In studio it worked for me too. Course this isn't the first time studio didn't match hardware for me; always skeptical now.


Why do you say this?

It is clearly documented that Studio does not simulate a Terminal or TWI etc.

But executing regular AVR instructions, simulating Timers and interrupts should work fine.

You are hardly trying to do anything complicated.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
sternst
PostPosted: Mar 11, 2010 - 11:30 AM
Resident


Joined: Jul 23, 2001
Posts: 975
Location: Osnabrueck, Germany

Your avr-gcc command line implies that you don't use the Makefile generated by AVR-Studio. Show us how you generate the hex-file. Uninitialized global variables are normally the result of not adding the .data section to the hex-file.

_________________
Stefan Ernst
 
 View user's profile Send private message  
Reply with quote Back to top
acurseoffear
PostPosted: Mar 12, 2010 - 04:11 AM
Newbie


Joined: Feb 24, 2010
Posts: 4


Thanks for mentioning the make rules. Hadn't looked at that yet. I got it working changing the first objcopy to:

Code:

$(OBJCOPY) -O $(FORMAT) -j .text -j .bss -j .data $< $@


what was run:
Code:

avr-gcc -c -mmcu=atmega8515 -I. -Os -Wall -std=gnu99 main.c -o main.o
avr-gcc -mmcu=atmega8515 -I. -Os -Wall -std=gnu99 main.o   --output main.elf
avr-objcopy -O ihex -j .text  main.elf main.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
        --change-section-lma .eeprom=0 -O ihex main.elf main.eep


Thanks a lot for the tip. Also good eyes.
 
 View user's profile Send private message  
Reply with quote Back to top
sternst
PostPosted: Mar 12, 2010 - 08:36 AM
Resident


Joined: Jul 23, 2001
Posts: 975
Location: Osnabrueck, Germany

There is no need for the "-j .bss".

_________________
Stefan Ernst
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 12, 2010 - 09:46 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 33138
Location: (using avr-gcc in) Finchingfield, Essex, England

If you want to use your own Makefile then why not use Mfile? - the template has had a LOT of thought put into it and you won't be caught by silly mistakes like this.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
acurseoffear
PostPosted: Mar 12, 2010 - 07:19 PM
Newbie


Joined: Feb 24, 2010
Posts: 4


Didn't know that existed. Now that I'm looking at it and mine; it looks like mine was based on mfile some time in its past. Thanks for the tip.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits