chapter4 dae32203

41
DAE 32203 Chapter 4 : Analog to Digital Converter Norhafiza Samion Pusat Pengajian Diploma

Upload: jay-leong

Post on 20-Jan-2015

170 views

Category:

Engineering


0 download

DESCRIPTION

chapter 4, microcontroller (dae32203)

TRANSCRIPT

Page 1: Chapter4 dae32203

DAE 32203

Chapter 4 : Analog to Digital Converter

Norhafiza Samion

Pusat Pengajian Diploma

Page 2: Chapter4 dae32203

Analog to Digital Converter

• Concept & configuration

• Application 1 : Variable Resistor

• Application 2: Temperature Sensor

Page 3: Chapter4 dae32203

Concept & configuration

• Many electrical signals around us are analog in nature. That

means a quantity varies directly with some other quantity.

• The first quantity is mostly voltage while that second quantity

can be anything like temperature, pressure, light, force or acceleration.

• For example in LM35 temperature sensor the output voltage varies according to the temperature, so if we could measure voltage, we can measure temperature.

Page 4: Chapter4 dae32203

Concept & configuration

• But most of our computer (or Microcontrollers) are digital in

nature. They can only differentiate between HIGH or LOW level on input pins.

• For example if input is more than 2.5v it will be read as 1 and if it is below 2.5 then it will be read as 0 (in case of 5v systems).

• So we cannot measure voltage directly from MCUs. To solve this

problem most modern MCUs have an ADC unit. ADC stands for

analog to digital converter. It will convert a voltage to a

number so that it can be processed by a digital systems like MCU.

Page 5: Chapter4 dae32203

Concept & configuration

• This enables us to easily interface all sort of analog devices with CPUs. Some really helpful example of analog devices are

• Light Sensors.

• Temperature Sensors.

• Accelerometers.

• Touch Screens.

• Microphone for Audio Recording.

And possibly many more.

Page 6: Chapter4 dae32203

Concept & configuration

• As the peripheral signals usually are substantially different from the ones that microcontroller can understand (zero and one), they have to be converted into a pattern which can be comprehended by a microcontroller.

• This task is performed by a block for analog to digital conversion or by an ADC.

• This block is responsible for converting an information about some analog value to a binary number and for follow it through to a MCU block so that MCU block can further process it.

Page 7: Chapter4 dae32203

Concept & configuration

• Most important specification of ADCs is the resolution. This

specifies how accurately the ADC measures the analog input signals.

• Common ADCs are 8 bit, 10 bit and 12 bit

• For example if the reference voltage* of ADC is 0 to 5v then a 8

bit ADC will break it in 256 divisions so it can measure it accurately up to 5/256 v= 19mV approx.

• While the 10 bit ADC will break the range in 5/1024 = 4.8mV approx. So you can see that the 8 bit ADC can't tell the difference between 1mV and 18mV. The ADC in PIC16 are 10 bit.

Page 8: Chapter4 dae32203

Concept & configuration

• reference voltage*

• The reference voltage specifies the minimum and maximum voltage range of analog input. In PIC 16, there are two reference voltage, one is the Vref- and one is Vref+. The Vref- specifies the minimum input voltage of analog input while the Vref+ specifies the maximum. For example if the input signal Vref- is applied to analog input channel then the result of conversion will be 0 and if voltage equal to Vref+ is applied to the input channel the result will be 1023 (max value for 10bit ADC).

Page 9: Chapter4 dae32203

Concept & configuration

• reference voltage*

• The Voltage Reference module is typically used in conjunction with the Comparator module. The comparator module’s inputs do not require very large drive, and therefore the drive capability of the Voltage Reference is limited.

• The Voltage Reference is a 16-tap resistor ladder network that provides a selectable voltage reference. The resistor ladder is segmented to provide two ranges of VREF values and has a power-down function to conserve power when the reference is not being used. The VRCON register controls the operation of the reference as shown in figure of Voltage Reference Block. Followed by the table of Typical Voltage Reference. Within each range, the 16 steps are monotonic (i.e. each increasing code will result in an increasing output).

Page 10: Chapter4 dae32203

Voltage Reference Block Diagram

Table of Typical Voltage Reference with VDD= 5V

Page 11: Chapter4 dae32203

Voltage Reference Block Diagram

