You are on page 1of 4

Eng.

Mohammad Aqel

Faculty of Engineering & Information Technology Al-AZHAR UNIVERSITY-GAZA

Embedded System Design Lab ITSE 4316

(Experiment 6)

Analog-to-Digital Conversion

Objective
Understand the Analog-to-digital conversion Basics. To read analog signal and convert it to digital. To acquaint the student with the PicBasic command ADCIN.

Background
This experiment uses one of the most useful features of the PIC16F877A, the analog-to digital (A/D) converter. Almost everything in the real world is not digital but instead analog. To control something in the real world, or to understand something in the PIC, we have to convert that realworld analog data into the digital form the PIC understands. That is done with an A/D converter. For example, if you have to read a temperature or light levels, you will need both a sensor to convert the measurement into a variable voltage and an A/D converter to change the resulting voltage into a digital value. In this example, our sensor will be a variable resistor called a potentiometer (POT). As we turn the POTs shaft, we want to read the variable resistance from that POT and light some LEDs to show how much we turned it. This could be compared to the volume adjustment you make on a stereo. As you turn the knob for volume, the sound from the stereo gets louder. Thats because it is reading the resistance of the POT connected to the knob you turned to adjust the amplifiers output. This experiment will use a POT connected to Port A pin RA0. Well control five LEDs using Port B. The program will have thresholds of A/D values associated with each LED so, as we turn the POT, the LEDs will light in order just like a bar meter on a stereo. 1

Fortunately, we dont need to know too much about the operation of the A/D circuit; the software just assumes the circuit works, and it does. A/D circuits come in different forms but they all do the same thingconvert an analog voltage into a digital voltage. An A/D registers digital output will have a resolution to it. That means it can output an 8-bit digital value, 10-bit digital value, or larger if required. The PIC 16F877A has a 10-bit resolution A/D register, but can also operate as an 8-bit. We will use it as an 8-bit since its a little easier to understand. Eight bits fit into one byte, and thats much easier to manipulate in code. The schematic diagram is given in the following Figure. The LEDs are connected to Port B with 100-ohm series resistors.

Figure 1: Schematic diagram of experiment

Looking at above Figure, notice we add the potentiometer to Port A RA0. By adjusting the POT, we are changing the voltage at RA0. The A/D port cannot handle voltages above 5 volts. Therefore if you need to measure larger voltages, you either have to step it down using resistors or build a voltage conversion circuit using an op amp IC. ADCON0 and ADCON1 are special function registers for controlling the A/D register and ADRESH and ADRESL are where the result of the A/D conversion is stored. If you operate in 10-bit mode, both ADRESH and ADRESL are used to hold the 10-bit result. If you use 8-bit resolution, then you only need one byte to store the result so that register is ADRESH. The adjustment of ADCON1 also clears the ADFM bit, to set the A/D output to 8-bit mode. This will put the full result in the ADREH register as a byte. To understand how the voltage at the POT is compared to the set values in the The A/D converter defaults to the 5-volt Vref as the reference voltage used internally by the A/D converter. It takes the ratio of the voltage at RA0 and the Vref voltage of 5 volts and multiplies it by 255. The result of that calculation is then stored in the ADRESH register. For example, if the voltage at RA0 is 2.30 volts, then the result would be: (2.30 / 5) * 255 = 117.30. PBPro Code For a real demonstration of the simplicity the PBPro compiler offers, well use the ADCIN command to read the A/D port RA0 and light the LEDs based on the same threshold values. We start off with the DEFINE statements required by PBPro. There are a series of DEFINE statements dedicated to the ADCIN command. These make it simple to set the output result to eight bits, the clock source to RC, and add a sample time that sets when we check the status of the A/D conversion. Next, at the init label the program establishes a byte variable called adval. This is where the A/D result will be stored. Then the ADCIN command is issued. Within this command we define which A/D port to read (0) and where to put the result (adval). The testing for completion of the A/D conversion is all done by the ADCIN command. After the conversion is complete, we can go right to work on the result and test it against the IF-THEN statement thresholds. At each step, we compare adval to the predefined values. We light the LEDs by working directly on the Port B register. We light the LEDs according to the value of adval. If adval is less than a predefined value in the IF-THEN statement, then the next command is the Port B

manipulation. If none of the values in the IF-THEN statements are larger than adval, then the Port B register is changed to light all five LEDs. After the LEDs are set, we pause 100 milliseconds to let the LEDs glow.

Lab Work
1. Wire the circuit shown in Figure 1. 2. Write a PICBasic Program to light the LEDs according to the value of analog voltage at RA0 and to display it on 2x16 LCD.

You might also like