optimization problem

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Compiling code for an ATMega328 with AVRStudio4, using avrgcc tools.

I tried to turn on the pullup on PD4 (pin 6). It wouldn't turn on,

unless I also set the pullup on PD1.

Here is the C code, and the generated assembly from the .lss file:

(Data direction register already set).

 

PORTD |= 0x00010000;

 EC6:  8b b1  in r24, 0x0b

 EC8:  8b b9  out 0x0b, r24

(this code doesn't set the PD4 pullup)

 

PORTD |= 0x00010010;

 EC6:  5c 9a  SBI 0x0b, 4

(this code does set the PD4 pullup)

 

Turning the optimization level from -Os to O0 makes the thing work, but greatly increases code size.

How could either of the above be correct compilations?

Thanks for any thoughts,

Jack

This topic has a solution.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please post your complete program by clicking on the <> in the taskbar of the editor and pasting your code in the window that pops up.

 

The internal pullups are usually set once only.  To use the pin as an output you write to the DDR register to set the PORT pin direction to output.  Then you can write the value to PORT pin to see it go high or low.

 

Any reason why you are using the very old AVR Studio 4 and not the most recent Atmel Studio 6.2sp2?

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Sun. Feb 8, 2015 - 08:02 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you really want 0x? Maybe 0b?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you really want 0x? Maybe 0b?

Good eye, I totally missed that!

 

What he has in the first instance is PORTD |= 0x00;  and in the second is PORTD |= 0x10;

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ezharkov wrote:
Did you really want 0x? Maybe 0b?
+1

 

Since PORTD expects an 8-bit quantity, the rest is truncated.  So this:

PORTD |= 0x00010000;

... becomes this:

PORTD |= 0x00;

... which is synonymous with this:

PORTD |= 0b00000000;

 

Whereas this:

PORTD |= 0x00010010;

... becomes this:

PORTD |= 0x10;

... which is synonymous with:

PORTD |= 0b00010000;

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We can always count on Joey for being more precise.wink

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Sun. Feb 8, 2015 - 08:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm not using the pin as an output, but as an input, depending on the internal pullup resistor.

I'm setting them at this point in the program because some initialization code, for the LCD or for

the low-speed USB port, doesn't leave stuff in the state I need to use pins not otherwise needed

as switch inputs.

I can work around this now; I just wondered how the compiler could come up with the assembly above.

 

(I'm using AVRStudio 4 because on my older machine running XP3, it takes a long time to start up, 

and I don't want to spend the time getting familiar with a new environment.)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wholly mackerel! Of course, you're right; a bone-headed mistake.

Thanks!

 

Jack

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using AVRStudio 4 because

...it's reliable and it works... wink

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using AVR Studio 4 because it's reliable

-and it works,  

-and because it doesn't take two+ minutes to load on a PC that has a multi-gigahertz CPU  and fast hard drive, 

-and because it doesn't load millions of bytes worth of program code to support every possible type of CPU in Atmel's company library,

-and because I have already learned how to use it productively (instead of spending hours learning Studio 6 which does the same thing as Studio 4 but has a completely different user interface).