Menu

ReadAD10 strange behaviour

Boguslaw
2024-01-18
2024-01-22
  • Boguslaw

    Boguslaw - 2024-01-18

    I want to use ADC 10bit 0-1023, but in my listing ADC read 0-4095 why ?

    #CHIP 18F56Q71,64
    #OPTION Explicit
    
        #startup InitPPS, 85
        #define PPSToolPart 18F56Q71
    
        Sub InitPPS
                  // Module: UART pin directions
                Dir PORTB.1 Out    ' Make TX2 pin an output
                // Module: UART2
                RB1PPS = 0x0018    // TX2 > RB1 11000
    
        End Sub
    
    
    
    #define AdRefSource = AD_REF_AVCC
    Dir     Porta.2 In
    
    'analogue port selection
    #DEFINE ADCChannelSelected ANA2
    #DEFINE ADSpeed LowSpeed
    
    
    
    #define USART2_BAUD_RATE 9600
    #define USART2_TX_BLOCKING
    #define USART2_DELAY OFF
    
    
        Do forever
    
    '
     hserprint str(ReadAD10( AN2 ),2)
      HSerPrintCRLF
    
      wait 500 ms
        Loop
    
        End
    
     
  • Anobium

    Anobium - 2024-01-18

    Use READ10( AN2, TRUE )

    The Help explains the second parameter.

     
  • Boguslaw

    Boguslaw - 2024-01-18

    Still is not good, with AD_REF_AVCC and "true" at 3.3V is 1023, but in the vicinity of low voltages when the ADC reaches 0, continue turning the ADC potentiometer indicates above 65000. I use Curiosity nano, maybe is hardware problem...

     

    Last edit: Boguslaw 2024-01-18
  • Geoffrey Younger

    You also need to Dimension your user variable - where the a2d result goes, like this.

    Dim adcKP as Integer ' for Forced 10 bit adc, and the function reads

    Function KPD '
    adcKP = ReadAD10(ANC7,TRUE ) ' Forced 10 bit conversion
    etc
    This worked for for me.
    BTW I never use PPS during R&D, It adds a layer of complexity you can do without. At power-on reset the original pinouts are valid !!
    Cheers,
    Geoffrey Younger

     
  • Boguslaw

    Boguslaw - 2024-01-19

    When I use integer variable to read AD10 , then I receive -500 to 1300 ADC, if I use AD 8bit, then is correct 0-255 , at reading AD12 I have 0-4095 ADC.
    It is certain that something is not working in AD10.

     
  • Anobium

    Anobium - 2024-01-19

    ReadAD10 requires a WORD variable. What happens then?

    I will be able to try this on real silicon next Monday. So, until then I will try to assist.

     
  • Boguslaw

    Boguslaw - 2024-01-19

    Word gives the numbers 65535 when the voltage is low

     
  • Anobium

    Anobium - 2024-01-19

    I would be guessing until I get onto some real silicon. Which will be next Monday.

    OK to wait till then?

    The issue could be in the ADC library but even that is a guess. I will do a root cause analysis and resolve for you and future users.

    Please help me by producing a program that has the bare minimum to show the error. The program should have the correct chip, pps for that chip, serial setup, variable definition, adc reading and serial display of the adc results. Thank you.

     
  • Boguslaw

    Boguslaw - 2024-01-19

    OK I will wait, below is my listing

    #CHIP 18F56Q71,64
    #OPTION Explicit
    
        #startup InitPPS, 85
        #define PPSToolPart 18F56Q71
    
        Sub InitPPS
                  // Module: UART pin directions
                Dir PORTB.1 Out    ' Make TX2 pin an output
                // Module: UART2
                RB1PPS = 0x0018    // TX2 > RB1 11000
    
        End Sub
    
    
    
    
    Dir     Porta.2 In'asc
    Dir     Portc.7 out'led
    'analogue port selection
    #define AdRefSource = AD_REF_AVCC
    #DEFINE ADCChannelSelected ANA2
    #DEFINE ADSpeed highSpeed
    #define AD_Delay 4 10us
    
    dim pot1 as integer
    #define USART2_BAUD_RATE 9600
    #define USART2_TX_BLOCKING
    #define USART2_DELAY OFF
    
    
       Do
    'pot1 =ReadAD12( AN2,true )
    pot1= ReadAD10( AN2,true )
     hserprint pot1'TX2
      HSerPrintCRLF
      wait 500 ms
        Loop
    
        End
    
     
  • Boguslaw

    Boguslaw - 2024-01-19

    I have one more problem with this PIC, it doesn't work #define AD_REF_SOURCE AD_REF_2048
    always is enabled AD_REF_AVCC.

     
    • Anobium

      Anobium - 2024-01-19

      I had already removed this from the test program below.

      Let us fix one thing at the time.

      1. Root cause the READ10() issue and fix.
      2. Root cause the AD REF iissue and fix.
       
  • Anobium

    Anobium - 2024-01-19

    I have cleaned up your program to create this baseline.

    Please test to ensure this shows incorrect results. Share a revised program if you have to change and confirm this does shows the incorrect result

     

    Last edit: Anobium 2024-01-19
  • Boguslaw

    Boguslaw - 2024-01-19

    Your code works similarly to mine, i.e. wrong

     
    • Anobium

      Anobium - 2024-01-19

      Hey, that is good.... at least I have code that does not work!

      There is nothing obvious from the ASM file. The first thing I look for is variables being created that look like Register names but there are none that meet this criteria ( I know this needs some experience but if something like ADCxxxxx would be an obvious bug... )

      Till next week... unless someone else can resolve!

       
  • Jerry Messina

    Jerry Messina - 2024-01-19

    This may be a red herring and I didn't try to track this down, but looking at the asm output with GC v1.01061, as part of ReadAD10 I noticed there are two writes to the ADFM0 bit but none to the ADFM1 bit:

            SYSSELECTEND1
    000000  0103    BANKSEL ADCON0
    0003F8  99F3    BCF ADCON0,ADCS,BANKED
    0003FA  0E01    MOVLW 1
    0003FC  6FFA    MOVWF ADCLK,BANKED
    0003FE  99F3    BCF ADCON0,ADCS,BANKED
    000400  0E0F    MOVLW 15
    000402  6FFA    MOVWF ADCLK,BANKED
    000404  95FA    BCF ADCLK,ADCS2,BANKED
    000406  93FA    BCF ADCLK,ADCS1,BANKED
    000408  81FA    BSF ADCLK,ADCS0,BANKED
    00040A  85F3    BSF ADCON0,ADFM0,BANKED      <<<<<<<<<<<
    00040C  85F3    BSF ADCON0,ADFM0,BANKED      <<<<<<<<<<<
    
     
  • Anobium

    Anobium - 2024-01-22

    I have a real chip on the bench now.


    The range returned by pot1 = ReadAD10( AN2) is correct as per the compiler specification. ReadAD10 will always return the specific range for the specific chip. In this case it is a 12-bit Differential ADC and the range returned is 0 to 4095 which is correct.

    Using the following program segment.

      // define variables
      dim pot1 as Word
    
      Do
    
        pot1 = ReadAD10( AN2 )
        hserprint pot1
        HSerPrintCRLF
        wait 500 ms
    
      Loop
    

    As we now know from the datasheet that the ADC is a 12-bit we know we have to use the 12bit routines to properly use the ADC.

    Using the following program segment.

      // define variables
      dim pot1 as Word
    
      Do
    
        pot1 = ReadAD12( AN2 )
        hserprint pot1
        HSerPrintCRLF
        wait 500 ms
    
      Loop
    

    So, before proceeding ... what is the requirement for 0-1023 ( 10bit value ) ?

    If you need to a true 10bit ADC and not a calculated 10bit value then you need to change the chip.


    So, the compiler is returning a 12bit when using ReadAD12( AN2 ) which is correct as this is a 12Bit ADC.

    Let me know what you thoughts are.

     

    Last edit: Anobium 2024-01-22
  • Boguslaw

    Boguslaw - 2024-01-22

    Ok, thank you for informations. I will use 12bit

     
    • Anobium

      Anobium - 2024-01-22

      Good luck.

       

Log in to post a comment.