PDA

View Full Version : How to count the time while reading ADC ?



fratello
- 13th June 2020, 17:50
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 !

@ __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

Jerson
- 14th June 2020, 11:18
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

richard
- 14th June 2020, 11:49
its almost like deja vu again
same answer as here (http://www.picbasic.co.uk/forum/showthread.php?t=21072&p=137448&highlight=#post137448)

fratello
- 14th June 2020, 13:29
Thanks for remember !!! I forgot the topic... Mea culpa, I became too old