A/D converter fails?


Closed Thread
Results 1 to 15 of 15
  1. #1
    egberttheone's Avatar
    egberttheone Guest

    Default A/D converter fails?

    I'm building a system to monitor a few powerlines it works well but some times the conversion results into a 0; i took the data sheet of the pic i use (16f88) read the whole chapter about it over and over and i finished up changing my code(snippets; of course the enable bit and so are set) from this:
    Code:
    'GO/DONE: A/D Conversion Status bit
    'If ADON = 1:
    '1 = A/D conversion in progress (setting this bit starts the A/D conversion)
    '0 = A/D conversion not in progress (this bit is automatically cleared by hardware when the A/D
    'conversion is complete)
    symbol AD_Progress = ADCON0.2
    
    
    ADCON0.5 = 0
    ADCON0.4 = 0
    ADCON0.3 = 0
    AD_Progress = 1
    while AD_Progress = 1: wend
    Sound_Level.lowbyte = ADRESL
    Sound_Level.highbyte = ADRESH
    to this:

    Code:
    'GO/DONE: A/D Conversion Status bit
    'If ADON = 1:
    '1 = A/D conversion in progress (setting this bit starts the A/D conversion)
    '0 = A/D conversion not in progress (this bit is automatically cleared by hardware when the A/D
    'conversion is complete)
    symbol AD_Progress = ADCON0.2
    
    ADCON0.5 = 0
    ADCON0.4 = 0
    ADCON0.3 = 0
    pause 50
    AD_Progress = 1
    while AD_Progress = 1: wend
    Sound_Level.lowbyte = ADRESL
    Sound_Level.highbyte = ADRESH
    as I understood of the manual: if I change the input port from the a/d converter I need to wait a minimum of time before recalling a new a/d conversion. So far so good but what happens is it stucks @ "while AD_Progress = 1: wend" because it simply does not clear any more the code above without the pause 50 does work! so if I remove that line "pause 50" the code runs but some times I get a 0 in the result of the a/d conversion :S I don;t get it do you?
    Last edited by egberttheone; - 7th February 2006 at 20:42.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    ADCON0.0 = 1 ' <-- Turn ON A/D module
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    egberttheone's Avatar
    egberttheone Guest


    Did you find this post helpful? Yes | No

    Thumbs down

    Quote Originally Posted by Bruce
    ADCON0.0 = 1 ' <-- Turn ON A/D module
    please read before replying; i told that all these bits are set AND i sad that it works without the pause command or do you want to say that beacause i added the command line "pause 50" that the a/d conversion module shuts off?

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Sorry. It's hard to "guess" how you're configuring things if you don't show it in your code.

    I'm sure this isn't what the rest of your own application is doing, but it works fine with the
    pause 50 in there;
    Code:
        @ DEVICE HS_OSC, WDT_ON, LVP_OFF, PROTECT_OFF
        DEFINE OSC 20
        
        SYMBOL AD_Progress = ADCON0.2
        SYMBOL ADON = ADCON0.0
        SYMBOL LED = PORTB.0
      
        Sound_Level VAR WORD
        
        TRISA.0 = 1     ' A/D input
        PORTB.0 = 1     ' LED on at boot
        TRISB.0 = 0     ' LED output
          
        ADCON0 = 128 ' A/D clock Fosc/32, RA0/AN0, A/D disabled
        ANSEL = 1       ' RA0 analog
        ADCON1.7 = 1  ' right justify for 10-bit
    
    Main:
        ADON = 1        ' turn on A/D module
        PAUSE 50        ' wait
        AD_Progress = 1 ' start conversion
        
        WHILE AD_Progress = 1 : WEND
        Sound_Level.lowbyte = ADRESL
        Sound_Level.highbyte = ADRESH
        LED = LED ^ 1    ' toggle LED for visual
        GOTO Main
        
        END
    I left the WDT enabled just to be sure PBP was handling it during the pause periods.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    egberttheone's Avatar
    egberttheone Guest


    Did you find this post helpful? Yes | No

    Default

    this code works but if you change the input port of the a/d convertor and then add a pause in the line between changing and a/d conversions start then it stucks.

    example:

    Code:
    'retrieve sound level
    ADCON0.5 = 0
    ADCON0.4 = 0
    ADCON0.3 = 0
    pause 50
    AD_Progress = 1
    while AD_Progress = 1: wend
    Sound_Level.lowbyte = ADRESL
    Sound_Level.highbyte = ADRESH
    
    'retrieve negative level
    ADCON0.5 = 0
    ADCON0.4 = 0
    ADCON0.3 = 1
    pause 50
    AD_Progress = 1
    while AD_Progress = 1: wend
    Negative_Voltage.lowbyte = ADRESL
    Negative_Voltage.highbyte = ADRESH
    
    'retrieve positieve level
    ADCON0.5 = 0
    ADCON0.4 = 1
    ADCON0.3 = 0
    pause 50
    AD_Progress = 1
    while AD_Progress = 1: wend
    Positive_Voltage.lowbyte = ADRESL
    Positive_Voltage.highbyte = ADRESH
    Last edited by egberttheone; - 8th February 2006 at 21:12.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    That's odd. It works for me without getting stuck.

    I modified your version to include 3 LED's in the last section of each channel read routine,
    and all 3 LED's blink right along.

    It doesn't appear to be getting getting stuck at any point.
    Code:
        @ DEVICE HS_OSC, WDT_ON, LVP_OFF, PROTECT_OFF
        DEFINE OSC 20
        
        SYMBOL AD_Progress = ADCON0.2
        SYMBOL ADON = ADCON0.0
        SYMBOL LED1 = PORTB.0
        SYMBOL LED2 = PORTB.1
        SYMBOL LED3 = PORTB.2
      
        Sound_Level VAR WORD
        Negative_Voltage VAR WORD
        Positive_Voltage VAR WORD
        
        TRISA.0 = 1         ' A/D input
        PORTB = %00000111   ' LED's on at boot
        TRISB = 0           ' LED outputs
          
        ADCON0 = 128    ' A/D clock Fosc/32, RA0/AN0, A/D disabled
        ANSEL = 1       ' RA0 analog
        ADCON1.7 = 1    ' right justify for 10-bit
    
    Main:
        ADON = 1        ' turn on A/D module
        PAUSE 50        ' wait
        AD_Progress = 1 ' start conversion 
        WHILE AD_Progress = 1: WEND
        Sound_Level.lowbyte = ADRESL
        Sound_Level.highbyte = ADRESH
        LED1 = LED1 ^ 1    ' toggle LED for visual
        
        'retrieve negative level
        ADCON0.5 = 0
        ADCON0.4 = 0
        ADCON0.3 = 1
        PAUSE 50
        AD_Progress = 1
        WHILE AD_Progress = 1: WEND
        Negative_Voltage.lowbyte = ADRESL
        Negative_Voltage.highbyte = ADRESH
        LED2 = LED2 ^ 1    ' toggle LED for visual
        
        'retrieve positieve level
        ADCON0.5 = 0
        ADCON0.4 = 1
        ADCON0.3 = 0
        PAUSE 50
        AD_Progress = 1
        WHILE AD_Progress = 1: WEND
        Positive_Voltage.lowbyte = ADRESL
        Positive_Voltage.highbyte = ADRESH
        LED3 = LED3 ^ 1    ' toggle LED for visual
        GOTO Main
        
        END
    Are you reading 0-5 VDC voltage levels?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    egberttheone's Avatar
    egberttheone Guest


    Did you find this post helpful? Yes | No

    Default

    yes, i'm reading voltage's between 0-5VDC. i found out that it doesn't mather where i place the pause command(in the interrupt routine); it instant freez the pic forever. The AD routine is placed in a interrupt routine is it not possible to place a pause periode into a interrupt handel?

    note: timer0, timer1 and serial interrupt are activated.

  8. #8
    egberttheone's Avatar
    egberttheone Guest


    Did you find this post helpful? Yes | No

    Default

    any one ?

  9. #9
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Are you using BASIC or assembler interrupts?

    Can you post the code you're having problems with?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  10. #10
    egberttheone's Avatar
    egberttheone Guest


    Did you find this post helpful? Yes | No

    Default

    i do not want to post the code; but i can email it to you. i'm using the basic interrupt routine (on interrupt goto handel)

  11. #11
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Are you clearing "all" interrupt flags before exiting your interrupt handler?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  12. #12
    egberttheone's Avatar
    egberttheone Guest


    Did you find this post helpful? Yes | No

    Default

    Yes, but can i use a pause command in the interupt?

    Code:
    Disable ' interrupt handler
    Handle:
    
    if RCIF = 1 then ' serial intterupt
    RCIF = 0 ' clearing interrupt flags
    IF OERR THEN            ' Clear over-runs if OERR set
    CREN = 0             ' Disable USART receive
    CREN = 1             ' Re-enable USART receive
    ENDIF
    'Serial commands.
    endif
    
    
    IF TMR0IF = 1 THEN 'RECEIVE all A/D value's
    TMR0IF = 0
    gosub CoversionsAD
    gosub saftycontrol  'check al received value's
    ENDIF
    
    if TMR1IF = 1 then
    TMR1IF = 0
    gosub PacketSend
    endif
    
    TimeOut:
    resume
    enable
    Last edited by egberttheone; - 13th February 2006 at 18:14.

  13. #13
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    You can use all the pause statements you want inside a BASIC interrupt, however, I don't recommend you place gosubs in your interrupt handler.

    This exits the interrupt handler, and if it lands in a sub routine where you are not disabling PBP's auto-insertion of interrupt checking stubs, then it's going to go totally nuts on you.

    Place all of your routines inside the interrupt handler, or set/clear some type of flags to indicate the interrupt source, then return, then act on the other sub routines as needed.

    Also note that RCIF can NOT be cleared with RCIF = 0. The only way to clear RCIF is to read the fifo USART buffer until it's empty. Trying to clear this flag like you are, you're leaving it set, and never clearing the USART interrupt flag bit.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  14. #14
    egberttheone's Avatar
    egberttheone Guest


    Did you find this post helpful? Yes | No

    Default

    ah! the gosubs seems to be the problem ! but now I have the problem that the a/d conversion some times fluctuates(value's are not the same every querying + 1000 some times). Where can I begin to debug? Thx !

  15. #15
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    A/D readings will pretty much always fluctuate. A small RC filter on each A/D input may help. If not, you might consider putting the PIC to sleep prior to A/D readings to reduce in-circuit noise from the oscillator & internal switching.

    If you think "code" may be causing the fluctuations, then simply strip down the program to nothing but reading & displaying A/D values. If it works smoothly, then take a hard look at the rest of your code.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Replies: 9
    Last Post: - 31st July 2008, 08:56
  2. A/D converter sample
    By allan josephus in forum mel PIC BASIC
    Replies: 5
    Last Post: - 23rd February 2008, 19:44
  3. A/D converter with pic16f84, i need a help
    By micro in forum General
    Replies: 0
    Last Post: - 13th December 2005, 21:17
  4. help with pic16f870 a/d converter
    By cammo in forum mel PIC BASIC
    Replies: 3
    Last Post: - 22nd March 2005, 05:16
  5. Re Cs5532 A/d Converter
    By massquip in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th April 2004, 16:31

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts