16
Electronic Engineering Laboratory IV BEE31101 Instruction Sheet Faculty of Electric and Electronic Engineering Lab No. 5 Lab Title Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM) Semester 02 Session 2019/20 Lab Durations 2 Hours Independent Studies 1 Hour

Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IVBEE31101

Instruction Sheet

Facu

lty

of E

lect

ric

and

Ele

ctro

nic

En

gin

eeri

ng

Lab No. 5Lab Title Analog to Digital Conversion (ADC)

and Pulse Width Modulation (PWM)

Semester 02Session 2019/20Lab Durations 2 HoursIndependent Studies 1 Hour

Page 2: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

ii

Table of Content

1.0

Outcomes 1

2.0 Guidlines 1

3.0 Pre-Lab 2

4.0 Procedures 3

Overview 3

5.0 Lab Activity 1 7

6.0 Lab Activity 2 8

7.0 Observations 7

8.0 Questions 8

9.0 References 8

FKEE, Sem02 Session 2019/20

Page 3: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

1

1.0 Outcomes

After completing this module, student should be able to:

(1) Students will be able to convert the analog voltage to the equivalent digital value and vice versa.

(2) Students will be able to write programs for reading analog input from analog sensor and process it using the microcontroller to control other devices.

(3) Students will be able to write programs to use PWM to control other devices.

2.0 Guidlines

1. Grouping: Lab group is not predetermine and consists with at most two team members.

2. Pre-Lab: Must be submitted to the instructor at the beginning of lab session. Verified by the

instructor and returned to the students at the end of lab session. The verified pre-lab will be

attached with the final report for submission.

3. Lab Activities: All lab activities such as sample code, examples and lab assignments must

be held in the respective lab location and completed within the given times.

4. Demonstration: Student must demonstrate the successful sample code, examples and lab

assignments to the respective instructor. Verification only will be given upon completion of all

lab activities and initialized by the instructor on the cover page.

5. Report Organization: Report must be organized according to given report template.

6. Report Submission: Report must be received by respective technical staff (at respective

lab) before 4.00pm; not later than three (3) days upon completion of lab session.

FKEE, Sem02 Session 2019/20

Page 4: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

2

3.0 Pre-Lab

1. 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) 500mV

(b) 2.5V

(c) 3.25V

(d) 1.4V

(4 marks)

2. For a DAC with 12-bit resolution, convert the following digital value to its equivalent

analog value if the reference voltage is 3.3V.

(a) 0x245

(b) 0xA12

(c) 0x123

(d) 0xFF0

(4 marks)

FKEE, Sem02 Session 2019/20

Page 5: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

3

4.0 Procedures

