PIC16F18446 - 12 bits ADC gives 16 bits results!!!!


+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891

    Default PIC16F18446 - 12 bits ADC gives 16 bits results!!!!

    Still going ahead with the PIC16F18446...

    I'm testing the 12 bits ADC.

    First, I had to find out the correspondance between Pin and Channel (I'm using a 20-pin device). Here it is:
    Name:  small.jpg
Views: 43
Size:  64.3 KB


    The code I use is here:
    Code:
    ' PIC 16F18446
    ' ====== FUSES =====================================================================================
    #CONFIG
        __config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT1 & _CLKOUTEN_OFF & _CSWEN_ON & _FCMEN_ON
        __config _CONFIG2, _MCLRE_ON & _PWRTS_PWRT_64 & _LPBOREN_OFF & _BOREN_SBOREN & _BORV_LO & _ZCDDIS_OFF & _PPS1WAY_OFF & _STVREN_ON
        __config _CONFIG3, _WDTCPS_WDTCPS_31 & _WDTE_SWDTEN & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
        __config _CONFIG4, _BBSIZE_BB512 & _BBEN_OFF & _SAFEN_OFF & _WRTAPP_OFF & _WRTB_OFF & _WRTC_OFF & _WRTD_OFF & _WRTSAF_OFF & _LVP_OFF
        __config _CONFIG5, _CP_OFF
    #ENDCONFIG
    
    ' ====== REGISTERS =================================================================================
    ' Oscillator Control Register1
    OSCCON1 = %01100000 ' $60
    
    ' HFFRQ - Oscillator's Frequency Selection Register
    OSCFRQ  = %00000010 ' 4MHz
    
    ' ====== DEFINES ===================================================================================
    DEFINE ADC_BITS 12   'Number of bits in ADCIN result
    
    ' ====== VARIABLES =================================================================================
    ADC_Value   var word
    Serial_Out  VAR PORTB.7
    Mode        Var BYTE
    
    ' ====== INITIALIZE VARIABLES ======================================================================
    Mode        = 84 ' serial com BdRate settings
    
    ' ====== TEST PROGRAM ==============================================================================
    TEST:
        ADCIN 23, ADC_Value ' ADC channel 23 is RC7
        Serout2 Serial_Out,Mode,["ADC in: ", dEC ADC_Value,13,10]
        PAUSE 500
        GOTO TEST
    END

    Now, the strange thing is: where I expect a 12 bits result (0..4095), I get a 16 bits value 0..65520 as result and it's amazingly steady, not a digit variation!!!

    I'm using a 10 turns 10kOhms pot and that's all.

    How can that be please?
    Last edited by flotulopex; - 24th February 2024 at 19:33.
    Roger

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: PIC16F18446 - 12 bits ADC gives 16 bits results!!!!

    Your code does not set the ADCON0 register properly to right justify the result. The default is Left justification, hence the 65520, or FFF0 or 1111 1111 1111 0000 result, which seems as 12 bit just fine, only left justified.

    You need to set FRM bit to 1. It is the bit 2 in the ADCON0 register. Look at the page 536 of the datasheet. There are many other settings regarding ADC converter if you need other features.

    I bet that your first reading after 0 was 16 then 32, then 48 etc instead of 1,2,3

    Ioannis
    Last edited by Ioannis; - 24th February 2024 at 22:50.

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default PIC16F18446 - 12 bits ADC gives 16 bits results!!!!

    Hum....its now obvious that I didn't read the DS first

    Thanks a lot for your time and patience for dreamers like me.

    Okay, let's go to page 536 then....


    PS: I forgot to mention that your information makes my PIC show the "correct" values now
    Last edited by flotulopex; - 25th February 2024 at 09:24.
    Roger

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: PIC16F18446 - 12 bits ADC gives 16 bits results!!!!

    Glad you got it working.

    Ioannis

Similar Threads

  1. Parity & Stop Bits settings and results
    By svjohngalt in forum Serial
    Replies: 4
    Last Post: - 27th January 2018, 21:16
  2. Replies: 2
    Last Post: - 23rd April 2013, 17:34
  3. Incorrect adc channel bits generated
    By grahamg in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th February 2010, 19:56
  4. DT_INTS-14 /interrupts enable bits / flag bits
    By bogdan in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 16th May 2009, 19:42
  5. parsing adc word into bits
    By GREYBIRDMAN in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 13th April 2007, 22:08

Members who have read this thread : 9

You do not have permission to view the list of names.

Posting Permissions

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