Page 12: Chapter4 dae32203

Concept & configuration

• reference voltage* - configuring

• The Voltage Reference can output 16 distinct voltage levels for each range.

• The equations used to calculate the output of the Voltage Reference are as follows:

• if VRR = 1: VREF = (VR3:VR0/24) x VDD

• if VRR = 0: VREF = (VDD x 1/4) + (VR3:VR0/32) x VDD

• The settling time of the Voltage Reference must be considered when changing the VREF output.

• Next are an example of how to configure the Voltage Reference for an output voltage of 1.25V with VDD = 5.0V. Generally the VREF and VDD of the system will be known and you need to determine the value to load into VR3:VR0. The equation shows how to calculate the VR3:VR0 value. There will be some error since VR3:VR0 can only be an integer, and the VREF and VDD levels must be chosen so that the result is not greater then 15.

Page 13: Chapter4 dae32203

Concept & configuration

• reference voltage* - configuring

Page 14: Chapter4 dae32203

Concept & configuration – example 1

• Up to now, we have considered inputs as being digital in operation, i.e. the input is either a 0 or 1. But suppose we wish to make temperature measurement, but not hot or cold (1 or 0). We may, for example, require to:

• Sound a buzzer if the temperature drops below freezing.

• Turn a heater on if the temperature is below 18°C.

• Turn on a fan if the temperature goes above 25°C.

• Turn on an alarm if the temperature goes above 30°C

Page 15: Chapter4 dae32203

Concept & configuration

• Example circuit using Thermistor to measure the temperature as analogue data input.

C

X

Thermistor

22k

0 V

5 V

Page 16: Chapter4 dae32203

Concept & configuration – example 1

• Making an A/D reading

• 0°C gave a voltage

• 18°C gave a reading of 1.4V

• 25°C gave a reading of 2.4V

• 30°C gave a reading of 3.6V

• The microcontroller would read these voltages and convert them to an 10-bit number where 0V is 0 and 5V is 1023. i.e. 1 bit is (5V/1024) = 4.88mV

Page 17: Chapter4 dae32203

Concept & configuration – example 1

• The 16F877A has eight analog inputs AN0 AN7. But these pins are also used as A0, A1, A2, A3/+Vref, A4 and A5 (digital pins and voltage reference).

Page 18: Chapter4 dae32203

Concept & configuration – example 1

• PIC16F877A has 10-bit resolution.

• So the number of steps is: 1024 = (0000000000 1111111111).

• When Vref 5V(internal voltage supply) is used, the step size is 5V/1024 = 4.88mV. So it’s mean that every bit increment is equal of single step voltage increment ~ 4.88mV.

• For example to calculate the binary value of ADC that use Vref = 5V and the analog input = 1.7V is by using the formula below.

• Dout = Vin / Step Size.

• Dout = 1.7V / 4.88mV

• = 348.36

• ≈ 348 0101011100

Page 19: Chapter4 dae32203

10-bit A/D Converter Block Diagram

• Example; The Vref+ and Vref- pins are available in PIN5 and PIN4 of the PIC16F877 chip. So you can connect the reference voltage here. For a simple design the Vref- is GND and Vref+ is Vcc.

• As this is such a common configuration that the ADC can be set up to use these reference internally. Therefore you do not need to connect these on the external Vref pins, so you can use them for other purpose.

Page 20: Chapter4 dae32203

Concept & configuration

• The analog-to-digital (A/D) converter module can have up to eight analog inputs for a device.

• The analog input charges a sample and hold capacitor. The output of the sample and hold capacitor is the input into the converter. The converter then generates a digital result of this analog level via successive approximation. This A/D conversion, of the analog input signal, results in a corresponding 10-bit digital number.

• The analog reference voltages (positive and negative supply) are software selectable to either the device’s supply voltages (AVDD, AVss) or the voltage level on the AN3/VREF+ and AN2/-VREFpins.

Page 21: Chapter4 dae32203

Concept & configuration

• The A/D converter has a unique feature of being able to operate while the device is in SLEEP mode. To operate in SLEEP, A/D clock must be derived from the A/D’s internal RC oscillator.

• The A/D module has four registers. These registers are:

• A/D Result High Register (ADRESH)

• A/D Result Low Register (ADRESL)

• A/D Control Register0 (ADCON0)

