Lab5 - 1 Eng
Lab5 - 1 Eng
OBJECTIVE:
REFERENCES:
a) Connect the ADC_VR1 and ADC_VR2 signals from header J86 to the ADC0 and ADC1
inputs. Connect UART0 to the RS232 module and connect the USB-Serial cable to the
computer. Connect ADC_VR1 and ADC_VR2 to the test points on header J56. Make
sure not to connect them to the GND pins on the header. Write a program to perform the
following tasks:
b) Select the VREF voltage as the internal VCCA voltage. Initialize UART with self-selected
configuration. (Note: configure the Hercules software on the computer in a similar way).
Right-click on the Hercules screen to select HEX Enable.
c) Write a program to sample the signal input to ADC0 and send it to the computer using
UART0 with the following transmission frame every 1 second. The 1-second delay can
be achieved using either a delay function or a timer.
0x55 ADCH ADCL 0xFF
d) Change the voltage applied to ADC0, measure it using a VOM, and compare it with the
sampled ADC result. Fill in the table in the report.
e) Connect an LCD to one of the AVR ports. Add to the existing program the functionality
to calculate the input voltage and display it on the LCD.
f) Change the reference voltage to the internal 2.56V reference. Repeat steps c, d, and e,
assuming the reference voltage is accurately set to 2.56V.
g) Measure the voltage on the VREF pin (header J57) using a VOM.
Source code:
https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC
.EQU ADC_PORT=PORTA
.EQU ADC_DR=DDRA
.EQU ADC_IN=PINA
.EQU LCD=PORTB ;PORTB data
.EQU LCD_IN=PINB
.EQU LCD_DR=DDRB
.EQU CONT=PORTB ;PORTB ?i?u khi?n
.EQU CONT_DR=DDRB
.EQU CONT_OUT=PORTB ;
.EQU CONT_IN=PINB ;
.EQU RS=0 ;bit RS
.EQU RW=1 ;bit RW
.EQU E=2 ;bit E
.EQU BCD_BUF=0X200 ;?/c ??u SRAM l?u s? BCD (kq chuy?n t? s? 16 bit)
.DEF OPD1_L=R24 ;byte th?p c?a s? nh? phân 16 bit
.DEF OPD1_H=R25 ;byte cao c?a s? nh? phân 16 bit
.DEF OPD2=R22
.DEF OPD3=R23
.DEF COUNT=R18
.ORG 0
RJMP MAIN
.ORG 0X40
MAIN:
LDI R16, HIGH(RAMEND)
OUT SPH, R16
LDI R16, LOW(RAMEND)
OUT SPL, R16
LDI R16, 0xFF ;PortD, C output
OUT DDRB, R16
OUT DDRC, R16
LDI R16, 0x00 ;PortA input
OUT ADC_DR, R16
OUT PORTD, R16 ;output=0x0000
OUT PORTB, R16
LDI R16, 0b01000000 ;Vref=AVcc=5V, SE ADC0
STS ADMUX, R16 ; x1,d?ch ph?i
LDI R16, 0b10000110 ;cho phép ADC,mode 1 l?n.
STS ADCSRA, R16 ;f(ADC)=fosc/64=125Khz
RCALL SETUART
LDI R16,0X07
https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC
https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC
RCALL HEX_ASC
MOV R17,R18
SBI CONT,RS
RCALL OUT_LCD4
LDI R17,44 ;xuat ','
SBI CONT,RS
RCALL OUT_LCD4
LDS R17,0X203 ; XUAT HANG CHUC
RCALL HEX_ASC
MOV R17,R18
SBI CONT,RS
RCALL OUT_LCD4
LDS R17,0X204 ; XUAT HANG DON VI
RCALL HEX_ASC
MOV R17,R18
SBI CONT,RS
RCALL OUT_LCD4
LDI R17,'V'
SBI CONT,RS
RCALL OUT_LCD4
RCALL DELAY1S
RJMP START ;ti?p t?c chuy?n ??i
SETUART:
LDI R16, (1<<TXEN0) ;cho phép phát
STS UCSR0B, R16
LDI R16, (1<<UCSZ01)|(1<<UCSZ00)
;8-bit data, không parity, 1 stop bit
STS UCSR0C, R16
LDI R16, 0x00
STS UBRR0H, R16
LDI R16, 51 ;9600 baud rate
STS UBRR0L, R16
RET
PHAT:
LDS R16,UCSR0A
SBRS R16,UDRE0 ; KIEM TRA CO TRONG KHONG
RJMP PHAT ; NEU CHUA TRONG THI TIEP TUC KIEM TRA LAI
STS UDR0,R17 ; KHI TRONG THI CHEP DU LIEU VAO UDR0
RET
DELAY1S:
LDI R16,200
LP_1: LDI R17,160
LP_2: LDI R18,50
LP_3: DEC R18
NOP
BRNE LP_3
DEC R17
BRNE LP_2
DEC R16
BRNE LP_1
RET
;HEX_ASC chuy?n t? mã Hex sang mã ASCII
;Input R17=mã Hex,Output R18=mã ASCII
;------------------------------------------
HEX_ASC:
CPI R17,0X0A
BRCS NUM
https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC
LDI R18,0X37
RJMP CHAR
NUM: LDI R18,0X30
CHAR: ADD R18,R17
RET
TACHKITU :
MOV R15,R17
LDI R16,0XF0
AND R17,R16 ; GIU LAI BIT CAO
SWAP R17
RCALL HEX_ASC
MOV R17,R18
RCALL PHAT
MOV R17,R15
ANDI R17,0X0F
RCALL HEX_ASC
MOV R17,R18
RCALL PHAT
RET
MUL_MATCH:
LDI R20,250
MUL R16,R20
MOV R10,R0
MOV R11,R1
MUL R17,R20
MOV R12,R0
MOV R13,R1
ADD R12,R11
CLR R0
ADC R13,R0 ;R13:R12:R10
RET
SHIFT_R:
LSR R12
BST R13,0
BLD R12,7
LSR R13
MOV R24,R12
MOV R25,R13
RET
;BIN16_BCD5DG chuy?n ??i s? nh? phân 16 bit sang s? BCD 5 digit
;Inputs: OPD1_H=R25:OPD1_L=R24 ch?a s? nh? phân 16 bit
;Outputs: BCD_BUF:BCD_BUF+4:??a ch? SRAM ch?a 5 digit BCD t? cao ??n th?p
;S? d?ng R17,COUNT,X,ctc DIV16_8
;---------------------------------------------------------
BIN16_BCD5DG:
LDI XH,HIGH(BCD_BUF);X tr? ??a ch? ??u buffer BCD
LDI XL,LOW(BCD_BUF)
LDI COUNT,5 ;??m s? byte b? nh?
LDI R17,0X00 ;n?p giá tr? 0
LOOP_CL:ST X+,R17 ;xóa buffer b? nh?
DEC COUNT ;??m ?? 5 byte
BRNE LOOP_CL
LDI OPD2,10 ;n?p s? chia (SC)
DIV_NXT:
RCALL DIV16_8 ;chia s? nh? phân 16 bit cho s? nh? phân 8 bit
ST -X,OPD3 ;c?t s? d? vào buffer
CPI OPD1_L,0 ;th??ng s?=0?
https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC
https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC
RCALL OUT_LCD
LDI R16,42 ;delay 4.2ms
RCALL DELAY_US
CBI CONT,RS
LDI R17,$30 ;mã l?nh=$30 l?n 2
RCALL OUT_LCD
LDI R16,2 ;delay 200?s
RCALL DELAY_US
CBI CONT,RS
LDI R17,$32 ;mã l?nh=$32
RCALL OUT_LCD4
RET
INIT_LCD4:
CBI CONT,RS ;RS=0 ghi l?nh
LDI R17,$24 ;Function set - giao ti?p 4 bit, 1 dòng, font 5x8
RCALL OUT_LCD4
CBI CONT,RS ;RS=0 ghi l?nh
LDI R17,$01 ;Clear display
RCALL OUT_LCD4
LDI R16,20 ;ch? 2ms sau l?nh Clear display
RCALL DELAY_US
CBI CONT,RS ;RS=0 ghi l?nh
LDI R17,$0C ;Display on/off control
RCALL OUT_LCD4
CBI CONT,RS ;RS=0 ghi l?nh
LDI R17,$06 ;Entry mode set
RCALL OUT_LCD4
RET
DELAY_US:
PUSH R15
PUSH R14
MOV R15,R16 ;1MC n?p data cho R15
LDI R16,200 ;1MC s? d?ng R16
L1:
MOV R14,R16 ;1MC n?p data cho R14
L2:
DEC R14 ;1MC
NOP ;1MC
BRNE L2 ;2/1MC
DEC R15 ;1MC
BRNE L1 ;2/1MC
POP R14
POP R15
RET ;4MC
DELAY: ;1s=32*250*250
LDI R25,32
LP3: LDI R26,250
LP2: LDI R27,250
LP1: NOP
DEC R27
BRNE LP1
DEC R26
BRNE LP2
DEC R25
BRNE LP3
RET
https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC
a) Connect both ADC1 and ADC0 to the voltage ADC_VR1. Adjust this voltage to 1V and
record the measured ADC value. This value represents the offset error.
b) From the offset error value, calculate the gain error using Table 4.
c) Rewrite the program from question c in exercise 3, using the calibrated ADC value.
https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:
Class group: Subject:
EXPERIMENT 1:
1. Answering the questions:
a. In Single Conversion mode, how do you start a sampling cycle and check
when it completes?
b. How do you select an ADC channel?
c. Does Atmega324 have an internal temperature measurement channel?
d. What is the formula to calculate the input voltage from the ADC when
VREF=VCCA?
e. What is the formula to calculate the input voltage from the ADC when
VREF=2.56V?
f. Record the ADC value compared to the measurement result from the
measuring instrument. Calculate the voltage value based on the ADC value
with VREF=VCCA.
VADC0 (V) 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
ADCH-ADCL 0.000 0.516 1.012 1.516 2.017 2.509 3.019 3.501 4 4.5 5
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:
Class group: Subject:
h. Repeat step g with VREF being the value measured from a VOM.
VREF = ...................................................................
VADC0 (V) 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
ADCH-ADCL
ADCH-ADCL
(Theoretical)
Error (LSB)
Bảng 3
2. Program source code with comments
EXPERIMENT 2:
1. Answering the questions:
a. What are the types of errors for an ADC?
b. What is offset error?
c. What is gain error?
d. What are the units of measurement for offset error and gain error?
e. Using the results from Table 1, taking two measurement points at Vin = 1
V and Vin = 4.5V, calculate the offset error and gain error.
2. Measurement and Calculation:
a. The measured ADC values corresponding to the input channels.
VADC0 (V) 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
ADCH-ADCL
(calibrated)
ADCH-ADCL
(Theoretical)
Error (LSB)
Bảng 4
b. Program source code with comments
....................................................................................................................
EXPERIMENT 3:
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:
Class group: Subject:
Values
Bảng 5
2. Program source code with comments
........................................................................................................................
EXPERIMENT 4:
1. Answering the questions:
a. How to measure offset error in differential mode?
b. Results obtained after calibration.chỉnh
VADC1 – VADC0 0 50 100 150 200 250 -50 -100 -150 -200 -250
(mV)
ADCH-ADCL
Values
https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:
Class group: Subject:
https://doe.dee.hcmut.edu.vn/