AVR GCC : Microcontrollers Port Management (Part 1)

Posted on

Management of the ports in the AVR GCC. Registers and DDRx PORTx.
FSE. Bitwise operations.
The function of delay. Unconditional jump in the program.

Ports of the microcontroller – this I / O devices, allowing the microcontroller to send or receive data. The default port of the AVR microcontroller has eight bits of data that can be transmitted or received simultaneously. Each grade (or bit) corresponds to the output (pin) microcontroller. The legs of the microcontroller is also called pins. To designate ports use letters A, B, C, etc. Number of I / O ports varies depending on the model of the microcontroller. Any port of the microcontroller can be configured as input or output.

To do this, you should write to the corresponding port register DDRx desired value. In addition, as an input or output can be configured separately, any output (pin) port. In any case, if  you want to configure all port or a single output, you will need to work with DDRx register.

DDRx – direction data register. This register determines whether one or the other output port of input or output. If a discharge register DDRx contains a logical unit, the corresponding port pin is configured as an output, otherwise – as an input. The letter x in this case should designate a port name with which you work. Thus, for this will be port A register DDRA, Port B – DDRB register, etc.

Using the AVR GCC , written in the appropriate register a certain value can be one of the following methods.

For all the ports at once.

DDRD = 0xFF;

All D port pins are configured as outputs.

0xFF – hexadecimal representation of ff, where 0x is the prefix used to write hexadecimal numbers. In decimal representation this will be number 255, and in binary it would look like 11111111. That is, all bits of DDRD register will be written to logic one.

In AVR GCC to represent binary numbers use the prefix 0b. Thus, the number 11111111 to be presented in the program as 0b11111111. We can write the previous command for better readability.

DDRD = 0b11111111;

While such a record and is more obvious when you configure the ports made ​​to use the hex representation of numbers.

In order to configure all port pins as inputs D, you should write down all the bits in the register DDRD logical zeros.

DDRD = 0x00;

in DDRD register can be written and the other numbers.  For example:

DDRD = 0xb3;

0xb3 – hexadecimal representation of the number 179. In binary it would look like 10,110,011. That is part of the output port D is configured as outputs, and some – as inputs.

PD0 – 1 (out)
PD1 – 1 (out)
PD2 – 0 (input)
PD3 – 0 (input)
PD4 – 1 (out)
PD5 – 1 (output)
PD6 – 0 (input)
PD7 – 1 (out)

Each bit registers DDRx can be installed separately. For example, to configure a separate output PD2 as output, we need a corresponding bit in DDRD register to record 1. To do this, use the following construction.

DDRD | = 1 <<2;

1 <<2 – performs the shift left 2 bits, that is to the right added two zero bits, and turns 100, and the sign “|”, standing in front of the sign of the assignment “= “performs bitwise logical operation of addition.

With the addition of a logical 0 +0 = 0, 0 +1 = 1 1 +1 = 1 . The logical addition in another operation called OR

Thus, the bits stored in register DDRD, added to a binary 100, presented at the 8-bit microcontroller register as 00,000,100, and the result is written back to register DDRD.

To configure the output PD2 separately as an input, we need a corresponding bit in DDRD register write 0. To do this, use the following construction.

DDRD & = ~ (1 <<2);

In this case, the result of shifting units at two positions to the left is inverted by inverting the bit-operations, denoted by a ” ~ . ”

When we get the inversion instead of zeros, but instead of ones – are zero. This logical operation is also called the operation NOT.

Thus, when one bit inversion 00,000,100 11,111,011 we get. (For information about working with numbers in the microcontroller, see the sidebar below.)

The resulting number by performing a bitwise logical multiplication is multiplied by k number stored in the register DDRD, and the result is written into the register DDRD.

When the logical multiplication of 0 * 0 = 0, 0 * 1 = 0, 1 * 1 = 1 . The operation of logical multiplication operation is also called AND.

So we shifted to the left by two positions ones transformed with inversion at zero and multiplied by the corresponding bit which stored in register DDRD. When multiplied by zero, we get zero. Thus, the PD2 bit is zero.

Gravatar Image
Robotics Technology , Tutorial and News

Leave a Reply

Your email address will not be published. Required fields are marked *