Sunteți pe pagina 1din 18

Chapter 4

ATmega328 Digital Input-Output


Interfacing
Assoc. Professor Dr. Rosbi bin Mamat
rosbi@fke.utm.my

Dept. of Control & Mechatronics Engineering


Faculty of Electrical Engineering


  

Universiti Teknologi Malaysia

4.1 On-chip Interfaces in General


Peripherals or I/O devices are devices that
exchange data with µcomputer. Examples of
peripherals: switches, LED, printer, keyboard,
monitor, hard-disc etc.
Due to differences in the characteristics & speed
between peripherals & µc, peripherals are not
connected directly to the µc buses. ∴ require
interfaces between peripherals & µc.
ATmega328 is equiped with some on-chip
interfaces to simplify the connection between µc &
external peripherals in an embedded system.
1. Parallel Interface (PORT B,C,D) 3. Serial Interfaces (Async., SPI, TWI)
2. Analog-to-digital Converter (ADC) 4. Timers & PWM
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.1/34

4.2 Accessing I/O in ATmega328
The internal I/O peripherals in the ATmega328 can
be acessed (read/write) using the I/O registers
mapped at address 0x20–0xFF in data memory.
This technique of I/O addressing is called memory-
mapped I/O. I/O appear to be memory locations, ∴ the
same instr. & addr. modes to access memories can be used
for I/O.
Sample of I/O registers addresses & their symbolic
names:

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.2/34


4.2 Accessing I/O in ATmega328


In Arduino, the I/O registers addresses are defined
with their symbolic names in the header file io.h.
This header file is automatically included by
Arduino IDE.
It is much easier to program I/O registers using
their symbolic names.
Read or write to I/O regs. can be performed using
assignment operator:
unsigned char var;

DDRB = 0xF0; /* Write to I/O reg DDRB */


var = PINC; /* Read I/O reg PINC */
PORTB = var; /* Write to I/O reg PORTB */

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.3/34



4.3 ATmega328 Parallel Digital I/O
There are 3 ports that provide parallel I/O interfaces
to outside world: Port B, Port C & Port D.
Each port provides 8 (only 7 on Port C) bidirectional
digital I/O lines which are connected to ATmega328
pins provided that alternate functions are not selected
on that port.
At any time the I/O line can either be Input or
Output.
The Directions of each I/O lines must be configured
(input or output) before they are used.
Naming convention:
PORTx ≡ I/O reg for Port x where x = B, C, D.
PORTxn ≡ Pxn ≡ Port x bit n where n = 0 – 7.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.4/34


4.3 ATmega328 Parallel Digital I/O


Mapping of ATmega328 pins with Arduino pins (in
bracket): ( (A5)/D19
(D0) (A4)/D18
(D1) (A3)/D17
(D2) (A2)/D16
(D3) (A1)/D15
(D4) (A0)/D14

(D13)
(D5) (D12)
(D6) (D11)
(D7) (D10)
(D8) (D9)

Digital I/O to the outside world with Arduino Uno


is provided by 14 pins marked with DIGITAL(PWM ˜).

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.5/34



4.3 ATmega328 Parallel Digital I/O
The programmer view of Port B, C & D :
DATA REGISTER DATA DIR. REG.
PORTx DDRx

PORTx DRIVERS

Inside ATmega328

Pins on ATmega328
Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0

Each Port x has three 8-bit Registers associated with it.


Register ≈ a memory :
DDRx – Data Direction Register for Port x (Read/Write).
PORTx –Data Register for Port x (Read/Write).
PINx – Port Input Pins Register for Port x (Read only).
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.6/34

4.3 ATmega328 Parallel Digital I/O


Data Direction Register is used to configure the
direction of I/O port lines. DDRx control PORTx.
7 6 5 4 3 2 1 0
DDRx

PORTx
Px.7
Px.6
Px.5
Px.4
Px.3
Px.2
Px.1
Px.0

Each bit in the DDRx determines the direction of the


corresponding bit in PORTx.
Writing a ‘1’ to a bit in DDRx makes the bit of PORTx
Output. Writing a ’0’ makes the bit Input.
After Reset, all the bits in DDRx is cleared (‘0’).
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.7/34

4.3 ATmega328 Parallel Digital I/O
Using Arduino library, to configure the direction of
digital I/O pins, this function is called:
pinMode(pinNumb, mode)
pinNumb is the digital pin number (0–13) to configure.
mode is direction of the digital pin (INPUT or OUTPUT or
INPUT_PULLUP).
example: pinMode(13, OUTPUT) set pin 13 as output ⇒
we can send digital signal through pin 13.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.8/34


4.3 ATmega328 Parallel Digital I/O


Data Direction Register – Example: Configure all
lines of Port B as inputs & Port D bits 4,5,6 & 7 as
outputs without changing the directions of bits 0–3 .
DDRB = 0x00; 0 0 0 0 0 0 0 0 DDRB

DDRD = DDRD | (0xF0); 1 1 1 1 X X X X DDRD

Using Arduino library:


pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);

pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.9/34

4.3 ATmega328 Parallel Digital I/O
Data Register– store data to be sent to
corresponding pins of that I/O port. Data register
for Port x is PORTx.
When an I/O port is configured as Output:
writing a ‘1’ to bit PORTxn generate logic ‘1’ on
the port n pin.
writing a ‘0’ to bit PORTx.n generate logic ‘0’
on the port n pin.
reading from PORTx returns the previous value
written to PORTx.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.10/34


4.3 ATmega328 Parallel Digital I/O


When an I/O port is configured as Input:
writing a ‘1’ to bit PORTxn will activate the
pull-up resistor for the port n pin.
writing a ‘0’ to bit PORTxn will switch off the
pull-up resistor for the port n pin.
reading from PORTx returns the previous value
written to PORTx.
Using Arduino library, the pull-up resistor can be
activated with:
pinMode(pin, INPUT_PULLUP)

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.11/34



4.3 ATmega328 Parallel Digital I/O
Arduino functions for writting to digital I/O:
digitalWrite(pinNum,state) – write/output the
logic state to the specified pinNum if the pinNum is
configure as OUTPUT.
int led = 13;
digitalWrite(13, HIGH); // set pin 13 to logic high ’1’
digitalWrite(led, LOW); // set pin 13 to logic low ’0’

Example: Generate logic ‘0’ on pins PB4, PB5 & logic


‘1’ on pin PD3.
DDRB = 0x30; /* 0x30 = %0011 0000 */
DDRD = 0x08; /* 0x08 = %0000 1000 */

PORTB = 0x00;
PORTD = 0x08;

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.12/34


4.3 ATmega328 Parallel Digital I/O


Example: Generate logics using Arduino Library.
pinMode(12, OUTPUT); /* PB4 = OUT */
pinMode(13, OUTPUT); /* PB5 = OUT */
pinMode(3, OUTPUT); /* PD3 = OUT */

digitalWrite(12, LOW);
digitalWrite(13, LOW);
digitalWrite(3, HIGH);

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.13/34



4.3 ATmega328 Parallel Digital I/O
Port Input Pins Register PINx– The logic levels on
I/O pins for PORTx can be read through the PINx.
Logic (‘1’ or ‘0’) in bit PINxn gives the voltage
level on pin Port x bit n, regardless of the setting in
DDRx.
7 6 5 4 3 2 1 0
X X X X X X X X DDRx

1 0 0 0 1 1 1 0 PINx

Px7-0
3.3V

0.1V

0.2V
0V
0V

5V
5V
3V

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.14/34


4.3 ATmega328 Parallel Digital I/O


Arduino functions for reading from digital I/O:
digitalRead(pinNum) – read from the specified
pinNum & returns the logic read (HIGH or LOW) if
the pinNum is configure as INPUT.
int state;
state = digitalRead(12); // read pin 12 &
// store in ’state’

Exercise: Read the logic level on pin PB0 & send the logic to
pin PB1. Do not change other pins.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.15/34



4.3 ATmega328 Parallel Digital I/O
Summary:
Port B registers: DDRB, PORTB, PINB.
Port C registers: DDRC, PORTC, PINC.
Port D registers: DDRD, PORTD, PIND.
Set pin direction: pinMode(pin, MODE)
Write Digital I/O: digitalWrite(pin, LOGIC)
Read Digital I/O: digitalRead(pin)

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.16/34


4.3 ATmega328 Parallel Digital I/O


Programming Exercise: A security system based on Arduino
Uno has 4 sensors to detect intruders connected to PD0–PD3. 2 alarms
are connected to PB0-PB1 & 1 lamp is connected to PB4. The operation
of this security system is: If the 2 sensors at PD0–PD1 are activated,
sounds the alarm at PB0. If the 2 sensors at PD2–PD3 are activated,
sounds the alarm at PB1. If all sensors are activated, sounds all alarms
& turn on the lamp. In normal state all alarms and lamp are not active.
Assume the sensors, alarms and lamp are active high logic devices.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.17/34



