Interfacing to Analog World Sensor Interfacing

Similar documents
MICROPROCESSORS A (17.383) Fall Lecture Outline

The University of Texas at Arlington Lecture 10 ADC and DAC

Embedded Systems. Interfacing PIC with external devices Analog to digital Converter. Eng. Anis Nazer Second Semester

PIC Analog Voltage to PWM Duty Cycle

Microprocessors A Lab 4 Fall Analog to Digital Conversion Using the PIC16F684 Microcontroller

PIC ADC to PWM and Mosfet Low-Side Driver

Building an Analog Communications System

Section 22. Basic 8-bit A/D Converter

Chapter 5: Signal conversion

Microcomputers. Digital Signal Processing

Music 270a: Fundamentals of Digital Audio and Discrete-Time Signals

EXPERIMENT 6: Advanced I/O Programming

Lab 10. Speed Control of a D.C. motor

EXERCISE 4: A Simple Hi-Fi

Continuous vs. Discrete signals. Sampling. Analog to Digital Conversion. CMPT 368: Lecture 4 Fundamentals of Digital Audio, Discrete-Time Signals

PIC Functionality. General I/O Dedicated Interrupt Change State Interrupt Input Capture Output Compare PWM ADC RS232

ME 461 Laboratory #3 Analog-to-Digital Conversion

Lab 12 Laboratory 12 Data Acquisition Required Special Equipment: 12.1 Objectives 12.2 Introduction 12.3 A/D basics

Digitizing Color. Place Value in a Decimal Number. Place Value in a Binary Number. Chapter 11: Light, Sound, Magic: Representing Multimedia Digitally

Designing with a Microcontroller (v6)

CMPT 318: Lecture 4 Fundamentals of Digital Audio, Discrete-Time Signals

Cyber-Physical Systems ADC / DAC

GCE A level 1145/01 ELECTRONICS ET5

Digital Sampling. This Lecture. Engr325 Instrumentation. Dr Curtis Nelson. Digital sampling Sample rate. Bit depth. Other terms. Types of conversion.

5/17/2009. Digitizing Color. Place Value in a Binary Number. Place Value in a Decimal Number. Place Value in a Binary Number

Analogue Signals. M J Brockway. February 5, 2018

ECE 556 BASICS OF DIGITAL SPEECH PROCESSING. Assıst.Prof.Dr. Selma ÖZAYDIN Spring Term-2017 Lecture 2

A-D and D-A Converters

Analog to Digital Conversion

GCE A level 1145/01 ELECTRONICS ET5. P.M. THURSDAY, 31 May hours. Centre Number. Candidate Number. Surname. Other Names

In this lecture. System Model Power Penalty Analog transmission Digital transmission

Chapter 8. Representing Multimedia Digitally

Module 13: Interfacing ADC. Introduction ADC Programming DAC Programming Sensor Interfacing

CHAPTER 4. PULSE MODULATION Part 2

Three-Stage Coil Gun

Based with permission on lectures by John Getty Laboratory Electronics II (PHSX262) Spring 2011 Lecture 9 Page 1

P a g e 1. Introduction

EE 109 Midterm Review

Embedded Hardware Design Lab4

Section bit A/D Converter

A NEW DECISION ALGORITHM FOR AUDIO VOTING SYSTEM

Follow this and additional works at: Part of the Engineering Commons

ADC Resolution: Myth and Reality

MicroToys Guide: Motors A. Danowitz, A. Adibi December A rotary shaft encoder is an electromechanical device that can be used to

PC-based controller for Mechatronics System

Pololu TReX Jr Firmware Version 1.2: Configuration Parameter Documentation

GCE A level 1145/01 ELECTRONICS ET5

Laboratory 11. Pulse-Width-Modulation Motor Speed Control with a PIC

Digital Signal Detector Interface IC PS202

2: Audio Basics. Audio Basics. Mark Handley

ELCT 912: Advanced Embedded Systems

