0% found this document useful (0 votes)
66 views26 pages

Report Lab4

Uploaded by

MINH VÕ QUANG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views26 pages

Report Lab4

Uploaded by

MINH VÕ QUANG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

VIETNAM NATIONAL UNIVERSITY HO CHI MINH CITY

HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY




REPORT LAB 4
MICROPROCESSOR

Lecture: NGUYỄN TRUNG HIẾU


GROUP TT05-GROUP CODE 6-SEMESTER 231

Student Student’s code Signature

Võ Quang Minh 1951080

Nguyễn Hoài Hiếu Ngân 2051154

Lê T ấn Phát 2151129

Ho Chi Minh City , 2023


LAB 4-1
COMMUNICATE WITH SERIAL PORT, EEPROM, RTC

OBJECTIVE:

● Understand and use UART, I2C, SPI peripherals.

● Understand how to communicate with RTC and EEPROM.

REFERENCES:

● Lab manual chapter 7, 9, 11

● Atmel-2505-Setup-and-Use-of-AVR-Timers_ApplicationNote_AVR130.pdf

EXPERIMENT 1:

a) Connect the TxD and RxD pins of UART0 to UART_TxD0 and UART_RxD0 signals on
header J85 in the UART block.
b) Connect a USB-Serial cable to the experimentation kit.
c) Set up the Hercules program with a baud rate of 9600, 8-bit data, no parity, 1 stop bit, and
no handshake.
d) Use sample programs from the experiment guide to write a program that initializes UART0
with the specified parameters, waits to receive one byte from UART0, and then sends it
back to UART0.
e) Use Hercules to send a character to the kit and observe the received data to check the
program's functionality. (Note: The CPU clock frequency on the kit is 8 MHz.)

EXPERIMENT 2:

a) Connect the SDA and SCL signals of AVR to the corresponding signals on the RTC
module. Connect one port pin to the MFP signal. Connect a 16x2 LCD to one port of AVR.
b) Write a subprogram to initialize the RTC with the current time, configure the MFP signal
to have a 1Hz frequency, and read the date, month, year, hour, minute, and second values
from the RTC. Then, update these values on the LCD.
c) Compile the program and observe the LCD to verify its functionality.

https://doe.dee.hcmut.edu.vn/
LAB 4-1
COMMUNICATE WITH SERIAL PORT, EEPROM, RTC

EXPERIMENT 3:

a) Connect the MOSI and SCK signals from the SPI port of AVR to the SDI and CLK signals
of the shift register. Connect two port pins to the nCLR and LATCH signals. Connect the
output of the shift register to the Bar LED.
b) Connect the UART signals as in exercise 1.
c) Write a program that receives one value from UART and outputs it to the Bar LED using
SPI.

EXPERIMENT 4:

a) Connect the MOSI, MISO, and SCK signals from the SPI port of AVR to the corresponding
signals on header J80. Connect one port pin to the nCS signal.
b) Connect the UART signals as in exercise 1.
c) Connect one port to the Bar LED.
d) Write a program to count the number of characters received from UART and display them
on the Bar LED. Every time a byte is received, increment the count and write it to
EEPROM. When the microcontroller loses power and then regains it, read the count from
EEPROM and use it as the starting value.

EXPERIMENT 5:

a) Connect the UART signals as in exercise 1.


b) Connect one port to the Bar LED.
c) Write a program to count the number of characters received from UART and display them
on the Bar LED. Every time a byte is received, increment the count and write it to the
AVR's internal EEPROM. When the microcontroller loses power and then regains it, read
the count from the internal EEPROM and use it as the starting value.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

EXPERIMENT 1:

1. Answer the following questions:


a. With a clock frequency of 8 MHz, how much will the actual baud rate deviate from
the desired 9600 baud?
Actual deviation from desire is 0.2%

b. What is the purpose of the UDRE flag?


