• AVR Freaks

Hot!PIC16F676 outputs 1v from AN3

Page: 123 > Showing page 1 of 3
Author
lakis70
Starting Member
  • Total Posts : 33
  • Reward points : 0
  • Joined: 2021/01/13 17:08:32
  • Location: 0
  • Status: offline
2021/01/15 19:53:44 (permalink)
0

PIC16F676 outputs 1v from AN3

Hello,
Happy New Year.
My first serious construction with PIC 16f676, i try to build a panel-voltmeter.
I have found many talking about using ADC module but all of them is with internal reference. I think the right configure to use ext reference is make the Vref pin input and analog.
 I use external voltage reference 2.56V in pin 12, voltage input (via a resistor divider 1:13 and a LM358 as buffer) pin 3. Pin_3 is RA4 and AN3. I do AD-conversions, refreshing 4 LED segments with TIMER1 interrupts and multiplexing 4 LED segments with  a ic CD4555b. That section works OK. My problem is that pin_3 always output 0.90V and faults all the measurements.
If i remove the LM324 from its base and the ADC input is on the air i measure with my multimeter 0.90V to 1v (it plays), if i put the LM324 in its socket i measure about 0.30V and the LCD displays 188 (digital number not volts) like it measures the volts it outputs. 
I try another pic. I think it has something to do with the external reference configuration. Any idea how to stop it output voltage?  
 
My configuration is
__code uint16_t __at (_CONFIG) __configword = _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF ;


void main(void) {
  INTCONbits.GIE = 1 ;  //Global interrupts enabled
  INTCONbits.PEIE = 1 ;  //Periferal interrupts enabled
  PIE1bits.ADIE = 1 ; //adc interrupt
  PIE1bits.TMR1IE = 1 ; //Timer0 interrupts enabled
  CMCON = 0x07 ; //Comparator OFF
  VRCON = 0x00 ;
  ANSEL = 0x00 ;
  IOCA = 0x00 ; //Interrupt on change off
  WPUA = 0x00 ; //pullups off
  TRISA = 0b00010010 ; //make all outputs except A1,A4=input
  ANSEL = 0b00001010 ; //AN3, AN1 analog input
  PORTA = 0x00 ; //make all outputs low
  TRISC = 0x00 ; //make all outputs
  PORTC = 0x00 ;
  TempC = 0x00 ; //

   ADCON0 = 0x00 ; //0b10000000 ADFM=1
  ADCON1bits.ADCS2 = 1 ;
  ADCON1bits.ADCS1 = 0 ;
  ADCON1bits.ADCS0 = 1 ; //f/16=62500Khz 16us
 
  T1CON = 0b00110001 ; //prescaler :8, timer1 ON
  TMR1L = 0 ;
  TMR1H = 0xFF ; //(65535-65379)*8 =1248 1.24mS  
  TMR1L = 0x63 ;
  metrisi = 0 ;
  loop: 
  goto loop ;
}
 

Sorry my English is not good.
#1