Analog signals can be repeatedly converted into digital representations, with a resolution and at a rate determined by the analog-to-digital converter (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.

The list of possible analog input signals is endless, including such diverse sources as audio and video, medical or climatic variables, and a host of industrially generated signals. Of these, some, such as temperature, have a very low frequency content.

By using the ADC, the digital output can be determined by

Where D is the digital output value, Vi is the input voltage, Vr is the reference voltage and n is the size of the ADC (in bit).

For example, if our ADC is 8-bit size, the reference voltage is given by 3.3V, the digital value for an analog voltage of 2V is therefore, b10011011.

Analog input with the mbed

The mbed LPC1678 has a built-in 12-bit ADC. This leads to a step width of 3.3/212, or 0.8 mV; the worst-case quantization error is therefore 0.4 mV. The “AnalogIn” API is used to read the analog input from the pin p15 to p20 as shown in Figure 1.

It is possible to read analog input data through the ADC, and then print the value to the PC screen. This can be done by using the “Serial” API. It is a very important step forward, as it gives the possibility of displaying any data, we are working with on the computer screen. To do this, we need to configure both mbed and host computer to be ready to send and receive data, and we need the host computer to be able to display that data.

For the computer a terminal emulator is required. The mbed site recommends use of Tera Term (download from the mbed website) for this purpose. The mbed can be made to appear to the computer as a serial port, communicating through the universal serial bus (USB) connection.

FKEE, Sem02 Session 2019/20

D=V iV r×2n

Page 6: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

4

Figure 1: Analog inputs of the mbed LPC1678 microcontroller

Example:

This example demonstrates the use of the mbed with analog temperature sensor (IC), LM35. Semiconductor action is highly dependent on temperature, so it is not surprising that semiconductor temperature sensors are made. A very useful form of sensor is one which is contained in an integrated circuit, such as the LM35. The LM35 and its symbol are shown in Figure 2.

Figure 2: LM35 Temperature sensor

This device has an output of 10 mV/C, with operating temperature from -55C to 150C. It is thus immediately useful for a range of temperature-sensing applications. The example shows 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. The application and its program flowchart are shown in Figure 3 and 4 respectively. For the LM35, each 10mV represent 1C. Therefore, 20C and 35C are equivalent to 0.2V and 0.35V respectively. We will use the AnalogIn function from mbed API to read the analog voltage applied to pin p20. Note that, the analog voltage in AnalogIn in normalized to the range from 0 to 1, hence we need to multiply the read value by 3.3 to get the actual voltage value. The program is given in Listing 1. Note that, we will use the variable resistor on the application board to replace the LM35.

FKEE, Sem02 Session 2019/20

Page 7: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

5

Figure 3: A simple ADC application

Figure 4: Program flowchart

Please download TERATERM mbed v 4.0 utk periferal communication.

Listing 1: Pr ogram for the flowchart in Figure 4FKEE, Sem02 Session 2019/20

Page 8: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

6

5.0 Lab Activity 1

Lab Assignment 1:

1. Modify the program in Listing 1 to display the temperature instead of voltage. Furthermore, modify the program such that the voltage and temperature are displayed on the LCD screen on the application board. Note: to display on the LCD screen, you need to use library "C12832.h". Example of using the "C12832.h" library is given in Listing 2 below.

FKEE, Sem02 Session 2019/20

#include "mbed.h"Serial pc(USBTX,USBRX); //used for printf to PC over USBAnalogIn Pot1(p20);DigitalOut OK(LED2);DigitalOut hot(LED1);DigitalOut cold(LED3);double voltage;int main() { OK=0; hot=0; cold=0; //turn off all LEDs while(1) { voltage = Pot1.read()*3.3; //read voltage printf("Voltage: %3.3f V\n\r", voltage); if (voltage > 0.35) //hot? {hot=1; cold=0; OK=0;} //if hot else if (voltage < 0.2) //cold? {hot=0; cold=1; OK=0;} //if cold else {hot=0; cold=0; OK=1;} //else OK wait(0.5); //delay }}

Page 9: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

7

.

Listing 2: Example of using LCD

Pulse Width Modulation (PWM)

PWM (Figure 5) represents a neat and remarkably simple way of getting a rectangular digital waveform to control an analog variable, usually voltage or current. Although we can create the PWM signal using software (just like we created the sawtooth signal, or using digitalOut), however, it will add load to the processor. In other words, when we create the PWM signal in software, the CPU will use all the CPU time for PWM, hence, it is difficult to use the same processor to do other job. Hence, most of the latest microcontroller has built-in PWM (hardware). The LPC1678 has six PWM outputs (p21 – p26). PWM control is used in a variety of applications, ranging from telecommunications to robotic control.

FKEE, Sem02 Session 2019/20

#include "mbed.h"Serial pc(USBTX,USBRX); //used for printf to PC over USBAnalogIn Pot1(p20);DigitalOut OK(LED2);DigitalOut hot(LED1);DigitalOut cold(LED3);double voltage;int main() { OK=0; hot=0; cold=0; //turn off all LEDs while(1) { voltage = Pot1.read()*3.3; //read voltage

temperature = voltage * 100; printf("Voltage: %3.3f V\n\r", temperature); if (voltage > 0.35) //hot? {hot=1; cold=0; OK=0;} //if hot else if (voltage < 0.2) //cold? {hot=0; cold=1; OK=0;} //if cold else {hot=0; cold=0; OK=1;} //else OK wait(0.5); //delay }}

#include "mbed.h"#include "C12832.h"

C12832 lcd(p5, p7, p6, p8, p11); //Define connection

int main() { lcd.cls(); lcd.locate(0,3); //Coordinate for the first character lcd.printf("Hello World!"); //Print to the LCD}

Page 10: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

8

Figure 5: PWM signal

The period is normally kept constant, and the pulse width, or ‘on’ time, is varied, hence the name. The duty cycle is the proportion of time that the pulse is ‘on’ or ‘high’, and is expressed as a percentage as follow

Note that on the LPC1768 microcontroller, and hence the mbed, the PWM sources all share the same period/frequency; if the period is changed for one, then it is changed for all. Example of using PWM in mbed is given in Listing 3.

Listing 3: Example of using PWM on the mbed microcontroller

Lab Assignment 2:

Write the program to use PWM to blink an LED on the mbed microcontroller. Set the ON/OFF time as follows, ON time is set to 2 s while OFF is set to 1 s.

Lab Assignment 3:

FKEE, Sem02 Session 2019/20

Duty cycle ¿ 'on' TimePeriod

×100 %

#include "mbed.h"PwmOut PWM1(p21); //create a PWM output called PWM1 on pin 21int main() {

PWM1.period(0.010); // set PWM period to 10 msPWM1=0.5; // set duty cycle to 50%

}

Page 11: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

9

Write the program to read the analog voltage from the variable resistor of the application board to vary the duration of ON time of an LED using PWM. The period of the PWM is 2 s.

7.0 Observations

1.0 Give a reason why we used below code to differentiate three differences temperature.

#include "mbed.h"Serial pc(USBTX,USBRX); //used for printf to PC over USBAnalogIn Pot1(p20);DigitalOut OK(LED2);DigitalOut hot(LED1);DigitalOut cold(LED3);double voltage;int main() { OK=0; hot=0; cold=0; //turn off all LEDs while(1) { voltage = Pot1.read()*3.3; //read voltage

temperature = voltage * 100; printf("Voltage: %3.3f V\n\r", temperature); if (voltage > 0.35) //hot? {hot=1; cold=0; OK=0;} //if hot else if (voltage < 0.2) //cold? {hot=0; cold=1; OK=0;} //if cold else {hot=0; cold=0; OK=1;} //else OK wait(0.5); //delay }}

(10 marks)

FKEE, Sem02 Session 2019/20

Page 12: Lab 2: Analog to Digital Conversion (ADC) and Pulse Width ...fkee.uthm.edu.my/.../3/BEE31101/BEE31101_Lab5_Labsh…  · Web viewLab X Lab Title. Lab 2: Analog to Digital Conversion

Electronic Engineering Laboratory IV (BEE31101)Lab 2: Analog to Digital Conversion (ADC) and Pulse Width Modulation (PWM)

10

8.0 Questions

1. Write a program that creates a square wave output. There are two possible frequencies, 100

Hz and 200 Hz, depending on an input switch position. i.e. if input is = 0, the frequency is

100 Hz, otherwise, frequency is 200 Hz.

(10 marks)

FKEE, Sem02 Session 2019/20