The hardware automatically sets the UDREn=1 flag when the UDRn transmit counter is
empty and clears the UDREn=0 flag when writing data to be transmitted to this register.

c. Explain the difference between hardware UART and software UART (bit-banging
UART).
The method used to perform UART communication is the primary distinction between
Hardware UART and Software UART. Hardware UART is built-in hardware on the
microcontroller, whereas Software UART is a software way for controlling the
microcontroller's GPIO pins to execute UART communication.

d. Which port pins correspond to the TxD0 and RxD0 pins of UART0?
TxD0: is PD1 pin
RxD0: is PD0 pin

e. How many hardware UARTs does the Atmega324 have?


There are 2 hardware UARTs: UART0 and UART1.

2. Provide the source code with comments.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

.include "m324padef.inc"
; Replace with your application code
call USART_Init
start:
call USART_ReceiveChar
call USART_SendChar
rjmp start
;init UART 0
;CPU clock is 1Mhz
USART_Init:
; Set baud rate to 9600 bps with 1 MHz clock
ldi r16, 51
sts UBRR0L, r16
;set double speed
;ldi r16, (1 << U2X0)
;sts UCSR0A, r16
; Set frame format: 8 data bits, no parity, 1 stop bit
ldi r16, (1 << UCSZ01) | (1 << UCSZ00)
sts UCSR0C, r16
; Enable transmitter and receiver
ldi r16, (1 << RXEN0) | (1 << TXEN0)
sts UCSR0B, r16

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

ret
;send out 1 byte in r16
USART_SendChar:
push r17
; Wait for the transmitter to be ready
USART_SendChar_Wait:
Lds r17, UCSR0A
sbrs r17, UDRE0 ;check USART Data Register Empty bit
rjmp USART_SendChar_Wait
sts UDR0, r16 ;send out
pop r17
ret
;receive 1 byte in r16
USART_ReceiveChar:
push r17
; Wait for the transmitter to be ready
USART_ReceiveChar_Wait:
lds r17, UCSR0A
sbrs r17, RXC0 ;check USART Receive Complete bit
rjmp USART_ReceiveChar_Wait
lds r16, UDR0 ;get data
pop r17
ret

EXPERIMENT 3:

1. Answer the following questions:


a. According to the datasheet of the 74HC595, what is the highest clock frequency it
can accept?
The maximum clock frequency supported by the 74HC595 is 100 MHz, according to the
datasheet. However, depending on factors such as load capacity and circuit design, the
maximum feasible frequency in a certain application may be lower. To ensure
appropriate operation of the device in a specific circuit, it is critical to review the
datasheet and any extra application notes.

b. With a clock of 8 MHz, what is the maximum SPI speed for the Atmega328?
Since the SPI speed cannot be greater than ¼ the clock speed for the chip, the max SPI speed
= Fosc/ 16 = ½ MHz.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

2. Provide the source code with comments.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

.def data_Rx = r19


.equ SS = 4
.equ MOSI = 5
.equ MISO = 6
.equ SCK = 7

.org 0x00
rjmp main

.org 0x40
main:
ldi r16, high(0x85F) ;Set the Stack Pointer to RAMEND
out SPH, r16
ldi r16, low(0x85F)
out SPL, r16

rcall USART_INIT

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

rcall SPI_INIT

clr data_Rx

start:
rcall USART_RECEIVE ; Receive data from USART
mov r16, data_Rx ; Transmit to SPI
cbi PORTB, SS ; Enable sending
rcall SPI_TRANS
sbi PORTB, SS ; Unable sending
rjmp start

USART_INIT:
ldi r16, (1<<RXEN0) | (1<<TXEN0) ;Enable Receive and Transmit
sts UCSR0B, r16

ldi r16, low(51) ; Baud rate = 9600


sts UBRR0L, r16
ldi r16, high(51)
sts UBRR0H, r16

ldi r16, (1<<UCSZ01) | (1<<UCSZ00) ; 8-bit, no parity and 1 stop bit


