avr serial port programming in assembly and...

26
AVR Serial Port Programming in Assembly and C Lecture# 17 Microprocessor System and Interfacing Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 1

Upload: others

Post on 11-Sep-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

AVR Serial Port

Programming in

Assembly and CLecture# 17

Microprocessor System and Interfacing

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

1

Page 2: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Sessional – II

Timers and Counters,

Interrupts and Serial

Port Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

2

Page 3: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

AVR Serial Port Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

3

UBRR register and Baud Rate in the AVR

PCs are widely used for communication with

AVR

AVR transfers and receives data serially at

different baud rates

Baud rate is controlled using UBRR register

For a given frequency, value of UBRR register

determines baud rate

The relation is given as

Baud Rate = Freq / (16 (X + 1))

X = (Freq / (16 (Baud Rate))) - 1

Page 4: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

AVR Serial Port Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

4

UBRR register and Baud Rate in the AVR

The equation can be related as

Baud Rate = Freq / (16 (X + 1))

Page 5: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Exercise 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

5

When frequency is 8MHz, find value of UBRR for different baud

rates provided in table below

Baud Rate = Freq / (16 (X + 1))

Baud Rate UBRR value

38400

19200

9600

4800

2400

Page 6: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

AVR Serial Port Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

6

UDR register and USART data IO

Provide full duplex communication, two shift registers are used

Transmit Shift register

Receive Shift register

Each shift register is connected to a buffer register

Both buffers share same IO data address

When UDR is written, data is copied to transmit buffer, when

UDR is read, contents of receive buffer are returned

Page 7: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

UCSR Registers and USART Config

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

7

UCSRA: USART Control and Status Register A

RXC: USART Receive Complete

Set to high, when buffer has new data

Cleared when buffer is empty

TXC: USART Transmit Complete

Set to high, when entire frame is transmitted

Cleared when empty buffer

UDRE: USART Data Register Empty

Is Set when transmit data buffer is empty

FE: Frame Error

Is Set, when there is error in next character

Detected using low stop bit

RXC TXC UDRE FE DOR PE U2X MPCM

Page 8: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

UCSR Registers and USART Config

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

8

UCSRA: USART Control and Status Register A

DOR: Data OverRun

Is set, when both receive data buffer and receive shift registers

are full

PE: Parity Error

Is set if receive buffer has parity error

U2X: Double the USART Transmission Speed

When this is high, the transmission speed is doubled

MPCM: Multi-Processor Communication Mode

Not the scope of this course

RXC TXC UDRE FE DOR PE U2X MPCM

Page 9: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

UCSR Registers and USART Config

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

9

UCSRB: USART Control and Status Register B

RXCIE: Receive Complete Interrupt Enable

Set this to one, to enable interrupt on RXC flag

TXCIE: Transmit Complete Interrupt Enable

Set this to one, to enable interrupt on TXC flag

UDRIE: USART Data Register Empty Interrupt Enable

this to one, to enable interrupt on UDRE flag

RXEN: Receive Enable

Set this to one to enable receiver

TXEN: Transmit Enable

Set this to one to enable transmitter

RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8

Page 10: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

UCSR Registers and USART Config

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

10

UCSRB: USART Control and Status Register B

UCSZ2: Character Size

Combined with UCSZ1:0, size of character is transmitted is

controlled using this bit

RXB8: Receive Data bit 8

When receiving 9-bits data, the 9th bit is stored in RXB8

TXB8: Transmit Data bit 8

This is 9th bit while transmitting 9-bits

RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8

Page 11: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

UCSR Registers and USART Config

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

11

UCSRC: USART Control and Status Register C

URSEL: Register Select

To access either UBRRH or UCSRC

UMSEL: USART Model Select

0 = Asynchronous

1 = Synchronous

UPM1:0 : Parity Mode

00 = Disabled, 01 = Reserved, 10 = Even Parity, 11 = Odd Parity

USBS: Stop Bit select

0 = 1 bit, 1 = 2 bits

URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL

Page 12: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

UCSR Registers and USART Config

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

12

UCSRC: USART Control and Status Register C

UCSZ1:0: Character Size

UCSZ2, UCSZ1 and UCSZ0 used to define character size

UCPOL: Clock Polarity

Used for synchronous mode

Not scope of this course

URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL

UCSZ2 UCSZ1 UCSZ0 Character Size

0 0 0 5

0 0 1 6

0 1 0 7

0 1 1 8

1 1 1 9

Page 13: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Exercise 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

13

(a) What are the values of UCSRB and UCSRC needed to

configure USART for asynchronous operating mode, 8 data bits

(character size), no parity and 1 stop bit? Enable both receive

and transmit.

(b) Write a Assembly program for the AVR to set the values of

UCSRB and UCSRC for this configuration.

RXEN = TXEN = 1 (Receive/Transmit)

UCSZ2:0 = 011 8-bit data

UMSEL = 0 Asynchronous mode

UPM1:0 = 00 No parity

USBS = 0 one Stop Bit

LDI R16, (1 << RXEN) | (1 << TXEN)

OUT UCSRB, R16

LDI R16, (1<<UCSZ1) | (1<<UCSZ0) |

(1<<URSEL)

OUT UCSRC, R16

Page 14: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Exercise 3

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

14

In last exercise, set the baud rate to 1200 and write a program for

the AVR to set up the values of UCSRB, UCSRC and UBRR with

frequency is 8MHz

LDI R16, (1 << RXEN) | (1 << TXEN)

OUT UCSRB, R16

LDI R16, (1<<UCSZ1) | (1<<UCSZ0) | (1<<URSEL)

OUT UCSRC, R16

LDI R16, 0x9F

OUT UBRRL, R16

LDI R16, 0x01

OUT UCSRC, R16

Page 15: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Serial Port Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

15

Programming AVR to Transfer Data Serially

UCSRB is loaded with 0x08 (Transmitter enabled)

UCSRC is loaded with 0x06 (Asynchronous mode, 8-bit size,

no parity and one stop bit)

UBRR is loaded with some value for Baud Rate

Store character byte to be transmitted in UDR register

Monitor UDRE bit of UCSRC to make sure UDR is ready for

next byte

To Transmit next character, go back and write next byte in

UDR

Page 16: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Serial Port Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

16

Importance of UDRE flag

If UDR is not empty, and it is written again

Data could be lost

Make sure, UDR register is not overloading

Can be monitored using

SBIS UCSRA, UDRE

Page 17: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Example 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

17

Write a program to transmit the message “YES” serially at 9600

baud, 8-bit data and 1 stop bit. Do this forever.

LDI R21, (HIGH) RAMEND

OUT SPH, R21

LDI R21, (LOW) RAMEND

OUT SPL, R21

LDI R16, (1 << TXEN)

OUT UCSRB, R16

LDI R16, (1 << UCSZ1) |

(1 << UCSZ0) |

(1 << URSEL)

OUT UCSRC, R16

LDI R16, 0x33

OUT UBRRL, R16

AGAIN:

LDI R17, ‘Y’

CALL TRANSMIT

LDI R17, ‘E’

CALL TRANSMIT

LDI R17, ‘S’

CALL TRANSMIT

LDI R17, ‘ ’

CALL TRANSMIT

RJMP AGAIN

TRANSMIT:

SBIS UCSRA, UDRE

RJMP TRANSMIT

OUT UDR, R17

RET

Page 18: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Serial Port Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

18

Programming AVR to Receive Data Serially

UCSRB is loaded with 0x10 (Receiver enabled)

UCSRC is loaded with 0x06 (Asynchronous mode, 8-bit size,

no parity and 1 stop bit)

UBRR is loaded with some value for Baud Rate

RXC flag bit of UCSRA is monitored for a HIGH (indicates full

character is received)

When RXC is raised, UDR register has the value and the

received contents are copied.

To receive next character monitor RXC again

Page 19: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Example 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

19

Program the Atmega32 to receive bytes of data serially and put

them on Port B. Set the baud rate at 9600, 8-bit data and 1 stop

bit.

LDI R16, (1 << RXEN)

OUT UCSRB, R16

LDI R16, (1 << UCSZ1) |

(1 << UCSZ0) |

(1 << URSEL)

OUT UCSRC, R16

LDI R16, 0x33

OUT UBRRL, R16

LDI R16, 0xFF

OUT DDRB, R16

RCVE:

SBIS UCSRA, RXC

RJMP RCVE

OUT PORTB, R17

RJMP RCVE