4.4 Interfacing with LED Displays
LED – an example of digital output device.
The basic of a LED circuit:
+V R I 



+V -


Vcc – Voltage supply to LED


R – Current limiting resistor
Vd – Minimum voltage required to turn on LED (≈1.5V)
Id – Current flowing in LED (5–25mA)

Example: Estimate the value of current limiting resistor


if the LED is connected to a +5V supply voltage.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.18/34


4.4 Interfacing with LED Displays


Solution: Let says we require a moderate brightness
of LED. Choose Id = 10mA.
Vcc − Vd = Id × R
(V −V )
R = ccId d
(5−1.5)
R = 10×10−3 Ω ≈ 330Ω

Connecting a LED to output port of ATmega328


+5V
Output Port
Output Port 330
Px.n
Px.n
330
Px.n = ‘1’, LED On
Px.n = ‘1’, LED Off Px.n= ’0ʹ LED Off
Px.n = ’0ʹ LED On
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.19/34

4.4 Interfacing with LED Displays
7-Segment LED Display

Anode Cathode
a a a
b b
f b c c
g d d
Cathodes e e
Anodes
f f
e c g g
dp dp dp
d
Common Anode Common Cathode
(CA) 7-segment (CC) 7-segment
Display Display

Requires 8 I/O lines (an output port) to interface


directly without a hardware decoder.
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.20/34

4.4 Interfacing with LED Displays


Example: Display ’rOSbI’ on a CA 7-segment display
using software encoding/decoding & look-up table
techniques. Wait 1 second between each character.
Lets connect the CA 7-segment display to Port D.
PD0 connects to segment a — PD7 connects to
segment dp.
7-segment display cannot display full character set !
To display characters ‘r’, ‘O’, ‘S’, ‘b’, ‘I’, segments to
on/off for each character are identified & the
required code to send to output port is determined –
encoding.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.21/34



4.4 Interfacing with LED Displays
7-Segment LED Display interfaced to Port D :
Port D +5V Port D +5V

PD0 a a
PD0
b CA b 220
PD1 c PD1 c CA
PD2 PD2
PD3 d d
PD3
PD4 e e
PD4
PD5 f f
g PD5 g
PD6 dp PD6 dp
PD7 PD7

Full current limiting resistors Cheaper version with non-uniform


version brightness defect

ROSBI ROSBI
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.22/34

4.4 Interfacing with LED Displays


Software encoding table & obtained codes for
look-up table building:
Coding (ʹ0ʹ=ON & ʹ1ʹ=OFF)
Character PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 Codes
dp g f e d c b a

r 1 0 1 0 1 1 1 1 $AF
o 1 0 1 0 0 0 1 1 $A3
S 1 0 0 1 0 0 1 0 $92
b 1 0 0 0 0 0 1 1 $83
I 1 1 0 0 1 1 1 1 $CF
These codes are stored in an array to serve as a
look-up table. C code for building look-up table:
const unsigned char myname[] = {0xAF, 0xA3, 0x92, 0x83, 0xCF};

AF A3 92 83 CF .. ..

myname
Memory Contents
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.23/34

4.4 Interfacing with LED Displays
Complete C code :
/* Display 'rOSbi' on 7-seg LED connect to Port D */

const unsigned char myname[]= {0xAF, 0xA3, 0x92, 0x83, 0xCF};

void setup() {
DDRD = 0xFF; /* Port D is output */
}

void loop() {
unsigned char ch;
int j;

for (j = 0; j < 5; j++) { /* get 5 char 1-by-1 */


ch = myname[j]; /* get 1 char */
PORTD = ch; /* display the char */
delay(1000); /* hold the display 1 sec */
}
}

Exercise: rewrite above code using the Arduino library.


Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.24/34

4.4 Interfacing with LED Displays


BCD to 7-Segment Hardware Decoder/Driver– for
display of digits with less port pins.

74LS47 for CA Display 74LS48 for CC Display

74L 74L
S47 S48



Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.25/34



4.4 Interfacing with LED Displays
Multiplexed Display
Example: Show how 3 common-cathode 7-Segments
LEDs can be connected to Atmega328 using multiplexed
display technique. Write a program to display ’123’ on
the displays.
8 buffers
ATmega328 (74LS244)
a a a
PD.0

