ATMEGA 32 interface with led

Introduction

  • AVR ATmega32 has 40 pins constituting four ports. The ports are listed below :
    1.  PORT A
    2.  PORT B
    3.  PORT C
    4.  PORT D.
  • Each port has 8 pins.
  • The pins of these four ports can be used as general-purpose inputs/outputs.
  • These pins can be configured as input or output using the three I/O registers for each port. These registers are listed below :
    1.  Data Direction Register/DDRxregisters: These are used to decide the direction of the pins, i.e. whether the pins will act as input pins or as output pins
    2.  Data Input Register/PINxregisters: These are used to read the logic level on the port pins.
    3.  PORTxregisters: These are used to set the logic on the pins HIGH or LOW.
    (Where x can be A, B, C, or D depending on which port registers are being addressed).
  • Each pin also has some special functionality associated with it as well.

The pin diagram of ATmega32 is shown in the figure below. The four ports, their pins, and the special functions associated with each pin can be seen in the figure.

I/O Port Registers

There are three I/O registers associated with each port. These are as follows:

  • Data register PORTx
  • Data direction register DDRx
  • Input pins address register PINx

Where x can be A, B, C, or D depending on which port is being addressed.

DDRx: Data Direction Registers

  • These are 8-bit registers.
  • These are used to configure the pins of the ports as input or output.
  • Writing a one to the bits in this register sets those specific pins as output pins.
  • Writing a zero to the bits in this register sets those specific pins as input pins.
  • All bits in these registers can be read as well as written to.
  • The initial value of these bits is zero.

Example :

  1. Setting Port D as an output port :
    DDRD = 0xFF;
    Writing 1 to the bits of this register configures the pins corresponding to those pins as output pins.
    Here we have configured all 8 pins of Port D as output pins by writing 1 to each of them. ( 0xFF = 0b11111111).
  2. Setting Port D as an input port :
    DDRD = 0x00;
    Writing 0 to the bits of this register configures the pins corresponding to those pins as output pins.
    Here we have configured all 8 pins of Port D as input pins by writing 0 to each of them. ( 0x00 = 0b00000000).

PORTx: Data Registers

  • These are 8-bit registers.
  • These are used to put the pins of the ports in a logic HIGH or logic LOW state.
  • Writing a one to the bits in this register puts a HIGH logic (5V) on those pins.
  • Whereas writing a zero to the bits in this register puts a LOW logic (0V) on those pins.
  • All bits in these registers can be read as well as written to.
  • The initial value of these bits is zero.

Example:

We will use the PORTx register of Port D to write a value 0x55 to Port D.

PORTD = 0x55;

PINx: Input Pins Address Registers

  • These are 8-bit registers.
  • These are used to read the values on the specific pins of the port.
  • These bits are read-only bits and cannot be written to.

Example:

We will read the value on Port D in an 8-bit variable named ‘port_value’.

port_value = PIND;

In the figure shown below, the above mentioned three registers for PortA are shown. These are 8-bit registers

Circuit Diagram-

As shown in the below figure, the LED is connected to pin 3 of Port A. The cathode of the LED is connected to gnd. Most of the LEDs require around 10mA current to glow properly.

If the current greater than the maximum rated current of the LED flows through it, the LED will get permanently damaged.The maximum current rating for LEDs is usually around 20-30mA. Refer to the datasheet of the LED you are using for this rating.In order to limit the current flowing through the LED, we connect a resistor in series with it.Assuming that the LED has a 0.7V forward voltage drop and that 10mA current is sufficient for the LED to glow properly, the value of resistance required can be calculated as follows

\mathbf{R = \frac{Voltage Supplied to LED - Forward Voltage Drop of LED}{Current Flowing Through LED} = \frac{5V - 0.7V}{10mA}}

Therefore, \mathbf{R = \frac{4.3V}{10mA} = 430 \Omega}

The nearest standard value of resistor available is 470Ω. So, that value is used in the circuit.

The Code

/*
Author:Dharmendra Kumar Yadav
*/

#include<avr/io.h>

#include<util/delay.h>

int main(void)
{
DDRA = 0xFF; /* Making all 8 pins of Port A as output pins
In order to make only PA3 as output while keeping direction of other pins as it is, do the following*/
DDRA = DDRA | (1<<3);
//This is equivalent to writing one to only PA3 while keeping the other pins unchanged

while(1)

{

PORTA = PORTA | (1<<3); //Making PA3 high. This will make LED ON

_delay_ms(1000);

PORTA = PORTA & (~(1<<3)); / *Making PA3 low. This will make LED OFF */

_delay_ms(1000); /

/* Do not keep very small delay values. Very small delay will lead to LED appearing continuously ON due to persistence of vision */
}
return 0;
}

Understanding I/O pins

All port pins have individually selectable pull-up resistors with resistance which is independent of the supply voltage. All pins have protection diodes to both VCC and Ground.

The equivalent schematic of each I/O pin is shown in the figure below.

Equivalent Schematic Of I/O Pin

Equivalent schematic of I/O pin

(Image Source: ATmega16 Datasheet)

Current sinking and sourcing

  • Each I/O pin can sink or source a maximum current of 40mA.
  • Although each pin can sink or source 40mA current, it must be ensured that the current sourced or sunk from all the ports combined, should not exceed 200mA.
  • There are further restrictions on the amount of current sourced or sank by each port. For information on this, refer to page 292 in the datasheet of ATmega16 given in the attachments section of this topic.

Pin States

  • A port pin can have four different states depending upon the state of the bits corresponding to this pin in the data direction register (DDRx) and a data register (PORTx) of that specific port.
DDxnPORTxnI/OPull-UpComment
00INoTri-State (High Z)
01IYesPxn will source current if pulled low externally
10ONoOutput Low (Sink)
11ONoOutput High (Source)
x is Port and can be A, B, C or Dn is Pin number of the Port, and can be 0, 1, 2, 3, 4, 5, 6 or 7
  • As discussed in the equivalent schematic of I/O pins, each port pin has an internal pull-up resistor. This internal pull-up resistor to all the pins can be disabled by writing one to the PUD bit in the SFIOR register. When this is done, the pins are in tri-state (High impedance states) if DDRxn = 0 and PORTxn = 1.

Leave a comment

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started