Report Lab4
Report Lab4
REPORT LAB 4
MICROPROCESSOR
Lê T ấn Phát 2151129
OBJECTIVE:
REFERENCES:
● 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:
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR
EXPERIMENT 1:
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
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:
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
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR
.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
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:
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.
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
.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
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
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
USART_RECEIVE:
lds r21, UCSR0A
sbrs r21, RXC0
rjmp USART_RECEIVE
lds r21, UDR0
ret
DELAY_8MS:
ldi r16, 124
out OCR0A, r16
EXPERIMENT 5:
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR
.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
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR
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
https://doe.dee.hcmut.edu.vn/
LAB 4-2
COMMUNICATE WITH SERIAL PORT, EEPROM,
RTC
OBJECTIVE:
REFERENCES:
• Atmel-2505-Setup-and-Use-of-AVR-Timers_ApplicationNote_AVR130.pdf
EXPERIMENT 1:
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR
EXPERIMENT 1:
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
.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
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:06
Class group:TT05 Subject:MICROPROCESSOR
MOV R28,REML
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/