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.
Like I said, untested code but the general idea is there and you should be able to figure it out.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.
/Henrik.
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.
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:
it should beCode:myValye = 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.Code:myValue = TMR0 * 32 'myValue now contains the time in us (5000us=5ms)
A chip with 16 bit timer would be more precise here.
Ioannis
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?
Last edited by grimmjow; - 15th October 2010 at 21:52. Reason: typing error
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.
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.
Bookmarks