Software ADC Comparator?


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Location
    Indiana, USA
    Posts
    72


    Did you find this post helpful? Yes | No

    Default No..

    the first suggestion by mackrackit doesn't take into account the level of "difference"
    i.e. setting the output high based on the amount of change of a varible number, not just by the fact that it changed for one.
    and its classified skimask! but, heres an example:

    say my input is a light dependent resistor, or solar cell, whatever, and its sitting in a dark room, doing nothing, no output. then someone turns on a dim light, the pic would detect the change in light level in the span of one loop, and toggle an output on and off one time because it saw a positve change in voltage on the ADC.
    Then it readjusts for the new light level in the room and sits there, doing nothing, but someone makes the light a bit brighter, and again the output toggles because there is a positve change in light. so therefore, it only toggles the output because there is a positve change in light, it would ignore someone turning the light off, or dimming it. I also need control of how much of a positive change it responds to, like the amount that the voltage level is increased, so if its a very slight change, nothing happens, but if there is a large change, it will respond..
    this is really messing with my poor ol' brain! but i seem to end up asking more questions on here than answering anyway!

    Also, when i say "readjusts" i mean the circuit automatically readjusts its self, like autogain, or something!

    thanks again for your help!
    Last edited by Ryan7777; - 13th February 2008 at 03:17.

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Ryan7777 View Post
    and its classified skimask! but, heres an example:
    I've seen classified (or have I?)...this isn't classified (or is it?)...

    say my input is a light dependent resistor, or solar cell, whatever, and its sitting in a dark room, doing nothing, no output. then someone turns on a dim light, the pic would detect the change in light level in the span of one loop, and toggle an output on and off one time because it saw a positve change in voltage on the ADC. Then it readjusts for the new light level in the room and sits there, doing nothing, but someone makes the light a bit brighter, and again the output toggles because there is a positve change in light. so therefore, it only toggles the output because there is a positve change in light, it would ignore someone turning the light off, or dimming it. I also need control of how much of a positive change it responds to, like the amount that the voltage level is increased, so if its a very slight change, nothing happens, but if there is a large change, it will respond..
    this is really messing with my poor ol' brain! but i seem to end up asking more questions on here than answering anyway!
    Also, when i say "readjusts" i mean the circuit automatically readjusts its self, like autogain, or something!
    If you read thru what you wrote carefully enough...you've just wrote the program that you want....no, really. I was writing it as I was reading it.
    Now my question is, the input can only go so high. Once it gets up to a maximum value, what's going to bring it back down to a nominal nothing?

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    969


    Did you find this post helpful? Yes | No

    Default

    Ryan

    What you're looking for is a derivative function. What you can do is something like this

    Code:
    LastADC  var   word    ' previous error
    CurrADC  var   word    ' current error
    Threshold con  1        ' can be more if you want
    
    loop:
      gosub ReadADC
      Error = CurrADC-LastADC
      if Error < 0 then Error = -Error ' make absolute error
      if Error > Threshold then
        High Output
        pause 1000    ' wait 1 second
        Low Output
      endif
      LastAdc = CurrAdc
      goto loop
    This will more or less achieve what you want to. However, this is pure untested code. Use at your own risk.

    Jerson

  4. #4
    Join Date
    Sep 2006
    Location
    Indiana, USA
    Posts
    72


    Did you find this post helpful? Yes | No

    Default still not it..

    I tired your code, Jerson, and it didn't seem to work right, say the ADC had 2.5 volts applied, and then i turned the voltage up to 3 volts, the output would turn on and off, over and over, rather than toggle once, to show a change happened, and then stop. i changed the threshold value to account for any instabilty in my input voltage, and still no dice. and like a i said before, i need the circuit to "readjust automatically" so that it only cares that there is a positive increase in voltage by a certain value and reacts once and then resets, it shouldn't care what the original ADC value was, only that the value increased. so it would have to "dynamically" adjust itself. you would figure that you would take a reading, and then some miliseconds later, take another reading, subtract the first value from the second and that number would show a difference (if there is any), which would mean a positive change. if that value was over that of the threshold, then react. you would have to clamp that value to a number between 0 and 1023, so there wouldn't be a negative value and a roll over (or under, in this case) which i think is what Jerson's code showed, and i already tried the same thing, in a slightly different way, and neither work... but i could be, and probably am missing something! i really don't understand absolute values (only their vodka!) and how it applies here, all your saying is that the number can only have a certain maximum value right? so couldn't i use MAX?
    Last edited by Ryan7777; - 13th February 2008 at 05:00.

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


    Did you find this post helpful? Yes | No

    Default

    Like skimask said, you have the code in you explanation of the project.
    Jerson gave an "untested" example to get you started.

    Think about this:
    Code:
    Two VAR : read ADC first : write that value to VAR1 : start LOOP : read ADC :_
    write ADC value to VAR2 : IF VAR2 > VAR1 THEN ? : Now make VAR1 = VAR2_
    GOTO LOOP
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Sep 2006
    Location
    Indiana, USA
    Posts
    72


    Did you find this post helpful? Yes | No

    Default This is what i have.

    This is what i got so far, and im not seeing why it doesn't work.
    please excuse the formatting and what not!

    @ DEVICE pic16F684, HS_OSC, WDT_OFF, PWRT_OFF, MCLR_ON, BOD_OFF, IESO_OFF, FCMEN_OFF

    DEFINE OSC 20
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 2
    DEFINE ADC_SAMPLEUS 11

    CMCON0 = %00000111
    VRCON = %00000000

    ANSEL = %00110100

    ADCON0.6 = 0
    ADCON0.7 = 0

    voltLevBf VAR WORD
    voltLevAf VAR WORD
    voltLevCk VAR WORD
    threshHold VAR WORD
    timOut VAR WORD

    prgSel VAR PORTC.2
    swtOut VAR PORTC.3

    CLEAR

    PAUSE 1000


    autoGainPrg:

    ADCIN 2, voltLevBf

    PAUSEUS 12

    ADCIN 4, threshHold

    PAUSEUS 12

    ADCIN 2, voltLevAf

    PAUSEUS 12

    voltLevCk = voltLevAf - voltLevBf

    IF voltLevCk > threshHold THEN

    HIGH swtOut

    PAUSE 60

    LOW swtOut

    ADCIN 5, timOut

    PAUSEUS 12

    PAUSE timOut

    ENDIF

    GOTO autoGainPrg:

    the ADC acts as if there are two seperate voltage values present at the pin even if the voltage is the same and doesnt change. like on the first reading it should read 3 volts, and the second reading should be 3 volts or close enough! it shouldn't flash the LED sitting on the output, but its does, over and over like a blinky circuit, like the two readings are never close to equal, no matter what threshold is set at! so that would make me think that the difference between the two readings is somehow always larger than threshold.. what i'd like to know is, what did Jerson mean by "if Error < 0 then Error = -Error ' make absolute error" ?

    IF Error < 0 THEN
    Error = Error - Error
    ENDIF
    ??? if so, i tried that version of it, and it didn't work either..

  7. #7
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    969


    Did you find this post helpful? Yes | No

    Default

    Ryan

    You seem to be reading 3 diff adc ports. First of all, you have to tell us what is each one for any of the code I gave to make sense.

    The code I gave wants only 1 adc port to sense the light intensity. So, the other 2 are redundant. And the code I gave should do exactly what you asked for.

    Check your code. You seem to have got it diff from what I have given you.

    in Pseudo code what I have sent you does this

    if CurrentADC != PreviousADC
    Output = high
    Wait 1 second
    Output = Low
    PreviousADC = CurrentADC
    endif

    Jerson

Similar Threads

  1. Stable Adc Reading Routine
    By gebillpap in forum General
    Replies: 27
    Last Post: - 13th May 2015, 02:18
  2. Comparator circuit thoughts....
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th October 2009, 06:04
  3. Software Stack
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th December 2007, 10:04
  4. ADC value with 2 decimals on an LCD
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd December 2005, 15:54
  5. 12F675 ADC 'Issues'
    By harrisondp in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2005, 01:55

Members who have read this thread : 0

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