sts UCSR0C, r16
ret

USART_RECEIVE:
push r17
WAIT_USART_RECEIVE:
lds r17, UCSR0A
sbrs r17, RXC0
rjmp WAIT_USART_RECEIVE
lds data_Rx, UDR0
pop r17
ret

SPI_INIT:
push r20
ldi r20, (1<<SS) | (1<<MISO) | (1<<MOSI) | (1<<SCK)
out DDRB, r20
ldi r16, (1<<SPE0) | (1<<MSTR0) | (SPR01) ;SPI enable, Master
mode
out SPCR0, r16
pop r20
ret

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

SPI_TRANS:
out SPDR0, R16
WAIT_SPI:
in r18, SPSR0
sbrs r18, SPIF0
rjmp WAIT_SPI
in r20, SPDR0
ret

EXPERIMENT 4:

1. Answer the following questions:


a. What is the EEPROM size of the 25AA1024?
According to the datasheet, the capacity of EEPROM 25AA1024 is 1 Megabit (128
Kbytes).

b. According to the datasheet, what is the fastest clock frequency that can be provided
to this EEPROM?
According to the datasheet, the fastest frequency of the CK pulse inserted into this
EEPROM is 10 MHz.

2. Provide the source code with comments.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

⮚ Videoclip:
https://drive.google.com/file/d/1hu5jjxRD8sE7C1sQSVyNdrtQ5Lmihp2P/view?usp=
sharing

.equ SPI_PORT = PORTB


.equ SPI_PORT_DDR = DDRB
.equ SS = 4
.equ MOSI = 5
.equ MISO = 6
.equ SCLK = 7

.equ WIP = 0 ;bit indicates that EEPROM is in process


.equ WREN = $06 ;Instruction format of enable writing EEPROM
.equ RDSR = $05 ;Instruction format of read status register of EEPROM
.equ WRSR = $01 ;Instruction format of writing status of EEPROM
.equ RD = $03 ;Instruction format of reading EEPROM
.equ WR = $02 ;Instruction format of writing EEPROM
.equ PE = $42 ;Instruction format of page erase EEPROM
.equ ADDR_HIGH = 0x00
.equ ADDR_MID = 0x01
.equ ADDR_LOW = 0x00

.org 0x0
rjmp start

.org 0x40
start:
ldi r16, high(0x85F)
out SPH, r16
ldi r16, low(0x85F)
out SPL, r16

rcall SPI_INIT
rcall USART_INIT
ldi r16, 0xFF
out DDRA, r16

rcall EEPROM_READ
rcall EEPROM_READ
out PORTA, r20

main:

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

rcall USART_RECEIVE
inc r20
out PORTA, r20
rcall EEPROM_ERASE
rcall EEPROM_WRITE
rjmp main

EEPROM_READ:
ldi r16, RD
cbi SPI_PORT, SS ;Allow to transmit SPI
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_HIGH ;Get the High Address
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_MID ;Get the Mid Address
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_LOW ;Get the Low Address
rcall SPI_TRANS ;Transmitting SPI
ldi r16, 0xFF ;Dump data
rcall SPI_TRANS ;Transmitting SPI
mov r20, r19
sbi SPI_PORT, SS ;End transmitting SPI
ret

EEPROM_WRITE:
ldi r16, WREN ;;GET THE INSTRUCTION OF ENABLING TO WRITE TO
EEPROM;;;;;
cbi SPI_PORT, SS ;Allow to transmit SPI
rcall SPI_TRANS ;Transmitting SPI
sbi SPI_PORT, SS ;End transmitting SPI

ldi r16, WR ;;;;GET THE INSTRUCTION OF WRITING TO EEPROM;;;;;


cbi SPI_PORT, SS ;Allow to transmit SPI
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_HIGH ;Get the High Address
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_MID ;Get the Mid Address
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_LOW ;Get the Low Address
rcall SPI_TRANS ;Transmitting SPI

