Electronic Engineering Laboratory IV BEJ40401 Instruction Sheet
Lab No. 5
Lab Title Analog-to-Digital and Digital-to-Analog Conversions in ARM Cortex-M3
Microcontroller (LPC1768)
Semester 2
Session 2024/25
Lab Durations 4 Hours (2 weeks)
Facult y of E lec tric and Elect roni c Engineering
Outcomes
After completing this module, student should be able to:
1. Configure, apply, and debug the ADC for the LPC1768 microcontroller in Keil MDK Vision.
2. Configure, apply, and debug the DAC for the LPC1768 microcontroller in Keil MDK Vision.
3. Organize time management in group effectively according to the given tasks.
4. Utilize the microprocessor/microcontroller user manual in order to use the specific peripheral.
Guidelines
1. Lab group: should consist of two to five team members (the number of team members will be decided by the instructor).
2. Pre-Lab: pre-lab questions should be answered before the lab session and should be attached to the lab report.
3. Lab Activities: all lab activities such as sample code, examples and lab assignments should be completed within the given times.
4. Demonstration: students should demonstrate the successful sample code, examples, and lab assignments to the respective instructor.
5. Report Organization: The report must be organized according to the given report template.
6. Report Submission: The report must be submitted no later than one week after the completion of the lab session.
Reminder: Any evidence of cheating, plagiarism, etc. on the lab report will automatically result in all students/groups involved receiving an average or zero mark.
Further evidence could result in further disciplinary action.
Lab5 Pre-Lab (3m)
1. The LPC1768 has how many ADC channels and what is the size of the ADC (in bits)?
(1 mark) 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 (2 marks)
Part 1: Analog-to-Digital Conversion
Analog signals can be repeatedly converted into digital representations, with a resolution and at a rate determined by the ADC. Following this analog-to-digital conversion, the microcontroller can be used to process or analyze this information, based on the value(s) of the analog input. An ADC is an electronic circuit whose digital output is proportional to its analog input. Effectively, it
‘measures’ the input voltage and gives a binary output number proportional to its size. Figure 5.1 shows a typical ADC unit in a microcontroller.
The LPC1768 consists of an 8-channel 12-bit Successive Approximation type Analog to Digital Converter. Here, 8-channels indicate that you can convert 8 different analog signals on 8 different Analog Input pins of the MCU at a time.
Figure 1.1: A typical ADC in a microcontroller
Since, the ADC in LPC1768 is having a resolution of 12-bits, it can produce 212 discrete values for a range of analog voltages, which is usually between 0V and 3.3V. This leads to a step width of 3.3/212, or approximately 0.8 mV, the worst-case quantization error is therefore 0.4 mV. The details of the ADC in the LPC1768 can be referred to the LPC1700 Series reference Manual and the datasheet which can be obtained from the official website of in Keil MDK. In Keil, go to “View >
Book Window” and you can browse to the manual.
Lab5 Activity 1: Setup the ADC in the LPC1768 microcontroller
There are two operation modes for the ADC in the LPC1768; One is the Software Controlled Mode and the other is Burst Mode (or Hardware Mode). In Software Controlled Mode, only one channel must be made active during a conversion and at a time only one conversion is possible. To convert again, you must repeat the process. Meanwhile, the Hardware Scan Mode or Burst Mode, the conversions will happen continuously on any number of channels starting from LSB (AD0.0) then progressing towards MSB (AD0.7). In this lab, we will focus on the Software-Controlled Mode.
1. The ADC is disabled on reset (power on) to minimize power consumption of the microcontroller. Therefore, to use the ADC, it needs to be enabled first by setting the 12th bit of the PCONP register as follow:
LPC_SC → PCONP |= (1<<12);
2. There is another power control for ADC in the ADCR register as by default, ADC is in Power-down mode, and this is controlled by the PDN (bit 21) of the ADCR Register. To make ADC operational, we must set the PDN bit as follow:
LPC_ADC → ADCR |= (1<<21);
3. By using the ADCR register, we can also select the ADC Channel and set the Clock Divider for ADC using SEL [7:0] and CLKDIV [15:8] bits.
LPC_ADC → ADCR |= (1<<0); //Select AD0.0 channel LPC_ADC → ADCR |= (1<<8); //Divide by 1
4. Next, you need to select the ADC input pin for the LPC1768, i.e., P0.23.
LPC_PINCON → PINSEL1 |= (1<<14)
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.
#include <lpc17xx.h>
void delay(void);
int main(void) { //Initialize ADC
LPC_SC -> PCONP |= (1<<12); //Enable the ADC Peripheral LPC_ADC -> ADCR |= (1<<0); //Select AD0.0 channel LPC_ADC -> ADCR |= (1<<21); //Power-on the ADC
LPC_ADC -> ADCR |= (1<<8); //Devide PCLK frequency by 2 LPC_PINCON -> PINSEL1 |= (1<<14) ; //select AD0.0 for P0.23
int result = 0;
while(1) {
LPC_ADC -> ADCR |= (1<<24); //Start the conversion while((LPC_ADC -> ADDR0 & (1u<<31)) == 0) {} //Wait for conversion to finish
result = ((LPC_ADC -> ADDR0 >> 4) & 0xFFF);
//Shift right by 4 and 12 bit Mask to extract result delay();
} }
void delay(void) //delay function {
unsigned int count,i=0;
for(count=0; count < 0xFFFF; count++) {
i++;
} }
6. Then, we wait for conversion to finish by observing the DONE bit (bit 31st) in the ADDR0 register by
while ((LPC_ADC → ADDR0 & (1<<31)) == 0);
7. After completion, the result is stored in the ADDR0 register in its RESULT [15:4] bits. We can extract the result by shifting the ADDR0 bits by 4 places and masking the 12 bits.
int result = ((LPC_ADC → ADDR0 >> 4) & 0xFFF);
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”.
9. Change the analog input value in AIN0 (value between 0 to 3.3V) and observe the conversion result in the A/D Channel 0 Data Register.
Listing 5.1: Simple ADC program
10. Modify the program to use channel 2 (AD0.2) of the ADC. Build and run the debug session to observe the results. (Observation Activity 1 - ADC)
Lab5 Assignment 1 (ADC)
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.
Figure 5.2: A simple temperature monitoring system
Part 2: Digital-to-Analog Conversion
A digital-to-analog converter (DAC) is a circuit that converts a binary input number into an analog output. A DAC has a digital input, represented by D, and an analog output, represented by VO. The DAC uses the Voltage Reference to calculate the VO. Figure 5.3 shows a DAC block diagram.
P2.2
Most DACs have a simple relationship between their digital input and analog output, with many (including the one inside the LPC1768) applying Equation.
V0 = D 2n Vr
Where n, D and Vr are size of digital value, digital value and reference voltage respectively. The LPC1768 has a 10-bit DAC; there will therefore be 210 steps in its output characteristic, i.e., 1024.
For Vr = 3.3V, the step size, or resolution, will therefore be 3.3/1024, i.e., 3.22 mV. The analog out pin in LPC1768 is at pin P0.26.
The DAC module in the LPC1768 has three registers which are the DACR, DACCTRL, DACCNTVAL registers. In this lab, we will go through DACR register since the other two are related with DMA operations.
The details of the DAC in the LPC1768 can be referred to the LPC1700 Series reference Manual and the datasheet which can be obtained from the official website of in Keil MDK. In Keil, go to
“View > Book Window” and you can browse to the manual.
Lab5 Activity 2: Setup the DAC in the LPC1768 microcontroller
11. The DAC is always connected to the VDDA, hence, no need to power it on. Register access is determined by PINSEL and PINMODE settings. To use AOUT pin (P0.26), set bit 21 of PINSEL1 register as follow:
LPC_PINCON → PINSEL1 |= (1<<21);
12. Write the desired digital value that will be converted to the analog value into the DACR register
LPC_DAC → DACR = (100<<6); //write digital value 100 to DACR.
13. Build the program in Listing 5.2 and debug the program. In the debug session, open the ADC debug window by selecting “Peripherals > D/A Converter”.
14. Observe the values in the DACR register (Observation Actvity 2 - DAC).
Listing 5.2: Simple DAC program
#include <lpc17xx.h>
void delay(void);
int main(void) {
LPC_PINCON -> PINSEL1 |= (1<<21); //Set pin P0.26 as AOUT unsigned int value;
while(1) {
value = 500;
LPC_DAC -> DACR = (value << 6);
delay();
value = 700;
LPC_DAC -> DACR = (value << 6);
delay();
} }
void delay(void) //delay function {
unsigned int count,i=0;
for(count=0; count < 0xFFFF; count++) {
i++;
} }
Lab5 Assignment 2 (DAC)
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).
Figure 5.4: Sawtooth waveform on Logic Analyzer Window
Lab5 Questions (5m)
An ideal 8-bit ADC has an input range of 5.12 V. What is its resolution, and what is its greatest quantization error?
(5 marks)