UNIT III Data Acquisition & Microcontroller System. Mr. Manoj Rajale

CS101 Lecture 18: Audio Encoding. What You ll Learn Today

EEE3410 Microcontroller Applications Department of Electrical Engineering. Lecture 10. Analogue Interfacing. Vocational Training Council, Hong Kong.

SIGMA-DELTA CONVERTER

APPLICATION NOTE. Atmel AVR127: Understanding ADC Parameters. Atmel 8-bit Microcontroller. Features. Introduction

II Year (04 Semester) EE6403 Discrete Time Systems and Signal Processing

Introduction. These two operations are performed by data converters : Analogue-to-digital converter (ADC) Digital-to-analogue converter (DAC)

Analog to Digital Conversion

In this lecture, we will look at how different electronic modules communicate with each other. We will consider the following topics:

ANALOGUE AND DIGITAL COMMUNICATION

Microprocessors & Interfacing

CSCI1600 Lab 4: Sound

DELTA MODULATION. PREPARATION principle of operation slope overload and granularity...124

IST TSic Temperature Sensor IC. Technical Notes ZACwire Digital Output

P89LPC935 ADC/DAC Tutorial

Hashemite University Faculty of Engineering Mechatronics Engineering Department. Microprocessors and Microcontrollers Laboratory

3 Design Lab III: An Electronic Governor for Electric Motor Speed Control

UNIT TEST I Digital Communication

Digital Design Laboratory Lecture 7. A/D and D/A

EE 210 Lab Exercise #4 D/A & A/D Converters

CprE 288 Introduction to Embedded Systems (Output Compare and PWM) Instructors: Dr. Phillip Jones

Sound/Audio. Slides courtesy of Tay Vaughan Making Multimedia Work

Debugging a Boundary-Scan I 2 C Script Test with the BusPro - I and I2C Exerciser Software: A Case Study

Lecture 7: Analog Signals and Conversion

Chapter 2: Digitization of Sound

Advantages of Analog Representation. Varies continuously, like the property being measured. Represents continuous values. See Figure 12.

Total Hours Registration through Website or for further details please visit (Refer Upcoming Events Section)

Measuring Distance Using Sound

Digital Acquisition of Analog Signals A Practical Guide

Data Converters. Dr.Trushit Upadhyaya EC Department, CSPIT, CHARUSAT

Chapter 7. Introduction. Analog Signal and Discrete Time Series. Sampling, Digital Devices, and Data Acquisition

UNIVERSITY OF NORTH CAROLINA AT CHARLOTTE Department of Electrical and Computer Engineering

Counter/Timers in the Mega8

FRIDAY, 18 MAY 1.00 PM 4.00 PM. Where appropriate, you may use sketches to illustrate your answer.

Triple Stage Incubator

ES /2 DMM. Features. General Description. Absolute Maximum Ratings

University of California at Berkeley Donald A. Glaser Physics 111A Instrumentation Laboratory

ML PCM Codec Filter Mono Circuit

Analog I/O. ECE 153B Sensor & Peripheral Interface Design Winter 2016

Digital-to-Analog Converter. Lab 3 Final Report

Linear Integrated Circuits

Analog Input and Output. Lecturer: Sri Parameswaran Notes by: Annie Guo

Microcontroller: Timers, ADC

Ch 5 Hardware Components for Automation

High Efficiency AC Input 12A 12V Laser Driver

DSP Project. Reminder: Project proposal is due Friday, October 19, 2012 by 5pm in my office (Small 239).

The counterpart to a DAC is the ADC, which is generally a more complicated circuit. One of the most popular ADC circuit is the successive

dspic Analogue to Digital Converter

AN3137 Application note

Transcription:

Interfacing to Analog World Sensor Interfacing Introduction to Analog to digital Conversion Why Analog to Digital? Basics of A/D Conversion. A/D converter inside PIC16F887 Related Problems Prepared By- Mohammed Abdul Kader Assistant Professor, EEE, IIUC

