Software ADC Comparator?


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    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

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


    Did you find this post helpful? Yes | No

    Default ok

    here is where i am now, and it still doesn't work, no matter.. also, if I were to set my current reading and previous readings equal, the next loop through would set them to what ever the ADC says at sample time anyway.. so it would be pointless wouldn't it?
    PreviousADC = CurrentADC ?

    @ 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 ' variable for first reading or "previous"
    voltLevAf VAR WORD ' variable for second reading "current"
    voltLevCk VAR WORD ' variable for holding the difference
    threshHold VAR WORD ' variable for the threshold
    timOut VAR WORD ' variable for holding the timeout

    prgSel VAR PORTC.2 ' not in use yet
    swtOut VAR PORTC.3 ' output to LED

    CLEAR

    PAUSE 1000


    autoGainPrg:

    ADCIN 2, voltLevBf ' sample voltage on ADC 2

    PAUSEUS 12

    ADCIN 4, threshHold ' get threshold from pot set up as volt. divider on ADC 4

    PAUSEUS 12

    ADCIN 2, voltLevAf ' sample voltage on ADC 2 again, to check for difference

    PAUSEUS 12

    IF lghtlevAf > lghtlevbf THEN ' IF the second reading is higher than the first,

    voltLevCk = voltLevAf - voltLevBf ' subtract the first reading from the second to get the Dif.

    IF voltLevCk > threshHold THEN ' IF there is a difference, and it is greater then threshold

    HIGH swtOut ' set the LED high

    PAUSE 60 ' pause for 60 mS

    LOW swtOut

    ADCIN 5, timOut ' check volt. div pot on ADC 5, use number as time out - 0 to 1023

    PAUSEUS 12

    PAUSE timOut ' Pause for the time out setting, determined by ADC 5

    ENDIF

    ENDIF

    GOTO autoGainPrg:

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


    Did you find this post helpful? Yes | No

    Default Hope this doesn't make anyones head explode!

    just to make sure the problem i need to solve is clear, i will explain what i want yet again.
    I will not be monitoring a sine wave, the signal will not vary that often actually and will never go negative in relation to ground. but for the sake of an example, I will use sinewave.

    Say that there is a sine wave, the frequency of which does not matter, that is 2 Vp-p.
    This Sine wave is "floating" with its zero point at 2 volts above ground, making the lowest peak 1 volt, and the highest peak 3 volts. then we'll say my threshold is set at 1.5V.
    When the sine wave reaches 1.5V above the 1 volt line (not ground!), it triggers a response from my circuit, or flashes the LED once, and then waits.
    Where the sine wave is "floating" (again, floating, not peaking) doesn't matter, it will be anywhere between 0 and 5 volts and is out of my control, it is an independent circuit.
    The PIC circuit only needs to trigger when there is a positive going change in voltage that exceeds a threshold that i will need to have control of. so therefore, the threshold has to follow the 1 volt ( or 2V or whatever!) line automatically, while i can vary the threshold level, it will always be in reference to the 1 volt line! not ground. please see my example.bmp
    (and yes i know that if my threshold is 1.5 volts, and the signal zero reference exceeds 3.5, the threshold will be out of range, and that is fine because 1.5V is only an example.)
    Attached Images Attached Images  

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Ryan

    Now, I understand what you're trying to do. However, reading the adc like this hardly gives you anything worthwhile.

    Code:
    ADCIN 2, voltLevBf ' sample voltage on ADC 2
    PAUSEUS 12
    ADCIN 4, threshHold ' get threshold from pot set up as volt. divider on ADC 4
    PAUSEUS 12
    ADCIN 2, voltLevAf ' sample voltage on ADC 2 again, to check for difference
    PAUSEUS 12
    Why? The difference between the 2 samples on ADC 2 is just around 25uS. Is this what you really want??

    On reading closely, I think you need to do this
    Code:
    'Find the minimum value
    MinAdc var word         'I'm assuming the ADC to be 10bit or more
    CurrAdc var word
    Threshold var word
    DelayVal  var word
    
    while 1                                     ' loop forever
        ADCIN 2, CurrAdc                    ' current ADC reading
        ADCIN 4, Threshold                 ' threshold setting
        ADCIN 5, DelayVal                   ' delay setting
        if CurrAdc > MinAdc + Threshold then
               High Output
               pause DelayVal
               Low Output
               if CurrAdc <> MinAdc then  ' check if the current ADC value < Min ADC
                 MinAdc = CurrAdc          ' and correct the MinADC value
               endif
        endif
    wend
    Does this do what you want?
    Jerson

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


    Did you find this post helpful? Yes | No

    Default Thanks

    I don't know yet, i'll have to give that code a shot, one thing i did figure out tho is that my justification was wrong, i should have had it set to right justify, and I should have saw that earlier! I used serout to spit out the ADC values and now everything looks like it should, but i'll have to do further testing.

    Thanks again!

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