mov r16, r20 ;Get the data to write to SPI


rcall SPI_TRANS ;Transmitting SPI
sbi SPI_PORT, SS ;End transmitting SPI
rcall DELAY_8MS
ret

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

EEPROM_ERASE:
ldi r16, WREN ;;;;GET THE INSTRUCTION OF ENABLING TO WRITE TO
EEPROM;;;;;
cbi SPI_PORT, SS ;Allow to transmit SPI
rcall SPI_TRANS ;Transmitting SPI
sbi SPI_PORT, SS ;End transmitting SPI

ldi r16, PE ;;;;GET THE INSTRUCTION OF PAGE ERASE EEPROM;;;;;


cbi SPI_PORT, SS ;Allow to transmit SPI
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_HIGH ;Get the High Address
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_MID ;Get the Mid Address
rcall SPI_TRANS ;Transmitting SPI
ldi r16, ADDR_LOW ;Get the Low Address
rcall SPI_TRANS ;Transmitting SPI
sbi SPI_PORT, SS ;End transmitting SPI
rcall DELAY_8MS
ret

SPI_INIT:
ldi r16, (1<<SS) | (1<<MOSI) | (1<<MISO) | (1<<SCLK)
out SPI_PORT_DDR, r16
ldi r16, (1<<SPE0) | (1<<MSTR0) | (1<<SPR00)
out SPCR0, r16
ldi r16, 1<<SPI2X0
sts SPSR0, r16
ret

SPI_TRANS:
out SPDR0, R16
WAIT_SPI:
in r18, SPSR0
sbrs r18, SPIF0
rjmp WAIT_SPI
in r19, SPDR0
ret

USART_INIT:
ldi r16, (1<<RXEN0) | (1<<TXEN0) ;Enable Transmit and Receive
sts UCSR0B, r16
ldi r16, (1<<UCSZ01) | (1<<UCSZ00) ; 8-bit data
sts UCSR0C, r16 ;no parity, 1 stop bit
ldi r16, low(51)
sts UBRR0L, r16 ;baud rate = 9600 and Fosc = 8MHz
ldi r16, high(51)

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

sts UBRR0H, r16


ret

USART_RECEIVE:
lds r21, UCSR0A
sbrs r21, RXC0
rjmp USART_RECEIVE
lds r21, UDR0
ret

DELAY_8MS:
ldi r16, 124
out OCR0A, r16

ldi r16, 1<<WGM01


out TCCR0A, r16
ldi r16, 1<<CS02
out TCCR0B, r16
AGAIN:
sbis TIFR0, OCF0A
rjmp AGAIN
sbi TIFR0, OCF0A
clr r16
out TCCR0B, r16
ret

EXPERIMENT 5:

1. Answer the following questions:


a. What is the EEPROM size of the Atmega324PA?
According to the datasheet, Atmega324PA has 1KB EEPROM capacity.

b. List the differences between SRAM and EEPROM.


According to the datasheet,
- SRAM is random access memory.
- EEPROM is a programmable read-only memory with an electronic erase function.

c. List the differences between Flash and EEPROM.


- Flash uses NAND type memory while EEPROM uses NOR type.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

- Flash is erasable while EEPROM is byte-erasable.


- Flash is constantly rewritten while other EEPROMs are rarely rewritten.
- Flash is when a large amount is needed while EEPROM is used when only a small
amount is needed.

2. Provide the source code with comments.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

.equ ADDR_LOW = $01


.equ ADDR_HIGH = $00
.def data_Rx = r19
.def counter = r20

.org 0x00
rjmp start

.org 0x40
start:
ldi r16, high(0x85F)
out SPH, r16
ldi r16, low(0x85F)
out SPL, r16

rcall USART_INIT
ser r16
out DDRA, r16
clr data_Rx

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

rcall EEPROM_READ
out PORTA, counter
main:
rcall USART_RECEIVE
inc counter
out PORTA, counter
rcall EEPROM_WRITE
rjmp main