Introduction to Analog to digital Conversion Signals in the real world are analog: light, sound, temperature, pressure, acceleration or other phenomenon. So, real-world signals must be converted into digital, using a circuit called ADC (Analog-to-Digital Converter), before they can be manipulated by digital equipment. When you scan a picture with a scanner what the scanner is doing is an analog-to-digital conversion: it is taking the analog information provided by the picture (light) and converting into digital. When you record your voice on your computer, you are using an analog-to-digital converter to convert your voice, which is analog, into digital information. When an audio CD is recorded at a studio, once again analog-to-digital is taking place, converting sounds into digital numbers that will be stored on the disc. Whenever we need the analog signal back, the opposite conversion digital-to-analog, which is done by a circuit called DAC, Digital-to-Analog Converter is needed. When you play an audio CD, what the CD player is doing is reading digital information stored on the disc and converting it back to analog so you can hear the audio. 2

Why Analog to Digital? 1. Reducing Noise: Since analog signals can assume any value, noise is interpreted as being part of the original signal. Digital systems, on the other hand, can only understand two numbers, zero and one. Anything different from this is discarded. Noise is added with Signal Information is unchanged 3

Why Analog to Digital? (Continued) 2. Signal Processing: The most strong tool of processing or analyzing signal is microprocessor. Microprocessor is a digital device. It can understand only digital signal. So, we should convert the analog physical parameters into digital to process it by microprocessor. 3. Data compression capability: Another advantage of digital system against analog is the data compression capability. Since the digital counterpart of an analog signal is just a bunch of numbers, these numbers can be compressed, just like you would compress a Word file using WinZip to shrink down the file size, for example. The compression can be done to save storage space or bandwidth. 4

ADC (A/D Converter) in Embedded System The main use of ADC in embedded system is to measure the voltage outputs of sensors. Most electronic sensors produce a voltage that corresponds to temperature, pressure, acceleration or other phenomenon. Music, speech, or other signals can be converted to digital form by A/D converters for storage or additional processing. Gives a decision (output) according to user instruction Physical parameter Sensor A/D Converter Microprocessor Converts physical parameter to corresponding analog voltage Converts analog voltage to digital voltage Analyze or process the physical parameter 5

Related terms of A/D conversion Resolution: The resolution of an A/D converter is specified by number of bits and determines how many distinct output levels or codes (2^N) the converter is capable of producing. For example, an 8-bit A/D converter produces 2^8, or 256, output codes or levels. Mathematically the resolution for an A/D converter is Here, Reference means the conversion range and N is the no of bit in digital output. In other Words, Resolution is the smallest change or steps in the analog voltage for which output digital voltage (codes) remain unchanged. For an 3 bit A/D converter with conversion range (0-5V): Resolution= 5/2^3 = 5/ 8= 0.625 V For an 8 bit A/D converter with conversion range (0-5V): Resolution= 5/2^8 = 5/ 256 = 0.0196 V For an 10 bit A/D converter with conversion range 0-5V: Resolution = 5/2^10 = 5/1024 = 0.00488 V 6

Related terms of A/D conversion (Cont.) Resolution of an 2-bit A/D converter is 1.25V Resolution of an 10-bit A/D converter is 0.00488V Another definition,the resolution of the A/D converter is the voltage change that will result in the returned value (output digital value) changing by 1. This means that with an 3 bit A/D the returned value will change by 1 when the voltage changes by about 1.25V. With a 10 bit A/D, a change of only 4.88 mv will change the count by 1. 7

Related terms of A/D conversion (Cont.) Clearly an A/D converter with more bits will give better accuracy. But, for a higher bit A/D converter you need more storage memory to store the result and at the same time A/D converter circuit become more complex and conversion time will increase. Problem 1: A weight sensor changes its output voltage 0.1 mv for 1gm change in input and it s output varies from 0-5V. What should be the resolution and no of bits in the A/D converter if you want to get the weight in gram. The resolution should be 0.1mV = 0.0001 V 8

