Getting Started

(lily) #1

Chapter 4: C Types, Operat


Each port has three associated I/O memory locations, that act as guards
etermining who shall pass (guess I won’t stop)


ors, and Expressions

:

nly

rection Register must be set to
ll the micro whether a pin will be used for input or output. To use a pin for
input, set the a se it as output set it to 1. For example,
to use the uppe and the lower 4 bits as output, set the
its to 0000111


DDRD = 0x0F;

e the PINB register to read the switches from port B
nd write the value to port D using the PORTD register.

that all the pins are used as inputs:

ext we set the DDRD register so that all the pins are used as outputs:

en w ts the switch data from port B using PINB

grammers Notepad and write the following
a new directory PortIO.

d



  1. Data Direction Register - DDRx – Read/Write

  2. Data Register – PORTx – Read/Write

  3. Port Input Pins – PINx – Read O


For example port A has: PORTA, DDRA, and PINA.


When used for general purpose I/O the port Data Di
te
ssociated DDRx bit to 0; to u
r 4 bits of PORTD as inputs
b 1, which, as we’ve seen, in hex is 0x0F:


In this project we will set port B to input data from switches and port D to output
+3V to drive LEDs. We us
a


First we set the DDRB register so


DDRB = 0x00.

N


DDRD = 0xFF.

Th e write an infinite loop that ge
and equates it to PORTD that will light the LEDs.


Open a new C/C++ file in Pro
program. Save it as PortIO.c in


// PortIO.c
#include <avr/io.h>
Free download pdf