Here some code that might help you. It limits the a-d outputs to AN0 to AN3 and the FVR. It could be expanded if need be. FVR should not be lower than 2.048V when used as an adc reference.
#define AN0 0
#define AN1 1
#define AN2 2
#define AN3 3
#define FVR b'00011111' 'define FVR buffer1 output for adc
...
...
...
Main:
FixedVoltRef = AD12F1822(FVR)
...
...
...
goto Main
Function AD12F1822(adport)
'ADCON1 = b'01100000' 'Left justified, FOSC/64, Vref+ to FVR
ADCON1 = b'01100000'
If adport = FVR Then
'ADCON1 = ADCON1 OR b'01100011'
FVRCON = b'10000010' 'turn on fixed voltage ref of 2.048V
FVRNotstable:
btfss FVRCON,FVRRDY
goto FVRNotstable
End if
ANSELA = adport + 1 'turn off digital input ANSA2
If adport > 3 Then ANSELA = 0 'internal FVR or Temp Indicator conversion
rotate adport left simple
rotate adport left simple
'bin2ascii adport
'HSERSend " "
ADCON0 = ADCON0 OR adport 'adport
'bin2ascii adport
'HSERSend " "
ADON = 1 'ENABLE ADC AND START CONVERSION
wait 10 us 'use nop's or ??? so no us delay sub????
'wait 1 ms 'need 200us min for temp module
'ADGO = 1
GO_DONE = 1
ADCPOLL:
BTFSC ADCON0,GO_DONE 'ADGO
goto ADCPOLL
FVRCON = 0
ADCON0 = 0
ANSELA = 0 'Shut down adc module
AD12F1822 = ADRESH 'read out the result
FVRCON = b'00000010' 'turn off fixed voltage ref of 2.048V
End Function
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thx , I asked because I thought it was possible somehow to change it simple and still using GC instructions readad10 , etc . Probably not implemented yet .
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This version has a version of FVR included. The use ADC as you do normally but you should check the latest help http://gcbasic.sourceforge.net/help/ so see all the updated ADC functionality. Remember you can really optimise the library and you can optimise the library with just a few #defines.
THX for the update , i read the help but i cant find so far how to use the updated functionality .
Just to add in program #define ADFVR_Initialize ( ADFVR_2x ) if i want 2,048V Ref ?
Last edit: Daniel Cioba 2017-07-09
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
So far the result is exactly the same as with the old library . ADC of course is working , but Vref is "stuck" at 4,096V.
Even with , ADFVR_Initialize (ADFVR_OFF) the readings are the same .
I didn't use the FVR test yet , so i don't know if that is working .
The hardware is simple , I use the PIC and an LCD display to measure a DC voltage . The program is displaying the ADC value read ( 0 - 1023 ) , so i can easily see if the value is changing.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The FVR could be setup properly, but it doesn't look like it is #defined as an a-d input in the new library, much like AN0, AN1, etc. (per above 12f1822 example)
In this way a constantly changing battery supply voltage (which is the actual Vdd reference for the a-d inputs) can be monitored, and an analogue input be given the proper context. From the earlier example a battery voltage is monitored. Once that is established then the adc inputs can be properly calculated.
Or, I haven't had my morning cup of coffee yet, and I failed to see the logic already in the library?
EDIT: Having a coffee now, so Beg your pardon, using the FVR as the a-d reference is correct. But the FVR as an a-d input is a thing too, sorry for the (my) confusion :)
Last edit: kent_twt4 2017-07-10
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tried again but is not working yet . Same as before , I can't change Vref .
Also , if i use#define ADSpeed HighSpeed the reading is "0" . Medium and low give normal readings , not sure if the ADC speed is actually changing .
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I cannot see any obvious - I can improve the code with your type of chip to use one byte less. I will do that once we have it working.
Can I just check a few things? Does ADC work normally? Remove all the optmisations - All ok? And, does the #define ADSpeed HighSpeed always give 0 but the others give a normal full range of 0 to 1023?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I checked again , ADC work normally , the PIC16F1828 is new ( i have many ) .
With Normal ( I asume this is anyway the default value used in library ) and Low is full range 0 - 4V , with High is allways 0.
This issue was present from the start , with the original library , but i didn't mentioned it before .
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Try this please. I have removed the optimisations. You do not need to define LAT for LCD. This is now handled correctly. But, if I broke the LCD please fix to complete the test.
Ok , the schematic is here ... of course for testing the circuit is simplified , I have one input as IN1 and I can jump over the resistive divider if I want ... not the circuit is the issue . If an ADC input is fixed at 1V , lets say ( aprox 0,8V in my case ) , if you change the refference at 1V will read aprox 1023 , with 2V 512 and with 4V aprox 250 . Very easy to see if refference is changing or not . So I got 175 all the time I change ADFVR_Initialize ... and 0 if i use ADSpeed High
I worked with ADC in many projects , but with Atmega microcontrollers in Bascom , thats why I prefer something similar with PICs ( BASIC instructions ).
I will try to look at your code , but i'm not very familiar with it , also i got PIC16F1936 , 1938 with the same reference register , I will check if with this microcontroller works .
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
We've had some problems with this before. Hidden away in the datasheet it is stated that the + voltage reference for the ADC cannot be less than 1.8V so the 1.024V ref will not work on the ADC.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi , how can i change internal reference between 1V , 2V , 4V when using ADC ?
CLOSURE NOTE: The methods and scripts in this posting have been replaced by public methods in V0.98.00 or later.
FVR_Initialize
andADFVR_IsOutputReady
are not supported as these have been replaced byFVRInitialize()
andFVRIsOutputReady
Last edit: Anobium 2017-10-02
Here some code that might help you. It limits the a-d outputs to AN0 to AN3 and the FVR. It could be expanded if need be. FVR should not be lower than 2.048V when used as an adc reference.
Thx , I asked because I thought it was possible somehow to change it simple and still using GC instructions readad10 , etc . Probably not implemented yet .
Let me check. I have a memory on that we have been through this before.
Not sure if FVR made it to the library or not. If help is needed to keep things moving, and change the above function to 10bit, let me know.
Look extremly simple to implement in the latest library. I can add in about an hour or so.
Yes/No?
Here is the latest ADC library. It is from the upcoming release. https://www.dropbox.com/s/k2a8kx6zjpzlem8/a-d.h?dl=0
This version has a version of FVR included. The use ADC as you do normally but you should check the latest help http://gcbasic.sourceforge.net/help/ so see all the updated ADC functionality. Remember you can really optimise the library and you can optimise the library with just a few #defines.
The FVR functionality is exposed as:
Where ADFVR_OFF, ADFVR_1x, ADFVR_2x and ADFVR_4x are defined constants to turn off, v1.024, v2.048 or v4.096 respectively.
Let us know if this works.
THX for the update , i read the help but i cant find so far how to use the updated functionality .
Just to add in program
#define ADFVR_Initialize ( ADFVR_2x )
if i want 2,048V Ref ?Last edit: Daniel Cioba 2017-07-09
Of course, a good question.
These are two methods not Defines.
The first is a sub routine called ADFVR_Initialize. You can pass one parameter.
ADFVR_Initialize ()
The one paramter is one of the following
ADFVR_OFF, ADFVR_1x, ADFVR_2x or ADFVR_4x ( which relate to OFF, 1.024v, 2.048v or 4.096v)
The second method is a function. No parameter but the function returns true or false.
ADFVR_IsOutputReady ' returns true or false
Typical Usage
To set to 1.024v
To set to 2.048v
To test if the FVR is ready.
To install. Make a backup of your existing library in you lowlevel folder and replace with the new library.
Let us know how you get on.
So far the result is exactly the same as with the old library . ADC of course is working , but Vref is "stuck" at 4,096V.
Even with ,
ADFVR_Initialize (ADFVR_OFF)
the readings are the same .I didn't use the FVR test yet , so i don't know if that is working .
The hardware is simple , I use the PIC and an LCD display to measure a DC voltage . The program is displaying the ADC value read ( 0 - 1023 ) , so i can easily see if the value is changing.
Another, final go hopefully. I failed to setup the ADC for you.
My test code - works on test here, hopefully, now works for you.
Please get the latest library again from https://www.dropbox.com/s/k2a8kx6zjpzlem8/a-d.h?dl=0
Anobium
The FVR could be setup properly, but it doesn't look like it is #defined as an a-d input in the new library, much like AN0, AN1, etc. (per above 12f1822 example)
In this way a constantly changing battery supply voltage (which is the actual Vdd reference for the a-d inputs) can be monitored, and an analogue input be given the proper context. From the earlier example a battery voltage is monitored. Once that is established then the adc inputs can be properly calculated.
Or, I haven't had my morning cup of coffee yet, and I failed to see the logic already in the library?
EDIT: Having a coffee now, so Beg your pardon, using the FVR as the a-d reference is correct. But the FVR as an a-d input is a thing too, sorry for the (my) confusion :)
Last edit: kent_twt4 2017-07-10
I tried again but is not working yet . Same as before , I can't change Vref .
Also , if i use
#define ADSpeed HighSpeed
the reading is "0" . Medium and low give normal readings , not sure if the ADC speed is actually changing .Please attach a zip with your source and your asm files
OK , here is the very basic code i'm using for testing
I cannot see any obvious - I can improve the code with your type of chip to use one byte less. I will do that once we have it working.
Can I just check a few things? Does ADC work normally? Remove all the optmisations - All ok? And, does the #define ADSpeed HighSpeed always give 0 but the others give a normal full range of 0 to 1023?
I checked again , ADC work normally , the PIC16F1828 is new ( i have many ) .
With Normal ( I asume this is anyway the default value used in library ) and Low is full range 0 - 4V , with High is allways 0.
This issue was present from the start , with the original library , but i didn't mentioned it before .
Still trying to figure out the root cause.
Try this please. I have removed the optimisations. You do not need to define LAT for LCD. This is now handled correctly. But, if I broke the LCD please fix to complete the test.
It is the same with no optimisation ... only 4V Vref and "0" reading with HighSpeed .
I need to understand more. Can you draw me a diagram of the connectivity?
Ok , the schematic is here ... of course for testing the circuit is simplified , I have one input as IN1 and I can jump over the resistive divider if I want ... not the circuit is the issue . If an ADC input is fixed at 1V , lets say ( aprox 0,8V in my case ) , if you change the refference at 1V will read aprox 1023 , with 2V 512 and with 4V aprox 250 . Very easy to see if refference is changing or not . So I got 175 all the time I change ADFVR_Initialize ... and 0 if i use ADSpeed High
I worked with ADC in many projects , but with Atmega microcontrollers in Bascom , thats why I prefer something similar with PICs ( BASIC instructions ).
Last edit: Daniel Cioba 2017-07-10
Still nothing jumping out at me.
Should only take a day or two but vacation period is looming.
I will try to look at your code , but i'm not very familiar with it , also i got PIC16F1936 , 1938 with the same reference register , I will check if with this microcontroller works .
What a good idea. I like the approach. Good to hear to the results.
I am away from real silicon at the moment - I am travelling.
We've had some problems with this before. Hidden away in the datasheet it is stated that the + voltage reference for the ADC cannot be less than 1.8V so the 1.024V ref will not work on the ADC.