Page 1 of 1

ADC BUG?

Posted: Thu Jan 26, 2017 5:09 pm
by hertz89
hi again

i was working with my friend on project using 16f886

when we set ADC external Vref+ ..the adc not work

he looked at the file PIC_CAL_ADC.c and changed "if (Vref != 0) st_bit(adcon0, VCFG0); " to "if (Vref != 0) st_bit(adcon1, VCFG1);" and the Vref+ worked fine

he have flowcode v5 ..i think 5.5 may be

Code: Select all

// ADC Type 7 Supported Devices ************************************************************
// 16F883, 16F884, 16F886, 16F887
// *****************************************************************************************/

#ifdef MX_ADC_TYPE_7
        void FC_CAL_Enable_ADC (MX_UINT8 Channel, MX_UINT8 Conv_Speed, MX_UINT8 Vref, MX_UINT8 T_Charge)
        {
                adcon1 = 0x00;

                switch (Channel)
                {
                        #ifdef MX_ADC_CHANNEL_0
                                case (0):
                                        tris_mask = 0x01;
                                        tris_reg = &trisa;
                                        ansel = 0x01;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_1
                                case (1):
                                        tris_mask = 0x02;
                                        tris_reg = &trisa;
                                        ansel = 0x02;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_2
                                case (2):
                                        tris_mask = 0x04;
                                        tris_reg = &trisa;
                                        ansel = 0x04;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_3
                                case (3):
                                        tris_mask = 0x08;
                                        tris_reg = &trisa;
                                        ansel = 0x08;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_4
                                case (4):
                                        tris_mask = 0x20;
                                        tris_reg = &trisa;
                                        ansel = 0x10;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_5
                                case (5):
                                        tris_mask = 0x01;
                                        tris_reg = &trise;
                                        ansel = 0x20;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_6
                                case (6):
                                        tris_mask = 0x02;
                                        tris_reg = &trise;
                                        ansel = 0x40;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_7
                                case (7):
                                        tris_mask = 0x04;
                                        tris_reg = &trise;
                                        ansel = 0x80;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_8
                                case (8):
                                        tris_mask = 0x04;
                                        tris_reg = ⧍
                                        anselh = 0x01;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_9
                                case (9):
                                        tris_mask = 0x08;
                                        tris_reg = ⧍
                                        anselh = 0x02;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_10
                                case (10):
                                        tris_mask = 0x02;
                                        tris_reg = ⧍
                                        anselh = 0x04;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_11
                                case (11):
                                        tris_mask = 0x10;
                                        tris_reg = ⧍
                                        anselh = 0x08;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_12
                                case (13):
                                        tris_mask = 0x01;
                                        tris_reg = ⧍
                                        anselh = 0x10;
                                        break;
                        #endif
                        #ifdef MX_ADC_CHANNEL_13
                                case (13):
                                        tris_mask = 0x20;
                                        tris_reg = ⧍
                                        anselh = 0x20;
                                        break;
                        #endif
                }

                old_tris = *tris_reg;                                                                        //store old tris value, and set the i/o pin as an input
                *tris_reg = old_tris | tris_mask;
                adcon0 = (0x01 | (Conv_Speed << 6)) | (Channel << 2);        //turn ADC on

                if (Vref != 0)                                                                                        //assign VREF functionality
                        st_bit(adcon0, VCFG0);

                delay_us(T_Charge);                                                                                //wait the acquisition time

                adcon0 = adcon0 | 0x02;                                                                        //begin next conversion
        }

        MX_UINT16 FC_CAL_Sample_ADC (MX_UINT8 Sample_Mode)
        {
                MX_UINT16 iRetVal;

                while (adcon0 & 0x02);
                if (Sample_Mode)
                {
                        iRetVal = (adresh << 2);                                                        //10-bit ADC
                        iRetVal = iRetVal | (adresl >> 6);
                }
                else
                        iRetVal = adresh;                                                                        //8-bit ADC

                adcon0 = adcon0 | 0x02;                                                                        //begin next conversion

                return (iRetVal);
        }

        void FC_CAL_Disable_ADC ()
        {
                *tris_reg = old_tris;                                                                        //restore old tris value, and reset adc registers
                ansel = 0x00;
                adcon0 = 0x00;
                #ifdef ANSELH
                        anselh = 0x00;
                #endif
        }
#endif

Re: ADC BUG?

Posted: Fri Jan 27, 2017 11:28 am
by LeighM
Hi
Thanks for that, yes it should be adcon1, I will update our file.
Although the datasheet for the device indicates VCFG0 set for Vref+ and VCFG1 set for Vref-
Thanks!
Leigh

Re: ADC BUG?

Posted: Sat Jan 28, 2017 11:04 am
by hertz89
you welcome

Re: ADC BUG?

Posted: Sat Jan 28, 2017 11:14 am
by jgu1
Hi!

I also still use Fc5. I am also very interested to have the corrected file if possible. :D

Thanks in advance

Jorgen

Re: ADC BUG?

Posted: Mon Jan 30, 2017 10:51 am
by LeighM
attached