In using the internal voltage reference on the LGT8Fx , I am getting conflicting results with the different sources.
When I use 1024 or AVCC as the reference, the ADC results are spot on, but when I use the 2048 or 4096 reference, the ADC results are consistently low by about 6%. I tested the same chip on the Arduino IDE and all the reference sources provide consistent results. The ASM indicates the correct assignment is made to REFS2:0, so I don't know what is going on. I have minimized the program to just using ReadAD12() and a serial connection to the terminal, and the errors do not go away.
Any advice would be appreciated!
I have been enjoying GCB since 2007. A big thanks to the whole team for their efforts! I have also found Evans videos most informative!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have 'disassembled' the arduino hex file with avr-objdump (lgt_asm.txt), and also attached the arduino sketch, and the GCB sketch. (I think I attached them correctly)
Good luck!
OK. Here is a test program.
This uses the vector ADREADPREREADCOMMAND to insert a method called LGTSetVREF ( in argREFS as Byte ) . Its use is very obvious. Once we have this sorted then we can move bac into the main line code.
The issue is that the ADC vref is not correct and the calibration register is missing from the .DAT So, you need to update the .DAT In Line 80 in the C:\GCB@Syn\GreatCowBasic\chipdata\lgt8f328p.dat add the VCALs as follows. Please inspect the datasheet to ensure I got these correct.
VCAL,200VCAL1,205VCAL2,206VCAL3,204'End of chip specific changes
The code attached is my test GCCode for you. This hooks ADREADPREREADCOMMAND then calls LGTSETVREF ( AD_REF_4096 ) - you must try the other values AD_REF_2048 and AD_REF_1024. Then this sets the registers. Please code review etc. As I said.. if this works then we need to move back into the main line code and fix all the DAT files.
Evan
'Evan - trial fixes using ADREADPREREADCOMMAND
#DEFINEADREADPREREADCOMMANDLGTSETVREF(AD_REF_4096)SubLGTSetVREF(inargREFSasByte)// update calibration as requiredREFS2=0// set voltage referenceADMUX=ADMUX&0x3fif(argREFS=AD_REF_4096)ThenVCAL=VCAL3// calibration for 4.096VREFS2=1// See register definition of ADCSRD and ADMUXREFS0=0REFS1=0elseif(argREFS=AD_REF_2048)ThenVCAL=VCAL2// calibration for 2.048VREFS0=0REFS1=1elseVCAL=VCAL1;// calibration for 1.024VREFS0=1REFS1=1EndifEndSub
Success! That fixed the 2.048 and 4.096 reference issues. Accuracy went from 6% to .5%!
And yes, just modifying the chip data file and using the new a-d.h file, all sources work perfectly.
Thank you, Evan for you speedy solution!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In using the internal voltage reference on the LGT8Fx , I am getting conflicting results with the different sources.
When I use 1024 or AVCC as the reference, the ADC results are spot on, but when I use the 2048 or 4096 reference, the ADC results are consistently low by about 6%. I tested the same chip on the Arduino IDE and all the reference sources provide consistent results. The ASM indicates the correct assignment is made to REFS2:0, so I don't know what is going on. I have minimized the program to just using ReadAD12() and a serial connection to the terminal, and the errors do not go away.
Any advice would be appreciated!
I have been enjoying GCB since 2007. A big thanks to the whole team for their efforts! I have also found Evans videos most informative!
Interesting.
Got any ASM from Arduino ? That may give a clue.
I have tried to get ASM, but apparently there is no way to get that, I have tried. I know it used to be an option, but not anymore.
Nice.
I can look Sunday morning London time.
Post your program please. I am assuming no external Vref.
Does Arduino even generate ASM ? I found this but it may be dated.....
https://forum.arduino.cc/t/how-can-one-view-the-assembly-code-output-from-the-arduino-ide/50302
Last edit: William Roth 2022-02-05
I disassemble using avr-objdump. I got into this when I wrote the LGT support within GCCode.
I have 'disassembled' the arduino hex file with avr-objdump (lgt_asm.txt), and also attached the arduino sketch, and the GCB sketch. (I think I attached them correctly)
Good luck!
I am sending files one at a time, I guess...
And the disassembled hex file....
A couple things I have learnt this morning.
For a VREF of 4.096v the VCC must be greater than 4.7v
For a VREF of 2.048v the VCC must be greater than 2.7v
But, I do not think the AVR code is correct for the LGT. I am working a change here. You will have to test.
OK. Here is a test program.
This uses the vector ADREADPREREADCOMMAND to insert a method called LGTSetVREF ( in argREFS as Byte ) . Its use is very obvious. Once we have this sorted then we can move bac into the main line code.
The issue is that the ADC vref is not correct and the calibration register is missing from the .DAT So, you need to update the .DAT In Line 80 in the C:\GCB@Syn\GreatCowBasic\chipdata\lgt8f328p.dat add the VCALs as follows. Please inspect the datasheet to ensure I got these correct.
The code attached is my test GCCode for you. This hooks
ADREADPREREADCOMMAND
then callsLGTSETVREF ( AD_REF_4096 )
- you must try the other valuesAD_REF_2048
andAD_REF_1024
. Then this sets the registers. Please code review etc. As I said.. if this works then we need to move back into the main line code and fix all the DAT files.Evan
'Evan - trial fixes using ADREADPREREADCOMMAND
The root cause is that ADC library DID NOT read the calibration data, and, set the VCAL appropriately. And, VCALs were missing from the DAT file.
Please test and resolve - if you are able/willing please resolve in A--D.H for everyone. I have attached a new version based upon the previous post
Evan
Last edit: Anobium 2022-02-06
Success! That fixed the 2.048 and 4.096 reference issues. Accuracy went from 6% to .5%!
And yes, just modifying the chip data file and using the new a-d.h file, all sources work perfectly.
Thank you, Evan for you speedy solution!
Brilliant. You analysis and your files really help. Thanks to you . it only took a few moments.
I will update all the DATs and update the master a-d.h
Now, go to this url and leave a very positive comment - https://sourceforge.net/projects/gcbasic/reviews/new?stars=5
When will a new gcb release that has this sorted be posted please?
Great question. You can get the latest build from the Release Candidate - https://sourceforge.net/projects/gcbasic/files/Release%20Candidates/Patches/gcb%40Syn.zip/download
That the fixes and a few more before this issue sprung up. :-)
Thanks, I'll wait for the next release. I use a lgt328nano but not tried a-d and it has
12bit a-d so nice this problem has been identified and sorted.
I wont be long. :-)