EEPROM_READ:
sbic EECR, EEPE ;check if programming enable or not ?
rjmp EEPROM_READ ; PE=0 --> out the loop
push r16
ldi r16, ADDR_LOW ;Get the low address
out EEARL, r16
ldi r16, ADDR_HIGH ;Get the high address
out EEARH, r16
sbi EECR, EERE ; EERE = 1 --> Read enable
in counter, EEDR ;increase the number
pop r16
ret

EEPROM_WRITE:
sbic EECR, EEPE ;check if programming enable or not ?
rjmp EEPROM_WRITE ; PE=0 --> out the loop
push r16
ldi r16, ADDR_LOW ;Get the low address
out EEARL, r16
ldi r16, ADDR_HIGH ;Get the high address
out EEARH, r16
out EEDR, counter
sbi EECR, EEMPE ;Master Programming Enable
sbi EECR, EEPE ;Enable Programming
rcall DELAY_8MS ;Wait
pop r16
ret

USART_INIT:
ldi r16, (1<<RXEN0)
sts UCSR0B, r16

ldi r16, low(51)


sts UBRR0L, r16 ; Baud rate =
9600 and Fosc = 8MHz
ldi r16, high(51)

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

sts UBRR0H, r16

ldi r16, (1<<UCSZ01) | (1<<UCSZ00) ; 8-bit


sts UCSR0C, r16 ; No parity, 1
stop bit
ret

USART_RECEIVE:
push r17
WAIT_USART_RECEIVE:
lds r17, UCSR0A
sbrs r17, RXC0
rjmp WAIT_USART_RECEIVE
lds data_Rx, UDR0
pop r17
ret

DELAY_8MS:
ldi r16, 124
out OCR0A, r16

ldi r16, 1<<WGM01


out TCCR0A, r16
ldi r16, 1<<CS02
out TCCR0B, r16
AGAIN:
sbis TIFR0, OCF0A
rjmp AGAIN
sbi TIFR0, OCF0A
clr r16
out TCCR0B, r16
ret

https://doe.dee.hcmut.edu.vn/
LAB 4-2
COMMUNICATE WITH SERIAL PORT, EEPROM,
RTC

OBJECTIVE:

• Understand and use UART, I2C, SPI peripherals.

• Understand how to communicate with RTC and EEPROM.

REFERENCES:

• Lab manual chapter 7, 9, 11

• Atmel-2505-Setup-and-Use-of-AVR-Timers_ApplicationNote_AVR130.pdf

EXPERIMENT 1:

a) Connect the additional necessary signals to control the character LCD.


b) Connect the UART signals to the RS232 module and establish a connection using a USB-
Serial cable.
c) Write a program to receive characters from UART using interrupts, display the received
character on the LCD at the leftmost position, and simultaneously count the number of
received characters and display it on a 4-digit 7-segment LED display. When the count
exceeds 1000, reset it to 0.
Instructions:
• Use UART interrupts to receive characters. Inside the UART interrupt, increment a 16-
bit counter variable, convert it to BCD format, and store it in four bytes of the
LED7segValue array. For example, if the count is 500, store 0-5-0-0 in these four bytes.
• The LED scanning part remains the same as in Exercise 3.
(Refer to Chapter 4, section 4.4, of the experiment guide for further guidance.)

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

EXPERIMENT 1:

1. Answer the questions:


a. Describe the connections on the kit.

b. Describe how to configure UART.


c. How is the conversion from a count to a BCD number performed to write it into
the LED display buffer?

The conversion from a count to a BCD (Binary Coded Decimal) number involves breaking
down the count into individual decimal digits and representing each digit in its 4-bit binary form.
Once you have the BCD representation, we can use it to update a 4-digit 7-segment LED
display.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

2. Program source code with comments


