News:

;) This forum is the property of Proton software developers

Main Menu

ADC on a pic18f27Q10

Started by TimB, Jan 04, 2026, 03:25 PM

Previous topic - Next topic

TimB


I have been trying really hard not to just come running here everytime I cannot make something work. However I'm at my wits end. I just cannot read the ADC on a 18f27Q10

I tried just using the standard ADC in (example 1)
Nothing I get 0

I'm sure I had this issue before and Les posted some code (example 2)
But again 0 as a result.

It works in the VSM but not on my board and not on the Dev board from John

Any help appreciated

'******************************************************************************
' Name      :  Untitled.bas                                                    *
' Author    :  Tim Box                                                         *
' Version   :  0.0.0.0                                                         *
' History   :                                                                  *
' Notes     :                                                                  *
'*******************************************************************************

   Device 18f27Q10

'-------------------------------------------------------------------------------
'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config_Start
    FEXTOSC = OFF              'Oscillator not enable
    RSTOSC = HFINTOSC_64MHZ    'HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
    CLKOUTEN = OFF             'CLKOUT function is disable
    CSWEN = ON                 'Writing to NOSC and NDIV is allowed
    FCMEN = ON                 'Fail-Safe Clock Monitor enable
    MCLRE = EXTMCLR            'MCLR pin (RE3) is MCL
    PWRTE = On                 'Power up timer is on
    LPBOREN = On               'Low power BOR is enabled
    BOREN = SBORDIS            'Brown-out Reset enabled , SBOREN bit is ignore
    BORV = VBOR_190            'Brown-out Reset Voltage (VBOR) set to 1.90
    ZCD = OFF                  'ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
    PPS1WAY = ON               'PPSLOCK bit can be cleared and set only once' PPS registers remain locked after one clear/set cycl
    STVREN = ON                'Stack full/underflow will cause Rese
    XINST = OFF                'Extended Instruction Set and Indexed Addressing Mode disable
    WDTCPS = WDTCPS_31         'Divider ratio 1:65536' software control of WDTP
    WDTE = OFF                 'WDT Disable
    WDTCWS = WDTCWS_7          'window always open (100%)' software control' keyed access not require
    WDTCCS = SC                'Software Contro
    WRT0 = OFF                 'Block 0 (000800-003FFFh) not write-protecte
    WRT1 = OFF                 'Block 1 (004000-007FFFh) not write-protecte
    WRT2 = OFF                 'Block 2 (008000-00BFFFh) not write-protecte
    WRT3 = OFF                 'Block 3 (00C000-00FFFFh) not write-protecte
    WRT4 = OFF                 'Block 4 (010000-013FFFh) not write-protecte
    WRT5 = OFF                 'Block 5 (014000-017FFFh) not write-protecte
    WRT6 = OFF                 'Block 6 (018000-01BFFFh) not write-protecte
    WRT7 = OFF                 'Block 7 (01C000-01FFFFh) not write-protecte
    WRTC = OFF                 'Configuration registers (300000-30000Bh) not write-protecte
    WRTB = OFF                 'Boot Block (000000-0007FFh) not write-protecte
    WRTD = OFF                 'Data EEPROM not write-protecte
    SCANE = ON                 'Scanner module is available for use, SCANMD bit can control the modul
    LVP = ON                   'Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignore
    CP = OFF                   'UserNVM code protection disable
    CPD = OFF                  'DataNVM code protection disable
    EBTR0 = OFF                'Block 0 (000800-003FFFh) not protected from table reads executed in other block
    EBTR1 = OFF                'Block 1 (004000-007FFFh) not protected from table reads executed in other block
    EBTR2 = OFF                'Block 2 (008000-00BFFFh) not protected from table reads executed in other block
    EBTR3 = OFF                'Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
    EBTR4 = OFF                'Block 4 (010000-013FFFh) not protected from table reads executed in other block
    EBTR5 = OFF                'Block 5 (014000-017FFFh) not protected from table reads executed in other block
    EBTR6 = OFF                'Block 6 (018000-01BFFFh) not protected from table reads executed in other block
    EBTR7 = OFF                'Block 7 (01C000-01FFFFh) not protected from table reads executed in other block
    EBTRB = OFF                'Boot Block (000000-0007FFh) not protected from table reads executed in other block
Config_End

