AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

Programming AVR Microcontroller Digital I/O in Assembly

Introduction to AVR Digital I/O Assembly Programming


This AVR tutorial looks at AVR programming for digital I/O in assembly. Before we start looking at actual programming AVR Digital Input/Output (I/O) in assembly language recall that each AVR microcontroller Digital I/O port is associated with I/O registers. Namely the DDRx, PINx and PORTx registers, where x represent the port: A, B, C .....


The DDRx register is use to configure the pins on portx as input or output pins. Each pin on a port is independent and thus the entire port does not have to be configured totally as an input or output port. Writing a 1 in the pin position in the DDRx will configure that pin as an output pin and writing a 0 will configure the pin as an input pin.


Logic values written to the PORTx register is outputted to the pins on Portx that are configured as output pins.


Logic values read from the PINx register is equivalent to the values presently on the pins of Portx which are configured as input pins.


Digital I/O Assembly AVR Programming in AVR Studio 5 / AVR Studio 6


The AVR assembly code below shows how to configure the pins on portA of an AVR ATMega8515 micocontroller.

.include "m8515def.inc"
 
                 LDI    R16,  0xFF       ; Load 0b11111111 in R16
                 OUT    DDRA, R16        ; Configure PortA as an Output port
 
                 LDI    R16,  0x00       ; Load 0b00000000 in R16
                 OUT    DDRB, R16        ; Configure PortB as an Input port
 
                 LDI    R16,  0xF0       ; Load 0b11110000 in R16
                 OUT    DDRC, R16        ; Configure first four pins on PortC
                                         ; as input and the others as output

The AVR assembly code section below shows how to write to or read from the pins of an AVR microcontroller port once they are configured. Assume here the configurations from the assembly code above.

                 LDI    R16,  0xFF       ; Load 0b11111111 in R16
                 OUT    PORTA, R16       ; Write all 1's to the pins of PortA
 
                 IN    R16, PINB         ; Read the values on the pins of PortB
                                         ; and store in R16



Important Note: Be reminded that in order for the recognize the I/O registers aliases, such as DDRA, DDRB, PORTA, PINB, etc., you must include the header file for the microcontroller you are using as shown in the example for the ATMega8515 microcontroller below.



Example


Write a program to be downloaded to the Atmel AVR ATMega8515 (ATMega16, ATMega32) microcontroller which continuously read the logic values on portB and write them to portC.

Solution
The program below, written in AVR Studio 5 / AVR Studio 6, accomplish the task that was asked above. There are several things to note here.

  1. The header file m8515def.inc must be included in order for us to use the name of the ports in writing the assembly code. Recall that the AVR instructions IN and OUT takes a General Purpose Register (GPR) and the memory address of an I/O register. Here the GPR is R16. Including the m8515def.inc file allows us to use the port's I/O register name instead of it's actual memory address.
  2. The ports must be configured before they are used. The first four (4) lines of the program are for configuration.
  3. Once the ports are configured you can then write to or read from them, were applicable.
  4. The use of the RJMP instruction allows for the continuous read and write operation.
/*
 *  Written in AVR Studio 5 / AVR Studio 6
 *  Author: AVR Tutorials
 *  Website: www.AVR-Tutorials.com
*/
 
.include "m8515def.inc"                  ; Header file for the ATMega8515 
                                         ; microcontroller
 
                 LDI    R16,  0x00       ; Load 0b00000000 in R16
                 OUT    DDRB, R16        ; Configure PortB as an Input port
                 LDI    R16,  0xFF       ; Load 0b11111111 in R16
                 OUT    DDRC, R16        ; Configure PortC as an Output port
 
Again:           IN     R16, PINB        ; Read the values on the pins of PortB
                                         ; and store in R16
                 OUT    PORTC, R16       ; Write the value in R16 to the pins
                                         ; of PortC
                 RJMP   Again

AVR Tutorials hope this AVR assembly tutorial on AVR programming for digital I/O was benificial to you and looks forward to your next visit.