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
jfeister
PostPosted: Mar 30, 2012 - 06:12 AM
Rookie


Joined: May 02, 2010
Posts: 23


Hi all,

I must be missing something very simple here. I've got an Atmega128 hooked up on my STK600. PORTA is connected to the LED's. Nothing else is connected. For some reason setting other ports to output in an external function seems to screw things up.

first LED off = YIPEE!Very Happy
first LED on = WTF?!?Evil or Very Mad

Code:

#include <avr/io.h>

// function prototype(s)
void initializeDisplay(void);

int main(void)   {
   initializeDisplay();
   PORTA = 0x01;
}

void initializeDisplay(void)   {
   DDRA  = 0xff;
//   DDRB  = 0xff;
}

= YIPEE!Very Happy

Code:

#include <avr/io.h>

// function prototype(s)
void initializeDisplay(void);

int main(void)   {
   initializeDisplay();
   PORTA = 0x01;
}

void initializeDisplay(void)   {
   DDRA  = 0xff;
   DDRB  = 0xff;
}

= WTF?!?Evil or Very Mad

Code:

#include <avr/io.h>

int main(void)   {
   DDRA  = 0xff;
   DDRB  = 0xff;
   PORTA = 0x01;
}

= YIPEE!Very Happy

Hurrrr?
 
 View user's profile Send private message  
Reply with quote Back to top
ka7ehk
PostPosted: Mar 30, 2012 - 06:48 AM
10k+ Postman


Joined: Nov 22, 2002
Posts: 12055
Location: Tangent, OR, USA

Not sure if this is a problem, but I not expect the ports to be in any predictable state after the code exits from main. It will execute your code and be done in a few microseconds - far to fast for you to even see what happens.

At the very least, I would do
Code:
#include <avr/io.h>

int main(void)   {
   DDRA  = 0xff;
   DDRB  = 0xff;
   PORTA = 0x01;
 
   while (1)  {
    }
}


Jim

_________________
Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA

"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: Mar 30, 2012 - 07:13 AM
10k+ Postman


Joined: Nov 17, 2004
Posts: 13852
Location: Vancouver, BC

avr-gcc puts an automatic while(1) at the end of main, so it shouldn't make a difference. If you were using interrupts, then that would be a different matter since it also puts a cli() just before the while(1).

I suspect that the real problem is that you didn't clear the mega103 compatibility fuse (the mega128 comes from the factory with it set). Because of this, the function call returns to a random address since the stack pointer is set incorrectly. Either clear the fuse, or compile for a mega103.

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
jfeister
PostPosted: Mar 30, 2012 - 03:16 PM
Rookie


Joined: May 02, 2010
Posts: 23


Yup, that did it. Thanks Koshchi!
 
 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