G2 - Lab 5 - Report
G2 - Lab 5 - Report
About Students:
Name Matric No. Section:
1. Aina Athirah binti Johari AE180034 Instructor’s Name:
2. Mohd Nawawi bin Ayup Jehmi @ Ayub CE180112 Dr. Chessda Uttraphan A/l Eh
3. Nick Cassidy Anak Merunei CE180169 Khan
4. Nurul Izyan Ainaa binti Jumari AE180084
About Experiment:
Title Submission date
Lab 5: Analog-to-Digital and Digital-to-Analog Conversions in ARM 1 - 1 - 2022
Cortex-M3 Microcontroller (LPC1768)
/100
1
Grading Rubric for Written Report (to be filled in by the instructor)
Scale
Criteria
5 4 3 2 1 Marks
Lab Activities (C) It Varies, depending on the activity assigned
Observations (C) It Varies, depending on the question assigned
Conclusion (P) Able to write a conclusion Able to write a conclusion Able to write a conclusion Able to write a conclusion Not able to write a
with excellent clarity, clearly and understandably. clearly but require minor with limited clarity and conclusion. /5
understandability, and improvements. require further
focus. improvements.
Demonstration (A) Very clear evidence of Able to demonstrate Able to demonstrate Able to demonstrate Demonstration without
knowledge and knowledge and knowledge and knowledge and understanding the
understanding understanding in practice understanding in practice understanding in practice but principle; cannot relate
and require minor require improvements.
demonstrated in practice. well. the concept to the scale * 2 = / 10
improvements.
demonstration.
Ethics (A) Solution ideas are written Solution ideas are written Little solution ideas are Some of the solution ideas Most of the solution
using their original using their original adopted from others, written are copied from others, ideas are copied from
language, written with clear language, written with with understandability. without fully understand the others, without fully
knowledge, knowledge and concept. understand the concept. /5
understandability, and understandability.
focus.
The report is submitted The report is submitted a The report is submitted 2 The report is submitted 3 The report is submitted
/ 10
before or on the dateline. day after the dateline. days after the dateline. days after the dateline. more than 3 days after /5
the dateline.
2
Table of Contents
Table of Contents 3
Pre-Lab Answers 4
Assignment 1 13
Assignment 2 18
Questions 20
Conclusion 21
3
Pre-Lab Answers
1. The LPC1768 has how many ADC channels and what is the size of the ADC (in bits)?
Answers:
LPC1768 has 8 channel and the size is 12-bit for Analog Digital Converter (ADC)
2. For an ADC with 10-bit resolution, convert the following analog voltage to its equivalent digital
value if the reference voltage is 3.3V.
(a) 400mV (b) 2.5V (c) 3.0V
Answers:
𝑉𝑖
𝐷= 𝑉𝑟 𝑥 2𝑛
Vi Input Voltage
Vr Reference Voltage
so
a) 400mV
400𝑚
𝐷= 3.3
𝑥 210 = 124
= b0001111100
b) 2.5V
2.5
𝐷= 3.3 𝑥 210 = 776
= b1100001000
c) 3.0V
3
𝐷= 3.3 𝑥 210 = 931
= b1110100011
4
Answers to Lab Activities
Lab Activity 1
Setup the ADC in the LPC1768 microcontroller
LPC_SC → PCONP |=
(1<<12);
LPC_ADC → ADCR |=
(1<<21);
LPC_ADC → ADCR |=
(1<<0); //Select AD0.0
channel
LPC_ADC → ADCR |=
(1<<8); //Divide by 1
LPC_PINCON → PINSEL1
|= (1<<14)
5
5. Now, we can start the
conversion immediately by
setting START [26:24] bits in
ADCR as 001 i.e., the 24th
bit is set to 1.
LPC_ADC → ADCR |=
(1<<24);
6
8. Build the program in Listing
5.1 and debug the program.
In the debug session, open
the ADC debug window by
selecting “Peripherals > A/D
Converter”.
7
1
𝐷= 3.3 𝑥 212 =1241 @ 04D9 Hex
RESULT 2 : VALUE 2V
8
AIN2 = 3.0000 with ADDR2 = 0x0000E8B0 and RESULT2 = 0x0E8B
9
Lab Activity 2
Setup the DAC in the LPC1768 microcontroller
LPC_PINCON → PINSEL1
|= (1<<21);
LPC_DAC → DACR =
(100<<6); //write digital
value 100 to DACR.
10
D/A Converter window:-
The VALUE under D/A control shows the value of 500 in hexadecimal,
0x01F4 and its analog output, AOUT=1.6113. Proving by manual
calculation,
𝐷
𝑉𝑜 = 𝑛 𝑉𝑟
2
500
𝑉𝑜 = 10 (3.3) = 1.6113 𝑉
2
Digital value = 700:-
11
Value in DACR register = 0x0000AF00
The VALUE under D/A control shows the value of 700 in hexadecimal,
0x02BC and its analog output, AOUT=2.2559. Proving by manual
calculation,
𝐷
𝑉𝑜 = 𝑛 𝑉𝑟
2
700
𝑉𝑜 = 10 (3.3) = 2.2559 𝑉
2
12
Assignment 1
Figure 5.2 shows a simple temperature monitoring system by using the LM35 analog temperature
sensor. Write the program to turn on the “hot” LED when the temperature exceed 35C, turn on the
“cold” LED when the temperature is below 20C and if the temperature is within 20C to 35C, turn on
the “OK” LED. You need to use the LM35 datasheet to determine the output values of the LM35
relative to the temperature.
Answer:
In this case, we assume the value of temperature equal to the value of voltage,
1mv = 1 °C, so
35 °C = 0.35 V
20 °C = 0.2 V
Condition:-
1) if the input value condition is greater than 0.35V in analog or in digital equal to
434, the output of P2.1 will active which is ’hot’ LED will Turn On.
2) If the input value condition is lower than 0.2V in analog or in digital equal to 248,
the output of P2.2 will active which is ‘cold’ LED will Turn On.
3) if the input value condition is between 0.2V and 0.35V, the output of P2.0 will
active which is ‘ok’ LED will Turn On.
C Programming Code
13
#include<lpc17xx.h>
void delay(void);
int main(void)
{
LPC_GPIO2->FIODIR0 = 0x07; // Configure Port2, bit 0,1,2 as output
LPC_SC -> PCONP |= (1<<12); //Enable the ADC Peripheral
LPC_ADC -> ADCR = (1<<1); //Select AD0.1 channel
LPC_ADC -> ADCR |= (1<<21); //Power-on the ADC
LPC_ADC -> ADCR |= (1<<8); //Devide PCLK frequency by 2
LPC_PINCON -> PINSEL1 |= (1<<16) ; //select AD0.1 for P0.24
double result = 0;
while(1)
{
LPC_ADC -> ADCR |= (1<<24); //Start the conversion
while((LPC_ADC -> ADDR1 & (1u<<31)) == 0)
{} //Wait for conversion to finish
result = ((LPC_ADC -> ADDR1 >> 4) & 0xFFF);
//Shift right by 4 and 12 bit Mask to extract result
delay();
Result
Condition 1:
14
Condition 2:
15
Condition 3:
16
17
Assignment 2
Write a program to create a sawtooth waveform as shown in Figure 5.4. To view the waveform on a
Logic Analyzer, go to “View > Analysis Windows > Logic Analyzer”. In the Logic Analyzer window,
click “Setup”, then click “New (Insert)” tab. Add signal name as “Aout” (which is the Aout pin).
Answer:
As showns in the Figure 5.4, the step of Sawtooth equal to 20 step while 3V as upper limit and 0V
as lower limit.
Vaout for each steps equal to 0.15V where 3V divide 20 total steps = 0.15.
0.15V will be incremental value, 0.15V need to convert to digital value since in DACR will enter the
value of digital, so 0.15V = 47
3V as upper limit also need to convert to digital value = 931
C Programming Code
#include <lpc17xx.h>
void delay(void);
int main(void)
{
LPC_PINCON -> PINSEL1 |= (1<<21); //Set pin P0.26 as AOUT
unsigned int i;
while(1)
{
for (i=0;i<931;i=i+47){ // 931 = 3V and 47 = 0.15
LPC_DAC -> DACR = (i<<6);
delay();
}
}
}
void delay(void) //delay function
{
unsigned int count,k=0;
for(count=0; count < 0xFFFF; count++)
{
k++;
}
}
18
Result
19
Questions
1. An ideal 8-bit ADC has an input range of 5.12 V. What is its resolution, and what is its greatest
quantization error?
Answer:
5.12
𝑅𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 = = 20 𝑚𝑉
28
Greatest quantization error = 10 𝑚𝑉
2. Draw a program flowchart which describes a program that continuously reads an analog
temperature sensor output once per second and displays the temperature in degrees Celsius
on a three-digit seven-segment display.
Answer:
20
Conclusion
For ADC:
Analog signal is transformed into digital representation by measuring the input voltage and produces
binary output number, with resolution and at a rate determined by ADC. This conversion can be
calculate manually by,
𝑉𝑖
𝐷= 𝑥 2𝑛
𝑉𝑟
where D = digital value
𝑉𝑖 = input voltage (analog)
𝑉𝑟 = voltage reference (in this lab = 3.3 V)
n = ADC resolution bit (for LPC1768, n = 12)
In ADC peripheral (Keil MDK Vision), the input voltage can be vary (0 to 3.3 V) at the Analog Input
and the result can be observed in A/D Channel Data. In this lab, only one channel must be made
active during conversion and only one conversion can be done at one time.
For DAC:
Binary input value turned into analog output. Formula used is,
𝐷
𝑉𝑜 = 𝑉
2𝑛 𝑟
where 𝑉𝑖 = input voltage (analog)
D = digital value
𝑉𝑟 = voltage reference (in this lab = 3.3 V)
n = DAC bit (for LPC1768, n = 10)
In Keil MDK Vision, the digital input can be vary at the coding file and the result can be observed at
Analog Output in DAC peripheral. The DACR used also shown with VALUE, that shows binary value
that want to be converted.
In this lab, the LPC1700 Series reference Manual and the datasheet for LPC1768 is used. For ADC, it
is important to know which ADC channel is used with its suitable ADC input pin like PINSEL1. Same
goes for DAC.
21