Menu

How to enable the external clock and external Vref on a 16F1788?

Help
2015-12-17
2015-12-19
  • Louis Testa

    Louis Testa - 2015-12-17

    Hello,
    I have used a PIC 16F1788 to make driver for a VFD display. If I use
    the internal clock (=4MHZ) and the default ADC reference voltage (=Vdd),
    everthing works fine.I want to switch these to an external clock input and an external Vref
    for accuracy and stability reasons.I have tried many combinations of commands to switch the
    clock and change the bits in the ADCON registers. Nothing I do has any effect. The chip always
    runs using the default conditions. Any ideas? Has anyone done this before? If so,how? Thank you.
    ( I am using a PICKit 3 to program the chip).

     
  • Anobium

    Anobium - 2015-12-17

    What have you tried? can you post some example of what has not worked?
    Are you using GCGB or the IDE?
    What version of the GCB compiler do you have ? This is important. GreatCowBasic\GCBASIC.exe /version will give you the version.

     
  • kent_twt4

    kent_twt4 - 2015-12-17

    External oscillator is handled in the config(uration) registers. Perhaps the easiest way to detect the options is looking at the chipdata file in GreatCowBasic folder of your installation. If it is an external crystal/resonator than there is the LP, XT, or HS designators. So for say a 20 Mhz crystal than the config would look like:

    #config OSC=HS
    

    For setting the a-d reference voltage than that would be the ADCON1 register, specifically the ADPREF0 and ADPREF1 bits. Setting the adc positive reference voltage to the Vref+ pin during initialization would be:

    ADPREF1 = 0
    ADPREF0 = 1
    
     
  • William Roth

    William Roth - 2015-12-17

    Hi Louis,

    Do you mean setting an external clock source for the PIC or setting the clock source for the ADC in the ADCON1 Register (ADCS Bits)?

    Please take the time to post your (non-working) GCB code so that we can have a look. I have a PIC16F1788 on a breadboard that I can test/debug.

    William

     
  • William Roth

    William Roth - 2015-12-19

    The PIC16F1788 defaults to differential ADC inputs so to get it working with an external VREF+ and the Negative VREF- set to Vss (GND) some bits need to be set.

    First the ADPREF and ADNREF bits need to be configured as follows

    SET ADPREF1 OFF 'Default
    SET ADPREF0 ON
    SET ADNREF OFF ' Default

    Then the CHSN Bits need to be set as follows.

    SET CHSN3 ON
    SET CHSN2 ON
    SET CHSN1 ON
    SET CHSN0 ON

    Below is working Code using a PIC16F1788 on a breadboard where a 10K POT connected to AN2 (PIN 4) for adjusting the ADC value and another 10K POT is connected to RA3 (Pin 5) to act as an adjustable VREF+. This works perfectly.

    In your application you will need to physically Connect the external positive reference source to AN3 (Pin5)

    I am using an external clock source ( 12 MHz Crystal)

    Here is the Complete Working Code:

    ; --- 16F1788 ADC TEST ----
    ;  By William Roth
    ;  12-18-2015
    ; *************************
    
    #chip 16F1788, 12
    #config OSC=HS
    #config MCLRE = OFF
    
    ;Setup LCD Parameters
    #define LCD_IO 4
    #define LCD_NO_RW
    #define LCD_Speed fast
    
    ; ----- Define Hardware settings
    #define LCD_RS PORTB.1
    #define LCD_Enable PORTB.0
    #define LCD_DB4 PORTC.4
    #define LCD_DB5 PORTC.5
    #define LCD_DB6 PORTC.6
    #define LCD_DB7 PORTC.7
    
    CLS
    
    'Set Vref+ to External Pin RA3/Pin5
    SET ADPREF1 OFF
    SET ADPREF0 ON
    SET ADNREF OFF  ' defaults to off (redundant)
    
    'Set VREF- to Vss
    SET CHSN3 ON
    SET CHSN2 ON
    SET CHSN1 ON
    SET CHSN0 ON
    
    LightLevel = ReadAD(AN2)   'Pin4
    
    ; Main loop
    Do Forever
    
        'Take a measurement and store in the LightLevel variable
         LightLevel = ReadAD(AN2)   'PinA.2
    
        'Display measurement
         Locate 0,0
         Print "Light Level: "
         Print LightLevel
         Wait 500 ms
         Locate 0,12  'clear value on screen"
         Print "      "
    Loop
    
     

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.