.equ DATAP=PORTA
.equ CTR=PORTA
.equ RS=0
.equ RW=1
.equ EN=2
.def ANSL = R2 ;To hold low-byte of answer
.def ANSH = R3 ;To hold high-byte of answer
.def REML = R4 ;To hold low-byte of remainder
.def REMH = R5 ;To hold high-byte of remainder
.def AL = R20 ;To hold low-byte of dividend
.def AH = R21 ;To hold high-byte of dividend
.def BL = R22 ;To hold low-byte of divisor
.def BH = R23 ;To hold high-byte of divisor
.def C = R24 ;Bit Counter

.org 00
rjmp START
.org 0X28
rjmp USART0_RX

.org 0X40
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
START:
CLR R30
LDI R16, HIGH(RAMEND) ;initialize high byte of
OUT SPH, R16 ;stack pointer
LDI R16, LOW(RAMEND) ;initialize low byte of
OUT SPL,R16
CALL INPUT_OUTPUT
CALL SUPPLY_LCD
CALL UART_REG
SEI
LED_7:
MOV AL,R0 ;Load low-byte of dividend into AL
MOV AH,R1 ;Load HIGH-byte of dividend into AH
LDI BL,LOW(10) ;Load low-byte of divisor into BL
LDI BH,HIGH(10) ;Load high-byte of divisor into BH
RCALL DIV1616
MOV R29,REML

MOV AL,ANSL ;Load low-byte of dividend into AL


MOV AH,ANSH ;Load HIGH-byte of dividend into AH
LDI BL,LOW(10) ;Load low-byte of divisor into BL
LDI BH,HIGH(10) ;Load high-byte of divisor into BH
RCALL DIV1616

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

MOV R28,REML

MOV AL,ANSL ;Load low-byte of dividend into AL


MOV AH,ANSH ;Load HIGH-byte of dividend into AH
LDI BL,LOW(10) ;Load low-byte of divisor into BL
LDI BH,HIGH(10) ;Load high-byte of divisor into BH
RCALL DIV1616
MOV R27,REML

MOV AL,ANSL ;Load low-byte of dividend into AL


MOV AH,ANSH ;Load HIGH-byte of dividend into AH
LDI BL,LOW(10) ;Load low-byte of divisor into BL
LDI BH,HIGH(10) ;Load high-byte of divisor into BH
RCALL DIV1616
MOV R26,REML
LED1:
MOV R16,R26
RCALL WRITEDATA
LDI R16,0XF7
OUT PORTB,R16
SBI PORTD,3
CBI PORTD,3
LDI R21,50
RCALL DELAY
RCALL RESET_LED
LED2:
MOV R16,R27
RCALL WRITEDATA
LDI R16,0XFB
OUT PORTB,R16
SBI PORTD,3
CBI PORTD,3
LDI R21,50
RCALL DELAY
RCALL RESET_LED
LED3:
MOV R16,R28
RCALL WRITEDATA
LDI R16,0XFD
OUT PORTB,R16
SBI PORTD,3
CBI PORTD,3
LDI R21,50
RCALL DELAY
RCALL RESET_LED
LED4:
MOV R16,R29

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

RCALL WRITEDATA
LDI R16,0XFE
OUT PORTB,R16
SBI PORTD,3
CBI PORTD,3
LDI R21,50
RCALL DELAY
RCALL RESET_LED
RJMP LED_7
DATA: .DB
0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0X88,0X83,0XC6,0X
A1,0X86,0X8E
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WRITEDATA:
LDI ZH,HIGH(DATA<<1)
LDI ZL,LOW(DATA<<1)
ADD ZL,R16
LPM R17,Z
OUT PORTB,R17
SBI PORTD,2
CBI PORTD,2
RET
RESET_LED:
LDI R16,0X0F
OUT PORTB,R16
SBI PORTD,3
CBI PORTD,3
RET
DELAY5MS:
LDI R16,-39 ; TCNT0 = -39
OUT TCNT0,R16
LDI R16,0X00; NORMAL MODE TIMER0
OUT TCCR0A,R16
LDI R16,0X05; N = 1024
OUT TCCR0B,R16
WAIT: SBIS TIFR0,TOV0 ; check TOV0 = 1
RJMP WAIT ;wait if TOV0=0
SBI TIFR0,TOV0 ;if TOV0=1
LDI R16,0x00 ;stop Timer0
OUT TCCR0B,R16
RET
DIV1616:
MOVW ANSH:ANSL,AH:AL ;Copy dividend into answer
LDI C,17 ;Load bit counter
SUB REML,REML ;Clear Remainder and Carry
CLR REMH ;
LOOP: ROL ANSL ;Shift the answer to the left

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

