ATmega-Chap6-IO - Interface H

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

The ATmega324 Microcontroller

Chapter 6
IO Interface

Nguyen Trung Hieu


Ho Chi Minh City University of Technology
Nguyễn Trung Hiếu 1
Outline
• I/O-Ports
• Push Buttons
• LED
• Seven-segment LED
• LED matrix
• LCD
• ADC
• Motor Control

Nguyễn Trung Hiếu 2


I/O Pin Equipvalent Schematic

Input Output

Nguyễn Trung Hiếu 3


I/O Pin Structure

Nguyễn Trung Hiếu 4


I/O Pin – Write 1,0

1, 0

Nguyễn Trung Hiếu 5


I/O Pin – Read 1,0

Schmitt Trigger

1, 0

Nguyễn Trung Hiếu 6


I/O Pin – Read 1,0 with Pull Up
1

Schmitt Trigger

1, 0

Nguyễn Trung Hiếu 7


I/O Pin – Parameters
Input:
• VILmax (Max Voltage Input Low - 0): 0.1VCC
• VIHmin (Min Voltage Input High - 1): 0.7VCC
• IIL (Current Input Low - 0): - 1 µA (leakage)
• IIH (Current Input High - 1): + 1 µA (leakage)

Output
• VOLmax (Max Voltage Output Low - 0): 0.9 V
• VOHmin (Min Voltage Output High - 1): 4.2 V
• IOL (Current Output Low - 0): + 20 mA
• IOH (Current Output High - 1): -20 mA

Nguyễn Trung Hiếu 8


I/O Pin – Fanout

Nguyễn Trung Hiếu 9


Outline
• I/O-Ports
• Push Buttons
• LED
• Seven-segment LED
• LED matrix
• LCD
• ADC
• Motor Control

Nguyễn Trung Hiếu 10


Push Button Interface

Nguyễn Trung Hiếu 11


Push Button Interface

SW1 is Push Button


- SW1 is pushed => PB0 = 0
- SW1 is unpushed => PB0 = 1
=> Active LOW

No pull up resistor

Nguyễn Trung Hiếu 12


Push Button Interface

SW1 is Push Button


- SW1 is pushed => PB0 = 0
- SW1 is unpushed => PB0 = 1
=> Active LOW

With pull up resistor

Nguyễn Trung Hiếu 13


Interface Algorithm
Check if SW is pushed:

LOOP: SBIC PINB, 0


RJMP LOOP
(Statement)
Nguyễn Trung Hiếu 14
Interface Algorithm
If include configure PIN:

CBI DDRB,0
SBI PORTB,0
LOOP: SBIC PINB, 0
RJMP LOOP
(Statement)

Nguyễn Trung Hiếu 15


Realistic Push Button
• Due to mechanical structure of the button => Bouncing
• Time for bouncing depends on each button: from 50ms to 300ms

PUSHED UNPUSHED
Lê Chí Thông 16
Debouncing Algorithm
Check if SW is pushed:
Method:
Delay through bouncing time

Nguyễn Trung Hiếu 17


Debouncing Algorithm
Check if SW is pushed:
Method:
Delay through bouncing time
CBI DDRB,0
SBI PORTB,0
LOOP: SBIC PINB, 0
RJMP LOOP
CALL DELAY100MS
SBIC PINB,0
RJMP LOOP
(Statement)
DELAY100MS:
….
RET Nguyễn Trung Hiếu 18
EXAMPLE
A push button is connected to pin PB0 of MCU ATmega324. Two
LEDs are connected to pin PC0 (LED D0) and PC7 (LED D1). Write a
program that continually reads the state of the push button.
- If the button is pushed odd times, turn LED D0 on.
- If the button is pushed even times, turn LED D1 off.
Debouncing the button. Assume that FOSC = 8 MHz

Nguyễn Trung Hiếu 19


EXAMPLE

Nguyễn Trung Hiếu 20


