Top Banner
AVR I/O Port Programming Lecture# 06 Microprocessor System and Interfacing Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 1
25

AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

Apr 27, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

AVR I/O Port

Programming Lecture# 06

Microprocessor System and Interfacing

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

1

Page 2: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

Lecture Review

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

2

Stack Limitations

Memory

Delay Calculation for AVR

Instruction Cycle Time for AVR

Pipelining in AVR

Page 3: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

3

Page 4: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

I/O Port Pins and their Functions

The number of ports in AVR family varies

depending on the number of pins in a chip

Any port can be used as in input or output

port

It needs to be configured before it can be

used

Each port has some other functions than

input and output operation

ADC, Timers, Interrupts and Serial

Communication

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

4

Page 5: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

I/O Port Pins and their Functions

Each port has three IO registers associated

PORTx

DDRx (Data Direction Register)

PINx (Port Input pins)

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

5

Page 6: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

I/O Port Pins and their Functions

Each of the IO register is 8 bits wide

Each bit of the IO register effects one of the

pins

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

6

Page 7: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

DDR Register Role in Outputting Data

Each port can be used for Input or Output.

The DDR is used to control the direction

Either port will be in Input port or will behave likean output port

For configuration,

write 1s to DDRx

register

If 1s not written to

DDRx register, the

data will not travel

from Port register to

pins

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

7

Page 8: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

8

LDI R16, 0xFF ; R16 = 0xFF

OUT DDRB, R16 ; make Port B an output port ;

L1: LDI R16, 0x55 ; R16 = 0x55 = 0b01010101

OUT PORTB, R16 ; put 0x55 on Port B

CALL DELAY

LDI R16, 0xAA ; R16 = 0xAA = 0b10101010

OUT PORTB, R16 ; put 0xAA on Port B

CALL DELAY

RJMP L1

Class Example 1

Output 0x55 and 0xAA on PortB after some delay

Page 9: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

DDR Register Role in Inputting Data

To make a Port Input, put 0s in DDRx register

At reset of AVR, all DDRs are loaded with 0s

PIN Register Role in Inputting Data

Read PIN register to get input data

PINs are not read directly

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

9

Page 10: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

PORT Register Role in Inputting Data

Pull-up resistor is used for all the AVR Pins

If PORTx register contains 1s then pull-up

resistors will be activated

What is the benefit then ?

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

10

Page 11: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

11

LDI R16, 0x00

OUT DDRC, R16

LDI R16, 0xFF

OUT DDRB, R16

L2: IN R16, PINC

LDI R17, 0x05

ADD R16, R17

OUT PORTB, R16

RJMP L2

Class Example 2

Take input from Port C and send it to port B after adding 5.

Repeat the loop infinitely.

Page 12: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

PORT A as Output and Input

Port A can be used as output port

It has Analog to Digital Conversion, not

preferred for IO operation

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

12

Page 13: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Port Programming in AVR

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

13

.EQU MYTEMP 0x100

LDI R16, 0x00

OUT DDRA, R16

NOP

IN R16, PINA

STS MYTEMP R16

Class Example 3 – Port A as Input

What is the role of NOP here ?

Input Circuit has a delay of one clock cycle, to

overcome that issue an NOP is necessary

Page 14: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

Class Exercise 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

14

LDI R16, HIGH (RAMEND)

OUT SPH, R16

LDI R16, LOW (RAMEND)

OUT SPL, R16

LDI R16, 0xFF

OUT DDRB, R16

OUT DDRC, R16

OUT DDRD, R16

LDI R16, 0x55

L3: OUT PORTB, R16

OUT PORTC, R16

OUT PORTD, R16

CALL QDELAY

COM R16

RJMP L3

Write a test program for the AVR chip to toggle all the bits of

PORTB, PORTC and PORTD every ¼ of a second. Assume a crystal

frequency of 1MHz.

QDELAY:

LDI R21, 200

D1: LDI R22, 250

D2: NOP

NOP

DEC R22

BRNE D2

DEC R21

BRNE D1

RET

Page 15: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Bit Manipulation Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

15

I/O Ports and Bit Addressability

Need to access 1 or 2 bits in the port

AVR has ability to access any bit without altering

other bits