'**** End of Fuse Configurator Settings ****
'-------------------------------------------------------------------------------
 '**** Compiler Defs *****

    Declare Create_Coff On


    Declare Xtal = 64                           ' Tell the compiler what frequency the device is operating at (in MHz)




    Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
    Declare ADin_Tad = FRC ' RC oscillator chosen for the ADC
    Declare ADin_Stime = 50 ' Allow 50us sample time

    Dim wADC_Result as Word ' Create a word variable to hold the ADC value

    ADCON0bits_ADFM = 1 ' Set the ADC to give a 10-bit result
    ANSELA = 0b00000001 ' Set analogue input on PORTA.0


Start:


    ADCON0bits_ADFM = 1


    Do


    wADC_Result = ADin 0 ' Place the A\D conversion into variable

    hrsoutln dec   wADC_Result

    delayms 500

    Loop




'******************************************************************************
' Name      :  Untitled.bas                                                    *
' Author    :  Tim Box                                                         *
' Version   :  0.0.0.0                                                         *
' History   :                                                                  *
' Notes     :                                                                  *
'*******************************************************************************

   Device 18f27Q10

'-------------------------------------------------------------------------------
'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config_Start
    FEXTOSC = OFF              'Oscillator not enable
    RSTOSC = HFINTOSC_64MHZ    'HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
    CLKOUTEN = OFF             'CLKOUT function is disable
    CSWEN = ON                 'Writing to NOSC and NDIV is allowed
    FCMEN = ON                 'Fail-Safe Clock Monitor enable
    MCLRE = EXTMCLR            'MCLR pin (RE3) is MCL
    PWRTE = On                 'Power up timer is on
    LPBOREN = On               'Low power BOR is enabled
    BOREN = SBORDIS            'Brown-out Reset enabled , SBOREN bit is ignore
    BORV = VBOR_190            'Brown-out Reset Voltage (VBOR) set to 1.90
    ZCD = OFF                  'ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
    PPS1WAY = ON               'PPSLOCK bit can be cleared and set only once' PPS registers remain locked after one clear/set cycl
    STVREN = ON                'Stack full/underflow will cause Rese
    XINST = OFF                'Extended Instruction Set and Indexed Addressing Mode disable
    WDTCPS = WDTCPS_31         'Divider ratio 1:65536' software control of WDTP
    WDTE = OFF                 'WDT Disable
    WDTCWS = WDTCWS_7          'window always open (100%)' software control' keyed access not require
    WDTCCS = SC                'Software Contro
    WRT0 = OFF                 'Block 0 (000800-003FFFh) not write-protecte
    WRT1 = OFF                 'Block 1 (004000-007FFFh) not write-protecte
    WRT2 = OFF                 'Block 2 (008000-00BFFFh) not write-protecte
    WRT3 = OFF                 'Block 3 (00C000-00FFFFh) not write-protecte
    WRT4 = OFF                 'Block 4 (010000-013FFFh) not write-protecte
    WRT5 = OFF                 'Block 5 (014000-017FFFh) not write-protecte
    WRT6 = OFF                 'Block 6 (018000-01BFFFh) not write-protecte
    WRT7 = OFF                 'Block 7 (01C000-01FFFFh) not write-protecte
    WRTC = OFF                 'Configuration registers (300000-30000Bh) not write-protecte
    WRTB = OFF                 'Boot Block (000000-0007FFh) not write-protecte
    WRTD = OFF                 'Data EEPROM not write-protecte
    SCANE = ON                 'Scanner module is available for use, SCANMD bit can control the modul
    LVP = ON                   'Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignore
    CP = OFF                   'UserNVM code protection disable
    CPD = OFF                  'DataNVM code protection disable
    EBTR0 = OFF                'Block 0 (000800-003FFFh) not protected from table reads executed in other block
    EBTR1 = OFF                'Block 1 (004000-007FFFh) not protected from table reads executed in other block
    EBTR2 = OFF                'Block 2 (008000-00BFFFh) not protected from table reads executed in other block
    EBTR3 = OFF                'Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
    EBTR4 = OFF                'Block 4 (010000-013FFFh) not protected from table reads executed in other block
    EBTR5 = OFF                'Block 5 (014000-017FFFh) not protected from table reads executed in other block
    EBTR6 = OFF                'Block 6 (018000-01BFFFh) not protected from table reads executed in other block
    EBTR7 = OFF                'Block 7 (01C000-01FFFFh) not protected from table reads executed in other block
    EBTRB = OFF                'Boot Block (000000-0007FFh) not protected from table reads executed in other block
Config_End