EXAMPLE
.ORG 0 LOOP: SBIC PINB, 0
CBI DDRB,0 RJMP LOOP
SBI PORTB,0 CALL DELAY100MS
LDI R16,$81 SBIC PINB,0
OUT DDRC,R16 RJMP LOOP
LDI R16,$80 COM R16
OUT PORTC,R16 OUT PORTC,R16
;(NO PUSH, TURN LED0 ON RJMP LOOP
TURN LED1 OFF)

CHANGE
STATE
Nguyễn Trung Hiếu 21
EXAMPLE
DELAY100MS:
LDI R22,4 FOSC = 8MHz  1MC = 0.125 µs
L1: LDI R21,200 T = 100 000 µs = 800 000 MCs
L2: LDI R20,250 = 4.250.200.4
L3: NOP
DEC R20
BRNE L3
DEC R21
BRNE L2
DEC R22
BRNE L1
RET
Nguyễn Trung Hiếu 22
DEBOUNCE IN HARDWARE

Nguyễn Trung Hiếu 23


DEBOUNCE IN HARDWARE

PUSHED UNPUSHED

Nguyễn Trung Hiếu 24


Multiple Buttons Interface Algorithm
Type 1: Assume that all SW is active low
- SW0 is connected to P1.0
- SW1 is connected to P1.1
… (Buttons can connect at
different PORT)
Note: buttons are not
pushed at the same time

Nguyễn Trung Hiếu 25


Multiple Buttons Interface Algorithm
CBI DDRB,0
SBI PORTB,0

Check0: SBIC PINB, 0
RJMP Check1
(Statement0)
RJMP Check0

Check1: SBIC PINB, 0


RJMP Check2
(Statement1)
RJMP Check0
….
Nguyễn Trung Hiếu 26
Multiple Buttons Interface Algorithm
Type 2: If all buttons are connected to one port
Example: PortB
SW0 push: PB = 1111 1110B = FEH
SW1 push: PB = 1111 1101B = FDH …

Nguyễn Trung Hiếu 27


Multiple Buttons Interface Algorithm
LDI R17,$FF
OUT DDRB,R17
LOOP: IN R16,PINB
Check0: CPI R16,$FE
BRNE Check1
(Statement0)
RJMP LOOP

Check1: CPI R16,$FE


BRNE Check2
(Statement0)
RJMP LOOP

Nguyễn Trung Hiếu 28
EXAMPLE
Write a program that follows these conditions:
- SW0 is pushed, SW1 is not: LED D0 turns on, LED D1 turns off
- SW1 is pushed, SW0 is not: LED D1 turns on, LED D0 turns off
Debouncing the buttons. Program runs continually. Given FOSC = 8MHz

Nguyễn Trung Hiếu 29


