Page 1 of 1

PIC16F1786 - No Suppport for 12 bit ADC

Posted: Tue Sep 05, 2017 3:04 pm
by loop60
Hi!

I'm want to use the 12 bit ADC on the PIC16F1786, but the ADC-Templates in Flowcode 7 only support 10 bit!?

I tried to bypass the Flowcharts and ... suprise :-) it works

ADCON1 = 0b10010000;
// Alle 8 A Ports auf Input stellen
TRISA = 0b11111111;
// A1 für analogen Input aktivieren
ANSELAbits.ANSA1 = 1;

ADCON0 = 0b00000101;
ADCON2 = 0b00001111;

// Starte Signalverarbeitung
ADCON0bits.GO =1;
// Warten bis der Lesezugriff fertig ist
while(ADCON0bits.GO ==1);

//Aufbereitung vom 12bit Ergebnis
//Register ADRESL beinhaltet maximal 255, bei Überlauf wird
//Register ADRESH um eins erhöht
FCV_I_VOLTAGE = ADRESL + (ADRESH*256);
//FCV_R_VOLTAGE beinhaltet die tatsächliche Spannung in V
FCV_R_VOLTAGE = FCV_I_VOLTAGE* 5.04/4095;

The Code above works - but i think this is a difficult solution. Please, can anybody help me?

thx, loop60

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Tue Sep 05, 2017 9:05 pm
by medelec35
Hi loop60,
I believe this has already been addressed.
Take a look here

Martin

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Fri Sep 15, 2017 8:21 am
by loop60
Hi!

Thank you for your reply. After the update of the definition files, the simulation works,.. great :D

But the same code still does not work on the pic16f1786. (i have tried three different chips)
Could this be a problem with the adc result format settings? Which value generates flowcode into bit 7 from the adcon0 register?

thx for further investigations

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Sun Sep 17, 2017 1:17 pm
by medelec35
Hi loop60,
I have done my own investigating on this issue and you are indeed correct.
The fcdx files have been updated, but the

Code: Select all

PIC_CAL_ADC.c
have not.
I have modified

Code: Select all

PIC_CAL_ADC.c
to work with both 10 and 12 bit devices as the ADC also have to continue to work for:

Code: Select all

16F722, 16F723, 16F724, 16F726, 16F727, 16F1933, 16F1934, 16F1936, 16F1937 & 16F1939
as its using the same ADC type.
Can you place the attached file in your \Flowcode 7\CAL\PIC directory.

This should hopefully work until Matrix produce an official updated file.

Martin.

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Sun Sep 17, 2017 9:29 pm
by loop60
Hi Martin,

thx for your investigations. Now the behavior has slightly changed, but i think it is still not correct.
The maximum integer value i get is 2065. Could this be a kind of overflow while doing the logical shift left?

btw: I've studied your changes in the PIC_CAL_ADC. Is it properly that the logical shift right of ADRESL is 6 when ADC_BITS is 12? -> 4?

#ifdef _ADRESH_ADRESH_POSN
#ifdef MX_ADC_BITS_12
iRetVal = (ADRESH << 4); //12-bit ADC
#else
iRetVal = (ADRESH << 2); //10-bit ADC
#endif
iRetVal = iRetVal | (ADRESL >> 6);
#else
#ifdef MX_ADC_BITS_12
iRetVal = (ADRES << 4); //12-bit ADC
#else
iRetVal = (ADRES << 2); //10-bit ADC
#endif
#endif

Christian

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Sun Sep 17, 2017 11:13 pm
by medelec35
Sorry i did make a mistake as my C is a bit rusty.
It should be:

Code: Select all

#ifdef _ADRESH_ADRESH_POSN
				 #ifdef MX_ADC_BITS_12
				iRetVal = (ADRESH << 4);				//12-bit ADC
				iRetVal = iRetVal | (ADRESL >> 4);
				#else
				iRetVal = (ADRESH << 2);	            //10-bit ADC
				iRetVal = iRetVal | (ADRESL >> 6);
				#endif		
			#else
				 #ifdef MX_ADC_BITS_12
				iRetVal = (ADRES << 4);							//12-bit ADC
				#else
			   iRetVal = (ADRES << 2);							//10-bit ADC
				#endif
		#endif
Just tried with Proteus and results:
12Bit ADC result.png
(88.02 KiB) Downloaded 3725 times
4095 looks correct to me.

Note:
Modified

Code: Select all

PIC_CAL_ADC.c
file on the bases that fcdx point to correct ADC type 18.

Martin

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Mon Sep 18, 2017 10:09 am
by Benj
Thanks Martin, much appreciated.

I've added these changes to our repository files.

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Mon Sep 18, 2017 8:58 pm
by loop60
Hi Martin,

i've imported the new file but my result looks different to yours.
The maximum integer value i get is still 2066 (instead of 2065)

Im using the standard xc8 compiler in free mode - could it now be a problem with an implicit type cast?

thank you for further research,
Christian

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Mon Sep 18, 2017 9:06 pm
by medelec35
Hi Christian, I'm also using xc8 free mode at home. Can you post flowchart and c code indicating 2065 so I can see what results I get.

Martin

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Wed Sep 20, 2017 9:42 pm
by loop60
Hi Martin,

i studied the PIC_CAL_ADC myself again and i think the problem is the initialization of the adcon2 register.

After inserting the below line into the FC_CAL_ADC_Enable() procedure, the adc works :D :D - yeah

Code: Select all

ADCON2 = 0xF;	
Thank you for the great support!

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Thu Sep 21, 2017 11:38 am
by medelec35
Hi Christian,
Thank you and your welcome.
great spot!
You are indeed correct.
The simulation is clearly not as accurate as hardware.
There will be an issue by dropping in

Code: Select all

ADCON2 = 0xF; 
It will work for 16F17XX devices but will fail to compile for the non 16F17XX series like the 16F1937.
With that in mind I have modified PIC_CAL_ADC again.
I have also added the extra devices in the

Code: Select all

ADC Type 18 Supported Devices
list.

Martin

Re: PIC16F1786 - No Suppport for 12 bit ADC

Posted: Fri Sep 22, 2017 5:36 am
by loop60
Hi Martin!

Ok. i was living in my one little world of PIC 16F1786

thx