Menu

LGT8F328P Nano AD_REF_SOURCE problem

Help
2022-02-05
2022-02-07
  • Don Rosselot

    Don Rosselot - 2022-02-05

    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!

     
    • Anobium

      Anobium - 2022-02-05

      Interesting.

      Got any ASM from Arduino ? That may give a clue.

       
  • Don Rosselot

    Don Rosselot - 2022-02-05

    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.

     
    • Anobium

      Anobium - 2022-02-05

      Nice.

      I can look Sunday morning London time.

      Post your program please. I am assuming no external Vref.

       
  • William Roth

    William Roth - 2022-02-05

    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
    • Anobium

      Anobium - 2022-02-06

      I disassemble using avr-objdump. I got into this when I wrote the LGT support within GCCode.

       
  • Don Rosselot

    Don Rosselot - 2022-02-05

    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!

     
  • Don Rosselot

    Don Rosselot - 2022-02-05

    I am sending files one at a time, I guess...

     
  • Don Rosselot

    Don Rosselot - 2022-02-05

    And the disassembled hex file....

     
  • Anobium

    Anobium - 2022-02-06

    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.

     
  • Anobium

    Anobium - 2022-02-06

    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,200
    VCAL1,205
    VCAL2,206
    VCAL3,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

    #DEFINE ADREADPREREADCOMMAND  LGTSETVREF (  AD_REF_4096 )
    
    
    Sub LGTSetVREF ( in argREFS as Byte )
    
        // update calibration as required
      REFS2 = 0
    
        // set voltage reference
        ADMUX = ADMUX & 0x3f
    
        if ( argREFS = AD_REF_4096 ) Then
            VCAL = VCAL3    // calibration for 4.096V
            REFS2 = 1       // See register definition of ADCSRD and ADMUX
        REFS0 = 0
        REFS1 = 0
        else if ( argREFS = AD_REF_2048 ) Then
            VCAL = VCAL2    // calibration for 2.048V
        REFS0 = 0
        REFS1 = 1
        else
            VCAL = VCAL1;   // calibration for 1.024V
        REFS0 = 1
        REFS1 = 1
        End if
    
    
    End Sub
    
     
  • Anobium

    Anobium - 2022-02-06

    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
  • Don Rosselot

    Don Rosselot - 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!

     
  • Anobium

    Anobium - 2022-02-06

    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

     
  • stan cartwright

    stan cartwright - 2022-02-07

    When will a new gcb release that has this sorted be posted please?

     
    • Anobium

      Anobium - 2022-02-07

      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. :-)

       
      • stan cartwright

        stan cartwright - 2022-02-07

        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.

         
        • Anobium

          Anobium - 2022-02-07

          I wont be long. :-)

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.