How to count the time while reading ADC ?


+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    568

    Default How to count the time while reading ADC ?

    Hi ! After a long time I re-started to use my solder station ...and I tried to build a simple switch.
    I need to read the ADC on 12F675 and doing two actions, one if the time is less than 2 sec, the second if time is bigger than 2 sec.
    I searched through forum but I haven't find a clue ...
    Can someone point me the right direction ?
    Thanks in advance !
    Code:
    @ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON & _CP_ON 
    
    DEFINE OSC  4
    
    CMCON    = 7
    TRISIO   = %00001001
    INTCON   = 0 
    IOC      = 0
    GPIO     = 0
    ANSEL    = %00110001
    
    DataW 		var word
    
    GPIO.1 = 0
    GPIO.2 = 0
    
    ADCON0 = %10000001
    Pauseus 50            			' Wait for channel to setup
    
    ;============================================================================
    
    Main: 
    
        ADCON0.1 = 1			' Start conversion
    	While ADCON0.1=1:Wend		' Wait for conversion
    	DataW.HighByte=ADRESH		' Read variable from ADC and save
    	DataW.LowByte=ADRESL
    
    
    IF DataW < 1020 then
       if time =< 2 sec then GPIO.1 = 1 ; how to count if DataW < 1020 for less than 2 sec ?
       if time  > 2 sec then GPIO.2 = 1 ; how to count if DataW < 1020 for more than 2 sec ?
    ENDIF
       
    
    Return

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    869

    Default Re: How to count the time while reading ADC ?

    I can offer you ideas only.

    If you're comfortable with interrupts, get a timer to ring every 1mS and keep tracking a variable that increments every ring and then use that variable that throughout your program. Use this as the reference timer for everything on your system.

    Other way, you use a hardware timer and poll it for overflow every fractional time. Then count a number of fractions equal to the 2 seconds you require. This does not need interrupts, but will block till the timer overflows

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    1,640

    Default Re: How to count the time while reading ADC ?

    its almost like deja vu again
    same answer as here
    This is more entertaining than Free to Air TV

  4. #4
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    568

    Unhappy Re: How to count the time while reading ADC ?

    Thanks for remember !!! I forgot the topic... Mea culpa, I became too old

Similar Threads

  1. Reading a LTC1865 ADC
    By JTCullins in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th December 2011, 14:54
  2. ADC reading voltage divider....
    By RFEFX in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 19th December 2008, 07:33
  3. Count pulses between VARIABLE TIME
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2007, 12:44
  4. Reading A Photo Resistor using ADC
    By jessey in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 25th January 2007, 09:28
  5. adc reading ac
    By Ruben Pena in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th December 2005, 21:57

Posting Permissions

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