PIC16F1786 - No Suppport for 12 bit ADC

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
loop60
Posts: 6
Joined: Tue Sep 05, 2017 2:53 pm
Been thanked: 3 times
Contact:

PIC16F1786 - No Suppport for 12 bit ADC

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post by medelec35 »

Hi loop60,
I believe this has already been addressed.
Take a look here

Martin
Martin

loop60
Posts: 6
Joined: Tue Sep 05, 2017 2:53 pm
Been thanked: 3 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post 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
Attachments
test_getvoltage.zip
Flowcode Project
(90.94 KiB) Downloaded 218 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post 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.
Attachments
PIC_CAL_ADC.c
(118.81 KiB) Downloaded 215 times
Martin

loop60
Posts: 6
Joined: Tue Sep 05, 2017 2:53 pm
Been thanked: 3 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post 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
Attachments
PIC_CAL_ADC.c
(118.85 KiB) Downloaded 213 times
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post by Benj »

Thanks Martin, much appreciated.

I've added these changes to our repository files.

loop60
Posts: 6
Joined: Tue Sep 05, 2017 2:53 pm
Been thanked: 3 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post 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
Martin

loop60
Posts: 6
Joined: Tue Sep 05, 2017 2:53 pm
Been thanked: 3 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post 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!

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post 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
Attachments
PIC_CAL_ADC.c
(119.03 KiB) Downloaded 187 times
Martin

loop60
Posts: 6
Joined: Tue Sep 05, 2017 2:53 pm
Been thanked: 3 times
Contact:

Re: PIC16F1786 - No Suppport for 12 bit ADC

Post by loop60 »

Hi Martin!

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

thx

Post Reply