0% found this document useful (0 votes)
24 views13 pages

Lab5 - 1 Eng

This document describes an experiment using the analog-to-digital converter (ADC) of an AVR microcontroller. The objectives are to understand and use the ADC for measurement. The experiment involves measuring single-ended signals by connecting inputs to the ADC, selecting a reference voltage, sampling the signals and sending the results to a computer and LCD display. Code is provided to initialize the ADC, sample inputs, convert results to voltages, and display on LCD in decimal and hexadecimal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

Lab5 - 1 Eng

This document describes an experiment using the analog-to-digital converter (ADC) of an AVR microcontroller. The objectives are to understand and use the ADC for measurement. The experiment involves measuring single-ended signals by connecting inputs to the ADC, selecting a reference voltage, sampling the signals and sending the results to a computer and LCD display. Code is provided to initialize the ADC, sample inputs, convert results to voltages, and display on LCD in decimal and hexadecimal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

LAB 5-1

PROGRAMMING USING ADC

OBJECTIVE:

 Understand and use the ADC of AVR.


 Understand how to use the ADC for measurement.

REFERENCES:

 Experiment Experiment guide, chapters 11


 AN2538-ADC-of-megaAVR-in-SingleEnded-Mode-00002538A.pdf
 AVR120: Characterization and Calibration of the ADC on an AVR

EXPERIMENT 1: MEASURING SINGLE-ENDED SIGNALS

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

LDI R16, 0b11000000 ;internal 2.56V

LDI R16, 0b01000000 ;Vref=AVcc=5V, SE ADC0

.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

OUT CONT_DR,R16 ;khai báo PB0,PB1,PB2 là output


CBI CONT,RS ;RS=PB0=0
CBI CONT,RW ;RW=PB1=0 truy xu?t ghi
CBI CONT,E ;E=PB2=0 c?m LCD
LDI R16,0XFF
OUT LCD_DR,R16 ;khai báo outport
RCALL RESET_LCD ;ctc reset LCD
RCALL INIT_LCD4 ;ctc kh?i ??ng LCD 4 bit
START:
LDS R16, ADCSRA
ORI R16, (1<<ADSC) ;b?t ??u chuy?n ??i
STS ADCSRA, R16
WAIT:
LDS R16, ADCSRA ;??c c? ADIF
SBRS R16, ADIF ;c? ADIF=1 chuy?n ??i xong
RJMP WAIT ;ch? c? ADIF=1
STS ADCSRA, R16 ;xóa c? ADIF
LDS R1, ADCL ;??c byte th?p ADC
LDS R0, ADCH ;??c byte cao ADC
LDI R17,'A'
RCALL PHAT
LDI R17,'D'
RCALL PHAT
LDI R17,'C'
RCALL PHAT
LDI R17,':'
RCALL PHAT
MOV R17,R0
RCALL TACHKITU
MOV R17,R1
RCALL TACHKITU
LDI R17,' '
RCALL PHAT
LDS R1, ADCL ;??c byte th?p ADC
LDS R0, ADCH ;??c byte cao ADC
MOV R17,R0
MOV R16,R1
RCALL MUL_MATCH
RCALL SHIFT_R
RCALL BIN16_BCD5DG
XUAT_LCD:
CBI CONT,RS ;RS=0 ghi lenh
LDI R17,$84 ;con tr? b?t ??u ? dòng 1 v? trí th? 1
RCALL OUT_LCD4
LDI R17,'A'
SBI CONT,RS
RCALL OUT_LCD4
LDI R17,'D'
SBI CONT,RS
RCALL OUT_LCD4
SBI CONT,RS
LDI R17,'C'
SBI CONT,RS
RCALL OUT_LCD4
LDI R17,':'
RCALL OUT_LCD4
LDS R17,0X202 ; XUAT HANG TRAM

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

BRNE DIV_NXT ;khác 0 chia ti?p