Related terms of A/D conversion (Cont.) Quantization Error : If an AC signal is applied to an ideal A/D converter, noise present in the digitized output due to quantization error. If the input to the quantizer is moved through its full range and subtracted from the discrete output levels the error signal will be a sawtooth waveform with a peak-to-peak value Q as shown in figure (b). The quantization error is dependent on the number of quantization level (discrete levels). The quantization levels are an index of the resolution. The output of a quantizer can be considered as a noise signal with an rms value of Which is the quantization error. Fig. (a) Fig. (b) 9

Related terms of A/D conversion (Cont.) Sampling: An analog signal is continuous and it has infinite number of samples. It is not possible to take the sample of analog signal at every instant of time. What the ADC circuit does is to take samples from the analog signal in a regular time interval. Each sample will be converted into a number, based on its voltage level. The frequency on which the sampling will occur is called sampling rate. If a sampling rate of 22,050 Hz is used, for example, this means that in one second 22,050 points will be sampled. Thus, the distance of each sampling point will be of 1 /22,050 second (45.35 µs, in this case). If a sampling rate of 44,100 Hz is used, it means that 44,100 points will be captured per second. In this case the distance of each point will be of 1 /44,100 second or 22.675 µs. And so on. 10

3-bit A/D converter with sampling rate of 10 samples/sec Basic of A/D Conversion Sampling Rate= 10 samples/sec 3-bit A/D converter So, to store the signal of length 1.1 s we need (10*1.1*3) = 33 bit memory. 11

4-bit A/D converter with sampling rate of 20 samples/sec Basic of A/D Conversion Sampling Rate= 20 samples/sec 4-bit A/D converter So, to store the signal of length 1.1 s we need (20*1.1*4) = 88 bit memory. = 11 Byte 12

3-bit A/D converter with sampling rate of 10 samples/sec Basic of A/D Conversion: Performance analysis of two A/D converters 4-bit A/D converter with sampling rate of 20 samples/sec 4-bit A/D converter with sampling rate 20 sample/sec is better than 3-bit A/D converter with sampling rate 10 sample/sec because converter output of 2 nd one is more closer to original signal. But, 2 nd one need 88-bit memory to store the signal where 1 st one need only 33-bit memory. 13

Basic of A/D Conversion: Minimum Sampling Rate (Nyquist Rate) So, we have this dilemma: if the sampling rate is too high, the output quality will be close to perfection, but you will need a lot of storage space to hold the generated data (i.e., the generated file will be very big); if the sampling rate is too low, the output quality will be bad. How can you know the best sampling rate to be used during analog-to-digital conversions to have the best storage/quality balance? The answer is the Nyquist Theorem. This theorem states that the sampling rate on analog-to-digital conversions must be at least two times the value of the highest frequency you want to capture. In practice it is necessary to sample at least 5 times the value of the highest frequency in order to reduce the effect of noise and non-sinusoidal filters. Since the human ear listens to sounds up to the frequency of 20 khz, for music we need to use a sampling rate of at least 40,000 Hz. In fact, the CD uses a 44,100 Hz sampling rate, thus capturing more than our ears can hear (this value was arbitrated by Phillips and Sony when they created the CD). Some professional audio applications use an even higher sampling rate. The phone system, on the other hand, was created to transmit only human voice, which has a lower frequency range, up to 4 khz. So on the digital part of the phone system, an 8,000 Hz sampling rate is used. That s why if you try to transmit music through the phone the quality is bad: the phone circuitry cancels all frequencies above 4 khz. 14

Basic of A/D Conversion: Explanation of Nyquist Rate Problem 2: There is an embedded system which has an 16-bit A/D converter with 8KHz sampling rate. To store a signal of 1 minute length, how much memory you need? 15

ANALOG MODULES of PIC16F887 Features of Analog Modules: The A/D converter module has the following features: The converter generates a 10-bit binary result using the method of successive approximation and stores the conversion results into the ADC registers (ADRESL and ADRESH); There are 14 separate analog inputs; The A/D converter converts an analog input signal into a 10-bit binary number; The minimum resolution or quality of conversion may be adjusted to various needs by selecting voltage references Vref- and Vref+. 16