• A/D Control Register1 (ADCON1)

Page 22: Chapter4 dae32203

Concept & configuration

• The ADCON0 register, controls the operation of the A/D module.

• The ADCON1 register, configures the functions of the port pins. The port pins can be configured as analog inputs (AN3 and AN2 can also be the voltage references) or as digital I/O.

Page 23: Chapter4 dae32203

Concept & configuration – ADCON0 Register

Page 24: Chapter4 dae32203

Concept & configuration – ADCON0 Register

• The user has to select the correct clock conversion. The period must be at least more than 1.6us to obtain an accurate conversion.

• For example, a 4MHz crystal oscillator has been used on PIC16F877A. So if one select Fosc/2, thats 2MHz and the period is just 500ns and it’s far less than the 1.6us required.

•What if Fosc/8 has been selected? That will give 0.5MHz and the period is 2us. That is more than 1.6us so it can be selected. So, the ADCON0 is now 01xx xxxx.

Page 25: Chapter4 dae32203

Concept & configuration – ADCON0 Register

• Now onto selecting the Analog Channel. The ADC can only have one input at a time so the user must select which pin he/she wants to use.

• If input wired into Analog Channel 3 (which is also PORTA bit 3), so the ADCON0 has to set 0101 1xxx.

Page 26: Chapter4 dae32203

Concept & configuration – ADCON1 Register

Page 27: Chapter4 dae32203

Concept & configuration – ADCON1 Register

• The ADFM bit determines how the result of the ADC is justified. The ADC on the PIC16F877A has 10-bits of resolution, so of course a single register (that has 8 bits) is not enough to contain the 10-bits result. Therefore, two registers are required to store the results. ADRESH and ADRESL (H is the high byte while L is the low byte).

• Two registers will allow us to store up to 16 bits, but since there are only 10 bits, we have the flexibility to align it right justified or left justified.

Page 28: Chapter4 dae32203

Concept & configuration – ADRESH & ADRESL Register

• ADFM: A/D Result Format Select bit;

• 1 = Right justified • 0 = Left justified

•ADRESH:ADRESL registers contain the 10-bit result of the A/D conversion. When the conversion is complete, the result is loaded into this A/D Result register pair, the GO/ DONE’ bit (ADCON0<2>) is cleared and the A/D interrupt flag bit ADIF is set.

Page 29: Chapter4 dae32203

Concept & configuration – ADCON1 Register

• Storing the result in left justified mode is weird and unusual but it gives

the user flexibility.

• e.g; an application does not need the 10-bit accuracy, 8 bits is more than enough. So, just take the result in ADRESH and ignore the remaining two least significant bits in ADRESL (you cannot ignore the two highest significant bit because that will cause the result to be inaccurate).

•That makes it easier to move values to other registers. Yes, the accuracy of the result will be slightly affected but it is not so critical in applications where you don’t need accuracy, like my ADC Demo shown here. So, now the value of ADCON1 is 0xxx xxxx.

Page 30: Chapter4 dae32203

Concept & configuration – ADCON1 Register

• Next is the ADCS2 bit.

• Earlier, calculated Fosc/8 is adequate, so for ADCON1, it has to be set on ADCS2 bit. So, the value of ADCON1 will be 00xx xxxx.

Page 31: Chapter4 dae32203

Concept & configuration – ADCON1 Register

Page 32: Chapter4 dae32203

Concept & configuration – ADCON1 Register

• Lastly, the most important part of the ADC configuration is to select the

mode for each Analog channel. As shown before, we have Analog Channel 0 to 7. All these inputs can either be set to analog or digital.

• Referring to the table above, if we don’t need any analog inputs and require more digital pins (let’s say for a few LCDs), we can set the PCFG3:0 bits to be 011x. But in this case we do need the Analog inputs, so for simplicity we will set all of them to be in analog mode. Therefore, our final value for ADCON1 is 0000 0000.

• One important thing to note: If Vdd was selected as the Vref+ and Vss as the Vref-, that means that my conversion range is from 0V to 5V. If you need it to be other than that, you can set a custom Vref value by choosing other configurations of PCFG3:0.

Page 33: Chapter4 dae32203

Analog to Digital Converter in C Programming

• ADC (Analog to Digital Converter) module is available with a number of PIC MCU models. Library function Adc_Read is included to provide you comfortable work with the module.

