Вы находитесь на странице: 1из 29

I/O Ports

ECET 209 – Lecture 2


Introduction to Microcontrollers
Overview

• Basic Microcontrollers
• Input / Output Operations

ECET 209 Purdue University 2


I/O

ECET 209 Purdue University 3


Input / Output

• Microcontrollers are used to control devices


or machines.

• Need the ability to:


– monitor or read inputs like switches or sensors
– control devices like actuators or motors

ECET 209 Purdue University 4


I/O Ports

• Allows the micro to see the “World” and to


modify or change the “World”

ECET 209 Purdue University 5


Quiz

ECET 209 Purdue University 6


ECET 209 Purdue University 7
Port Configuration

• The Atmel AVR Family of


microcontrollers requires that the ports
be configured for either input or output.

ECET 209 Purdue University 8


Port Configuration

• The microcontroller has a data direction


register for each port that controls the
direction (input or output) of the desired
port.

ECET 209 Purdue University 9


ECET 209 Purdue University 10
Simplified IO Port Diagram
PULLUP

DATA BUS

5 2
Q D

6 3

CLR
Q CLK
Write DDRn

1
RESET

Read DDRn

PORT PIN 5 2
Q D

6 3

CLR
Q CLK
Write PORTn

1
RESET

Read PORTn Register

Read PORTn PIN

ECET 209 Purdue University 11


Port Registers

ECET 209 Purdue University 12


Data Direction Registers

• Port's Data Direction is the DDRn


register
– Controls whether or not it is an input or an
output
– Setting (writing a one) to a bit in the DDRn
configures the pin as an Output
– Clearing (writing a zero) to a bit in the DDRn
configures the pin as an Input

ECET 209 Purdue University 13


How is this Accomplished?

• Remember

– We are going to use C as a Tool.

ECET 209 Purdue University 14


Port Registers

1 1 1 1 1 1 1 1

ECET 209 Purdue University 15


C to Configure Ports

• PORTC as an Output (write 1’s to DDRC)

DDRC = 0xFF; // configure PORTC as Output

ECET 209 Purdue University 16


C to Configure Ports

• PORTA as an Input (write 0’s to DDRA)

DDRA = 0x00; // configure PORTA for Input

ECET 209 Purdue University 17


Internal Pull-up Resistors

• To Float or not to Float


– Largely depends on the design
– For this class, We will use the Pull-up Option

ECET 209 Purdue University 18


Configuring Pull-ups

ON

ECET 209 Purdue University 19


Configuration

• The PORTn register controls if the Pull-up


is activated or not.

– Writing a one to the PORTn register will


activate the internal pull-up resistor.

– Writing a zero to the PORTn register will


deactivate or turn off the internal pull-up
resistor.
ECET 209 Purdue University 20
The C Code

• PORTA configured as an input with the


internal pull-up resistors on.

DDRA = 0x00; // configure PORTA for Input

PORTA = 0xFF; // turn on the pull-up resistors

ECET 209 Purdue University 21


Any Questions about Configuring
Ports ?

ECET 209 Purdue University 22


Reading and Controlling Ports

• Now we are ready to actually use the ports.

• When using the port as an input, we read


from the input PINS.
• When using the port as an output, we write
to the output PORT register.

ECET 209 Purdue University 23


The C Code

• Reading and writing to and from Ports

PORTC = PINA; // echo value on PortA to PortC

ECET 209 Purdue University 24


Block Diagrams

PORTB PORTA

PORTD PORTC

ECET 209 Purdue University 25


ECET 209 Purdue University 26
Picking the Right Micro

• The AVR 8-Bit RISC family has ~35


members
– # of I/O pins range from 6 to 54
– Flash from 1K to 128K
– Supply voltage from 1.8V to 5.5V
– Frequency range from 1 to 20 MHz
– …

ECET 209 Purdue University 27


Picking the Right Micro

• The right microcontroller can make or break


your project.

• Influence the development time


• Influence the budget

• Influence your Senior Design Grade!!


ECET 209 Purdue University 28
Questions?

ECET 209 Purdue University 29