News:

Let's find out together what makes a PIC Tick!

Main Menu

HOW TO USE FASTEST ADC

Started by Abdullah, Mar 01, 2026, 11:46 AM

Previous topic - Next topic

Abdullah

Hello everyone.
I am trying to build a project (BOOST Converter) and want to make the ADC faster. I need some guidance on how to configure the ADC at register level. I am using a PIC16F684 with 20MHz crystal which registers and commands are used to configure the ADC at register level? Any guidance would be appreciated.
thanks in advance
Declare Adin_Res 10
Declare Adin_Tad FRC
Declare Adin_Stime 50

I use this command
Abdullah

Pepe

#1
Put Declare Adin_Tad 32_FOSC for 1.6us with 20mhz and Declare Adin_Stime 20

Abdullah

Quote from: Pepe on Mar 01, 2026, 12:12 PMPut Declare Adin_Tad 32_FOSC for 1.6us with 20mhz
Declare Adin_Stime 50
does this value need to be changed?
Abdullah

Pepe

Declare Adin_Stime 20

top204

#4
The declares can be used to set the conversion clock and the charge time for the ADC's capacitor. However, to see what is happening, and to easily change things to suit, a set of procedures cannot be beaten.

For example, the code listing below will setup the ADC on a PIC16F684 device, operating at 20MHz, for fast operation. It also has procedures to read the ADC, just like the ADin command, but it can be seen what is happening, and is more efficient, and changes can easily be made, such as reading 10-bit or reading 8-bit ADC values.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Setup a PIC16F684 device to operate at 20MHz using an external crystal or resonator.
' And setup the ADC peripheral for fast operation, with procedures to read 8-bit and 10-bit ADC values.
'
' Written for the Positron8 BASIC Compiler by Les Johnson.
'
Declare Hints Off                                   ' Disable the hints for the optimiser being enabled
    Device = 16F684                                 ' Tell the compiler what device is being compiled for
    Declare Xtal = 20                               ' Tell the compiler what speed the device will be operating at (in MHz)
    Declare Optimiser_Level = 2                     ' Set the optimiser to level 2
    Declare Dead_Code_Remove = 1                    ' Remove Dead Code for extra optimising
'
' Setup RSout and RSin
'
    Declare Serial_Baud = 9600                      ' Baud rate of 9600
    Declare RSOut_Pin = PORTC.0                     ' Transmit from PORTC pin 0
    Declare RSOut_Mode = 0                          ' Inverted mode
'
' Create any global variables here
'
    Dim wADC_Value As Word                          ' Holds the 10-bit result of the ADC read

'-----------------------------------------------------------------------
' The main program starts here
' Read ADC channel AN0 and Transmit the ADC value to a serial terminal.
'
Main:
    Setup()                                         ' Setup the program and any peripherals
    RSoutLn "Start"                                 ' An initial transmit to see that the device is working at the correct speed

    Do                                              ' Create a loop
        wADC_Value = ADC_Read10(0)                  ' Read ADC channel AN0
        RSoutLn Dec wADC_Value                      ' Transmit the ADC value to a serial terminal
        DelayMs 512                                 ' A delay so the values do not swamp the serial terminal
    Loop                                            ' Do it forever

'-----------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    ADC_Setup()                                     ' Setup the ADC
EndProc

'-----------------------------------------------------------------------
' Read the ADC at 10-bit
' Input     : pChan holds the ADC channel to read
' Output    : Returns the 10-bit value read from the ADC
' Notes     : For use on a PIC16F684 device
'
Proc ADC_Read10(pChan As Byte), Word
    pChan = pChan << 2                              ' Move the value in pChan into the correct position for ADCON0
    ADCON0 = ADCON0 & %11100011                     ' Clear the channel bits in ADCON0
    ADCON0 = ADCON0 | pChan                         ' Or the pChan value into the channel bits
    DelayUs 5                                       ' Allow time for the ADC capacitor to charge (change for faster or slower ADC reading)
    ADCON0bits_GO = 1                               ' Start an ADC conversion
    Repeat: Until ADCON0bits_GO = 0                 ' Wait for it to finish
    Result.Byte1 = ADRESH                           ' \
    Result.Byte0 = ADRESL                           ' / Return the 10-bit result
EndProc

'-----------------------------------------------------------------------
' Read the ADC at 8-bit
' Input     : pChan holds the ADC channel to read
' Output    : Returns the 8-bit value read from the ADC
' Notes     : For use on a PIC16F684 device
'
Proc ADC_Read8(pChan As Byte), Byte
    pChan = pChan << 2                              ' Move the value in pChan into the correct position for ADCON0
    ADCON0 = ADCON0 & %01100011                     ' Clear the channel bits in ADCON0 and Left justify the result
    ADCON0 = ADCON0 | pChan                         ' Or the pChan value into the channel bits
    DelayUs 5                                       ' Allow time for the ADC capacitor to charge (change for faster or slower ADC reading)
    ADCON0bits_GO = 1                               ' Start an ADC conversion
    Repeat: Until ADCON0bits_GO = 0                 ' Wait for it to finish
    Result = ADRESH                                 ' Return the 8-bit result
EndProc

'-----------------------------------------------------------------------
' Setup the ADC on a PIC16F684 device
' Input     : None
' Output    : None
' Notes     : Set for default of 10-bit operation, and fast Conversion Clock
'
Proc ADC_Setup()
    ADCON0 = %10000001                              ' Right Justified for 10-bit operation. ADC enabled
    ADCON1 = %00000000                              ' A/D Conversion Clock is FOSC/2
    ANSEL.0 = 1                                     ' Set pin AN0 for analogue
EndProc

'-------------------------------------------------------------------
' Setup the config fuses for an external crystal on a PIC16F684 device
'
    Config HS_OSC,_
           WDTE_OFF,_
           PWRTE_OFF,_
           MCLRE_OFF,_
           CP_OFF,_
           CPD_OFF,_
           BOREN_OFF,_
           IESO_OFF,_
           FCMEN_OFF

A quick test in a simulator showed that the ADC procedures are working as they should, and a screenshot of it is shown below:

ADC_Read_Screenshot.jpg

Regards
Les