Thursday, November 5, 2015

ADC PROGRAMMING USING PIC184550

ADC Analog to Digital Converter:
We all know how analog to digital converter works. It’s simple
There are 3 steps:
1.)           Find the resolution of ADC i.e. in our case it’s 10-bit. Let us assume n = resolution. So n =10.


2.)           Find the Step Size using the formula:
Step Size = (Vref) / (2n – 1)
       We Know Vref = (V+ ) – (V-), i.e. V+ is connected to
         Vcc = +5V and V- is connected to Vss = GND
        By ADCON1 Register we can assign Vref for PIC18
        So Formula is converted as follow:
        Step Size = (Vcc) / (210 - 1)
                        = (Vcc) / (1023) ; if Vcc = +5V
                        = (5) / (1023)
        Step Size = (0.004887585532746823069403714565)

3.)           Find the Digital out i.e. it varies from 0 – 1023.

Dout = (Vin) / (Step Size)

   Here Vin is the input analog voltage which is given to AN0 –            AN12 i.e. 12 Channel in PIC18 for ADC.

   Now Formula for Vin = 5

Dout = (5) / (Step Size); Dout = 1023 which is 3FF H
    
   If Vin = 2.5
   
        Dout = (2.5) / (Step Size); Dout = 511.2 ≈ 511 i.e. 1FF H

     So in this way we can get all the digital value for analog input.

In PIC18 we have an inbuilt ADC with 10-bit resolution.

There are 3 control register associated with PIC18 i.e.
1.)           ADCON0.
2.)           ADCON1.
3.)           ADCON2.

1.) ADCON0:

Bits
7
6
5
4
3
2
1
0


--
--
CHS3
CHS2
CHS1
CHS0
GO/DONE
ADON

Value
0
0
0
0
0
0
0
1
0x01

ADON = 1; Enable ADC Module in PIC18

CHS3 CHS0 = 0000 – 1100; (Analog Channel Select bits) 12 channel for ADC. [Channel Zero is selected in this Case]

Go/Done = A/D Conversion Status

Go = 1; Start of Conversion [Write]

Done = 0; Read this for Zero, i.e. End of Conversion.



2.) ADCON1:

Bits
7
6
5
4
3
2
1
0


--
--
VCFG1
VCFG0
PCFG3
PCFG2
PCFG1
PCFG0

Value
0
0
0
0
1
0
0
1
0x09

VCFG1= 0; Voltage Reference Configuration bit (VREF- source) Vcc

VCFG0= 0; Voltage Reference Configuration bit (VREF+ source) VDD

PCFG3PCFG0= 0000 – 1111; (A/D Port Configuration Control bits)
To select 15 I/O Lines of PIC18 as Digital or Analog we must configure it. [In this case AN0 – AN5 as Analog, but we can configure it as we wish]. Refer Data Sheet of PIC18F4550


3.) ADCON2:

Bits
7
6
5
4
3
2
1
0


ADFM
--
ACQT2
ACQT1
ACQT0
ADCS2
ADCS1
ADCS0

Value
1
0
1
0
1
1
0
1
0xAD

ADFM: A/D Result Format Select bit
1 = Right justified
0 = Left justified

ACQT2:ACQT0: A/D Acquisition Time Select bits
101=12 TAD

ADCS2:ADCS0: A/D Conversion Clock Select bits
101 = FOSC/16

There are two 8bit Register to store ADC’s 10bit Output.

1.)           ADRSEL.
2.)           ADRESH.

Right Adjust Means:

ADRESH:                             

15
14
13
12
11
10
9
8
x
x
x
x
x
A
A
A


ADRESL:
7
6
5
4
3
2
1
0
A
A
A
A
A
A
A
A

     

Only 0-10 bit of Register ADRESH & ADRESL are occupied if Right Adjust is used. For Left Adjust it 15-5 bits of ADRESH & ADRESL.

Now for Programming Follow Step are used.

Step 1: Create lcd.h file for lcd function to display all data.
                Refer previous videos for lcd.h and Blog.

Step 2: Create adc.h file for adc function.
1.)           Initialize ADCON0 , ADCON1, ADCON2
2.)           Write Function to start the conversion and return the output digital data.
3.)           Write function to display all digital output in LCD

Step 3: Create main.c file and call all fnction in it.
         Step 1: Declaration of input and output port.
         Step 2: Call LCD and adc initialization function

        Step3: In while continuous read all data from adc and display it in LCD.






































Thank You for Reading .


Check Out Video for Above Description: Click for video on ADC


ADC using PIC Code: Click Here For Code

Pic18 Data sheet:Click for Datasheet