Using Type 1 – No Debouncing
.ORG 0 Check0: SBIC PINB, 0
LDI R17,$00 RJMP Check1
OUT DDRB,R17 SBI PORTC,0
LDI R17,$41 CBI PORTC,7
OUT PORTB,R17 RJMP Check0
; PB0,PB7: input, pull up
Check1: SBIC PINB, 6
LDI R17,$81 RJMP Check0
OUT DDRC,R17 CBI PORTC,0
LDI R17,$00 SBI PORTC,7
OUT PORTC,R17 RJMP Check0
; PC0,PC7: output 0 at reset
Nguyễn Trung Hiếu 30
Using Type 1 – Debouncing
.ORG 0 Check0: SBIC PINB, 0
LDI R17,$00 RJMP Check1
OUT DDRB,R17 CALL DELAY100MS
LDI R17,$41 Debouncing SBIC PINB,0
OUT PORTB,R17 RJMP Check0
; PB0,PB7: input, pull up SBI PORTC,0
CBI PORTC,7
LDI R17,$81 RJMP Check0
OUT DDRC,R17
LDI R17,$00 …
OUT PORTC,R17 DELAY100MS:
; PC0,PC7: output 0 at reset …
Nguyễn Trung Hiếu
RET 31
Using Type 2 – No Debouncing
.ORG 0 LOOP: IN R16,PINB
LDI R17,$00 ANDI R16,$41
OUT DDRB,R17 Check0: CPI R16,$40
LDI R17,$41 Isolate bit 0 BRNE Check1
OUT PORTB,R17 and 6 SBI PORTC,0
; PB0,PB7: input, pull up CBI PORTC,7
RJMP LOOP
LDI R17,$81
OUT DDRC,R17 Check1: CPI R16,$01
LDI R17,$00 BRNE LOOP
OUT PORTC,R17 CBI PORTC,0
; PC0,PC7: output 0 at reset SBI PORTC,7
RJMP LOOP
Nguyễn Trung Hiếu 32
Using Type 2 – Debouncing
.ORG 0 LOOP:IN R16,PINB
LDI R17,$00 ANDI R16,$41
OUT DDRB,R17 Check0: CPI R16,$40
LDI R17,$41 BRNE Check1
OUT PORTB,R17 CALL DELAY100MS
; PB0,PB7: input, pull up IN R16,PINB
ANDI R16,$41
LDI R17,$81 Debouncing CPI R16,$40
OUT DDRC,R17 BRNE Check0
LDI R17,$00 SBI PORTC,0
OUT PORTC,R17 CBI PORTC,7
; PC0,PC7: output 0 at reset RJMP LOOP
….
Nguyễn Trung Hiếu 33
Your Turn !!!
Draw the diagram of the algorithm and write the program using
debouncing buttons.

Nguyễn Trung Hiếu 34


Outline
• I/O-Ports
• Push Buttons
• LED
• Seven-segment LED
• LED matrix
• LCD
• ADC
• Motor Control

Nguyễn Trung Hiếu 35


LED Interface

PORT IN

Current flows
when ON

PORT OUT

Nguyễn Trung Hiếu 36


Example
A LED is connected to pin PC0 of ATmega324P. Assume that the LED
has VAK=1.2V and ILED =15mA. A pin of ATmega324P has maximum
current of 20mA, VIL = 0.6V, VOL = 0.3V, VIH = 5V, VOH = 4.3V, VCC = 5V.

VCC  VAK  VOL 5  1.2  0.3


R   230
I LED 15mA

 R  330

LED on when PC0 = 0


=> VOL = 0.3V
Nguyễn Trung Hiếu 37
Example
A LED is connected to pin PC7 of ATmega324P. Assume that the LED
has VAK=1.2V and ILED =15mA. A pin of ATmega324P has maximum
current of 20mA, VIL = 0.6V, VOL = 0.3V, VIH = 5V, VOH = 4.3V, VCC = 5V.

VOH  VAK  0 4.3  1.2  0


R   206
I LED 15mA

 R  220

LED on when PC7 = 1


=> VOH = 4.3V
Nguyễn Trung Hiếu 38
BAR LED
A bar led is connected to PC of ATmega324P. Write a program to
blink each led sequentially after 1 second.

Nguyễn Trung Hiếu 39


BAR LED

Nguyễn Trung Hiếu 40


BAR LED

.ORG 0 DELAY1S:
LDI R16,$FF …
OUT DDRC,R16 RET
LDI R16,$00
OUT PORTC,R16

LDI R16,$01
LOOP: OUT PORTC,R16
All LEDs turn off one
CALL DELAY1S
time in a period
ROL R16
RJMP LOOP Can you do better?

Nguyễn Trung Hiếu 41


Outline
• I/O-Ports
• Push Buttons
• LED
• Seven-segment LED
• LED matrix
• LCD
• ADC
• Motor Control

Nguyễn Trung Hiếu 42


Common Cathode and Common Anode

Nguyễn Trung Hiếu 43


Table for common anode
D7 D6 D5 D4 D3 D2 D1 D0
g f e d c b a
0 0 1 0 0 0 0 0 0 40H
1 0 1 1 1 1 0 0 1 79H
2 0 0 1 0 0 1 0 0 24H
3 0 0 1 1 0 0 0 0 30H
4 0 0 0 1 1 0 0 1 19H
5 0 0 0 1 0 0 1 0 12H
6 0 0 0 0 0 0 1 0 02H
7 0 1 1 1 1 0 0 0 78H
8 0 0 0 0 0 0 0 0 00H
9 0 0 0 1 0 0 0 0 10H

