Do Output Pins Send a 0 When Set?

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

Hi all,

 

I'm wondering, when I set a PORT pin to output, does anything get written to that pin immediately after its set? Clarifying; I have a few SS lines hooked up to pins on PORTD of my ATmega16u2 and i'm wondering if all SS lines will see a value of 0 being sent as soon as I set DDRD as outputs. 

 

I don't think these SS lines would see any values until the PORTD pins they're individually wired to are explicitly set high or low in code, but I want to make sure.

This topic has a solution.
Last Edited: Thu. Aug 10, 2017 - 04:29 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should write to the port pin the level you want just before you configure DDRD as output. Otherwise you will have a random value.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I don't think these SS lines would see any values until the PORTD pins they're individually wired to are explicitly set high or low in code

 When your AVR is first powered on, and the pins are in input mode (tri-stated, or high-impedence), they WILL have a random value (assuming typical connection to another high-impedence digital input.)  If you need to ensure a particular state very early, you will need external pullup or pulldown resistors.

 

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

All of the above are true but do not quite answer the original question, as posed.

 

1. After reset, all of the PORTx bits are 0 on all ports.

 

2. After reset, all of the DDRx bits are 0 on all ports. This makes all GPIO port pins to be inputs. Previous condition specifies NO pull-up.

 

3. Hence, after reset, the logic state of all port pins is undefined, since they are inputs with no internal pull-up.

 

4. After reset, if you write a 1 to a DDRx bit, that pin becomes an output. Since the corresponding PORTx bit is still zero, it is a LOW OUTPUT. 

 

5. BUT, after an arbitrary change of input to output on a port pin, the logic state of that new output is totally governed by the state of the corresponding bit in the PORTx register.

 

6. If you want a glitch-free direction change from input to high output, FIRST set the bit in PORTx. This turns on the pull-up and the input is at a high level. THEN, set the the bit in DDRx. This changes the pin from input with pull-up on to high output.

 

NO, there is nothing that sets a port pin to a specific logic level when it is changed to an output. If you want to insure that the pin is always high except when explicitly set to be a low output, add an external pull-up resistor.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net