Announcement

Collapse
No announcement yet.

Adc on 18f67k22 - an4

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adc on 18f67k22 - an4

    hi DARREL ,

    in the process of porting code to 18F67K22 and need some input on the ADC setup for AN4 input ,

    The code worked prior to porting to the chip so its goto be a register / setting not quite right , so if anyone can advise

    The adc setup of AN4 is shown , for what i think is correct for all the other registers that may effect the input pin.
    at the moment i get no reading at all

    the osc is set to 32mhz internal
    a Voltage divider resistor is on AN4 input

    AVDD = 3.3v
    AVSS = GND
    Regards

    Sheldon
    Code:
    HLVDCON = $00             'Turn off HIGH /LOW Voltage Detect Module
    T1CON  = %01110100     ' Timer 1 ,bit7-6 = TMR1 CLK source 00 = Instuction Clk(Fosc/4),01 =sys clk,10=ext clk,11=cap sense osc
                               ' bit5-4 = TMR1 prescale 11= 1:8 ,bit3=0 LP off,bit2=1 no sync ext CLK ,bit1= 0 n/a,Bit0=0 Timer 1 on/off
    
     T1GCON = %00000000     ' Timer 1 Gate Control bit7 0= counts regardless of gate 1 = gates in use  , bit6 gate active when 1= High  ,0=low
                               ' bit5 0= toggle mode diabled 1= enabled  bit4 single pulse mode 1- en 0= dis, bit 2 -status bit,1-0 - gate source sel
    
    T3GCON = $00           ' Timer 3 Gate  disable 
    
    
        DEFINE OSC 32             ' Timing referance for pause , pauseus commands
        DEFINE ADC_BITS 12        ' Number of bits in ADCIN result - Required for adcin command 
        DEFINE ADC_CLOCK 5        ' fosc/32 0.8uS @ 32Mhz
        DEFINE ADC_SAMPLEUS 20    ' ADC sampling time in microseconds - Required for adcin command 
      
    
    '  -------   Setup port A Variables  & directions  ----------
        ANCON0.0 = 0              ' Set PortA:0 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL0
        ANCON0.1 = 0              ' Set PortA:1 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL1
        ANCON0.2 = 0              ' Set PortA:2 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL2
        ANCON0.3 = 0              ' Set PortA:3 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL3
        ANCON0.4 = 1              ' Set PortA:5 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL4 Set RA5 (AN4/ ANSEL4) to be analog input for V-Mon 
            
        TRISA =  %00100000     ' setup Port A input=1,output=0 for I/O pins , set input for RA5  
        PORTA  = %00100000     ' Clear Outputs 
    
    ' ------------ADC Settings , routines and Varaiables ----------------
     ' note: ADCON0 - channel selection done in routines using the required channel   
     
      ADCON1 =  %00000000           ' bit 7-6 = TRIGSEL 11= triger from RTCC, 10 = trigger from timer1 ,01 = trigger from CTMU, 00= trigger from ECCP2
                                    ' Bit 5-4 - VCFG+ 11= Internal VREF+(4.096v) ,10= Internal VREF+ (2.048v) 01=External VREF+ ,00= AVDD
                                    ' Bit3 - VNCFG-  1= External Vref- ,  0= AVSS Vref/A ,
                                    ' Bit 2-0 = Analoge Neg Ch Select ( Differential measurement ) 
                                    ' 111 = AN6, 110 = AN5, 101 = AN4,100 = AN3,011 = AN2, 010 = AN1, 001 = AN0,000 = AVSS,
      
     
      ADCON2 =  %10101010           ' bit 7 = 1 ADFM ( ADRESH, ADRESL - right justify ) , Bit6 n/a , 
                                    ' Bits 5-3 ACQT ( acquision time ) 111 = 20TAD ,110=16TAD,101-12TAD,100=8TAD,011=6TAD,101=4TAD,001=2TAD,000=0TAD
                                    ' Bit 2-0 ADCS ( AD clock sel) x11 =FRC , 110=Fosc/64,101= FOSC/16 ,100=FOSC/4 ,010=FOSC/32 ,001=FOSC/8, 000=FOSC/2
                                    
      
      
      Value         Var Word        ' ADC varable for result
    '-------------------------------------------------------
     Get_volts :
      
          ADCON0 =  %00010001           ' bit 7 -N/A , Bit 6-2 = ADC chan Sel /Tempature output /FVR output = AN4 sel = 00100 ,
                                        ' Bit 1 - Go/_done status flag - 1 = Go do ADC / 0 = Done ADC , Bit 0 = Enable ADC = 1  / 0 = Disable ADC
     ADCIN 4 ,Value           ' Leave out for Rev 2 of 16F1947 - Read PortA.5 value into variable , this applies the fixed voltage ref (via ADCON0,ADCON1,FVRCON)
         write $10, Value.HighByte ' debug
         write$11, Value.LowByte   ' debug
    return

  • #2
    bad eye's...
    Dave Purola,
    N8NTA
    EN82fn

    Comment


    • #3
      Sheldon,

      It would be nice if you could create complete test programs to illustrate the problem.
      Pulling 20 lines out of a large program makes us guess at what else you are doing.
      And, the process of making those test programs usually finds the problem anyhow.

      And I understand that you are trying many things to get it to work.
      But when you add something that doesn't fix the problem ... remove it.
      Don't leave a bunch of things that didn't help in the program.
      You are changing all kinds of stuff that doesn't need to be changed.
      And the code can't be run on a chip. As is ... the PIC continually resets on a stack underflow.

      For instance, here is a complete program that can actually be compiled and run.
      This should be all you need to read AN4 and display it.
      Code:
      ;----[18F67K22 Hardware Configuration]------------------------------------------
      #CONFIG
        CONFIG  RETEN = ON       ; Enabled
        CONFIG  INTOSCSEL = HIGH ; LF-INTOSC in High-power mode during Sleep
        CONFIG  SOSCSEL = HIGH   ; High Power SOSC circuit selected
        CONFIG  XINST = OFF      ; Disabled
        CONFIG  FOSC = INTIO2    ; Internal RC oscillator
        CONFIG  PLLCFG = ON      ; Enabled
        CONFIG  FCMEN = OFF      ; Disabled
        CONFIG  IESO = OFF       ; Disabled
        CONFIG  PWRTEN = OFF     ; Disabled
        CONFIG  BOREN = SBORDIS  ; Enabled in hardware, SBOREN disabled
        CONFIG  BORV = 3         ; 1.8V
        CONFIG  BORPWR = ZPBORMV ; ZPBORMV instead of BORMV is selected
        CONFIG  WDTEN = SWDTDIS  ; WDT enabled in hardware; SWDTEN bit disabled
        CONFIG  WDTPS = 512      ; 1:512
        CONFIG  RTCOSC = SOSCREF ; RTCC uses SOSC
        CONFIG  CCP2MX = PORTC   ; RC1
        CONFIG  MSSPMSK = MSK7   ; 7 Bit address masking mode
        CONFIG  MCLRE = ON       ; MCLR Enabled, RG5 Disabled
        CONFIG  STVREN = ON      ; Enabled
        CONFIG  BBSIZ = BB1K     ; 1K word Boot Block size
      #ENDCONFIG
      
      DEFINE OSC 32
      DEFINE ADC_BITS 12
      DEFINE HSER_BAUD 9600
      
      ADval   VAR WORD
      
      OSCCON = %01100000         ; 8Mhz
      OSCTUNE.6 = 1              ; Enable x4 PLL
      ADCON2.7 = 1               ; Right Justify
      
      Main:
          ADCIN 4, ADval
          HSEROUT ["A/D=",DEC ADval,"   ",13,10]
          PAUSE 500
      GOTO Main


      The program works fine in the simulator. I will have access to a real 67K22 tomorrow.
      PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
      Never download a PIC datasheet from anywhere but microchip.com

      Comment


      • #4
        thanks darrel ,

        your right , i was just taking adc part out of large program and not gone and make it separate most simple program to sort , with a new cpu

        believe it or not this is the first time on the forum that any sample config was put up for the 18f67k22 , 18f87k22

        thought id still need to set TRESA input , adc clock ,Tad setting , adcon1 , Avss , Avdd , Vref , etc before any meaning full input would be produced

        was surprised to see that the ADCin command set the TRESA to input

        but that sample compiles and gives a result on the hardware ( 214dec) for a 2.9v applied voltage to adc4


        the reading for the same ADC voltage using the 16f1947 was about 950dec , so other settings are need to to get the correct value , but which ones ill find out as i play with the test program.

        also if you are getting the hardware there was new errata posted a last week 10/1/14 on this chip family , relating to the adc error offset which is need to be allowed for if your VDD is above 3v3,

        something which id like to see the code to over come when you goto ground the AD input to get the offset reading then then apply the voltage,

        it appears that any offset value would be the same for any ADC channel used

        would need to be a calibration arrangement test on setting up an unused adc pin , grounding it , and then apply the offset value found for every other channel in used in the program , am i correct ?


        be interested to see what silicon revision you get as i just ordered 10 or so of these for further testing

        also the hardware Vreg needs an input before you get any thing in the real world to work

        cheers

        sheldon

        Comment


        • #5
          Well, I have to disagree with several statements there, but I'll get to those later.

          You previously said that you had AVDD=3.3V, AVSS=GND.
          So I just assumed that VDD was 3.3V too.

          But from your last post, I'm getting the feeling that your VDD is not the same as AVDD, and you're actually running the chip from 5V.

          Say it ain't so!
          PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
          Never download a PIC datasheet from anywhere but microchip.com

          Comment


          • #6
            nope definaly 3.3v AVDD , 0v on Avss,

            found the problem in that i had set the PMD0.0 to disable the adc in the run idle mode - ref section 4.4 of the datasheet , this chip has some nice power features ( if you set them up /use them well)

            currently i am getting a reading of 2623dec for 2.9v to AN4, so just looking at why its so differ reading to the 16f1947

            looking at the errata section 1.2 is not an issue if using 10 bit ADC , but ill do the offset reading of other AD channel that is grounded just , to see what it is

            cheers

            Sheldon

            Comment


            • #7
              Hi darrel , for your info i am seeing a ADC offset error of +20 at 30deg

              i have 100R to ground on a spare ad channel

              thanks for you help on this one

              hope you can emulate the user ID issue of programming the serial serial number to this chip using the melabs U2 prog , when u get the hardware

              cheers

              sheldon

              Comment


              • #8
                Originally posted by Darrel Taylor View Post
                But from your last post, I'm getting the feeling that your VDD is not the same as AVDD, and you're actually running the chip from 5V.
                Say it ain't so!

                Originally posted by longpole001 View Post
                nope definaly 3.3v AVDD , 0v on Avss,
                I was referring to VDD.
                What voltage is your VDD? 3.3V or 5V?

                found the problem in that i had set the PMD0.0 to disable the adc in the run idle mode ...
                That's a good example of why you don't go around setting all the registers for no reason.
                And why you should remove things from your program that you were just "trying".

                currently i am getting a reading of 2623dec for 2.9v to AN4
                Well, I can't duplicate that result.
                But I can say that the 67K22 has the crappiest A/D converter I've ever seen.
                It goes way past what the errata sheet says.

                Originally posted by longpole001 View Post
                Hi darrel , for your info i am seeing a ADC offset error of +20 at 30deg
                I'm only seeing an offset of 5.
                But that, or your 20 are insignificant compared to the measurement errors.

                In keeping with your example ... my VDD/AVDD is 3.33V, and I'm applying 2.90V to AN4.
                The 12-bit A/D value should be ... 2.90/(3.33/4095) = 3566

                According to the datasheet, the correct ADC_CLOCK is 2 = FOSC/32.
                With this clock, the readings range from 3009 to 3032, it's bouncing in a range of 23 counts, and is off by -540 from the expected 3566.

                PBP always defaults to ADC_CLOCK 3, which is the FRC clock.
                Using this clock, the readings range from 3639 to 3776, it's bouncing in a range of 137 counts, and is off by +140.
                This actually gives the best measurements. If you can call it that.

                The datasheet says not to use the FRC clock unless the measurement is taken in SLEEP mode.
                Using FRC in SLEEP mode, the readings range from 2863 to 2898, it's bouncing 35 counts, but it's off by a whopping -686.
                That's worse than any of the other modes.

                You had ADC_CLOCK 5 in your original code which is FOSC/16, this clock is too fast for 32Mhz OSC, but I ran it for comparison ...
                With this clock, the readings range from 3159 to 3200, a bounce of 41, and it's off by -386.
                Doesn't seem any worse than the other crappy modes.

                No hardware changes were made between the above readings. Only the ADC_CLOCK and SLEEP mode were changed.
                This is the test setup.
                Solid power supply ... well bypassed ... 10K pot with 1uF cap.



                My take on these results is ... if you need Analog measurements, don't use the 18F67K22.
                PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
                Never download a PIC datasheet from anywhere but microchip.com

                Comment


                • #9
                  the chip is connected to 3.3v,

                  the adc results have been all over the place as per your testing , i am fortunate that the voltage reading i need is not need to be too accurate , but this is of big concern

                  thanks for confirming that the errase of user ID is an issue , hopefully a updated firmware / software is not far away


                  cheers

                  Sheldon

                  Comment


                  • #10
                    for me i would like to use the 16f1947 , silicon rev5 , but we already know that pbp3.07 is not allow port F,G to work

                    lost weeks on this problem oh well , a learning curve i guess

                    Comment


                    • #11
                      correction to darrel calculation for those reviewing

                      In keeping with your example ... my VDD/AVDD is 3.33V, and I'm applying 2.90V to AN4.
                      The 12-bit A/D value should be ... 2.90/(3.33/4095) = 3566
                      The 12-bit A/D value should be ... 2.90 x (4095/3.33) = 3566

                      Comment


                      • #12
                        also i found changing the TAD acquisition time from 000 to 4TAd gave a better result ,on more regular basis
                        Code:
                        ADCON2 =  %10010010           ' bit 7 = 1 ADFM ( ADRESH, ADRESL - right justify ) , Bit6 n/a , 
                                                        ' Bits 5-3 ACQT ( acquision time ) 111 = 20TAD ,110=16TAD,101-12TAD,100=8TAD,011=6TAD,010=4TAD,001=2TAD,000=0TAD
                                                        ' Bit 2-0 ADCS ( AD clock sel) x11 =FRC , 110=Fosc/64,101= FOSC/16 ,100=FOSC/4 ,010=FOSC/32 ,001=FOSC/8, 000=FOSC/2
                                                        ' Note:  define ADCIN clock  and define ADcin sample overide this setting

                        Comment


                        • #13
                          Originally posted by longpole001
                          correction to darrel calculation for those reviewing
                          The 12-bit A/D value should be ... 2.90 x (4095/3.33) = 3566
                          Let me guess ...

                          You skipped algebra classes when you were in school?
                          PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
                          Never download a PIC datasheet from anywhere but microchip.com

                          Comment


                          • #14
                            yep ,must have , lol ,

                            your calc is correct , just the way i have i have done it in the past

                            thanks for your help on this one ,

                            i still along way to go i agree

                            Comment

                            Working...
                            X