Control of A/D Converter inside PIC 16F887 17 The operation of A/D converter is in control of the bits of four registers: ADRESH Contains high byte of conversion result; ADRESL Contains low byte of conversion result; ADCON0 Control register 0; and ADCON1 Control register 1.

Control of A/D CONVERTER inside PIC 16F887 (Cont.) ADRESH and ADRESL Registers The result obtained after converting an analog value into digital is a10-bit number that is to be stored in the ADRESH and ADRESL registers. There are two ways of handling it - left and right justification which simplifies its use to a great extent. The format of conversion result depends on the ADFM bit of the ADCON1 register. In the event that the A/D converter is not used, these registers may be used as general-purpose registers. 18

ADCON0 Register Control of A/D CONVERTER inside PIC 16F887 (Cont.) ADCS1, ADCS0 - A/D Conversion Clock Select bits select clock frequency used for internal synchronization of A/D converter. It also affects duration of conversion. ADCS1 ADCS2 CLOCK 0 0 Fosc/2 0 1 Fosc/8 1 0 Fosc/32 1 1 RC * GO/DONE - A/D Conversion Status bit determines current status of conversion: 1 - A/D conversion is in progress. 0 - A/D conversion is complete. This bit is automatically cleared by hardware when the A/D conversion is complete. ADON - A/D On bit enables A/D converter. 1 - A/D converter is enabled. 0 - A/D converter is disabled. 19

ADCON0 Register (cont.) Control of A/D CONVERTER inside PIC 16F887 (Cont.) CHS3-CHS0 - Analog Channel Select bits select a pin or an analog channel for A/D conversion, i.e. voltage measurement: 20

ADCON1 Register Control of A/D CONVERTER inside PIC 16F887 (Cont.) ADFM - A/D Result Format Select bit 1 - Conversion result is right justified. Six most significant bits of the ADRESH are not used. 0 - Conversion result is left justified. Six least significant bits of the ADRESL are not used. VCFG1 - Voltage Reference bit selects negative voltage reference source needed for the operation of A/D converter. 1 - Negative voltage reference is applied to the Vref- pin. 0 - Power supply voltage Vss is used as negative voltage reference source. VCFG0 - Voltage Reference bit selects positive voltage reference source needed for the operation of A/D converter. 1 - Positive voltage reference is applied to the Vref+ pin. 0 - Power supply voltage Vdd is used as positive voltage reference source. 21

A/D ACQUISITION REQUIREMENTS In order to enable the ADC to meet its specified accuracy, it is necessary to provide a certain time delay between selecting specific analog input and measurement itself. This time is called 'acquisition time' and mainly depends on the source impedance. There is an equation used to calculate this time accurately, which in the worst case amounts to approximately 20uS. So, if you want the conversion to be accurate, don t forget this important detail. The time needed to complete a one-bit conversion is defined as TAD. It is required to be at least 1-6 us depends on ADC clock frequency which is set by ADCS1 and ADCS0 bits. One full 10-bit A/D conversion is slightly longer than expected and amounts to 11 TAD periods. S 22

Steps of using A/D Module In order to measure voltage on an input pin by the A/D converter, the following should be done: Step 1 - Port configuration: Write a logic one (1) to a bit of the TRIS register, thus configuring the appropriate pin as an input. Write a logic one (1) to a bit of the ANSEL register, thus configuring the appropriate pin as an analog input. Step 2 - ADC module configuration: Configure voltage reference in the ADCON1 register. Select ADC conversion clock in the ADCON0 register. Select one of input channels CH0-CH13 of the ADCON0 register. Select data format using the ADFM bit of the ADCON1 register. Enable A/D converter by setting the ADON bit of the ADCON0 register. Step 3 - ADC interrupt configuration (optionally): Clear the ADIF bit. Set the ADIE, PEIE and GIE bits. 23