ROL ANSH ;
DEC C ;Decrement Counter
BREQ DONE2 ;Exit if sixteen bits done
ROL REML ;Shift remainder to the left
ROL REMH ;
SUB REML,BL ;Try to subtract divisor from remainder
SBC REMH,BH
BRCC SKIP ;If the result was negative then
ADD REML,BL ;reverse the subtraction to try again
ADC REMH,BH ;
CLC ;Clear Carry Flag so zero shifted into A
RJMP LOOP ;Loop Back
SKIP: SEC ;Set Carry Flag to be shifted into A
RJMP LOOP
DONE2: RET

INPUT_OUTPUT:
LDI R16,0B11110111
OUT DDRA,R16
LDI R16,0XFF
OUT DDRB,R16
OUT PORTB,R16
LDI R16,0B00000110
OUT DDRD,R16
CBI PORTD,2
CBI PORTD,3
RET
SUPPLY_LCD:
LDI R20,$30
CALL CMDWRITE
LDI R21,50
CALL DELAY
LDI R20,$30
CALL CMDWRITE
CALL DELAY100US
LDI R20,$30
CALL CMDWRITE
CALL DELAY100US
LDI R20,$20
CALL CMDWRITE
CALL DELAY100US
LDI R20,$28
CALL CMDWRITE4BIT
LDI R20,$0E
CALL CMDWRITE4BIT
LDI R20,$06
CALL CMDWRITE4BIT

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

LDI R20,$01
CALL CMDWRITE4BIT
RET
CMDWRITE4BIT:
PUSH R20
ANDI R20,$F0
CALL CMDWRITE
POP R20
ANDI R20,$0F
SWAP R20
CALL CMDWRITE
RET
CMDWRITE:
OUT DATAP,R20
CBI CTR,RS
CBI CTR,RW
SBI CTR,EN
NOP
CBI CTR,EN
LDI R21,20
CALL DELAY
RET
DATAWRITE4BIT:
PUSH R20
ANDI R20,$F0
CALL DATAWRITE
POP R20
ANDI R20,$0F
SWAP R20
CALL DATAWRITE
RET
DATAWRITE:
OUT DATAP,R20
SBI CTR,RS
CBI CTR,RW
SBI CTR,EN
NOP
CBI CTR,EN
LDI R21,20
CALL DELAY
RET
DELAY:
L1:
CALL DELAY100US
DEC R21
BRNE L1
RET

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR

DELAY100US:
PUSH R21
LDI R21,200
L2: NOP
DEC R21
BRNE L2
POP R21
RET

UART_REG:
LDI R16, 51
;Baud rate = 9600
STS UBRR0L, R16
LDI R16, (1 << RXEN0)|(1<<RXCIE0) ;Enable Receive
and Transmit
STS UCSR0B, R16
LDI R16, (1 << UCSZ01) | (1 << UCSZ00) ;8-bit data
STS UCSR0C, R16 ;no
parity, 1 stop bit
RET
USART0_RX:
LDS R17,UDR0
;
LDI R20,$80 ;Force cursor to the beginning of
1st line
CALL CMDWRITE4BIT
MOV R20,R17
CALL DATAWRITE4BIT
;
LDI R30,1
INC R15
MUL R15,R30
RETI

https://doe.dee.hcmut.edu.vn/

You might also like