Stable ADC readings


Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 40 of 91
  1. #1
    Join Date
    Jul 2010
    Posts
    19

    Default Stable ADC readings

    I am trying to get stable ADC readings.

    I got a PIC 16F688 hooked up to a battery with a voltage regulator and with proper bypass capacitors. Each time I poll the ADC using ADCIN I get a different reading. Even averaging 50 samples is not enough to get a stable value. I have tried both internal and external oscillators (up to 20MHz).

    I am testing it using a simple voltage divider with two resistors of the same value so that I get a value around 512 (ADC in 10 bit mode).

    Is there anything I can do to stabilize the readings? And what is the source of all the noise?

    Typically I am getting values oscillating from 432 up to 457, that's about a 5% oscillation.

    There has to be a way to make the readings more accurate.6

  2. #2
    Join Date
    Mar 2006
    Location
    China
    Posts
    266


    Did you find this post helpful? Yes | No

    Default Voltage reference

    If you use VDD as the voltage reference for your AD then it is most likely a not stable powerline. I know you said battery and voltage regulator but have you tried to measure the VDD with an ocsillocope?

    If you really need to get high accuracy reading you might have to consider an external high precition voltage reference. That one you can connect to the VREF pin and then you can set up the AD to use that one.

    Also if you are switching other stuff while doing the measurement it might affect the power supply to the pic as well.

    I would start with making sure the power nice and clean and batteries are not the best source when you start doing new designs.

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,

    May we get a scheme , plus your program ???

    Crystal ball is out of range ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Adding to what Jumper said, I also face similar issues.
    Usually, using 7805 or 78L05 may not provide a stable 5V.
    Say it is actually between 4.98V and 5.02V. (approx 5% datasheet statement)

    So, if you are using 10-bit reading there is your problem.
    Eventually, Jumper's suggestion of having a high precision voltage reference is a good idea.

    Another issue :
    Pausing 2ms after each ADCIN command may help.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    And...

    More CAPs.
    A capacitor from the ADC pin to VSS will help smooth things out. Will not make up for a bad power supply though.
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi, Sayzer

    I am testing it using a simple voltage divider with two resistors of the same value so that I get a value around 512 (ADC in 10 bit mode).
    That means he works in ratiometric mode and supply changes do not interfere here ...
    so, problem is elsewhere ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi, Sayzer



    That means he works in ratiometric mode and supply changes do not interfere here ...
    so, problem is elsewhere ...

    Alain
    Yes Alain, I missed that.

    Then, may be he should pause ~2ms after each read.

    Also, for example, reading lower voltages and reading higher voltages may behave differently.
    If reading higher voltages are accurate, but lower voltages are not, then there is something else, too.




    ---------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Input impedance too high, too short of ADC aquisition time, noisy PSU, unstable PSU, no averaging routine, multiplexed function on the ADC pin, loose/dirty bread board contacts, poor wiring, poor oscillator, poor decoupling, etc... quite a few possibilities...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Jul 2010
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    As mentioned I am decoupling, using averaging, and testing the ratio of two resistors hooked up to the same battery after the regulator.

    Let me draw the schematic and I will post.

    But before that, let me say that I switched from ADCIN to doing a manual ADCON0.1 = 1 reading, and now I get a stable value. So my problem is an erroneous use of the ADCIN command or a problem in the logic of my routine.

    BEFORE:
    Code:
    DEFINE ADC_BITS 10 ' Set number of bits in result (8, 10 or 12)
    DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds
    ...
    WSum = 0
    Pause 100
    
    For i = 0 to 49
    ADCIn PinProbe, W0
    Pause 10
    WSum = WSum + W0
    Next i
    
    W0 = WSum / 50
    NOW:
    Code:
    Pauseus 50 'Stabilize
    ADCON0.1 = 1  'start the ADC
    WHILE ADCON0.1 : WEND
      
    W0.HighByte = ADRESH
    W0.LowByte = ADRESL
    It works, now the reading stays fixed and it is much faster. (I even didn't have to do any averaging)

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,

    What about reinitialising your Wsum value to 0 @ each 50 samples batch ??? don't you run this loop in another continuous loop ... ??? " par hasard " ...

    BTW " PinProbe " is the alias for which pin ???

    Alain
    Last edited by Acetronics2; - 22nd January 2011 at 18:10.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  11. #11
    Join Date
    Jul 2010
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi,

    What about reinitialise your Wsum value to 0 @ each 50 samples batch ??? don't you run this loop in another continuous loop ... ???

    Alain
    Oh I do. I left that part out of the code I pasted, but it is indeed running in an outside continuous loop.

    Code:
    PinProbe VAR PORTA.2
    ...
    myLoop:
    
    WSum = 0
    Pause 100
    
    For i = 0 to 49
    ADCIn PinProbe, W0
    Pause 10
    WSum = WSum + W0
    Next i
    
    W0 = WSum / 50
    ...
    LCDout $fe, 1 'Clear Screen
    Lcdout $fe, $80
    Lcdout "Reading: ", DEC W0
    
    Goto myLoop
    The readings I get with the ADCIN method are in the ballpark but move around too much. With the "manual" ADC method I get a stable value.
    Last edited by dream-; - 22nd January 2011 at 18:15.

  12. #12
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    ADCIN also polls for the ADCON0.1 bit ...

    sooo .... aheum, how to tell it ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    ADCIN also polls for the ADCON0.1 bit ...

    sooo .... aheum, how to tell it ...

    Alain
    Yeah but we already say some odds with ADCIN in the past on some specific PIC, Microchip are good to change the way register are done, and how things work for PIC to PIC model...

    This said, I never use ADCIN myself, but manually write/read ADC registers... it is also a bit less code hungry... down side "may" be when you port your code on another PIC who's not working the same way, or have different register name, bit assignment. big deal
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  14. #14
    Join Date
    Jul 2010
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Well if I understood what the problem was I would not be asking here

    The fact is that the ADCIN code gives me an oscillating reading, and the manual method gives me a stable value.

    I don't know the reason, but I would like to know. This is a 16F688.

  15. #15
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    For what it is worth I agree with Steve.
    I tried ADCIN a couple of times and did not like it. Others swear by it though. ..

    Keep doing it the way you are.
    Dave
    Always wear safety glasses while programming.

  16. #16
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I would love to see the whole code with ADCIN. Sometime if you play with some ADC registers, and you place them Before or After the DEFINEs, it may end-up with unexpected results.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  17. #17
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Found in the Datasheet ...

    Note:
    The GO/DONE bit should not be set in the
    same instruction that turns on the ADC.
    Refer to
    Section 8.2.5 “A/D Conversion

    Procedure”
    .
    a beginning of an explanation ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

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


    Did you find this post helpful? Yes | No

    Default

    ADCIN doesn't work with port pin aliases. Use ADCIN 2,W0, or define PinProbe as a constant. PinProbe CON 2 would work.

    Also ADCIN doesn't setup TRIS, ANSEL or ADCON1. So you end up with Fosc/2 for your A/D clock because ADCON1 defaults to all 0's. So you're way out of spec for a 4MHz osc. Fosc/8 would be a good one to use with 4MHz (see datasheet).
    Regards,

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

  19. #19
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Bruce ... 1K points ...

    as always !!!...

    Shame on me.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  20. #20
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Blonde moment here too

    Disclaimer: "Blonde moment" is a deliberate sentence that belongs to Mister_e user and plah plah plah, shall not be considered as offensive plah plah plah. Complaints about that shall not be send to Admin, Mods, user but only to Mister_e user plah plah plah....
    Last edited by mister_e; - 23rd January 2011 at 20:37. Reason: Disclaimer
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  21. #21
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi, Steve

    I Think the "Blonde" term use rights already belong to KarenHornby ...

    so, you'll have to buy a licence ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  22. #22
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    busted .....
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  23. #23
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    So to summarize 'in speak' that won't scare folks who thought if/then statements were an uinsurmountable hurdle.....are we saying that using the ADCIN is not optimum...and if not could someone please post a chunk of code to do this in an alternative method?

  24. #24
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Dave
    Always wear safety glasses while programming.

  25. #25
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Excellent.....many thanks, I shall use that code gratuitously.

  26. #26
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Have you tested this fast averaging with Hysterisis by Darrel:

    http://www.picbasic.co.uk/forum/cont...ith-hysteresis

    Ioannis

  27. #27
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Yes I have (& very good it is too) , but I'd rather have this jittery AtoD values 'cleaned up' at the source (rather than putting averaging in place to address unstability caused by avoidable factors)

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


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Just include an RC filter on the A/D input, and put the PIC to sleep just before enabling the A/D conversion. This returns very stable readings in most applications with a stable power supply or Vref.
    Regards,

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

  29. #29
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Nice subject !
    I use a similar code for reading the over-current of one DC motor :
    Code:
    check:           
     vt  =0
     vs  =0
     grup=0
     cnt =0
     pause 50					
     for cnt = 1 to 10
     	adcin 3, adval
     	vt=adval * 5000
     	vt=div32 1023
     	grup=vt+grup
     	pause 5
     next cnt
     vs=grup / 10
          if (vs > checkin) or (T1Count > 50) then 		   
      	     	gpio.0=0
               	gpio.5=0
          endif
    If the averge of 10 readings it's bigger than checkin value (or time exceed 5 seconds) then gpio.0 and gpio.5 goes low.
    It's one good ideea to use ADCON instead ADCIN ? How to do this and keeping average of 10 readings ?

  30. #30
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Need advice, please !

    It's this correct ?
    In header of code I add :
    Code:
        ADCON0 = %10001101      ' ADC input on GPIO.4/AN3/pin3 (page 41)
                                ' MSB 1 = Right Justify                   "1"    
                                '     0 = Vdd as voltage reference        "0"
                                '     0 = n/a                             "00"
                                '     0 = n/a
                                '     1 = select channel 03 (bit 2 of 2)  "11"
                                '     1 = select channel 03 (bit 1 of 2)
                                '     0 = conversion status (see routine) "0"
                                ' LSB 1 = A/D converter module is on      "1"
    And I modify the "check" subroutine like that :
    Code:
    check:           
     vt  =0
     vs  =0
     grup=0
     cnt =0
     pause 50			
     for cnt = 1 to 10
          ADCON0.1 = 1
          NOT_DONE:
    		if ADCON0.1 = 1 THEN NOT_DONE
                Pause 10
                adval.highbyte = ADRESH
                adval.lowbyte = ADRESL
     PAUSE 10
     	vt=adval * 5000
     	vt=div32 1023
     	grup=vt+grup
     PAUSE 10
     next cnt
     vs=grup / 10
          if (vs > checkin) or (T1Count > 50) then 		   
      	     	gpio.0=0
               	gpio.5=0
          endif
    The hardware it's in picture.
    Thanks !
    Attached Images Attached Images  

  31. #31
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    I've tested the code ; it's OK.

  32. #32
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    I've used ADCIN pretty much from day one, but I noticed I was getting some *very* jittery readings (255,243,246,255,243,246 etc)...I couldn't get to the bottom of it (spent a good while trying the usual stuff), I'd forgot about this thread but thankfully foiund it with Google)

    It's the first time I've used the manual ADC method......but I'm pleased to also report my ADC readings are now solid as a rock.

    Just for the record, here's what I used on my 12lf1822 (I'm only using 8 dits ADC)...

    Code:
    ADCON0 = %00000100     ' select ch1 (AN1 Pin 6)
    ADCON1 = %00000000     'set ADC +ve reference to VCC 
    ' 
    main:
    gosub Do_ADC
    pause 100
    hserout [DEC ADC_RESULT,13,10]
    goto main
    ' 
    'ADC sub routine...
    Do_ADC:
    PAUSEUS 50              ' Wait for A/D channel acquisition time
    ADCON0.1 = 1           ' Start conversion
    WHILE ADCON0.1       ' Wait for it to complete
    WEND
    ADC_Result = ADRESH
    return
    Many thanks to you all on this thread...I shall never go back to the ADCIN method!
    Last edited by HankMcSpank; - 26th June 2011 at 18:52.

  33. #33
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    I wish the edit feature was open longer - I forgot to turn the ADC on above, sohere's the correction....

    Code:
    ADCON0 = %00000101     ' select chan1 (AN1 Pin 6), turn ADC On Bit 0




  34. #34
    Join Date
    Aug 2011
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Hello,
    i made this to programm ADC

    bmf_comm1 VAR PORTB.0
    bmf_comm2 VAR PORTB.1
    'Variables
    bmfsD VAR WORD
    bmfs1D VAR WORD
    iled var byte
    blink1 var byte 'pour clignotement 1ère led
    blink2 var byte 'pour clignotement 2ème led
    'configuration du port pour lire l'entrée analogique
    TRISA = %00000011 ' RA0 et RA1 en entrée
    TRISB = %11111100 ' RB0 et RB1 en sortie
    ADCON1 = $10000010 'RA0 à RA3

    test_depart:
    Low bmf_comm1
    Low bmf_comm2
    High bmf_comm1
    high bmf_comm2
    pause 2000
    goto mainloop
    'lecture de la valeur
    getad:
    PAUSEUS 50 ' Wait for A/D channel acquisition time
    ADCON0.2 = 1 ' Start conversion
    WHILE ADCON0.2 ' Wait for it to complete
    WEND
    Return

    ' Subroutine to get pot x value
    getx:
    ADCON0 = $41 ' Set A/D to Fosc/8, Channel 0, On
    Gosub getad
    bmfsD = ADRESL + (ADRESH*256)
    Return

    ' Subroutine to get pot y value
    gety:
    ADCON0 = $49 ' Set A/D to Fosc/8, Channel 1, On
    Gosub getad
    bmfs1D = (ADRESH*256) + ADRESL
    Return
    val_leds:
    low bmfs_comm1
    low bmfs_comm2
    gosub getx
    gosub gety

    if (bmfsD > 1000) Then Goto val_leds
    if (bmfsD > 750) Then Goto boucle_led
    if (bmfsD > 500) Then Goto boucle1_led
    if (bmfsD > 250) Then Goto boucle2_led

    High bmf_comm1
    High bmf_comm2
    pause 1500
    goto val_leds
    boucle_led:
    iled = 0
    while iled < 5
    if iled = 1 or 3 or 5 then high blink1:low blink2
    if iled = 0 or 2 or 4 then low blink1:high blink2
    iled = iled + 1
    pause 1000
    wend
    goto val_leds
    boucle1_led:
    High bmf_comm1
    low bmf_comm2
    pause 3000
    goto val_leds
    boucle2_led:
    low bmf_comm1
    high bmf_comm2
    Goto val_leds
    End

    The leds works but always lightning , action on button (RB0 only for the moment) don't change anything.
    No blinking .
    I don't know what .
    thanks.

  35. #35
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Salut et Bienvenue sur le forum

    I haven't check the whole thing in deep but I think you should modify the boucle_led section to
    Code:
    boucle_led:
            iled = 0
            while iled < 5
                    if iled = 1 or 3 or 5 then 
                            high blink1
                            low blink2     
                            endif
                            
                    if iled = 0 or 2 or 4 then 
                            low blink1
                            high blink2
                            endif
                    iled = iled + 1
                    pause 1000
                    wend
            goto val_leds
    Probably it will solve your problem though... let me know the PIC you're using and your PBP version.

    EDIT: Your code have 2 compilation error
    1) no mainloop label
    2) adcon1: the way you have written it, it's value should start with a % instead of a $
    Last edited by mister_e; - 24th August 2011 at 19:07.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  36. #36
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Looking at your code, I guess it's your first program, There's quite a few mistakes in and it is also hard to follow what you want to do.

    No problem, been there...done that. Tell us what you want to do and we could build something to help you to get started.

    Tell us the PIC your're using and post your schematic if you have any, or describe it.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  37. #37
    Join Date
    Aug 2011
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Sorry for double post but i tried many many modifications.
    my pic is 16F877A , i desire to put multifunctions buttons of steering wheel and decode for command radio car system .
    i make a test with leds for ADC value input .
    Oh,oh,oh,
    i have just see that i don't put MCLR to any connection , a resistor of 4.7kohms to +5V ?

    It is a very simple schematic for test but the entire schematic is for steering wheel command and FIS Tachometer car display modification in live .
    example : see the kwp1281 protocol codes of line K (not can bus) like VCDS soft but more simple.

    This is the first part ( BMF )

    For mainloop , it is because i modify the labels and i forgot it (val_leds) .
    For $ , it is a mistake , THANKS.
    i will try now

    Regards
    Last edited by mazoul72; - 24th August 2011 at 21:42.

  38. #38
    Join Date
    Aug 2011
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    my leds light off after 2 sec , it is ok for starting (mainloop replaced by val_leds)
    But after i have only two leds lightning together , it is not good with all boutons
    and i have no blinking leds

    And now i have no actions on leds (off always) , i don 't undertand why , AD not stable or other....
    it is the same with or without MCLR .

  39. #39
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Ah ok I see, you read a voltage then act upon. Cool, I did something like that awhile back but it was for car remote starter purpose (Door lock, alarm bypass, Windows roll-up/down) Oh well, those were the days

    Yes MCLR must be tied to Vdd. 4.7K is fine.

    not sure of what you want to do and how it should flash your LEDs but try this one
    Code:
            bmf_comm1   VAR PORTB.0
            bmf_comm2   VAR PORTB.1
            
            LEDBlinkRate CON 200
            
            'Variables
            bmfsD       VAR WORD
            bmfs1D      VAR WORD
            iled        var byte
            
            'configuration du port pour lire l'entrée analogique
            TRISA = %00000011       ' RA0 et RA1 en entrée
            TRISB = %11111100       ' RB0 et RB1 en sortie
           
            DEFINE ADC_BITS 10      ' Set number of bits in result
            DEFINE ADC_CLOCK 3      ' Set clock source (rc = 3)
            DEFINE ADC_SAMPLEUS 50  ' Set sampling time in 
            ADCON1 = %10000010      ' RA0 à RA3
    
    test_depart:
            PORTB = 3               ' Turn ON PORTB<1:0>
            pause LEDBlinkRate      '  
           
    val_leds:
            PORTB = 0               ' Turn OFF PORTB<1:0>
            PAUSE LEDBlinkRate        
            ADCIN 0,BMFSd           ' Read CH0
            ADCIN 1,BMFS1d          ' Read CH1
            if (bmfsD > 1000) Then val_leds
            if (bmfsD > 750)  Then boucle_led
            if (bmfsD > 500)  Then boucle1_led
            if (bmfsD > 250)  Then boucle2_led
            
            goto test_depart
            
    boucle_led:
            PORTB = 1
            for ILED = 0 to 5
                    PORTB = PORTB ^ 3
                    PAUSE LEDBlinkRate
                    NEXT
            goto val_leds
            
    boucle1_led:
            High bmf_comm1
            pause LEDBlinkRate
            goto val_leds
            
    boucle2_led:
            high bmf_comm2
            PAUSE LEDBlinkRate
            Goto val_leds
            End
    seems to work for me here, 4MHz oscillator, some different led blinking opattern depending of the value of AN0.
    Last edited by mister_e; - 24th August 2011 at 22:54.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  40. #40
    Join Date
    Aug 2011
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Re: Stable ADC readings

    Hello,

    it still doesn't work ....I don't understand why .
    The leds are still off whenever i select any buttons .
    I see 3.96 V between middle point of my buttons and external pull up resistors (on RA0 and RA1) .
    When i push buttons , i have 1.44V / 0.80 V / 0.40 V .
    Whith quantum 4.88xxx mV , it gives to me 295 , 164 , 82 .
    For 3.96V , 811 aproximatively .

    LED replace resistors out (to test)

    Name:  bmf.JPG
Views: 4542
Size:  31.4 KB

Members who have read this thread : 3

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