40 Replies Related Threads

    ric
    Super Member
    • Total Posts : 29861
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F676 outputs 1v from AN3 2021/01/15 20:58:56 (permalink)
    +1 (1)
    Where did you get this construction from?
    __code uint16_t __at (_CONFIG) __configword = _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF ;

     
    In XC8, the old syntax was:
     
    __CONFIG(_INTRC_OSC_NOCLKOUT & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF)

    but that has been dropped from the latest versions of the compiler, which only support the later syntax:
    #pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
    #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config MCLRE = OFF      // RA3/MCLR pin function select (RA3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
    #pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
    #pragma config CP = OFF         // Code Protection bit (Program Memory code protection is disabled)
    #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)

    I suspect your config statement has not worked at all, so your oscillator selection is actually:
    "111 = RC oscillator: CLKOUT function on RA4/OSC2/CLKOUT pin, RC on RA5/OSC1/CLKIN"
     
    AN3 is RA4. so you are actually seeing the CLKOUT signal on that pin. Your meter is just showing the average voltage of the clock signal.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 08:01:05 (permalink)
    0
    Ric thank you for your reply,
    I use sdcc compiler and gpsim for simulation. The config works ok.
    As i said the readout section works ok, ihave multiplex and read values.
    The problem is the voltage output.
    In another construction (a 12V battery protection)  i use again 16F676 and the same config the same pin for ADC but with internal reference and it works ok (no voltage output). I suspect something with the voltage reference makes the problem because in previous stage i was using RA2 for ADC RA0, RA1 for multiplexing, RA5 for the seventh LED of the 7segment, internal reference and it works ok.
    I choose ext reference in pin RA1, RA0 and RA2 for multiplexing and RA5 for the seventh LED of the 7segment and i got the problem.
    #3
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 10:57:32 (permalink)
    0
    ric
     
    I suspect your config statement has not worked at all, so your oscillator selection is actually:
    "111 = RC oscillator: CLKOUT function on RA4/OSC2/CLKOUT pin, RC on RA5/OSC1/CLKIN"
     

    I made the same mistake on my first C program converted from assembler.
    In MPLAB X click on "Window"  "target memory views" "configuration bits" where you set your own configs and it will produce source code for you.
    #4
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:11:36 (permalink)
    0
    MurtonPike thank you, but it isnt that. Look up i use sdcc.
    How you configure the pic for external voltage reference for ADC?
    #5
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:13:59 (permalink)
    0
    lakis70
    MurtonPike thank you, but it isnt that. Look up i use sdcc.
    How you configure the pic for external voltage reference for ADC?




     Look at VRCON register in datasheet.
    post edited by Murton Pike Systems - 2021/01/16 11:18:44
    #6
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:23:53 (permalink)
    0
    I thought VRCON is only for comparator module, i will look
    Thank you
    #7
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:32:56 (permalink)
    0
    VCFG ADCON0 sets VREF source.
    VREF is on RA1.
    ANSEL and TRISA set port pins for analogue inputs.
     
    #8
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:33:34 (permalink)
    0
    What to do VRCON, disable it?
    VRCON=0x00
    its value is 0 by default.
     
    #9
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:37:29 (permalink)
    0
    In the datasheet A2D part it just shows VREF on RA1.
    It doesnt show any connection to the VRCON.
    To be sure set VRCOM output voltage highest and see if RA1 changes.
     
    #10
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:41:16 (permalink)
    0
    MurtonPike i think you drive me to the solution
    i had        ADCON0 = 0b10001101 ; // adfm=1 10bit result, vcfg=1, channel AN3, adon=1
    i make it 1 only in remarks. I will fix it and try


            ADCON0 = 0b11001101 ; // adfm=1 10bit result, vcfg=1, channel AN3, adon=1


     
    #11
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:43:16 (permalink)
    0
    what you describe is a way to make external vref?
    I use a LM317 regulated at 2.56V at RA1.
    #12
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 11:53:58 (permalink)
    0
    "i had        ADCON0 = 0b10001101 ; // adfm=1 10bit result, vcfg=1, channel AN3, adon=1
    i make it 1 only in remarks. I will fix it and try "
    I confused watching another code. My configuration is
        ADCON0 = 0b11001101 ; // adfm=1 10bit result, vcfg=1, channel AN3, adon=1
       TRISA = 0b00010010 ; //make all outputs except A1,A4=input
       ANSEL = 0b00001010 ; //AN3, AN1 analog input
     i think they are right.
    I describe the problem again, it outputs 1V from AN3-RA4 when vcfg=1, it ok when vcfg=0.
     
    #13
    1and0
    Access is Denied
    • Total Posts : 12080
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 12:19:35 (permalink)
    +2 (2)
    lakis70
      TRISA = 0b00010010 ; //make all outputs except A1,A4=input
      ANSEL = 0b00001010 ; //AN3, AN1 analog input
     
    I describe the problem again, it outputs 1V from AN3-RA4 when vcfg=1, it ok when vcfg=0.

    Your TRISA sets RA4 as input, not output.
    #14
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 16:53:40 (permalink)
    0
    1and0 you are right thank you.
     
    My stupidity i confused the needed digital output and analog input.
    What about the TRISA and ANSEL for Vref pin A1? The same digital output and analog input?
    #15
    dan1138
    Super Member
    • Total Posts : 4242
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 17:35:05 (permalink)
    +2 (2)
    From what I can understand of the Original Posters(OP) requests the PIC16F676 is to configure the ADC to use an external voltage reference then read an analog input voltage and drive a 4 digit 7-segment LED display.
     
    This is code than can do what I think the OP wants:
    /*
     * File:   main.c
     * Author: dan1138
     * Target: PIC16F676
     * Compiler: XC8 v2.31
     * IDE: MPLABX v5.45
     *
     * Created on January 16, 2021, 12:36 PM
     *
     *                              PIC16F676
     *                    +------------:_:------------+
     *           GND -> 1 : VDD                   VSS : 14 <- 5v0
     *   SEG_g_DRIVE <> 2 : RA5/T1CKI     PGD/AN0/RA0 : 13 <- ANALOG_IN
     *   DIGIT_MUX_B <> 3 : RA4/AN3       PGC/AN1/RA1 : 12 <- VREF
     *               -> 4 : RA3/VPP           AN2/RA2 : 11 <> DIGIT_MUX_A
     *   SEG_f_DRIVE <> 5 : RC5/CPP1          AN4/RC0 : 10 <> SEG_a_DRIVE
     *   SEG_e_DRIVE <> 6 : RC4/C2OUT         AN5/RC1 : 9  <> SEG_b_DRIVE
     *   SEG_d_DRIVE <> 7 : RC3/AN7           AN6 RC2 : 8  <> SEG_c_DRIVE
     *                    +---------------------------:
     *                               DIP-14
     *
     */
    /*
     * Initialize the target specific configuration bits
     */
    #pragma config FOSC = INTRCIO
    #pragma config WDTE = OFF
    #pragma config PWRTE = OFF
    #pragma config MCLRE = ON
    #pragma config BOREN = OFF
    #pragma config CP = OFF
    #pragma config CPD = OFF
    /*
     * Include target specific definitions
     */
    #include <xc.h>
    /*
     * Define the frequency of the system oscillator will be set to.
     */
    #define _XTAL_FREQ (4000000ul)
    /*
     * Segment locations
     * of an LED display
     *      ---a---
     *     :       :
     *     f       b
     *     :       :
     *      ---g---
     *     :       :
     *     e       c
     *     :       :
     *      ---d---
     */
    const unsigned char LEDDigit[] = {
    /*     gfedcba, Segment on = 1 */
        0b00111111, // "0"
        0b00000110, // "1"
        0b01011011, // "2"
        0b01001111, // "3"
        0b01100110, // "4"
        0b01101101, // "5"
        0b01111101, // "6"
        0b00000111, // "7"
        0b01111111, // "8"
        0b01100111, // "9"
        0b01110111, // "A"
        0b01111100, // "b"
        0b00111001, // "C"
        0b01011110, // "d"
        0b01111001, // "E"
        0b01110001  // "F"  
    };
    /*
     * Global data
     */
    unsigned char Segments[4];

    /*
     * Initialize this PIC for the hardware target.
     */
    void Init_PIC(void)
    {   
        INTCON = 0;     /* turn off interrupts */
        PIE1   = 0;
        
        CMCON = 0x07;   /* turn off comparators */
        VRCON = 0x00;

        ADCON1 = 0x10;  /* set FOSC/8 as ADC clock source */
        ADCON0 = 0xC0;  /* Right justified, External VREF, select channel 0 and turn off ADC */
        ANSEL  = 0x03;  /* set RA0,RA1 as analog inputs, all others as digital */
        ADCON0bits.ADON = 1;
        ADCON0bits.GO_nDONE = 1;
        
        __delay_ms(500); /* wait for ICD before making PGC and PGD outputs */
        
        TRISA = 0xCB;   /* Set PORTA RA0,RA1,RA3 inputs, RA2,RA5 outputs */
        TRISC = 0xC0;   /* Set all PORTC as outputs */
        
        OPTION_REG = 0xD1; /* Select FOSC/4 as clock source for TIMER0 and 1:4 prescaler */
        /*
         * Initialize LED display
         */
        Segments[0] = 0;
        Segments[1] = 0;
        Segments[2] = 0;
        Segments[3] = 0;
        /*
         * Enable interrupts
         */
        TMR0 = 0;
        INTCONbits.T0IF = 0;
        INTCONbits.T0IE = 1;
        INTCONbits.GIE  = 1;
    }   
    /*
     * Interrupt handlers
     */
    void __interrupt() ISR_Handler(void)
    {
        static unsigned char Digit = 0;
        unsigned char Temp;
        if(INTCONbits.T0IE)
        {
            if(INTCONbits.T0IF)
            {
                INTCONbits.T0IF = 0;
                if(Digit >= sizeof(Segments))
                {
                    Digit = 0;
                }
                else
                {
                    Digit++;
                }
                Temp = Segments[Digit];
                
                /* Turn off all segment drivers */
                PORTC = 0;
                PORTAbits.RA5 = 0;
                
                /* Select a digit to be on */
                PORTAbits.RA2 = 0;
                PORTAbits.RA4 = 0;
                if(Digit & 1) PORTAbits.RA2 = 1;
                if(Digit & 2) PORTAbits.RA4 = 1;
                
                /* Turn on segments for selected digit */
                PORTC = Temp;   
                if (Temp & (1<<6)) PORTAbits.RA5 = 1;
            }
        }
    }
    /*
     * Put a hexadecimal value, range 000 to FFFF in LED display
     */
    void LED_PutHex(unsigned int Data)
    {
        Segments[3] = LEDDigit[Data & 0x0F];
        Data >>= 4;
        Segments[2] = LEDDigit[Data & 0x0F];
        Data >>= 4;
        Segments[1] = LEDDigit[Data & 0x0F];
        Data >>= 4;
        Segments[0] = LEDDigit[Data & 0x0F];
    }
    /*
     * Main Application
     */
    void main(void)
    {
        unsigned int Count;
        Init_PIC();
        Count = 0;
        /*
         * Application loop
         */
        for(;;)
        {
            LED_PutHex(((unsigned int)(ADRESH) << 8) | ADRESL);
            ADCON0bits.GO_nDONE;
            __delay_ms(1000);
        }
    }

    Let us know if it helps.
    #16
    blue_led
    New Users
    • Total Posts : 87
    • Reward points : 0
    • Status: online
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 20:29:45 (permalink)
    0
    lakis70
     I use external voltage reference 2.56V 



    why this particular value ?
    adc resolution is 1024 ( max adc value 0x03FF ) steps so if this number mean volts you have 1-1 corespondence up to 1,023 kV or 102,3 V or 10,23 V or 1,023 V depending on the ratio of the resistive divider at the input
    value of choice for me can be 2,048 or 4,096 V ( for 5 V Vdd ) for vref for ease of calibration and displaying calculations. ( binary to unpacked BCD conversion )
    of course it can be any arbitrary value for vref and resistive divider in front of adc input.
     
    why you use an aditional CD4555B . this pic can drive 4 digit display directly using charlieplexing
    post edited by blue_led - 2021/01/16 20:45:17

    Attached Image(s)

    #17
    lakis70
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2021/01/13 17:08:32
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 20:38:22 (permalink)
    0
    Finally
    Datasheet is very confusing, they say
    7.1.1
    ANALOG PORT PINS
    The ANS7:ANS0 bits (ANSEL<7:0>) and the TRISA
    bits control the operation of the A/D port pins. Set the
    corresponding TRISA bits to set the pin output driver to
    its high-impedance state. Likewise, set the correspond-
    ing ANS bit to disable the digital input buffer.


    The right configuration for me is
      TRISA = 0b00010010 ; //make all digital outputs except RA4, RA1
      ANSEL = 0b00001010 ; //AN3, AN1 analog input
     
    #18
    1and0
    Access is Denied
    • Total Posts : 12080
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 20:47:53 (permalink)
    0
    lakis70
    The right configuration for me is
      TRISA = 0b00010010 ; //make all digital outputs except RA4, RA1
      ANSEL = 0b00001010 ; //AN3, AN1 analog input

    ADC's are ANALOG INPUT, so set the corresponding TRIS bits to INPUT and the corresponding ANSEL bits to ANALOG.
    #19
    dan1138
    Super Member
    • Total Posts : 4242
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC16F676 outputs 1v from AN3 2021/01/16 20:49:42 (permalink)
    0
    lakis70
    The right configuration for me is
      TRISA = 0b00010010 ; //make all digital outputs except RA4, RA1
      ANSEL = 0b00001010 ; //AN3, AN1 analog input

    This configuration means that you cannot use an external VREF for the ADC.
     
    Only RA1/AN1 can be used as the input for an external VREF for the ADC in the PIC16F676.
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2021 APG vNext Commercial Version 4.5