Steps of using A/D Module (Cont.) Step 4 - Wait for the required acquisition time to pass (approximately 20uS). Step 5 - Start conversion by setting the GO/DONE bit of the ADCON0 register. Step 6 - Wait for ADC conversion to complete. It is necessary to check in the program loop whether the GO/DONE pin is cleared or wait for an A/D interrupt (must be previously enabled). Step 7 - Read ADC results: Read the ADRESH and ADRESL registers. 24

Problem 3: Write codes to read analog value from channel 2 and displays it on PORTD and PORTB as 10-bit binary number. Use A/D converter reference voltage from external pin. 25

Solution of Problem 3 unsigned int temp_res; void main() { ANSEL = 0x0C; TRISA = 0xFF; ANSELH = 0; TRISB = 0x3F; TRISD = 0; ADCON1.F4 = 1 ; // Pins AN2 and AN3 are configured as analog // All port A pins are configured as inputs // Rest of pins is configured as digital // Port B pins RB7 and RB6 are configured as outputs // All port D pins are configured as outputs // Voltage reference is brought to the RA3 pin. } do { temp_res = ADC_Read(2); // Result of A/D conversion is copied to temp_res PORTD = temp_res; // 8 LSBs are moved to port D PORTB = temp_res >> 2; // 2 MSBs are moved to bits RB6 and RB7 } while(1); // Endless loop In order to make this example work properly, it is necessary to tick off the ADC library in the Library Manager prior to compiling: ADC 26

Problem 4: Lamp on street are used to lighten the street at night or cloudy time when day light becomes very dim. Design a circuit using PIC16F887 microcontroller for automatic control of street light, such that, lamp on street will lighten only at night or cloudy time, when there is not enough light. Use LDR to sense the light. Suppose, resistance of LDR is less than 1M when there is enough light and greater than 1M when light is not enough. Use internal A/D converter of microcontroller to measure the voltage across LDR. Solution of Problem 4 27

Program (Problem-4) unsigned int light; void main() { ANSEL = 0x04; TRISA = 0xFF; ANSELH = 0; TRISB.F0 =0; ADCON1.F4 = 0 ; ADCON.F5=0; while(1) { } } // Pins AN2 is configured as analog // All port A pins are configured as inputs // Rest of pins is configured as digital // Port B pin PB0 is configured as output // Power supply voltage Vdd is used as positive voltage reference source. // Power supply voltage Vss is used as negative voltage reference source. light= ADC_Read(2); // Result of A/D conversion is copied to light if(light>512) PORTB.F0=1; else PORTB.F0=0; 28

Problem 5: Gas content in the air is measured by ppm (parts per million) unit. Suppose, when 500 ppm CH4 gas is present in the air, it is harmful for health and when it exceeds 700 ppm, the situation is dangerous. Develop an embedded system for an industry to give- beef sound when CH4 content in the air is greater than 500 ppm and continuous alarm when CH4 content exceeds 700 ppm. The characteristics curve of the gas sensor and its pin diagram is given below. Use a buzzer to produce sound. 29

Solution of Problem 5 Circuit Diagram RB0 Out 30

Program (problem-5) unsigned int gas; void main() { ANSEL = 0x04; // Pins AN2 is configured as analog TRISA = 0xFF; // All port A pins are configured as inputs ANSELH = 0; // Rest of pins is configured as digital TRISB.F0 =0; // Port B pin PB0 is configured as output ADCON1.F4 = 0 ; // Power supply voltage Vdd is used as positive voltage reference source. ADCON.F5=0; // Power supply voltage Vss is used as negative voltage reference source. while(1) { gas= ADC_Read(2); // Result of A/D conversion is copied to gas if(gas>307 && gas<409) { PORTB.F0=1; delay_ms(300); PORTB.F0=0; delay_ms(300); } else if (gas>409) PORTB.F0=1; else PORTB.F0=0; } } 31