• Adc_Read(unsigned short channel)

• i.e: adcresult = Adc_Read(0); //Read the 10bits of conversion value from PORTA.F0 and put in in variable “adcresult”

• Before using the function, be sure to configure the appropriate TRISA bits to designate the pins as input. Also, configure the desired pin as analog input, and set Vref (voltage reference value).

Page 34: Chapter4 dae32203

Analog to Digital Converter in C Programming

• C code to Read ADC and show the result at PORTB

unsigned int temp_res;

void main() {

ADCON1 = 0x80; // Configure analog inputs and Vref(Internal Vref +5V)

TRISA = 0xFF; // PORTA is input

TRISB = 0x00; // Pins PORTB are outputs

while(1) {

temp_res = Adc_Read(0); // Get results of AD conversion at AN0

PORTB = temp_res; // Send lower 8 bits to PORTB

}

Page 35: Chapter4 dae32203

• 0v = 0000000000

• 5v = 1111111111

• Resolution = (Vref+ – Vref-)/(1024-1) (as it is 10 bit ADC)

• = 5/1023

• = 4.887 mV

• Thus it means that for a change in 4.887mV, the binary output changes by 1.

CASE 1: Analog-to-Digital Conversion – Variable Resistor

• This example project will convert analog input to channel 1 to 10 bit digital number with low voltage reference (Vref-) 0v and high voltage reference (Vref+) 5V.

• The output will be displayed using 10 LEDs. You can change the Vref- and Vref+ by configuring the ADCON1 register.

Page 36: Chapter4 dae32203

• Circuit Diagram

CASE 1: Analog-to-Digital Conversion - Variable Resistor

Page 37: Chapter4 dae32203

• The C Programming :

• unsigned int adc;

• void main()

• {

• ADCON1= 0x80;

• TRISA = 0xFF; // PORTA is input

• TRISC = 0x3F; // Pins RC7, RC6 are outputs

• TRISB = 0; // PORTB is output

• do

• {

• adc = ADC_Read(1); // Get 10-bit results of AD conversion

• //of channel 1

• PORTB = adc; // Send lower 8 bits to PORTB

• PORTC = adc >> 2; // Send 2 most significant //bits to RC7, RC6

• } while(1);

• }

CASE 1: Analog-to-Digital Conversion - Variable Resistor

Page 38: Chapter4 dae32203

• Expected Result

• OUTPUT: ????

CASE 1: Analog-to-Digital Conversion - Variable Resistor

Page 39: Chapter4 dae32203

void main()

{

unsigned short ADCResult;

TRISD = 0b00000000;

//Setting PORTD as output port

PORTD = 0b00000000;

//Initializing PORTD

ADCON0bits.ADCS = 0;

//Selecting the clk division factor = FOSC/2

ADCON1bits.ADCS2 = 0;

//Selecting the clk division factor = FOSC/2

ADCON1bits.ADFM = 1;

//Result right justified

ADCON1bits.PCFG = 0;

//Setting all the analog ports as Analog inputs

ADCON0bits.ADON = 1;

//Turns on ADC module

ADCON0bits.CHS = 0; //Selects channel 0 ( AN0 )

}

CASE 2: Analog-to-Digital Conversion – Temperature Sensor

while(1)

{

__delay_us(25); //Waits for the acquisition

to complete

ADCON0bits.GO = 1; //Starts ADC conversion

while (ADCON0bits.nDONE) continue; //wait

till ADC conversion is over

ADCResult = (ADRESH<<8) + ADRESL ;

//Merging the MSB and LSB

if(ADCResult>512)

PORTD = 255;

else

PORTD = 0;

}

}

Page 40: Chapter4 dae32203

TRANSITIONAL PAGE

Page 41: Chapter4 dae32203

www.animationfactory.com

Thank you

Credits & special thanks to :

1. En. Mohd Hamim bin Sanusi @ Ikhsan

2. En. Mohamad bin Md. Som

3. http://tutorial.cytron.com.my/2011/08/09/project-4-%E2%80%93-analog-sensor-potentiometer-bb-psj/

4. http://extremeelectronics.co.in/microchip-pic-tutorials/using-analog-to-digital-converter-%E2%80%93-pic-microcontroller-tutorial/

5. www.microchip.com