PDA

View Full Version : 16F877 elapsed time control



BerkayT
- 22nd January 2009, 18:30
Hello everyone,

I am pretty new with Pic Programming . I have to measure the time elapsed between V0-V voltages. I am planning to use a POT as an input and display the time on 2x16 display. I am planning to use 16 F877 .


Any ideas?


Thanks

Berkay

mackrackit
- 23rd January 2009, 02:41
What are the minimum and maximum times you will be needing to measure?

Will you be needing anything else going on at the same time?

Maybe something like


for X = 1 to 255
pause 10
t = t + 1
do whatever, pot
if whatever = thisorthat then tCalc
next x
tCalc:
display t

I know, not real code But an idea for you to try.

So if "t" equals 100 , that would mean about a second has passed.

BerkayT
- 23rd January 2009, 06:58
This is for my master thesis project.

My time range is between 300 - 700 microseconds . Depending on the elapsed time value i will control the valves.

Thanks for your help

BerkayT
- 23rd January 2009, 08:37
how can i start the timer for example when the analog input from the pot is 3 V and stop the timer when it is 5 V ?

thanks

mackrackit
- 23rd January 2009, 13:23
loop:
If pot value => 3V then time
goto loop

time:
for X = 1 to 255
pauseus 10
t = t + 1
do whatever, pot
if pot value = 5V then tCalc
next x
tCalc:
display t
goto time


You may want to consider using ADCIN in place of POT.
Notice I changed the example to PAUSEUS.
And someplace around here there is a discussion about code execution time, or just test it to adjust the PAUSEUS.

BerkayT
- 26th January 2009, 09:40
Thanks for your idea,

One more question. How am i gonna define the voltage. I have added a loop like but it didnt work. What can be the problem ?

ADC_READ:

ADCIN 0, ADCINFO
D=ADCINFO*100/51

timeloop:
if D>3 then
T1CON.0=1
if D>=4 then
T1CON.0=0
endif
ELSE
goto timeloop
endif

mackrackit
- 26th January 2009, 12:07
First off the ADC needs setup, here is an example.
http://www.microengineeringlabs.com/resources/samples/pbp/adcin8.bas

Voltage...
Lets say you are using an 8 bit ADC. Voltage reference is VDD and the PIC is running at 5 volts.
There are 256 steps, 0 to 255.
The ADC value of 255 will represent full scale and be 5 volts.
The ADC value of 127 will represent 2.5 volts.
Looking for 3 volts? The ADC vale would be 153.
Want more accuracy? The 10 bit ADC has 1024 steps.

Your loop. After the set up.


loop:
ADCIN 0, adval ' Read channel 0 to adval
IF adval <= 153 then gosub Xlabel '3 volt and less trigger
IF adval = 255 then gosub Zlabel '5 volt trigger
Pause 100 ' Wait .1 second
Goto loop ' Do it forever

Xlabel:
'Do something
Return

Zlabel:
'Do someting
Return
End
That is the basics.

BerkayT
- 29th January 2009, 14:07
thanks alot
another newbie question
can i use tmrcon istead of for loop
such as;


CalcTime var word
Define LCD_DREG PORTC
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 0
Define LCD_EREG PORTB
Define LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000000 ' Set PORTA analog

ADCINFO VAR BYTE


'================================================= =======================

T1CON.0=0
TMR1H = 0
TMR1L = 0
ADCON1 = 0


ADC_READ:
PAUSE 500

timeloop:
ADCIN 0, ADCINFO
IF ADCINFO => 511 THEN GOSUB STARTTIMER
IF ADCINFO == 980 THEN GOSUB STOPTIMER
PAUSE 100
GOTO timeloop

STARTTIMER:
T1CON.0=1
RETURN

STOPTIMER:
T1CON.0=0
RETURN

GOTO ADC_READ

CLEARWDT
CalcTime.Highbyte=TMR1H
CalcTime.Lowbyte=TMR1L

Loope:
LCDOUT $FE, 1
LCDOUT "t:", CalcTime
Pause 500
Goto loope

GOTO ADC_READ
END

mister_e
- 29th January 2009, 16:57
euh yeah, but the above will never do something else than reading the ADCs?

What do you want to do exactly?

BerkayT
- 30th January 2009, 09:59
euh yeah, but the above will never do something else than reading the ADCs?

What do you want to do exactly?

hmm i want it to read the adc s and there will be a voltage change from 2.5 v to 4.8 V my aim is to measure the time elapsed between 4.8 and 2.5 volts and write it to lcd

thanks alot for helping

mister_e
- 30th January 2009, 14:09
Any idea of the time range between both voltage?

Looks like a kind of ESR measurement to me :D

mackrackit
- 30th January 2009, 14:24
Looks like a kind of ESR measurement to me :D
Electron Spin Resonance ?
http://hyperphysics.phy-astr.gsu.edu/hbase/molecule/esr.html

mister_e
- 30th January 2009, 14:27
;) nope, ESR meter... you know this kind of handy meter to find faulty capacitor?

mackrackit
- 30th January 2009, 14:30
;) nope, ESR meter... you know this kind of handy meter to find faulty capacitor?
Thought so...
Just being a smart _ _ _ :D

BerkayT
- 30th January 2009, 19:21
pretty difficult for a rookie guy aha :)

My time range is between 300 - 700 microseconds .

mister_e
- 30th January 2009, 19:40
Ok so in theory, you shouldn't have any problem with a 16Bits timer. BUT you'll need to compensate for the ADC sampling time.

What you could also do is to use a comparator interrupt...

BerkayT
- 12th February 2009, 08:45
Hi mackrackit

i tried this



loop:
If pot value => 3V then time
goto loop

time:
for X = 1 to 255
pauseus 10
t = t + 1
do whatever, pot
if pot value = 5V then tCalc
next x
tCalc:
display t
goto time


You may want to consider using ADCIN in place of POT.
Notice I changed the example to PAUSEUS.
And someplace around here there is a discussion about code execution time, or just test it to adjust the PAUSEUS.

but it doesnt stop counting for a defined voltage once it is triggered it does not stop for the final voltage.


loop:

adcin 0, adval

If adval => 500 then time
goto loop

time:
for X = 1 to 1023
pauseus 10
t = t + 1
If adval = 1000 then tcalc
next x


tCalc:
LCDOUT $fe, 2,"Time" ,":",DEC t
goto time

Once i stop it i ll adjust the pauseus value
Anything missing? any suggestions?

mackrackit
- 12th February 2009, 17:23
You may want to explore the WHILE-WEND command.