6 March 2014 ADC Objectives To be familiar with analog to digital converter module in PIC18F4550. Introduction Analog-to-digital conversion (ADC) is necessary because, while embedded systems deal with digital values (as we have dealt with keypads and switches). Analog signals such as, temperature, speed and pressure are generated by peripheral devices such as microphones, analog cameras, sensors, and etc. They all need to be converted into digital data before being processed by the microcontroller. At this lab, we will see how to read an external analog signal using a microcontroller, and display the conversion output (a digital number) on a LCD. The input analog signals will be a varying voltage between 0-5V. Analog to digital conversion Signals in the real world are analog: light, sound, etc. 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 such as microcontroller. Let's say you have a sound wave, and you wish to sample it with an ADC. Here is a typical wave: 2 Embedded Systems Lab When you sample the wave with an analog-to-digital converter, you have control over three variables: The sampling rate: Controls how many samples are taken per second. The sampling precision (resolution): Controls how many different gradations (quantization levels) are possible when taking the sample. The reference voltages The VREF + represents the maximum analog value that can be converted by the Analog to Digital converter. The VREF represents the minimum analog value that can be converted by the Analog to Digital converter. You can see that as the sampling rate and precision (resolution) increase, the similarity between the original wave and the ADC's output improves. The figure below shows an analog signal and quantized versions for several different number of quantization levels. With L levels, we need N=log2 L bits to represent the different levels or conversely, with N bits we can represent L = 2N levels. 3 Embedded Systems Lab PIC Internal Analog to Digital Converter A number of microcontrollers have built in Analog to Digital Converter (ADC). Commonly, these AD converters have 8-bit or 10-bit resolution allowing them voltage sensitivity of 19.5mV or 4.8mV, respectively. The ADC for PIC 18F4550 module has the following characteristics • Number of analog channels - The Analog-to-Digital (A/D) Converter module has 13 inputs for PIC18F4550 (AN0 – AN12) which corresponds to (RA0-RA5 & RE0-RE2 &RB0-RB4) as indicated on PIC18F4550 pins. - The analog channels can be configured as digital input/output pins by configuring ADCON1. 4 Embedded Systems Lab • Resolution (precision) - The conversion of an analog input signal results in a corresponding 10-bit digital number this means the conversion result has 1024 levels. - The lower 8 bits of the result are stored at ADRESL register; the upper 2bits are stored at ADRESH register. • VRef+ / Vref- Configured by ADCON1 register such that - Vref + is determined by RA3 voltage level pin or by VDD - Vref- is determined by RA2 voltage level or by VSS Pic18f4550 divides input to 1024 level (0-1023) with references VDD (5 V) and VSS(0 V) Level 0 => 0V, level 1023:5 V If we have a level number between 0-1023, we can get the voltage by the following equation: Level voltage 1023 5 X ? => voltage =x*5/1023 V • The module has five registers: - - A/D Result High Register (ADRESH) - - A/D Result Low Register (ADRESL) - - A/D Control Register 0 (ADCON0) - - A/D Control Register 1 (ADCON1) - - A/D Control Register 2 (ADCON2) 5 Embedded Systems Lab The configuration of ADC module can be determined by configuring ADCON0, ADCON1 and ADCON2 registers ADCON0: (mainly Select A/D input channel) 6 Embedded Systems Lab ADCON1(Configure analog pins, voltage reference and digital I/O) 7 Embedded Systems Lab A DCON2 (Select A/D conversion clock) 8 Embedded Systems Lab We will deal with adcon1 - Bits 0-3: will be changed according to our needs of using analog inputs. - At the past, we have used to write adcon1=adcon1 or 0f => referring to the table below => f (the value of 4 bits) means that all the inputs are digital which was suitable with our work at that time. Working with code In order to deal with analog signals, we must read the signal and make its sampling and quantization operations. ADC_READ function reads the channel signal and returns its quantization level value. 9 Embedded Systems Lab Lab Work 1 1. Write a basic program for the simplified digital voltmeter circuit for one analog input and show the results at the LCD. 1. 2. Simulate the circuit using Proteus ISIS program. 10 Embedded Systems Lab Proteus - Variable resistor: POT-LIN (or POT-HG) - As its value changes, voltage changes Mikrobasic - Variable for quantization level as integer. - Variable for voltage value after the equation. - Variable to store values for view in LCD For 1-analog input, from the table Or adcon1 = 0x0E (00001110) Read the signal at the first channel (AN0) Function to change the type of float into string to view it on LCD, take the float value and the destination string value as parameters. 11 Embedded Systems Lab You can use voltmeter device to check your work: Lab Work 2 1. Repeat the previous part but with 2-analog inputs. 2. Simulate the circuit using Proteus ISIS program. Proteus 12 Embedded Systems Lab Mikrobasic Interfacing LM35 Temperature Sensor with PIC Microcontroller The are many cool sensors available now a days, ranging from IR distance sensor modules, accelerometers, humidity sensors, temperature sensors and many more(gas sensors, alcohol sensor, motion sensors, touch screens). Many of these are analog in nature. That means they give a voltage output that varies directly (and linearly) with the sensed quantity. For example in LM35 temperature sensor, the output voltage is 10mV per degree centigrade. That means if output is 300mV then the temperature is 30 degrees. At the following part we will learn how to interface LM35 temperature sensor with PIC18F4550 microcontroller and display its output on the LCD. 13 Embedded Systems Lab Lab Work 3 1. Interface LM35 Temperature Microcontroller. (LM35 at proteus) Sensor 2. Simulate the circuit using Proteus ISIS program. Proteus In order to change from voltage to temperature: Voltage 10m X 14 degree 1 => degree =x/10m = x*100 ? Embedded Systems Lab with PIC Mikrobasic Light Dependent Resistor (LDR) Used to measure Light Intensity, when the resistance lowers, the light intensity is increased. LDR at proteus are: - LDR - Torch LDR Do you’re extrasearching for them by yourself 15 Embedded Systems Lab Homework 1. Write a basic program for the simplified digital voltmeter circuit for one 2- analog input (AN2, AN3) for any type of LDR and variable resistor and show the results at the LCD. 2. Write a basic program that will turn on the led if the temperature (analog input) is greater than 60 degrees. Bonus: Implement a Light intensity measurement circuit using LRD (output is not a voltage, but light measurement by Lux unit of light). … Good Luck … 16 Embedded Systems Lab
© Copyright 2025 ExpyDoc