PIC 18F47K40 ADC - HELP Please?


+ Reply to Thread
Results 1 to 8 of 8

Thread: PIC 18F47K40 ADC - HELP Please?22 days old

  1. #1
    Member craig's Avatar
    Join Date
    Dec 2010
    Posts
    209
    Thumbs Up
    Received: 10
    Given: 0
    Total Downloaded
    1.52 GB

    0 Not allowed!

    Default PIC 18F47K40 ADC - HELP Please?

    Hello I am trying to get the ADC to Work on a 18F47K40 device but the Resolution Remains LOW.
    I have a Standard 10K POT Connected from the Pic's +5V Rail and the the other leg to - ground and the wiper of the POT going to RA5(ANA5).
    If I measure the Pot with a multi meter I get basically the full range from 0Volts to 5Vdc. The Readings on the ADC are out they go to a
    Maximum of 750 Steps and not 1023 through the whole range. I have tried everything I can imagine but, to No avail !
    Can someone Please explain to me where I Can look to get the ADC to Work Correctly?

    Compiler Version 3.7.3.6 Running Windows 7 x64.
    Config_Start
    FEXTOSC = HS ;HS (crystal oscillator) above 8 MHz; PFM set to high power
    RSTOSC = EXTOSC ;EXTOSC operating per FEXTOSC bits (device manufacturing default)
    Declare Xtal 20

    TRISA = %00111011
    ANSELA = %00101011 ' Analogue ANA0,ANA1,ANA3,ANA5 Rest Digital
    ADREF = %00000011 ' VRef = Vss & Vdd
    FVRCON = %11000010 ' Fixed Voltage Reference (Gain 1x ,1.024v)
    ADCON0 = %10010100

    Declare Adin_Stime = 50 ' Sample time of A/D Convertor 50 ms
    Declare Adin_Res = 10 ' 10-bit result required
    Declare Adin_Tad = FRC ' RC oscillator chosen

    Dim ADchold As Word

    adc:
    ADchold = ADIn 5 ' Load RA5/ANA5(AdIn 5) data into ADchold Variable
    Print At 2,1, Dec ADchold," "
    return

    __________________________________________________ ___________________________

    Any help is Very Much appreciated!
    Kind Regards
    Craig

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Member John Lawton's Avatar
    Join Date
    Feb 2003
    Posts
    276
    Thumbs Up
    Received: 5
    Given: 16
    Total Downloaded
    1.35 GB

    0 Not allowed!

    Default Re: PIC 18F47K40 ADC - HELP Please?

    Hi Craig,
    I'm not sure that you've set ADREF correctly if you want to use Vss and Vdd as reference voltages. Reading the datasheet it looks like ADREF should be all 0.
    Good luck!
    John Lawton

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,857
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default Re: PIC 18F47K40 ADC - HELP Please?

    Hi Craig,
    A couple of things.
    Using PICconfigurator is always a good way to make sure your fuses are set correctly.
    Secondly, it might be a typo but you say you set the sample time at 50ms. That's way too long and may be the cause of the low readings.
    Cheers
    John

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Member craig's Avatar
    Join Date
    Dec 2010
    Posts
    209
    Thumbs Up
    Received: 10
    Given: 0
    Total Downloaded
    1.52 GB

    0 Not allowed!

    Default Re: PIC 18F47K40 ADC - HELP Please?

    Hi John's
    Thanks for the feedback, I tried many Different scenarios with setting up the ADC but, the Resolution Remains Low +/-750.
    I did set ADREF = %00000000 and change the Sample time down to 1uS - 10uS but the results stay more or less the same.
    I always setup the Fuses with Pic Fuse Configurator but, I only put in the top Two lines to keep the thread short.
    This is an Existing PCB which does work which I have upgraded from a 18F4620 to the 18F47K40 So everything else is working
    but, The ADC remains an issue. I have tried using a Include file which LES did on the 18F24K40 but that gave a lower resolution.
    http://www.protonbasic.co.uk/showthr...e.-Please-help.

    I will post the Full configs below:

    Device = 18F47K40

    Config_Start
    FEXTOSC = HS ;HS (crystal oscillator) above 8 MHz; PFM set to high power
    RSTOSC = EXTOSC ;EXTOSC operating per FEXTOSC bits (device manufacturing default)
    CLKOUTEN = OFF ;CLKOUT function is disabled
    CSWEN = On ;Writing to NOSC and NDIV is allowed
    FCMEN = OFF ;Fail-Safe Clock Monitor disabled
    MCLRE = INTMCLR ;If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR
    PWRTE = OFF ;Power up timer disabled
    LPBOREN = OFF ;ULPBOR disabled
    BOREN = OFF ;Brown-out Reset disabled
    BORV = VBOR_2P45 ;Brown-out Reset Voltage (VBOR) set to 2.45V
    ZCD = OFF ;ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    PPS1WAY = On ;PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle
    STVREN = On ;Stack full/underflow will cause Reset
    Debug = OFF ;Background debugger disabled
    XINST = OFF ;Extended Instruction Set and Indexed Addressing Mode disabled
    WDTCPS = WDTCPS_31 ;Divider ratio 1:65536; software control of WDTPS
    WDTE = OFF ;WDT Disabled
    WDTCWS = WDTCWS_7 ;window always open (100%); software control; keyed access not required
    WDTCCS = SC ;Software Control
    WRT0 = OFF ;Block 0 (000800-003FFFh) not write-protected
    WRT1 = OFF ;Block 1 (004000-007FFFh) not write-protected
    WRT2 = OFF ;Block 2 (008000-00BFFFh) not write-protected
    WRT3 = OFF ;Block 3 (00C000-00FFFFh) not write-protected
    WRT4 = OFF ;Block 4 (010000-013FFFh) not write-protected
    WRT5 = OFF ;Block 5 (014000-017FFFh) not write-protected
    WRT6 = OFF ;Block 6 (018000-01BFFFh) not write-protected
    WRT7 = OFF ;Block 7 (01C000-01FFFFh) not write-protected
    WRTC = OFF ;Configuration registers (300000-30000Bh) not write-protected
    WRTB = OFF ;Boot Block (000000-0007FFh) not write-protected
    WRTD = OFF ;Data EEPROM not write-protected
    SCANE = On ;Scanner module is available for use, SCANMD bit can control the module
    LVP = On ;Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored
    Cp = OFF ;UserNVM code protection disabled
    CPD = OFF ;DataNVM code protection disabled
    EBTR0 = OFF ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
    EBTR1 = OFF ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
    EBTR2 = OFF ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
    EBTR3 = OFF ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
    EBTR4 = OFF ;Block 4 (010000-013FFFh) not protected from table reads executed in other blocks
    EBTR5 = OFF ;Block 5 (014000-017FFFh) not protected from table reads executed in other blocks
    EBTR6 = OFF ;Block 6 (018000-01BFFFh) not protected from table reads executed in other blocks
    EBTR7 = OFF ;Block 7 (01C000-01FFFFh) not protected from table reads executed in other blocks
    EBTRB = OFF ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
    Config_End

    ;**** End of Fuse Configurator Settings ****
    ;-------------------------------------------------------------------------------

    Declare Xtal 20 ' 20Mhz External Xtal Used

    PORTA = 0 ' PORTA Outputs Set To LOW
    TRISA = %00111011 ' PORTA 2,6,7 Outputs, 0,1,3,4,5 Inputs
    Declare Dead_Code_Remove=On
    Declare Warnings=off
    Declare Reminders=off

    ANSELA = %00101011 ' Analogue ANA0,ANA1,ANA3,ANA5 Rest Digital
    ADREF = %00000000 ' VRef = Vss & Vdd
    FVRCON = %11000010 ' Fixed Voltage Reference
    ADCON0 = %10010100

    Declare Adin_Stime = 1 '50 ' Sample time of A/D Convertor 50 ms
    Declare Adin_Res = 10 ' 10-bit result required
    Declare Adin_Tad = FRC ' RC oscillator chosen

    adc:
    ADPCH = %00000101
    ADchold = ADIn 5 ' Load RA5/ANA5(AdIn 5) data into ADchold Variable
    Print At 2,1, Dec ADchold," "
    return

    __________________________________________________ ____________________
    Any help is Very Much appreciated!
    Kind Regards
    Craig

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  9. #5
    Prolific Poster
    Join Date
    Mar 2012
    Posts
    2,308
    Thumbs Up
    Received: 80
    Given: 182
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: PIC 18F47K40 ADC - HELP Please?

    Not quite what you're looking for but it may help. A proc that reads Vcc on 18F27K40. I've forgotten the source of the comments but I suspect from the errata.

    Code:
    '*******************************************************************************
    ' SUBROUTINE: TestADC
    ' PURPOSE   : GetADC from A0
    ' REQUIRES  : nothing
    ' RETURNS   : a 12 bit value
    ' DESTROYS  : nothing
    '*******************************************************************************
    
    'ADCON0bits.FM = 1;           //right justify                         ensur that ADC is enabled in PMD2
    'ADCON0bits.CS = 1;           //FRC Clock
    'ADPCH = 0x00;                //RA0 is Analog channel
    'TRISAbits.TRISA0 = 1;        //Set RA0 to input
    'ANSELAbits.ANSELA0 = 1;      //Set RA0 to analog
    'ADCON0bits.ON = 1;           //Turn ADC On
    'while (1) {
    'ADCON0bits.GO = 1;           //Start conversion
    'while (ADCON0bits.GO);       //Wait for conversion done
    'resultHigh = ADRESH;         //Read result
    'resultLow
    
    ' 3.1 ADC Conversion in FOSC Mode
    '     The ADCON0.GO bit remains set and the conversion
    '     does not complete successfully when
    '     configured to operate in FOSC mode
    '     (ADCON0.CS=0) with non-zero clock divider
    '     (ADCLK register).
    
    ' Work around
    ' a) Use FOSC as the clock source (ADCON0.CS=0)
    '    and set the clock divider (ADCLK register) to
    '    zero. Ensure that the FOSC frequency does not
    '    violate timing requirements for the ADC.
    ' b) Use ADCRC as the clock source
    '    (ADCON0.CS=1).
    
    TestADC:
      ADCMD = 0
      TRISA  = %11101111         ' A4=LED A0=ADC test
      ADPCH  = %00000000         ' RA0 is Analog channel
      ANSELA = %00000001         ' Select channel AN0
      ADCON0 = %10010100         ' ADC on, Right justify, FRC oscillator
    
      DelayMS 100                ' Wait for sample/hold capacitors to charge
      GO_DONE = 1                ' Start conversion
      While GO_DONE = 1 : Wend   ' Poll the GO_DONE flag for completion of conversion
      ResultHi = ADRESH
      ResultLo = ADRESL
    
      Inc nCounter
      Return
    George.

  10. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  11. #6
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,513
    Thumbs Up
    Received: 318
    Given: 152
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: PIC 18F47K40 ADC - HELP Please?

    The 18FxxK40 devices have a dreadufully over complex ADC that the compiler's ADin command cannot control fully or automatically because there are so many iterations of the ADC's operation. The ADin command, essentially, sets the channel to read and takes a reading.

    I always create a procedure to manipulate all the ADC's SFRs at the beginning of the program on the newer devices, so ADin can take correct readings. For example, on the 18FxxK40 devices to read a standard 10-bit ADC, the procedure I use is:

    Code:
    '------------------------------------------------------------------------------
    ' Setup the ADC peripheral
    ' Input     : None
    ' Output    : None
    ' Notes     : Set for 10-bit operation
    '
    Proc ADC_Setup()
        ADLTHL  = %00000000
        ADLTHH  = %00000000
        ADUTHL  = %00000000
        ADUTHH  = %00000000
        ADSTPTL = %00000000
        ADSTPTH = %00000000
        ADRPT   = %00000000
        ADPCH   = %00000000
        ADCAP   = %00000000
        ADCON0  = %10000100         ' Right justify for 10-bit operation, FRCADGO stop, ADON enabled, ADCONT disabled  , ADCS is ADCLK
        ADCON1  = %00000000         ' ADDSEN disabled, ADGPOL digital_low, ADIPEN disabled, ADPPOL VSS
        ADCON2  = %00000000         ' ADCRS 0, ADMD Basic_mode, ADACLR disabled, ADPSIS ADRES
        ADCON3  = %00000000         ' ADCALC First derivative of Single measurement, ADTMD disabled, ADSOI ADGO not cleared
        ADSTAT  = %00000000         ' ADAOV ACC or ADERR not Overflowed
        ADREF   = %00000000         ' ADNREF VSS, ADPREF VDD
        ADACT   = %00000000         ' ADACT disabled
        ADCLK   = %00000100         ' FOSC/16 (FOSC / (2 * (n + 1)))
        ADACQ   = %00000000
    EndProc
    This also makes the code easier to manipulate and read.
    Last edited by top204; 7th March 2020 at 10:35.

  12. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  13. #7
    Member craig's Avatar
    Join Date
    Dec 2010
    Posts
    209
    Thumbs Up
    Received: 10
    Given: 0
    Total Downloaded
    1.52 GB

    1 Not allowed!

    Default Re: PIC 18F47K40 ADC - HELP Please?

    Thanks Very Much Les and George for all the help, I only got back to this project now,hence the delay!
    I have the ADC Working with the Procs, they make life a lot simpler but, I still have to do a bit more testing
    on the ADC.

    Thanks Again
    Kind Regards

    Craig

  14. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  15. #8
    Member craig's Avatar
    Join Date
    Dec 2010
    Posts
    209
    Thumbs Up
    Received: 10
    Given: 0
    Total Downloaded
    1.52 GB

    0 Not allowed!

    Default Re: PIC 18F47K40 ADC - HELP Please?

    For others who battle with this a solution which works perfectly and gives the correct resolution stepping from (0 - 1023).
    I Did Not use the ADIN Command as I was experiencing problems with the Resolution Going Up to to 65000 Steps (200 - 65000).
    The following Example works Perfectly Thanks again to the Guys on the forum for their help.

    TRISA = 111011 ' PORTA 2,6,7 Outputs, 0,1,3,4,5 Inputs
    WPUB = 000000 ' Switch Off Weak Pullups on PORTB
    ANSELA = 110000 ' Select Analogue Channel ANA4 & ANA5

    'Use Les's Proc at the top of the Program:
    Proc ADC_Setup()
    ADLTHL = 000000
    ADLTHH = 000000
    ADUTHL = 000000
    ADUTHH = 000000
    ADSTPTL = 000000
    ADSTPTH = 000000
    ADRPT = 000000
    ADCAP = 000000
    ADCON0 = 010100 ' ADC on, Right justify, FRC oscillator
    ADCON1 = 000000 ' ADDSEN disabled, ADGPOL digital_low, ADIPEN disabled, ADPPOL VSS
    ADCON2 = 000000 ' ADCRS 0, ADMD Basic_mode, ADACLR disabled, ADPSIS ADRES
    ADCON3 = 000000 ' ADCALC First derivative of Single measurement, ADTMD disabled, ADSOI ADGO not cleared
    ADSTAT = 000000 ' ADAOV ACC or ADERR not Overflowed
    ADREF = 000000 ' ADNREF VSS, ADPREF VDD
    ADACT = 000000 ' ADACT disabled
    ADCLK = 1 ' FRC
    ADACQ = 000000
    EndProc

    goto adc

    adc:
    PMD2.5 = 0 ' Enable ADC - "ADCMD = 0"
    ADPCH = 000011 ' RA3/ANA3 - Battery Voltage Level Monitoring
    ANSELA = 001000 ' Analogue ANA3 - Select channel AN3

    DelayMS 100 ' Wait for sample/hold capacitors to charge
    Set ADCON0bits_GO_DONE ' Start a conversion
    DelayCS 1 ' Delay for 1 cycle after setting the Go_Done Bit
    While ADCON0bits_GO_DONE = 1 : Wend ' Wait for a conversion to complete
    ADchold.LowByte = ADRESL
    ADchold.HighByte = ADRESH

    Print At 1,2, Dec ADchold," "
    Return
    Last edited by craig; 20th March 2020 at 07:47.

  16. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. ADC Help on Pic 24 Please?
    By craig in forum The Lounge
    Replies: 5
    Last Post: 4th March 2019, 09:36
  2. 18F47K40 Port B issue
    By geoff_c in forum Proton Plus Compiler v3
    Replies: 17
    Last Post: 24th January 2019, 06:35
  3. Measuring 72Vdc with a pic adc?
    By GAMBOA in forum The Lounge
    Replies: 8
    Last Post: 17th September 2012, 06:00
  4. 12 bit ADC high clock speed PIC(R)?
    By charliecoultas in forum Proton Plus Compiler v3
    Replies: 26
    Last Post: 1st October 2011, 10:26
  5. cheapest pic with adc
    By ADLIN SYSTEMS in forum The Lounge
    Replies: 4
    Last Post: 26th March 2010, 03:59

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts