0
\$\begingroup\$

In AVR ATmega2560 microcontroller, there're 3 register for I/O port: DDxn, PORTxn, and PINxn

DDxn decide the direction of each pin while other two, according to the datasheet

  • PINxn toggle the value of PORTxn by written 1 into PINxn

  • If pin is input (DDxn is written 1), written 1 to PORTxn turn on pull-up resistor and written zero turn it off

  • If pin is output (DDxn is written 0), port pin is driven high (one) if PORTxn is written 1 and low (zero) if PORTxn is written zero

I don't understand what these term means: pull-up resistor, driven high and low. What is the difference between 2 terms. What do they do with I/O port and what is the use of them? Can anybody give me explanation and example.

I also don't know what is register used for store address for I/O. I means for input and output there must be some address tell where input data is stored or address tell where data is ouput. Furthermore, if 8 pin of one port is configured to be mixed between output and input then how the data of each bit of each pin is organized to be store in a register (which contain 8 bit)

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

Driven High - The signal is driven to your positive voltage (VCC)

Driven Low - The signal is driven down to ground (GND)

Pull-up resistor: In short, this means that there is an internal resistor that is connecting the pin to your positive voltage, thus driving it high unless a connection to GND happens. Sparkfun has an amazing tutorial

Should your PORT register have a mix of pins defined as inputs and outputs, you can still read the values just fine. To read the pins defined as inputs, you'd read the PIN register like so: uint8_t inputs = PINx;

As for the outputs though, you can just set the PORTx register and you won't have to worry about it affecting the inputs: Say you have pin 2 defined as an input and the others as outputs, writing a value like this, PORTx = 0b10001111 , won't affect the value for the input pin and will set all the others to the respective values. Arduino has a good article on this

\$\endgroup\$
1
\$\begingroup\$

Figure 13-2 in the datasheet (which shows a single bit of an I/O port) should make it clear. I have highlighted some parts to be discussed:-

enter image description here

The Data Direction Register (red) controls a Tri-State buffer which connects the output of the PORT flipflop (green) to the pin. When the DDR bit set to input the buffer is disabled and the PORT output is disconnected from the pin.

The triple input AND gate (blue) enables the pullup resistor only when the DDR bit is set to input, the PORT bit is a 1, and PUD (pullup disable) is not active.

The output PORT (green) is a register which you can write to and read. When you read it you are seeing the value you put there, which may not be the same as the level on the pin.

Reading the PIN 'register' (purple) will always give you the current level on the pin. The value may be synchronized to the I/O clock but it is not stored - there is no flipflop to hold its value as the PORT and DDR registers have.

Writing the PIN 'register' actually toggles the PORT bit. It does this by feeding the output of the PORT flipflop back into its input and clocking it to create a divide by 2 counter (yellow).

\$\endgroup\$
0
\$\begingroup\$

If a GPIO pin is configured as an input, PORTxn determines whether the internal pullup is enabled (1) or if the input is floating (0).

If a GPIO pin is configured as an output, it reflects the value of PORTxn (0=GND, 1=Vcc).

The current value on a pin is always available as PINxn.

When a port is mixed input and output, bit manipulation operations must be used when reading or writing the DDRx, PORTx, and PINx registers.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.