Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C Programming for microcontrollers (Joe Pardue, 2005).pdf
Скачиваний:
277
Добавлен:
12.08.2013
Размер:
4.55 Mб
Скачать

Chapter 4: C Types, Operators, and Expressions

Projects

Port Input and Output

Figure 13 ATMega169 Block Diagram

We skimmed over a lot in Chapter 2 so that we could get some LEDs blinking. Let’s now take a more detailed look at I/O ports.

65

Chapter 4: C Types, Operators, and Expressions

When this book was written, Digi-Key listed AVRs with as few as 6 I/O pins on the ATTINY11 ($0.54) to as many as 54 on the ATMEGA169 ($8.60), the microcontroller used on the Butterfly. Most of these pins are organized into ports, collections of pins that are setup and used with port specific access and control registers. Many of the pins have more than one possible function: they can be used to input or output digital logic data or they might be used for detecting external interrupts or as input for clocks or for analog to digital conversions and so on. In this section we’ll be looking at digital I/O.

The ATMEGA169 on the Butterfly has six 8-bit and one 4-bit general purpose I/O ports shown in Figure 13 ATMega169 Block Diagram (copied from the ATMega169 data book page 3, Figure 2.) Looks mighty complex doesn’t it? Well this is a simplified block diagram of a circuit that is vastly more complex. When you see a photomicrograph of these chips they resemble aerial photos of a vast ancient city with streets laid out in a grid surrounded by a wall. The ports are the gates to the city where the ancient electrons riding their very tiny ancient donkeys enter and leave the city. I’d continue in this vein but then I’d probably win a prize in the awful metaphor competition so I’ll stop.

ATmega169 Silicon Die Curtesty of Christopher Tarnovsky from Flylogic.net

66

Chapter 4: C Types, Operators, and Expressions

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

1.Data Direction Register - DDRx – Read/Write

2.Data Register – PORTx – Read/Write

3.Port Input Pins – PINx – Read Only

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

When used for general purpose I/O the port Data Direction Register must be set to tell the micro whether a pin will be used for input or output. To use a pin for input, set the associated DDRx bit to 0; to use it as output set it to 1. For example, to use the upper 4 bits of PORTD as inputs and the lower 4 bits as output, set the bits to 00001111, which, as we’ve seen, in hex is 0x0F:

DDRD = 0x0F;

In this project we will set port B to input data from switches and port D to output +3V to drive LEDs. We use the PINB register to read the switches from port B and write the value to port D using the PORTD register.

First we set the DDRB register so that all the pins are used as inputs:

DDRB = 0x00.

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

DDRD = 0xFF.

Then we write an infinite loop that gets the switch data from port B using PINB and equates it to PORTD that will light the LEDs.

Open a new C/C++ file in Programmers Notepad and write the following program. Save it as PortIO.c in a new directory PortIO.

// PortIO.c #include <avr/io.h>

67

Chapter 4: C Types, Operators, and Expressions

int main (void)

{

// Init port pins

DDRB = 0x00; // set port B for input DDRD = 0xFF; // set port D for output

while(1)

{

PORTD = PINB;

}

}

Open the makefile in the Blinky directory and save it to the PortIO directory then change:

TARGET = PortIO.

Follow the Blinky example to write, compile, and download this little program. Remember to turn the Butterfly off and back on, then hold down the center joystick button before and while clicking on the ‘AVR prog…’ menu item in AVRStudio. Also remember to browse to the PortIO.hex file in AVRStudio (I often forget to change the hex file and end up programming the Butterfly with an earlier hex file). And finally after the code downloads, remember to turn the Butterfly off and back on then click the joystick to the upper position to start the program.

If everything goes as planned, the LEDs will display the state of the switches as shown below. I told you we’d have some lame examples.

68

Chapter 4: C Types, Operators, and Expressions

Figure 14: Port I/O switch input and LED output

69