📜 ⬆️ ⬇️

Device and operation of input / output ports of AVR microcontrollers. Part 1

Work I / O ports

After reviewing this material, in which everything is described in great detail and in detail with many examples, you can easily master and program the input / output ports of AVR microcontrollers.


')
An example will be considered on an ATMega8 microcontroller.

We will write the program in Atmel Studio 6.0 .

We will emulate the circuit in Proteus 7 Professional .

The microcontroller communicates with the outside world through the input / output ports. The scheme of the input / output port is indicated in the datasheet

image

But for a beginner, it's pretty difficult to figure out the scheme. Therefore, we simplify the scheme:

image

Pxn is the port name of the microcontroller, where x is the letter of the port (A, B, C or D), n is the number of the port's discharge (7 ... 0).
Cpin - stray port capacity.
VCC - supply voltage.
Rpu - disconnectable load resistor (pull-up).
PORTxn - bit n register PORTx.
PINxn is the n-bit of the PINx register.
DDRxn is the n register bit of the DDRx.

Consider what is the output of the microcontroller. At the input of the microcontroller there is a small protection of two diodes (see 1) , it is designed to protect the input of the microcontroller from short-term voltage pulses exceeding the supply voltage. If the voltage is higher than the power supply, the upper diode will open and this voltage will be bled to the power bus, where the power supply and its filters will already fight with it. If the input gets negative (below zero) voltage, it will be neutralized through the lower diode and extinguished to the ground. However, the diodes are frail there and this protection only helps against microscopic pulses and interference. If the voltage on the microcontroller’s foot is 6-7 volts with 5 volts of power, then the internal diodes will not save it.

A capacitor (see 2) is a parasitic output capacitance. Although she is tiny, but present. Usually it does not take into account, but it is. Don't bother with your head, just know it.

Next are the keys of management (cm.3,4) . Each key is subject to a logical condition, which are drawn in the figure. When the condition is satisfied - the key is closed.

Each AVR microcontroller port (usually named A, B, and sometimes C or even D) has 8 bits, each of which is tied to a particular leg of the case. Each port has three special registers DDRx , PORTx and PINx (where x corresponds to the letter of port A, B, C or D). Register purpose:

DDRx — Configure the bits of port x for input or output.

PORTx - Controls the status of the outputs of port x (if the corresponding discharge is configured as an output), or by connecting an internal pull-up resistor (if the corresponding discharge is configured as an input).

PINx - Reads the logical levels of the bits of port x.

PINx is a read register. From it you can only read. The PINxn register contains information about the actual current logic level on the port pins. Regardless of the port settings. So if we want to find out what we have at the input - we read the corresponding bit of the PINxn register. Moreover, there are two borders: the boundary of the guaranteed zero and the boundary of the guaranteed unit - the thresholds beyond which we can clearly define the current logic level. For a five-volt power supply, this is 1.4 and 1.8 volts, respectively. That is, when the voltage drops from the maximum to the minimum, the bit in the PINx register switches from 1 to 0 only when the voltage drops below 1.4 volts, but when the voltage rises from minimum to maximum, switching the bit from 0 to 1 will only happen when the voltage reaches 1.8 volts. That is, there is a switching hysteresis from 0 to 1, which eliminates the chaotic switch under the action of noise and interference, and also eliminates the erroneous reading of the logic level between the switching thresholds.

By reducing the supply voltage, of course, these thresholds are also reduced.

DDRxn is the port direction register. A port at a specific point in time can be either an input or an output (but this does not matter for the state of the PINxn bits. You can always read the real value from PINxn ).

DDRxy = 0 - output works as INPUT.

DDRxy = 1 - the output works on the OUTPUT.

PORTxn - output state control mode. When we set up the output to the input, then the type of input depends on PORTx (Hi-Z or PullUp, more on that below).

When the foot is set to output, the value of the corresponding bit in the PORTx register determines the status of the output. If PORTxn = 1 then on pin log.1, if PORTxn = 0 then on pin log.0.

When the foot is tuned to the input, then if PORTxn = 0, then the output is in Hi-Z mode. If PORTxn = 1 then the output in PullUp mode with a 100k pull-up resistor to the power supply.

Table. Configuration of port pins.

DDRxn PORTxn I / O Comment
0 0 I (Input) Input High-impedance input. (I do not recommend to use, as they may be induced power supply)
0 1 I (Input) Input Tightened internal resistance.
1 0 O (Output) Output The output is low.
1 1 O (Output) Output The output is high.

The overall picture of the port is shown in the figures:

image

Fig. DDRxn = 0 PORTxn = 0 - Mode: HI-Z - high impedance input.

image

Fig. DDRxn = 0 PORTxn = 1 - Mode: PullUp - input with pull-up to log.1.

image

Fig. DDRxn = 1 PORTxn = 0 - Mode: Output - output log.0. (almost GND)

image

Fig. DDRxn = 1 PORTxn = 1 - Mode: Output - output log.1. (almost VCC)

Hi-Z input - high-impedance input mode.
This mode is enabled by default. All keys are open and port resistance is very high. In principle, compared to other modes, it can be considered infinity. That is, the electrically output is not connected anywhere at all and does not affect anything. But! At the same time, he constantly reads his state into the PINn register and we can always find out what we have at the entrance - one or zero. This mode is good for listening to any data bus, because It has no effect on the tire. And what will happen if the entrance is hanging in the air? And in this case, the voltage will jump on it depending on external interference, electromagnetic interference and, in general, on the phase of the moon and the weather on Mars (an ideal way to chop random numbers!). In this case, the unstable sine of 50 Hz is very often on the port - pickup from the 220V network, and in the PINn register 0 and 1 will change with a frequency of about 50 Hz

Input PullUp - input with a pull.
With DDRxn = 0 and PORTxn = 1, the lift key closes and a 100kΩ resistor is connected to the line, which instantly brings the line not connected anywhere to the log.1 state. The purpose of the suspenders is obvious - to prevent chaotic changes in the state at the entrance under the influence of interference. But if a logical zero appears at the input (short circuit of the line to earth with a button or another microcontroller / microcircuit), then a weak 100kOhm resistor will not be able to hold the line voltage at log.1 and the input will be log.0.

Exit mode
Here, I think everything is clear - if we need to output log.1 to the port, we turn on the output port ( DDRxn = 1) and issue log.1 ( PORTxn = 1) - this closes the upper key and a voltage appears at the output close to nutrition. And if log.0 is needed, then we turn on the output port ( DDRxn = 1) and issue log.0 ( PORTxn = 1) - this opens the lower gate, which gives about zero volts at the output.

Source: https://habr.com/ru/post/253213/


All Articles