Menu

Using ReadAD10

Help
MBB
2018-12-19
2018-12-21
  • MBB

    MBB - 2018-12-19

    NO FAULT FOUND. THIS IS A POISSON ROUGE.

    I'm using 98.03 with an 18F4550.

    Lets Assume I put a voltage into A/D port AN2 that the PIC A/D should convert to a level of 753.
    With I use this code I get the wrong value:
    #define USE_AD2 TRUE
    Dim X_Plus as word
    X_Plus = ReadAD10(AN2, TRUE)
    print X_Plus ; Reads 1 not 753

    BUT if I change the last line to:
    Print ReadAD10(AN2, TRUE) ;It correctly gives 753.

    Why is this?

     

    Last edit: Anobium 2018-12-21
  • Chris Roper

    Chris Roper - 2018-12-19

    Not tested my end as my simulator crashed but try:
    Dim X_Plus as Integer

     
  • William Roth

    William Roth - 2018-12-20

    1) The 18F4550 has a 10-Bit single-ended ADC. Therefore, "true" is unnecessary and should not be used as a parameter with READAD10. "True" is typically used with 12-bit differential ADC's where a 10 -bit result is desired. By using "true" the library assumes a differential ADC and therefore requires the user variable to be an integer.

    2) Since the ADC result will never be negative if "true" is NOT used, X_Plus can be a word or an integer. If the "true" parameter IS used then X_Plus needs to be an integer.

    3) By default, the a-d.h library defines all ADC channels with "#define USE_ADx TRUE".
    Therefore " # Define USE_AD2 TRUE" really does nothing
    To optimize code: Define the unused ADC channels to FALSE

    My best guess is that #1 above may be causing the the issue is it calls the inappropriate subroutine in the library for single-ended 10-Bit ADC reads . If correcting #1 does not resolve then we will likely need to escalate

    Bill

     

    Last edit: William Roth 2018-12-20
  • MBB

    MBB - 2018-12-20

    Chris and Bill,
    Thanks for the responses.

    I tried all the ideas But, unfortunately I still have the same problem.

    Any other ideas?

     
    • Anobium

      Anobium - 2018-12-20

      Share you code please.

       
    • Anobium

      Anobium - 2018-12-20

      Works here on real silicon.
      I am using the LCD Print routines via the serial to ensure they are working as expected.

      I get the same value printed twice.

      ;Chip Settings
      #chip 16F1789,32
      #config mclr_on
      'USART settings
      #define USART_BAUD_RATE 9600
      #define USART_TX_BLOCKING
      ; Required to LCD to Serial redirection
       #define LCD_IO 0
      #include  <lcd2serialredirect.h>
      
      Dim X_Plus as integer
      X_Plus = ReadAD10(AN2, TRUE)
      Print "     "
      print X_Plus 
      Print "     "
      Print ReadAD10(AN2, TRUE) 
      
       

      Last edit: Anobium 2018-12-20
  • Anobium

    Anobium - 2018-12-20

    Tested on real silicon. The attachment shows the optimisation.

    I get two numbers, the same, lots of them.

    ;Chip Settings
    #chip 18F4550,1
    #config mclr_on
    #option Explicit
    'USART settings
      Dir PORTC.6 Out   'Tx = Pin 25
      Dir PORTC.7 In    'Rx = Pin 26
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    ; Required to LCD to Serial redirection
     #define LCD_IO 0
    #include  <lcd2serialredirect.h>
    
    Dim X_Plus as word
    do
      X_Plus = ReadAD10(AN2, true)
      Print "     "
      print X_Plus
      Print "     "
      Print ReadAD10(AN2, true)
      wait 250 ms
    loop
    
     

    Last edit: Anobium 2018-12-20
  • Anobium

    Anobium - 2018-12-20

    But, you are using a real LCD....

    try this.. what happens?

      print [Integer]X_Plus
    
      Print [Integer]ReadAD10(AN2, TRUE)
    
     
  • Anobium

    Anobium - 2018-12-20

    There may be an issue with the Print handler. Test with the X_Plus as an integer. Three different program runs please. What is the result?

      Print [Integer]X_Plus
      Print [Integer]ReadAD10(AN2, TRUE)
    

    then

      Print [Word]X_Plus
      Print [Word]ReadAD10(AN2, TRUE)
    

    then

      Print X_Plus
      Print ReadAD10(AN2, TRUE)
    
     

    Last edit: Anobium 2018-12-20
  • MBB

    MBB - 2018-12-20

    I found the problem but I'm not sure why it is a problem.

    I'm using this code to read a 4-resistor touchscreen. As you probably know a touchscreen changes its input and outputs to read the X and Y positions i.e the DIR command is used alot.

    I used:
    #define X_Plus PORTA.2
    to simplify the renaming. But then I had to use the DIR commands to change my direction throughout the code. For some reason GCB didn't like using X_Plus for this.

    When I changed X_Plus to PortA.2 everything started working.

    Incidently, I'm using a TFT LCD with this touchscreen and #include <glcd.h>. Everytime I compile I get this warning message:</glcd.h>

    WARNINGs / ERRORs reported by Great Cow BASIC (if Syntax Error, doubleclick on the errormessage below) <<<
    glcd.h (148): Warning: Cannot find C:\GCB@Syn3\GreatCowBasic\include\glcd_nt7108c.h

     
    • Anobium

      Anobium - 2018-12-20

      Oh. OK. So, Dim X_Plus as word was actaully #define X_Plus PORTA.2? That does not make sense - but, no fault found - so this ok.

      Upgrade to v0.90.04 just relased to resolve the glcd issue.

       
  • Anobium

    Anobium - 2018-12-20

    Sorry, I cannot reproduce. I have hooked up a real 4-wire LCD - works ok.

    What it the difference? I get same values on both lines.

      ;Chip Settings
      #chip 18F4550,1
      #config mclr_on
      #option Explicit
    
      ;Setup LCD Parameters
      #define LCD_IO 4
      #define LCD_NO_RW
      #define LCD_Speed Medium
    
    ; ----- Define Hardware settings
      #define LCD_RS PORTc.0
      #define LCD_Enable PORTc.2
      #define LCD_DB4 PORTb.4
      #define LCD_DB5 PORTb.5
      #define LCD_DB6 PORTb.6
      #define LCD_DB7 PORTb.7
    
    
        cls
        X_Plus = ReadAD10(AN2, TRUE)
        print X_Plus
    
        locate 1,0
        Print ReadAD10(AN2, TRUE)
        wait 2 s
    
        Dim X_Plus as word
        do
          cls
          X_Plus = ReadAD10(AN2, TRUE)
          print X_Plus
    
          locate 1,0
          Print ReadAD10(AN2, TRUE)
    
          Wait 250 ms
    
        loop
    
     
  • William Roth

    William Roth - 2018-12-21

    The initial issue seems to be caused by setting X_Plus as both a variable and a constant.
    This code reproduces the symptoms in the first post:

    #define X_PLUS PORTA.2
    
    Dim X_Plus as word
    
    do
       X_Plus = ReadAD10(AN4)
       Locate 0,0
       print X_Plus : Print "    "
       Locate 0,7
       Print ReadAD10(AN4)
       Print "    "
    loop
    

    With the code above the display shows " 0 ................... 451"
    Print X_PLUS returns a 0 and Print ReadAD10(AN4) works as expected

    X_Plus cannot be both a variable and a constant. One or the other but not both .

    When tracking down issue such as this in order to provide good help we need to see the complete code, not just snippits that may not show the of source of the error,

     

    Last edit: William Roth 2018-12-21
    • Anobium

      Anobium - 2018-12-21

      I get this... but, Dim X_Plus as word was in the first post.......

       
  • William Roth

    William Roth - 2018-12-21

    But is that the root cause?

    For a test . . . With 18F25K22, I used the (unnecessary) "true" parameter and made the user variable a WORD. Had no issues.

     
    • Anobium

      Anobium - 2018-12-21

      Root cause. Incorrect posting of the issue.

      True is required, therefore necessary, to force READAD10 to return a 10bit value on specific parts.

      The design of READAD10 as follows. When using ReadAD10 ( ANX ) the returned value is the full range of the ADC module. Therefore, the method may return an 8 bit value [0-255], or an 10 bit value [0-1023] or an 12 bit value [0-4095]. This is dependent on the microcontrollers capabilities. For a 10 bit value [0-1023] always to be returned use user_variable = ReadAD10( ANX , TRUE )

       

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.