Menu

Pic16f877a, setting voltage reference

Help
fisyon
2016-06-01
2016-06-03
  • fisyon

    fisyon - 2016-06-01

    Hi,
    I need to use voltage reference input (just Vref+, not Vref- ).
    As i understand the chips pdf the register (ADCON1) must be set as "00000001" for all the analog inputs and Vref+ enabled.
    how can i do this in the GCB?

    is it ok to use this line:
    set adcon1=1 'eg: adcon1=5 for 00000101
    or
    set adcon1=b´00000001´

    i have another silly question:
    which quotation mark must be used to write b´00000001´ ?
    i have at least 3 ( ' ` ´ ) of them which i know and use only one for comment lines ( ' )
    btw i am using GCB version of 27/02/2016 with a turkish keyboard (https://en.wikipedia.org/wiki/QWERTY#Turkish_.28Q-keyboard.29)

     
  • kent_twt4

    kent_twt4 - 2016-06-01

    What I did was copy the adc.h include file and renamed to my project "xxxxadc.h" In my particular case I used RVA2D.h

    Then I dug down to where the 16f877A is affected in the initialization, and changed there. With the newest GreatCowBasic folder/include/lowlevel:

    macro LLReadAD (ADLeftAdjust)
    
        #IFDEF PIC
    
            'Set up A/D
            'Make necessary ports analog
            'Code for PICs with older A/D (No ANSEL register)
            #IFDEF NoVar(ANSEL)
                #IFDEF NoVar(ANSEL0)
                    #IFDEF NoVar(ANSELA)
                        #IFDEF NoVar(ANSELB)
    
                            #IFDEF NoBit(PCFG4)
                                #IFDEF NoVar(ADCON2)
                                    #IFDEF NoBit(ANS0)
                                        'Example: 16F877A
                                        #IFDEF Bit(PCFG3)
                                            SET PCFG3 OFF
                                        #ENDIF
                                        SET PCFG2 OFF
                                        SET PCFG1 OFF
                                        SET PCFG0 OFF
    

    So to set up with two available a2d's (AN0, AN1) and Vref+ in ADCON1, then do like you say, and comment out the old code and insert the new like:

                    ;#IFDEF Bit(PCFG3)
                    ;   SET ADCON1.PCFG3 OFF
                    ;#ENDIF
                    ;SET ADCON1.PCFG2 OFF
                    ;SET ADCON1.PCFG1 OFF
                    ;SET ADCON1.PCFG0 OFF
                    '16f877a setup
                    #IFDEF Bit(PCFG3)
                        SET ADCON1.PCFG3 OFF
                    #Endif
                    SET ADCON1.PCFG2 ON
                    SET ADCON1.PCFG1 OFF
                    SET ADCON1.PCFG0 ON
    

    Now you should be good to go with the Vref+. Be sure to add the include file during initialization like:

    #include <RVA2D.h>
    

    I don't know specifically how the bin numbers are parsed, but the straight apostrophe is what I use like b'00001111'.

     
  • kent_twt4

    kent_twt4 - 2016-06-01

    Not quite done yet. In order to not confuse the include a-d.h file, the functions "ReadAD(ANx)" and "ReadAD10(ANx)" must be renamed in the new RVA2D.h include file, and referenced in the main code. So for instance; the ReadAD10(ADReadPort) function name could be RVAD10(ADReadPort) and would be called in Main like:

    Volts = RVAD10(AN0)
    
     
  • fisyon

    fisyon - 2016-06-02

    thank you for your detailed answer.

    i am trying to understand where to insert/replace these lines, especially the lines beginning with ";"

    if i understand correctly from the microchips pdf file (page 128), for my needs (all 7 analog pins for analog input and Vref+ enabled) PCFG bits must be "0001".
    so in your example, this line should be:
    ...
    SET ADCON1.PCFG2 OFF
    ...
    right?

    however, i wish to set for Vref simpler...

    have a nice day

     
  • kent_twt4

    kent_twt4 - 2016-06-02

    The code with ";" is just commenting out the old code that is being replaced. I find it useful sometimes to leave the original code in to form a reference point in case I need to go back to it if the new code does not work.

    I wish their was an easy way to get the Vref+ to work for you. The a-d.h file is kind of hard to follow, and the changes could be done to just that file. Lets work on that then. Here is something to try. Lets start by making a define for the 16f877A Vref+ and then testing that define if it is true or not, and if true set the PFG <0-3> bits to "0001" enabling all the a-d pins and Vref+ on RA3.

    In main program initialization:

    #define Vref_plus
    

    Open a-d.h and copy and paste changes to the 16f877A code below and then "save" file.

                                        'Example: 16F877A
                                        #IFDEF Bit(PCFG3)
                                            SET PCFG3 OFF
                                        #ENDIF
                                        SET PCFG2 OFF
                                        SET PCFG1 OFF
                                        #IFNDEF Vref_plus
                                            SET PCFG0 OFF
                                        #ENDIF
                                        #IFDEF Vref_plus
                                            SET PCFG0 ON
                                        #ENDIF
    

    Report back if that works for you.

     
    • Anobium

      Anobium - 2016-06-02

      @Kent. If this works I can lift into the main distribution. Let me know how this progress in terms of results.

       
  • William Roth

    William Roth - 2016-06-02

    Careful with the H file. Not all PIC chips use the same register bits to set Vref+/Vref-. If we are going to add features then I suggest a concise & deliberate method that works not only on 877A but across the board. There are also internal VREF settings to consider on the less archaic chips. This stuff is not trivial. AD-H is complex and can be easily broken.

    Here's how I have done it in source with PIC chips like 877a that use PCFG bits in the same manner.

    ADCON1 = ADCON1 AND b'11110000' OR b'00000001'

     

    Last edit: William Roth 2016-06-02
  • kent_twt4

    kent_twt4 - 2016-06-02

    Agreed William on messing with the a-d.h file; it is a real can of worms. Doing a Vref+ across the board is going to be a real time consumer. Well the next question is Vref- compatibility and so forth.

    Took me forever, but my Vref+ code seems compatible (by inspection) for the following device(s) or device families.

    16f818_819
    16f87X
    16f87XA
    16c925_926
    18cXX2
    18fXX2
    18fXX8
    18fXX39

    Not compatible with following devices. Which are also not reccommended for new designs.

    14000 (10-16 bit a-d, mystery device for me).
    16c773_774 (12bit a-d, and uses VCFG2:VCFG0 for Vref+ and Vref-).

    My methodology used command line findstr VCFG3 across the .dat files to find those devices, and then deducted those devices that in turn had either the ADCON2 register or PCFG4 bit.

    No testing done. Comments welcome.

     
  • fisyon

    fisyon - 2016-06-03

    @kent
    it works, thanks a lot.
    i backed up the original a-d.h file and saved new one with your lines. i hope i won't forget to replace it with the original in the future.

    @all
    You guys, doing great job with the GCB but your kindness and patience is more precious in my opinion.

    have a nice day

     
  • kent_twt4

    kent_twt4 - 2016-06-03

    @fisyon
    Glad it worked. I'll let Anobium and others decide if worthy of inclusion in trunk, or if more work is required to include other devices. Microchip has too darn many PIC's to keep track of .....haha.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.