Can access all 8-bits or any number of bits

Different instructions are used for bit operations

IO Registers

Page 16: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Bit Manipulation Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

16

SBI – Set Bit in I/O Register

To set a single bit high in I/O register

SBI ioReg, bit_num

ioReg is the lower 32 bit register

bit_num is desired bit number from 0 - 7

Mostly used for IO ports

Example

SBI PORTB, 5

Page 17: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Bit Manipulation Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

17

CBI – Clear Bit in I/O Register

Clear a single bit in I/O register

CBI ioReg, bit_num

ioReg is the address of lower 32 bit register

bit_num is desired bit number from 0 - 7

Mostly used for IO ports

Example

CBI PORTB, 5

Page 18: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Bit Manipulation Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

18

SBI DDRB, 2 ; bit = 1, make PB2 an output pin

AGAIN SBI PORTB, 2 ; bit set (PB2 = high)

CALL DELAY

CBI PORTB, 2 ; bit clear (PB2 = low)

CALL DELAY

RJMP AGAIN

Class Example 4

Toggle pin PB2 continuously

Page 19: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

Class Exercise - 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

19

LDI R20, HIGH (RAMEND)

OUT SPH, R20

LDI R20, LOW (RAMEND)

OUT SPL, R20

LDI R20, 0xFF

OUT DDRD, R20

SBI PORTD, 0

CALL DELAY

SBI PORTD, 1

CALL DELAY

SBI PORTD, 2

CALL DELAY

SBI PORTD, 3

CALL DELAY

An LED is connected to each pin of Port D. Write a program to

turn on each LED from pin D0 to pin D7. Call a delay subroutine

before turning on the next LED.

SBI PORTD, 4

CALL DELAY

SBI PORTD, 5

CALL DELAY

SBI PORTD, 6

CALL DELAY

SBI PORTD, 7

CALL DELAY

Page 20: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

Home Task - 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

20

Write the following program:

(a) Create a square wave of 50% duty cycle on bit 0 of Port C

(b) Create a square wave of 66% duty cycle on bit 3 of Port C

Page 21: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Bit Manipulation Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

21

SBIS – Skip if Bit in I/O Register is Set

Monitor the status of a single bit for HIGH

If condition is true, Skips the next instruction

SBIS a, b

a is any I/O register

b is desired bit number from 0 - 7

Page 22: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Bit Manipulation Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

22

SBIC – Skip if Bit in I/O Register is Cleared

Monitor the status of a single bit for LOW

If condition is true, Skips the next instruction

SBIC a, b

a is any I/O register

b is desired bit number from 0 - 7

Page 23: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

I/O Bit Manipulation Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

23

CBI BBRB, 3 ; Make PB3 input

SBI DDRC, 5 ; Make PC5 output

HERE: SBIC PINB, 3 ; monitor PB3 for HIGH

RJMP HERE

SBI PORTC, 5 ; make PC5 HIGH

CBI PORTC, 5 ; make PC5 LOW

; for High-to-LOW

RJMP HERE

Class Example 5

Assume that bit PB3 is an input and represents the condition of a

door alarm. If it goes LOW, it means that the door is open. Monitor

the bit continuously. Whenever it goes LOW, send a HIGH-to-LOW

pulse to PC5 to turn on a buzzer.

Page 24: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

Home Task - 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

24

A switch is connected to pin PB2. Write a program to check the

status of SW an perform the following:

(a) If SW = 0, send the letter ‘N’ to PORTD

(b) If SW = 1, send letter ‘Y’ to PORTD

Page 25: AVR I/O Port Programmingsaqazi.com/EEE342/FA19_MP_Lecture06_20190924_IO_Port... · 2019-10-01 · I/O Port Programming in AVR I/O Port Pins and their Functions The number of ports

Class Exercise - 3

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

25

CBI DDRB, 0 ; make PB0 an input

SBI DDRB7, 7 ; make PB7 an output

AGAIN:

SBIC PINB, 0 ; skip next if PB0 is clear

RJMP OVER

CBI PORTB, 7

RJMP AGAIN

OVER:

SBI PORTB, 7

RJMP AGAIN

A switch is connected to pin PB0 and an LED to pin PB7. Write a

program to get the status of SW and send it to the LED.