RET
;---------------------------------------
;DIV16_8 chia s? nh? phân 16 bit OPD1 cho 8 bit OPD2 (Xem gi?i thu?t chia ? Ch??ng 0)
;Input: OPD1_H,OPD1_L= SBC(GPR16-31)
; OPD2=SC(GPR0-31)
;Output:OPD1_H,OPD1_L=th??ng s?
; OPD3=DS(GPR0-31)
;S? d?ng COUNT(GPR16-31)
;---------------------------------------
DIV16_8: LDI COUNT,16 ;COUNT=??m 16
CLR OPD3 ;xóa d? s?
SH_NXT: CLC ;C=0=bit th??ng s?
LSL OPD1_L ;d?ch trái SBC L,bit0=C=th??ng s?
ROL OPD1_H ;quay trái SBC H,C=bit7
ROL OPD3 ;d?ch bit7 SBC H vào d? s?
BRCS OV_C ;tràn bit C=1,chia ???c
SUB OPD3,OPD2 ;tr? d? s? v?i s? chia
BRCC GT_TH ;C=0 chia ???c
ADD OPD3,OPD2 ;C=1 không chia ???c,không tr?
RJMP NEXT
OV_C: SUB OPD3,OPD2 ;tr? d? s? v?i s? chia
GT_TH: SBR OPD1_L,1 ;chia ???c,th??ng s?=1
NEXT: DEC COUNT ;??m s? l?n d?ch SBC
BRNE SH_NXT ;ch?a ?? ti?p t?c d?ch bit
RET
OUT_LCD4:
LDI R16,1
RCALL DELAY_US
IN R16,CONT
ANDI R16,(1<<RS)
PUSH R16
PUSH R17
ANDI R17,$F0
OR R17,R16
RCALL OUT_LCD
LDI R16,1
RCALL DELAY_US
POP R17
POP R16
SWAP R17
ANDI R17,$F0
OR R17,R16
RCALL OUT_LCD
RET
OUT_LCD:
OUT LCD,R17 ;1MC,ghi l?nh/data ra LCD
SBI CONT,E ;2MC,xu?t xung cho phép LCD
CBI CONT,E ;2MC,PWEH=2MC=250ns,tDSW=3MC=375ns
RET
RESET_LCD:
LDI R16,250 ;delay 25ms
RCALL DELAY_US ;ctc delay 100?sxR16
LDI R16,250 ;delay 25ms
RCALL DELAY_US ;ctc delay 100?sxR16
CBI CONT,RS ;RS=0 ghi l?nh
LDI R17,$30 ;mã l?nh=$30 l?n 1

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

EXPERIMENT 2: MEASURING OFFSET ERROR AND GAIN ERROR

a) Calculate the offset error and gain error of the ADC.


b) Rewrite the program with the requirements mentioned in question e of exercise 1, using
the calibrated ADC. Vref = VCCA. Send the calibrated ADC values to the computer and
display the measured voltage values on the LCD.
(Note: Please refer to the documentation "AVR120: Characterization and Calibration of
the ADC on an AVR" for detailed instructions and guidelines on the calibration process.
.include "m328pdef.inc"

EXPERIMENT 3: MEASURING ADC IN BIAS MODE

a) Calibrate channel VR1 to a voltage level of 2.5V and connect it to ADC0.


b) Write a program to initialize the ADC in bias mode with two input channels, ADC0 and
ADC1, amplification factor of 10, and a reference voltage of 2.56V. Start the ADC in
FreeRunning mode.
c) Write a program to display the voltage value of VR1 on the LCD and send the ADC
measurement results to the computer every 1 second, as described in experiment 1.

EXPERIMENT 4:MEASURING OFFSET ERROR AND GAIN ERROR IN BIAS


MODE

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.

EXPERIMENT 5: MEASURING TEMPERATURE USING MCP9701

a) Connect the sensor to header J73.

https://doe.dee.hcmut.edu.vn/
LAB 5-1
PROGRAMMING USING ADC

b) Connect the voltage signal V_TEMP on header J18 to ADC0.


c) Write a program to measure the voltage value of V_TEMP with the calibration
parameters as in experiment 1, calculate the temperature value, and display it on the
LCD.

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

ADCH-ADCL 0.000 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5


(Theoretical)
Error (LSB) 0 0.016 0.012 0.016 0.017 0.009 0.019 0.001 0 0 0
Bảng 1
g. Repeat step f with VREF=2.56V. Compare the calculation formula and the error
with step e.
VADC0 (V) 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
ADCH- 0.000 0.261 0.512 0.770 1.023 1.274 1.530 1.783 2.038 2.291 2.743
ADCL
ADCH- 0.000
ADCL
(Theoretical)
Error (LSB)
Bảng 2

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:

1. Answering the questions:


a. In differential mode, what is the voltage range within which the input
voltages of the ADC oscillate?
b. When selecting the signal pair ADC0, ADC1 with a gain of 10,
VREF=2.56V, and VADC0 = 2.5V, what is the measurement range for ADC1?
c. Fill in the measured values and calculate the input voltage based on the
theoretical information.
VADC1 – VADC0 0 50 100 150 200 250 -50 -100 -150 -200 -250
(mV)
ADCH-ADCL

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

2. Program source code with comments


........................................................................................................................
EXPERIMENT 5:
1. Answering the questions:
a. According to the datasheet, what is the thermal coefficient of the MCP9701
sensor ?
b. If VREF = VCCA is used, what is the measurement range of the circuit? What is
the resolution of the measurement circuit in degrees Celsius ?
c. If VREF = 2.56V is used, what is the measurement range of the circuit? What is
the resolution of the measurement circuit in degrees Celsius ?

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

d. If we use the ADC in differential mode, input a voltage of 400 + 19.53*20


(mV) to ADC0, and connect the output of MCP9751 to ADC1, with a gain of
10 and VREF=2.56V, what is the measurement range and resolution of the
measurement circuit ?
2. Program source code with comments
........................................................................................................................

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

You might also like