: : : :
:
: : : :
:
dp dp dp
PD.7
Cathode Cathode Cathode
220 220 220
10K 10K 10K
PB.0
PB.1
PB.2

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.26/34


4.4 Interfacing with LED Displays


Multiplexed Display
In the multiplexed technique, each LED is turn on
one-by-one while other LEDs are disabled/off.
Each LED is on for a while ( 5ms) & then off. This is
repeated for other LEDs.
Since the rate of turning on & off is very fast, human eyes
will perceive this as simultaneous display.
Port PB0PB2 together with NPN transistors are used to turn
on (’1’) & off (’0’) the 3 7-Segments LEDs.
Port D (PD0PD7) is used to send the display code.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.27/34



4.4 Interfacing with LED Displays
Program: Use the software encoding/decoding
technique to display each character.
Digit Coding ('1' = on & '0' = off) Code
PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0
dp g f e d c b a

1 0 0 1 1 0 0 0 0 $30
2 0 1 0 1 1 0 1 1 $5B
3 0 1 0 0 1 1 1 1 $4F

1 23
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.28/34

4.4 Interfacing with LED Displays


Program:
void setup() {
DDRD = 0x00; // Port D as input
DDRB = 0x07; // PB0-PB2 as output
PORTB = 0x00; // Turn off all LED
}

/* This should normally run under interrupt */


void loop() {
PORTD = 0x30; // send code for ‘1’
digitalWrite(8, HIGH); // turn on 1st LED (PB0=D8)
delay(5); // wait 5ms
digitalWrite(8, LOW); // turn off 1st LED
PORTD = 0x5B; // send code for ‘2’
digitalWrite(9, HIGH); // turn on 2nd LED (PB1=D9)
delay(5); // wait 5ms
digitalWrite(9, LOW); // turn off 2nd LED
PORTD = 0x4F; // send code for ‘3’
digitalWrite(9, HIGH); // turn on 3rd LED (PB2=D10)
delay(5); // wait 5ms
digitalWrite(9, LOW); // turn off 3rd LED
}
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.29/34

4.5 Interfacing with Switches
Symbols for mechanical switches:
Push Buttons

SPST Push to make Push to break


SPDT

Circuits for switches with TTL/CMOS logic:


+5V +5V +5V

10KΩ
10KΩ 10KΩ
Vo
Vo
Vo

When the switch is not press or open, Vo = ’1’ & when


switch is pressed/closed, Vo = ’0’
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.30/34

4.5 Interfacing with Switches


Bouncing in mechanical switches – Most
mechanical switches bounce when pressed/closed.
Switch is pressed
Open
Switch Position

Closed
Bouncing
'1'
Vo

'0'

Multiple logic '0'

Bouncing time ≈ 5–50ms. Bouncing produces multiple logic ’0’ –


gives false reading to µcontroller!. ∴ bouncing or multiple logic
’0’ must be eliminated – Debouncing.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.31/34



4.5 Interfacing with Switches
Debouncing using hardware:
+5V

'1'
Vo
C 74LS13
'0'

Choose the time constant R×C much larger than


bouncing time.
Example: choose R × C = 100ms ⇒ R=100KΩ,
C=1µF.

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.32/34


4.5 Interfacing with Switches


Debouncing using software:
1. Detect if switch is pressed & read the switch logic
2. Wait or delay for 20–25ms
3. Read the switch again
4. If the current switch logic , reading in step 1 above, repeat step 1
Example: For the following circuit, write a program to blink the LED
5 times when the switch is pressed & after that turn it off. Turn off the
LED if the switch is not pressed.

+5V

ATmega328 330

PB.1 PB.0

Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.33/34



4.5 Interfacing with Switches
Debouncing using software:
void setup() {
pinMode(8, OUTPUT); // PB0 as output
pinMode(9, INPUT_PULLUP); // PB1 as input+PU
digitalWrite(8, HIGH); // Turn off LED
}

void loop() {
while (digitalRead(9) == HIGH) // Step 1: wait for key press
;
delay(20); // Step 2: wait for 20ms
if (digitalRead(9) == 0) // Step 3: check bouncing
{ // Finish bouncing
for (int i = 0; i < 5; i++)
{ // Blink 5 times
digitalWrite(8, LOW); // On
delay(500);
digitalWrite(8, HIGH); // Off
delay(500);
}
}
}
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.34/34

S-ar putea să vă placă și