PIC16F1786 - No Suppport for 12 bit ADC
Moderator: Benj
PIC16F1786 - No Suppport for 12 bit ADC
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
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
-
- 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
Hi!
Thank you for your reply. After the update of the definition files, the simulation works,.. great
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
Thank you for your reply. After the update of the definition files, the simulation works,.. great
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
-
- 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
Hi loop60,
I have done my own investigating on this issue and you are indeed correct.
The fcdx files have been updated, but the have not.
I have modified to work with both 10 and 12 bit devices as the ADC also have to continue to work for:
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.
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
I have modified
Code: Select all
PIC_CAL_ADC.c
Code: Select all
16F722, 16F723, 16F724, 16F726, 16F727, 16F1933, 16F1934, 16F1936, 16F1937 & 16F1939
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
Re: PIC16F1786 - No Suppport for 12 bit ADC
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
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
-
- 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
Sorry i did make a mistake as my C is a bit rusty.
It should be:
Just tried with Proteus and results:
4095 looks correct to me.
Note:
Modified file on the bases that fcdx point to correct ADC type 18.
Martin
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
Note:
Modified
Code: Select all
PIC_CAL_ADC.c
Martin
- Attachments
-
- PIC_CAL_ADC.c
- (118.85 KiB) Downloaded 213 times
Martin
- 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
Thanks Martin, much appreciated.
I've added these changes to our repository files.
I've added these changes to our repository files.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: PIC16F1786 - No Suppport for 12 bit ADC
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
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
-
- 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
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
Martin
Re: PIC16F1786 - No Suppport for 12 bit ADC
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 - yeah
Thank you for the great support!
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 - yeah
Code: Select all
ADCON2 = 0xF;
-
- 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
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
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 list.
Martin
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;
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
Martin
- Attachments
-
- PIC_CAL_ADC.c
- (119.03 KiB) Downloaded 187 times
Martin
Re: PIC16F1786 - No Suppport for 12 bit ADC
Hi Martin!
Ok. i was living in my one little world of PIC 16F1786
thx
Ok. i was living in my one little world of PIC 16F1786
thx