PDA

View Full Version : Calculating time delay between two inputs?



grimmjow
- 15th October 2010, 13:30
Hi everyone,
i'm new at pic programming though i know basic logic of c and qbasic programming. i have a project that i have to calculate time between PORTA.0 and PORTA.1 inputs. (like i'm applying +5 volt for PORTA.0 and after 3ms apply +5 volt for PORTA.1). i dont have any problems about basic statements and loops and i need codes for; when PORTA.0=1 timer will start and it will end when PORTA.1=1 and 5ms would be enough for maximum value.
I've read about ON INTERRUPT and did not understand it well, and if there's no way around ON INTERRUPT i'd appreciate if you can explain it simple.

Thnx a lot.

i'm using XTAL 4 MHZ and pic16f84 but i can use other pics if i have to.

Ioannis
- 15th October 2010, 14:26
Oh dear, Does this '84 have 7 lives?

Anyway, what about:



while !porta.0:wend
start_your_timer_here
while !porta.1:wend
stop__your_timer_here
get_timer_value

grimmjow
- 15th October 2010, 17:19
thnks for your concern, i want excatly that algorythm but i need codes for
start_your_timer_here
stop__your_timer_here
get_timer_value

but as i know there's no command for them at picbasic. Could you share if you have those commands?


Oh dear, Does this '84 have 7 lives?

Anyway, what about:



while !porta.0:wend
start_your_timer_here
while !porta.1:wend
stop__your_timer_here
get_timer_value

HenrikOlsson
- 15th October 2010, 18:15
Hi,
Well, the 'F84 certanly isn't the most flexible device.... Here's an utested piece of code. Bring out the datasheet, look specifically at the TMR0 section. Go back and forth between it and the code below and see if you can understand it.


'We need the WDT postscaler for TMR0. Make sure to disable the WDT in CONFIGs
DEFINE NO_CLRWDT 1

myValue VAR WORD 'Variable to store the count.

Option_Reg.5 = 1 'Set TMR0 to external clock, Make sure to tie PortA.4 LOW.
Option_Reg.3 = 0 'Assign prescaler to TMR0

'We need to time up to 5ms, that's 5000 ticks with a 4Mhz clock.
'TMR0 is only 8 bits wide meaning it will overflow in ~0.25ms
'therefor we need to prescale the clock. We select 1:32 which will
'allow us to time 8.192ms with a resolution of 32us


Option_Reg.2 = 1 'Set prescaler to 1:32
Option_Reg.1 = 0
Option_Reg.0 = 0

TRISA.0 = 1 'Make PortA.0 an input
TRISA.1 = 1 'Make PortA.1 an input

TMR0 = 0 'Clear the TMR0 register.

Main:
WHILE !PortA.0 : WEND 'Wait for PortA.0 to go high
Option_Reg.5 = 0 'Switch TMR0 to internal clock (start counting)
While !PortA.1 : WEND 'Wait for PortA.1 to go high
Option_Reg.5 = 1 'Switch back TMR0 to external clock. (stop counting)
myValye = TMR0 * 32 'myValue now contains the time in us (5000us=5ms)

'Now do whatever with the value.

TMR0 = 0 'Reset count in preparation for next round.
Goto Main 'And do it again.
Like I said, untested code but the general idea is there and you should be able to figure it out.

/Henrik.

grimmjow
- 15th October 2010, 19:19
i am really greatful you for spending your time for my problem HenrikOlsson. I have understood your explanations and logic behind the codes. These codes helped me out.

Thanks a lot.

Ioannis
- 15th October 2010, 20:37
I was hopping that eventually grimmjow would use a more recent chip with a timer switch to set it on or off.

Anyway, nice trick Henrik.

Your code has a typo in the line:



myValye = TMR0 * 32 'myValue now contains the time in us (5000us=5ms)


it should be



myValue = TMR0 * 32 'myValue now contains the time in us (5000us=5ms)


Also while TMR0 is zeroed before new cycle, the Prescaller still holds the previous value.

A chip with 16 bit timer would be more precise here.

Ioannis

grimmjow
- 15th October 2010, 21:52
Yes i do not have obligation to use f84, and i know f877 has Timer1(16bit). Let us check this;

i will use 4MHZ xtal and f877, and it has 16 bit timer ~= 66ms is more than enough for me.

So i do not need to use watchdog timer prescaler commands and i just need to use,

OPTION_REG.5=1 and OPTION_REG.5=0 commands for timer right?

grimmjow
- 16th October 2010, 06:44
i've looked up f877 datasheet and TMR1 has its own control register (we can enable and disable it) and it's two paired (TMR1H and TRM1L)

i am not sure but this is algorythm as i understand, if TMR1L=255 then TRM1H=TMR1H+1

and if i sum them up, timepassed=TRM1H*255+TMR1L i can find time in useconds.

Thanks for your helps.


Yes i do not have obligation to use f84, and i know f877 has Timer1(16bit). Let us check this;

i will use 4MHZ xtal and f877, and it has 16 bit timer ~= 66ms is more than enough for me.

So i do not need to use watchdog timer prescaler commands and i just need to use,

OPTION_REG.5=1 and OPTION_REG.5=0 commands for timer right?

HenrikOlsson
- 16th October 2010, 07:44
Ioannis,
Thanks for catching the typo! As for the prescaler not clearing, I actually thought about that but the datasheet says that writing to TMR0 does clear the prescaler (when it is assigned to TMR0) so it shouldn't be an issue. Now, with the '877 it's of little importance anyway.

grimmjow,
Yes, TMR1 on the 'F877 is controlled thru its own control register and you get its 16bit value almost like you shown:

timepassed = TMR1H * 256 + TMR1L

Make sure you stop the timer before reading it or you MAY get weird results if the low byte happens to roll over between you reading it and the high byte.

And no, you don't need to use any prescaler since the 16bit wide timer allows you to measure 65535us which is far more than you need.

Good luck!
/Henrik.

grimmjow
- 16th October 2010, 08:36
Thanks a lot for your helps, HenrikOlsson and Ioannis.

It is great for beginners to ask pros like you. i will try these codes and im pretty sure that it'll work. I'll let you know if i have problems about some parts again.

Have a nice day.

Ioannis
- 17th October 2010, 13:13
Henrik: I missed that part about the prescaler on F84. Good one!

grimmjow:

remember to declare timepassed as a word variable.

Given that you will still be using 4MHz crystal, the timer step will be 1usec.

Ioannis

grimmjow
- 18th October 2010, 14:52
Henrik: I missed that part about the prescaler on F84. Good one!

grimmjow:

remember to declare timepassed as a word variable.

Given that you will still be using 4MHz crystal, the timer step will be 1usec.

Ioannis

Thank you Ioannis, i'm using it as word.

Btw circuit works fine thnx for your efforrs both Ioannis and HenrikOlsson