Page 20: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Class Exercise 4

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

20

Transmit and Receive

Assume AVR Serial port is connected to COM port of PC

Port A is connected to LEDs

Port B is connected to Switches

Write AVR program

Send message ‘YES’ once to the PC screen

Get data from Switches on Port A and transmit it via the serial

Port to the PC’s screen (repeatedly)

Receive any key press from PC put it on LEDs (repeatedly)

Page 21: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Class Exercise 4

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

21

Define Input and Output Ports

Define Stack for Call Instructions

Enable Transmitter and Receiver

Enable Asynchronous, 8-bit data, no parity

and one stop bit mode

Define Baud Rate

Transmit ‘YES’

Check if new byte is available to receive ?

If yes then receive the byte and send to Port B,

else skip

Check if UDR is empty

If empty then read from Port A and transmit else

skip reading Port A.

Loop last two points again and again

Page 22: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Class Exercise 4

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

22

LDI R21, 0x00

OUT DDRA, R21

LDI R21, 0xFF

OUT DDRB, R21

LDI R21, (HIGH) RAMEND

OUT SPH, R21

LDI R21, (LOW) RAMEND

OUT SPL, R21

LDI R16, (1<<TXEN) | (1<<RXEN)

OUT UCSRB, R16

LDI R16, (1 << UCSZ1) |

(1 << UCSZ0) |

(1 << URSEL)

OUT UCSRC, R16

LDI R16, 0x33

OUT UBRRL, R16

LDI R17, ‘Y’

CALL TRANSMIT

LDI R17, ‘E’

CALL TRANSMIT

LDI R17, ‘S’

CALL TRANSMIT

AGAIN:

SBIS UCSRA, RXC

RJMP SKIP_RX

IN R17, UDR

OUT PORTB, R17

SKIP_RX:

SBIS UCSRA, UDRE

RJMP SKIP_TX

IN R17, PINA

OUT UDR, R17

SKIP_TX:

RJMP AGAIN

TRANSMIT:

SBIS UCSRA, UDRE

RJMP TRANSMIT

OUT UDR, R17

RET

Page 23: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Example 3

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

23

Write a C function to initialize the USART to work at 9600 baud

rate, 8-bit data and 1 stop bit. Assume XTAL is 8MHz.

void usart_init (void)

{

UCSRB = (1 << TXEN);

UCSRC = (1 << UCSZ1) | (1 << UCSZ0) | (1 << URSEL);

UBRRL = 0x33;

}

Page 24: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Class Exercise 5

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

24

Convert the following Assembly Code to C language

LDI R16, (1 << TXEN)

OUT UCSRB, R16

LDI R16, (1 << UCSZ1) |

(1 << UCSZ0) |

(1 << URSEL)

OUT UCSRC, R16

LDI R16, 0x33

OUT UBRRL, R16

AGAIN:

SBIS UCSRA, UDRE

RJMP AGAIN

LDI R16, ‘G’

OUT UDR, R16

RJMP AGAIN

void (main)

{

UCSRB = (1 << TXEN);

UCSRC = (1 << UCSZ1) | (1 << UCSZ0)

(1 << URSEL);

UBRRL = 0x33;

while (1)

{

while (! (UCSRA & (1 << UDRE)));

UDR = ‘G’;

}

}

Page 25: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Serial Programming and Interrupts

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

25

Interrupt Based Data Receive

Set RXCIE (Receive Complete Interrupt Enable) in UCSRB

Whenever RXC flag is raised, the program jumps to

interrupt vector

Interrupt Based Data Transmit

Set UDRIE (USART Data Register Empty Interrupt Enable) in

UCSRB

Whenever UDRE flag is raised, the program jumps to

interrupt vector

Page 26: AVR Serial Port Programming in Assembly and Csaqazi.com/EEE342/FA19_MP_Lecture17_20191114_AVR...Assignment No. 2 –Question 2 - 7 Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad

Assignment No. 2 – Question 2 - 7

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

26

Example 11 (Page 420) – Transmitter

Example 13 (Page 421) – Receiver

Example 15 (Page 422) – Receiver Interrupt

Example 17 (Page 424) – Receiver Interrupt

Example 16 (Page 423) – Transmitter Interrupt

Example 18 (Page 423) – Transmitter Interrupt