TABLE: DB 40h,79h,24h,30h,19h, 12h,02h,78h,00h,10h

Nguyễn Trung Hiếu 44


Table for common cathode
D7 D6 D5 D4 D3 D2 D1 D0
g f e d c b a
0 0 0 1 1 1 1 1 1 3FH
1 0 0 0 0 0 1 1 0 06H
2 0 1 0 1 1 0 1 1 5BH
3 0 1 0 0 1 1 1 1 4FH
4 0 1 1 0 0 1 1 0 66H
5 0 1 1 0 1 1 0 1 6DH
6 0 1 1 1 1 1 0 1 7DH
7 0 0 0 0 0 1 1 1 07H
8 0 1 1 1 1 1 1 1 7FH
9 0 1 1 0 1 1 1 1 6FH

TABLE: DB 3Fh,06h,5Bh,4Fh,66h, 6Dh,7Dh,07h,7Fh,6Fh

Nguyễn Trung Hiếu 45


EXAMPLE FOR 7-SEGMENT LED INTERFACING
Give a common-anode 7-seg LED connected to PortC. Write a
program that increases the display of the 7-seg LED after each 1
second.

Nguyễn Trung Hiếu 46


EXAMPLE FOR 7-SEGMENT LED INTERFACING

Only display from 0 to 9

Nguyễn Trung Hiếu 47


EXAMPLE FOR 7-SEGMENT LED INTERFACING

Only 1 LED

Nguyễn Trung Hiếu 48


EXAMPLE FOR 7-SEGMENT LED INTERFACING

LOOK UP

Nguyễn Trung Hiếu 49


(Remember) Look-up Table DATA: 8 bits (byte)
LDI R16,ENTRY_NUMBER
(LDI R17,$0)
LDI ZH,HIGH(TABLE<<1)
LDI ZL,LOW(TABLE<<1)
ADD ZL,R16
(ADC ZH,R17)
LPM R17,Z
TABLE: .DB data1, data2, data3, …
If ENTRY_NUMBER = 0,
Address High Byte Low Byte Z POINT TO
TABLE’S ADDRESS data2 data1
data4 data3
… …
Program Memory …
Nguyễn Trung Hiếu … 50
EXAMPLE FOR 7-SEGMENT LED INTERFACING
.ORG 0
LOOKUP:
LDI R16,$FF ...
OUT DDRC,R16 RET
LDI R16,$00
OUT PORTC,R16
START: LDI R16,$01 TABLE: .DB 40h,79h,24h,30h,19h,...
LOOP: CALL LOOKUP
OUT PORTC,R17 DELAY1S:
CALL DELAY1S …
INC R16 RET
CPI R16,$10
BRNE LOOP
RJMP START Nguyễn Trung Hiếu 51
INTERFACE USING IC 7447
IC 7447 CONVERTS BCD CODE
TO 7-SEGMENT LED COMMON ANNODE CODE
=> DON’T NEED TO USE LOOK UP TABLE

Nguyễn Trung Hiếu 52


RETURN TO THE PREVIOUS EXAMPLE
Give a common-anode 7-seg LED connected to PortC. Write a
program that increases the display of the 7-seg LED after each 1
second.

Nguyễn Trung Hiếu 53


EXAMPLE
.ORG 0
LDI R16,$0F
OUT DDRC,R16
LDI R16,$00
OUT PORTC,R16
START: LDI R16,$01
LOOP: OUT PORTC,R16
CALL DELAY1S
INC R16
CPI R16,$10
BRNE LOOP
RJMP START
DELAY1s: …
Nguyễn Trung Hiếu
RET 54
References

• Giáo trình vi xử lý, BMĐT


• LCD datasheet: HD44780U datasheet
• Internet

Lê Chí Thông 55

You might also like