Como fazer a interface do LED com o microcontrolador AVR (ATmega16) – (Parte 4/46)

How to interface the LED with the AVR microcontroller (ATmega16) – (Part 4/46)

ATmega16 has 32 I/O pins for communicating with external devices. Before interfacing with external devices, these pins must be configured as input or output pins. This article demonstrates the basic I/O operation of the ATmega 16 using LEDs.
All four ports can be configured to read an input from some external device or to provide output to any external device as per the application. For example a switch is connected to a specific pin, that pin must be configured as input to read the values ​​from the switch (external device in this case) and if you are connecting an LED to any port pin then that specific pin must be configured as output to transmit the signal to the LED (external device in this case). A single port can be configured so that some of the pins on the same port are input and others are output.
Configuring IO ports:
Each port (PORTx, x = A or B or C or D) of AVR microcontrollers has three associated registers:
1. DDRx: Data direction register, to define the direction of each PORTx pin and configure it to be as input or output.
two. PORTx: The values ​​that must be provided at the port output are written to this register. These values ​​act as input to the device connected to the microcontroller output port (through the configured PORTx output pins).
3. PINx: This register stores the input value of the connected external hardware, when the port is configured as an input port. Input data is read from the PINx register.
Therefore, the first step in configuring or initializing any IO port is to define its direction in the data direction register (DDRx) to define the behavior of individual pins as input or output. A high value (1) in any bit of the DDRx register means that the corresponding pin is set to output and vice versa.
Example: Considering the switch-LED scenario, suppose a switch is connected to Pin5 (PORTD.4) of the PORTD and the LED is connected to Pin8 (PORTD.7) of the PORTD. Now we need to initialize the pins accordingly. The other PORTD pins are not used in this case, so they can be ignored from now on.
STEP 1: To configure PORTD.4 as input, the value of Pin-5 (DDRD.5) in the DDRD register is set to 0.
DDRD.7
DDRD.6
DDRD.5
DDRD.4
DDRD.3
DDRD.2
DDRD.1
DDRD.0
0
Fig. 2: DDRD register bit value to define PORTD as input to the AVR
Step 2: To initialize PORTD.7 as output, the value of Pin-8 (DDRD.7) in the DDRD register is set to 1.
DDRD.7
DDRD.6
DDRD.5
DDRD.4
DDRD.3
DDRD.2
DDRD.1
DDRD.0
1
0
Fig. 3: DDRD register bit value to define PORTD as output on the AVR
Step 3: The rest of the pins can have any value as they are not being used in this case. The default values ​​of the DDRx register are 0 for each pin, that is, all AVR microcontroller ports are initialized as input.
DDRD.7
DDRD.6
DDRD.5
DDRD.4
DDRD.3
DDRD.2
DDRD.1
DDRD.0
1
0
0
0
0
0
0
0
Figure 4:

Therefore, the DDRD value will be initialized to 0x80.

Figure 5:
The above command can also be written this way:
Figure 6:
Where, 0b is the symbol used to represent binary numbers and next is the actual 8-bit value.
Setting the pull-up value:
All four ports of the Atmega16 are equipped with internal software-controlled pull-up resistors. Each port pin can be obtained by setting the values ​​in the PORTx register. The following table illustrates the actual pin state with different combinations of DDRx and PORTx values.
DDRx
PORTx
I/O State
Pull up
Description
0
0
Prohibited
No
PORTx pins set to input and pull-ups disabled.
0
1
Prohibited
Yes
PORTx pins defined as input with pull up enabled
1
0
Exit
No
PORTx pins set to output and pull-ups disabled.
1
1
Exit
Yes
PORTx pins set to output with pull ups enabled.
Fig. 7: Pin status for different bit combinations of DDRx and PORTx on the LED interface
Example: The following commands can be used to disable and enable pull-ups in PORTD.
PORTD = 0xFF; //PORTD pins pulled up.
PORTD = 0x00; //Disables pull-up registers.
To understand the above commands, a simple LED blinking experiment is explained below.
Circuit Description:
Connect the circuit as shown in the circuit diagram. There is no crystal in the circuit. This project uses the AVR microcontroller's built-in crystal, thus avoiding the need for an external crystal. The value of the internal crystal ranges from 1MHz to 8MHz, which can be adjusted using the fuses. (Readers can connect the external crystal but modify the fuse bits accordingly. Fuse bits are explained in later articles).
Code explanation:
#include
To include all AVR microcontroller input and output files.
#include
WinAvr has a built-in function to provide delay. To use the delay functions, include the above header file.
DDRA=0xFF;
To make port A the output port
PORT=~PORT;
Whatever the DOOR value is, praise it and send it back to the DOOR.
_delay_ms(1000);
A delay function provides a delay of 1000 milliseconds.
To understand compiling and running the above program, see the Working with AVRstudio tutorial.
What should be observed?
Likewise, a code could be for the LED to blink on all four DOORS. It is observed that all pins are working correctly, except pins PC2, PC3, PC4 and PC5. The reason is that AVR microcontrollers have built-in JTAG, which needs to be disabled to use these pins as I/O pins. Disabling JTAG is explained in a separate article.

Project source code

###


 // Program to blink LED using AVR microcontroller (ATmega16)
#include
#include int main(empty) { DDRA=0xFF; while(1) { PORT=~PORT; _delay_ms(1000); } }

###

Circuit diagrams

Circuit diagram of how to interface the LED with the AVR-ATmega16 microcontroller

Project Components

  • ATmega16
  • LED

Project video

Conteúdo Relacionado

A network of sensors is embedded in every vehicle,...
The motor controller is one of the most important...
ESP32-CAM is a compact camera module that combines the...
A evolução dos padrões USB foi fundamental para moldar...
A SCHURTER anuncia um aprimoramento para sua conhecida série...
A Sealevel Systems anuncia o lançamento da Interface Serial...
A STMicroelectronics introduziu Diodos retificadores Schottky de trincheira de...
Determinar uma localização precisa é necessário em várias indústrias...
O novo VIPerGaN50 da STMicroelectronics simplifica a construção de...
A Samsung Electronics, fornecedora de tecnologia de memória avançada,...
O mercado embarcado tem uma necessidade de soluções de...
You have probably come across the term ' drag...
You probably have a support insulator if you've noticed...
You've probably seen stand an insulator sit on power...
You've probably seen shackle insulators enthroned on electricity poles,...
You have probably experienced situations where controlling a circuit...
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.