'**** End of Fuse Configurator Settings ****
'-------------------------------------------------------------------------------
 '**** Compiler Defs *****

    Declare Create_Coff On


    Declare Xtal = 64                           ' Tell the compiler what frequency the device is operating at (in MHz)




    Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
    Declare ADin_Tad = FRC ' RC oscillator chosen for the ADC
    Declare ADin_Stime = 50 ' Allow 50us sample time

    Dim wADC_Result as Word ' Create a word variable to hold the ADC value

    ADCON0bits_ADFM = 1 ' Set the ADC to give a 10-bit result
    ANSELA = 0b00000001 ' Set analogue input on PORTA.0


Start:
    ADCC_Init()

    Do


    wADC_Result = ADCC_Read( 0) ' Place the A\D conversion into variable

    hrsoutln dec   wADC_Result

    delayms 500

    Loop                                                       



'-----------------------------------------------------------------------------
' Reads the ADC and returns the result
' Input     : Channel
' Output    : Word
' Notes     :
'-----------------------------------------------------------------------------

    Proc ADCC_Read(pChan As Byte), Word
        Dim wADRES As ADRESL.Word

        ADPCH = pChan                                           ' Select the A/D Channel
        ADCON0bits_ADON = 1                                     ' Turn on the ADC module
        ADCON0bits_ADCONT = 0                                   ' Disable the continuous mode
        ADCON0bits_ADGO = 1                                     ' Start the conversion
        DelayCS 1
        While ADCON0bits_ADGO = 1 : Wend                        ' Wait for the conversion to finish

        Result = wADRES                                         ' Return the result
    EndProc




'*******************************************************************************
'* Title  :  ADCC_Init()
'* Input  :  None
'* Notes  :  Setup the ADC
'*        :
'*******************************************************************************
Proc ADCC_Init()
    ADLTHL  = $00
    ADLTHH  = $00

    ADUTHL  = $00
    ADUTHH  = $00

    ADSTPTL = $00
    ADSTPTH = $00

    ADRPT   = $00
    ADPCH   = $00

    ADCAP   = $00
    ADPRE   = $00
    ADCON1  = $00
    ADCON2  = $00
    ADCON3  = $00
    ADSTAT  = $00
    ADREF   = $00
    ADACT   = $00
    ADCLK   = $00           ' ADCS FOSC/2
    ADCON0  = $94           ' 10010100
    ADACQ   = $64           ' 01100100
EndProc

TimB

To answer my own question I missed a key line

TRISA.0 = 1 ' Set PORTA.0 as an input

Now it seems to work

CPR

#2
I think that's happened to all of us! If not once than a couple of times  8) (and it's vexing and infuriating)

TimB

In case its of use to others here is an improved version (Procs)

Also tris and ANSELA settings

    ' Ensure Pins are Inputs (0 = Output, 1 = Input)
    TRISA = TRISA | 0b00001111

    ' Ensure Pins are Analog (0 = Digital, 1 = Analog)
    ANSELA = 0b00001111


'-----------------------------------------------------------------------------
' Reads the ADC and returns the result
' Input     : Channel
' Output    : Word
' Notes     :
'-----------------------------------------------------------------------------

    Proc ADCC_Read(pChan As Byte), Word
        Dim wADRES As ADRESL.Word

        ADPCH = pChan                                           ' Select the A/D Channel
        ADCON0bits_ADON = 1                                     ' Turn on the ADC module
        ADCON0bits_ADCONT = 0                                   ' Disable the continuous mode
        ADCON0bits_ADGO = 1                                     ' Start the conversion
        DelayCS 1
        While ADCON0bits_ADGO = 1 : Wend                        ' Wait for the conversion to finish

        Result = wADRES                                         ' Return the result
    EndProc


Proc ADCC_Init()
    ' Set Clock to Fosc/128 (Slow and stable for 64MHz)
    ADCLK = $3F

    ' Disable Precharge entirely (This stops cross talk)
    ADPRE = $00

    ' Maximize Acquisition time (255 Tad cycles)
    ' This gives the pin plenty of time to pull the cap to 0V
    ADACQ = $FF

    ' Right Justified, No Precharge, No Discharge
    ADCON0 = $80

    ' Basic Mode (No math/filtering)
    ADCON2 = $00

    ' Vref+ = VDD (5V), Vref- = VSS (GND)
    ADREF = $00

    ' Turn ADC ON
    ADCON0 = $80

EndProc