Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Programming The Digital I

The document discusses programming the digital I/O pins of an AVR microcontroller in C code. It shows how to configure the pin directions on ports as either inputs or outputs using DDR registers. It also demonstrates how to write to or read from pin values on ports using PORT registers once the ports are configured. The code example configures ports A, B, and C for both input and output pins, then continuously reads the pin values on port B and writes them to the pins on port C.

Uploaded by

Nadia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Programming The Digital I

The document discusses programming the digital I/O pins of an AVR microcontroller in C code. It shows how to configure the pin directions on ports as either inputs or outputs using DDR registers. It also demonstrates how to write to or read from pin values on ports using PORT registers once the ports are configured. The code example configures ports A, B, and C for both input and output pins, then continuously reads the pin values on port B and writes them to the pins on port C.

Uploaded by

Nadia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming the Digital I/O pins of an AVR in C

The AVR C code below shows how to configure the pins on a port
DDRA = 0xFF;

//Configure PortA as an Output port

DDRB = 0x00;

//Configure PortB as an Input port

DDRC = 0xF0;

//Configure first four pins on PortC as


//Input pins and the others as output

The AVR C code below shows how to write to or read from the pins of a port once they are
configured. Assume here the configurations from the C code above.
PORTA = 0xFF;

//Write all 1's to the pins of PortA

PORTA = PINB;

//Read values from pins of PortB and


//write to pins of PortA

Solution
The program below, written in AVR Studio 5 and AVR Studio 6, accomplish the task that
was asked above. There are several things to note here.
1. The header file avr/io.h must be included in order for us to use the name of the
ports.
2. The ports must be configured before they are used. The first two(2) lines in the
main() function 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 while(1) loop allows for the continuous read and write operation.
/*
*
*
*
*
*
*/

Written in AVR Studio 5 / AVR Studio 6


Compiler: AVR GNU C Compiler (GCC)
Author: AVR Tutorials
Website: www.AVR-Tutorials.com

#include<avr/io.h>
int main()
{
DDRB = 0x00;
DDRC = 0xFF;

//configure portB as input


//configure portC as output

while(1)
{
PORTC = PINB;
}
return 0;

For another Digital I/O C programming example check out the Interfacing LEDs with the AVR
Microcontrollerstutorial.
AVR Tutorials hope this AVR C tutorial on AVR programming for digital I/O was benificial to
you and looks forward to your next visit.

You might also like