ATMega 128 - PORTD

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

Hello Everyone,

 

I'm learning to code for ATMega 128 with Assembly and I encountered the following problem:

 

When I compile/simulate my program the PIND bit 0 is set to 0 (where all the other bits are set to 1) and no matter which code is in main.asm, even before any instruction is executed, this bit is always set to 0. After initializing the DDRD with bit 7 and bit 6 as outputs and bit 0..5 as inputs, all the bits are correctly set except bit 0 which still maintains its value as 0 on PIND.

 

Any idea why this is happening? Is there any way to set PIND bit 0 to 1 as default like all the other PINs on PORTD (and other ports)?

 

I've tried to search for this and got 0 matches and I'm using ATMEL Studio 7 (latest version) to code my programs for ATMega 128.

 

Thanks for your attention!

 

Best regards

Last Edited: Wed. Oct 9, 2019 - 01:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AndreMP wrote:
Any idea why this is happening?

Sorry the crystal ball is in the shop, perhaps if you would be so kind as to show some code (specially the port init code) someone here may be able to help.

What is the state of the M103C fuse?  Default is M103 compatibility mode is on!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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


On the basis that a picture is worth a 1,000 words I see what he means:

 

 

The surprise to me here is not the bit 0 that is at level 0 but the other 7 bits that have mysteriously defaulted to level 1 ? For the other ports (without doing anything) I see:

 

 

 

 

So it looks like a bit of a crap shoot as to what you might get in fact !! As the datasheet actually says:

 

(not surprisingly because at power on they are untied inputs so could have any random value)

 

So perhaps the authors of the sim (which does not attempt to simulate external hardware) thought  - "any random value will do" ??

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

Hello clawson, thanks for the pictures, I was on the process of taking them myself.

 

The thing is, when I upload any program to the ATMega 128, it does exactly the same as it's on the simulator. E.g. PINA all set to 1 and PIND bit 0 set to 0. Also I must add that I'm using a IO board where a switch is activated by value 0 and a LED turns on with also value 0, hence why I was "surprised" by PIND bit 0 having the 0 value.

 

Also If I set DDRD with bit 7 and bit 6 as 1s and the rest as 0s, the bit 0 is still at 0, where I assume it should be 1?

 

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

AndreMP wrote:
hence why I was "surprised" by PIND bit 0 having the 0 value.
Well it's true that the simulation models in AS7 are built (by Atmel) from the VHDL of the chip so it is perhaps no accident that they are wired and default in the same way.

 

Why does any of this actually matter? In reality, as soon as you connect a signal to PIND.0 then the value you read will reflect the electrical level anyway. If the pin is going to be O.C. (maybe with a switch to Vcc/Gnd?) then you'd either be enabling the internal pull-up or using an external pull-down so the state would be whatever you choose in the O.C. state anyway.

 

If you don't plan to use PIND.0 at all then just mask any read of PIND to exclude it - then it won't matter what value it has!

Last Edited: Wed. Oct 9, 2019 - 02:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But I will need to use PIND.0 as a switch, where the switch is activated with value 0, hence I need it to be 1 as "default" (after initializing and I can't seem to do that).

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

 

Yes but think about the switch circuit. You will use one of:

On the left when the switch is open the weak pull-up (10..50K) makes the input read '1' and then it goes to '0' when the switch is closed.

On the right when the switch is open the weak pull-down (10..50K) makes the input read '0' and then it goes to '1' when the switch is closed.

 

The point being that the input never "floats". Now as it happens Atmel did you a favour when they designed the AVR to save you the cost of one resistor. The AVR input ports have the potential to use an internal pull-up. So it's more like:

 

So you just turn on that internal pull up (by writing 1 to the PORT bit while DDR=0 (input)) and now this guarantees the pin reads 1 when open circuit from the switch. The switch closes, makes a connection to ground that over-rides the pull up (which is about 50K) and the pin then reads 0.

 

It's true that pull-ups force you to use active-0 switches but that's easily overcome with bit inversion in the software anyway.

 

So it simply does not matter what the high-Z pin reads as you won't use high-Z.

 

 

Last Edited: Wed. Oct 9, 2019 - 03:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AndreMP wrote:
he thing is, when I upload any program to the ATMega 128, it does exactly the same as it's on the simulator. E.g. PINA all set to 1 and PIND bit 0 set to 0.

C'mon.  when that program [show the program] is the voltage RIGHT ON THE PIN of each of the mentioned AVR pins.

 

I say "c'mon", since if untouched input pins have mystery values, then many of my production designs would not work, would they? 

 

I can upvote Cliff's reply, but I'd still like to get an answer to the original assertion about the real chip, since on the surface it is ridiculous.

 

Now the simulator...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.