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.
Code:
'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.
Bookmarks