PDA

View Full Version : TMR1 How it works?



ngeronikolos
- 17th December 2006, 19:28
Hi,

I was looking to the Melanie's Olympic Timer project trying to find some
answers for my project.What I do not understand is how the TRM1 works.How it works backround of
my main code?How it starts and how it stops?
Can I store any time the current value of TMR1 in a variable?

My project:
I have two PIC's (A&B). 8 photransistors are connected to PORTB of each PIC.
I have a loop checking the PORTB of the PIC A.If there is a trigger of any phototransistor of PIC A it is sure that I will
have a trigger from the second group of phototransistors (PICB).I want to measure
the time (is critical for me accuracy in μsecs) between the trigger in A anb B.

CAN I DO MY MEASURMENT WITH TMR1.I NEED SOME EXAMPLES TO SEE HOW IT WORKS.
PLEASE ADVICE


Thanks in advance
Regards
Nikos

mister_e
- 17th December 2006, 20:53
read the section 4.0 of the OlympicTimer document
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=84&d=1091819883

RunningFlag variable start and stop the timer.. at least the increment of. Look at the TickCount section.

Yes you should be able to Store the TMR1 result at any time without too much effect. But the results you need are not store there. See ...

LCDOut $FE,$80,DEC2 Hours,":",DEC2 Minutes,":",DEC2 Seconds,".",DEC2 Hundredths

ngeronikolos
- 18th December 2006, 14:31
hi,

I have already read the OlympicTimer document and it is hard for me to undertand how it works(TMR)?
That is the reason I am asking you?
Let's have an example:

Check all the portb of the PIC A for trigger
SELECT CASE PORTB
CASE 00000001
STORE TMR1 <------
CASE 00000010
STORE TMR1 <------
CASE 00000100
STORE TMR1 <------
CASE 00001000
STORE TMR1 <------
CASE 00010000
STORE TMR1 <------
CASE 00100000
STORE TMR1 <------
CASE 01000000
STORE TMR1 <------
CASE 10000000
STORE TMR1
END SELECT


How I can do that?How I can start the TMR RUNNING?

Please advice
Nikos

ngeronikolos
- 29th December 2006, 17:01
Hello to everyboby and Happy New Year.

I hope that all of your dreams came true in 2007.
Back to code now...
I find, while I search and search, the TEST_ELAPSED_SER.PBP code by Darrel Taylor.
With this you can view a real time clock with HyperTerminal.
That is what I need. But Taylor has a TICK every 1/100th of a second=10msec with 4Mhz external osc.
I want 1msec TICK in a format 0d-00:00:00:000.How I am going to do it?
What i did but it did not work.
TIMER1 should be tick every 1000μsec 65536-1000 = 64536 FC18 (HEX).

Have a look to my code and please let me know where is the faulty?
I have change in the Elapsed.bas
1.Ticks var word
2.TimerConst to 0FC18h
3.if Ticks = 1000 then
Ticks = Ticks-1000

The clock did not working.

Please if Ticks = 1000 then
Ticks = Ticks-1000
Regards
Nikos Geronikolos

Darrel Taylor
- 30th December 2006, 02:59
In changing the Interrupt Handler (ClockCount: ), it has changed the number of system variables it uses. The comparison [if Ticks = 1000 THEN] uses the CMPNE macro which uses the R4 system var.

So the R4 variable needs to be saved and restored the same way as R0 and R1 are saved in the ClockCount routine.

And the constant should be $FC20 which is 8 counts less. This accounts for the time it takes to reload the timer.

HTH,

ngeronikolos
- 11th January 2007, 10:05
Thanks Taylor and Happy New Year,

I manage to make my RTC with resolution in 1m, following your instructions.
I test it for 7 hours and I have lost 1sec.That is ok for my project.

Continioing my project I wonder if I can check all the ports 12 TOTAL (PORTA&PORTB) of the PIC16F628 in a time aproximatelly 500μsec?
I am going to use 20Mhz osc do the pic faster.

Please advice
Nikos

Darrel Taylor
- 12th January 2007, 02:59
In 500μs, you can do all kinds of stuff.

Checking 2 ports at 20mhz shouldn't take more than 1-2μs. but I guess it depends on what you want to do with it after that.
<br>

ngeronikolos
- 12th January 2007, 12:50
Taylor,

Thanks for your interested.
So, you said that the following code running in 20mhz take 2μsec:
----------------------------
START:
If PORTA.1 = 1 THEN
GO TO SOMEWHERE1
ENDIF
If PORTA.2 = 1 THEN
GO TO SOMEWHERE2
ENDIF

GOTO START
--------------------------

I need to check all the ports of A&B in a loop.
1 LOOP SHOULD TAKE MAXIM 500μsec.

Please advice
Nikos

skimask
- 12th January 2007, 14:24
Taylor,

Thanks for your interested.
So, you said that the following code running in 20mhz take 2μsec:
----------------------------
START:
If PORTA.1 = 1 THEN
GO TO SOMEWHERE1
ENDIF
If PORTA.2 = 1 THEN
GO TO SOMEWHERE2
ENDIF

GOTO START
--------------------------

I need to check all the ports of A&B in a loop.
1 LOOP SHOULD TAKE MAXIM 500μsec.

Please advice
Nikos

MPLab has a fine, working simulator in it that will count cycles/time for whatever you want. You just have to set breakpoints at the correct places and keep track of the count in the window. And you can check your .lst file to actually count the number of instruction words used by a particular statement.

At any rate,

Clk = 20mhz = 5mhz instruction rate = .2us per instruction

START: = 0 cycles

If PORTA.1 = 1 THEN
GO TO SOMEWHERE1
ENDIF

is roughly equivalent to a BTFSS instruction, 2 cycles

depending on the PIC and options used, checking all bits in Port A and B is 16 checks. Total of 32 cycles just for the checking. Add in 2 cycles for the jump back to the start, 34 cycles.
So, this loop, not including the GOTO SOMEWHEREs, takes about 34 cycles.

But, we haven't including any PBP interrupt checking or CLRWDTs yet. So, assume that you have an ON INTERRUPT statement somewhere, add in another 8 cycles (I think, I'll check when I get home) for each interrupt check, which happens before each PBP statement. Same thing for a CLRWDT.

Total so far = 34 + ( 17 * 8, interrupt check) + ( 17 x 1, CLRWDT ) = 187 cycles. .2us per cycle * 187 cycles = 37.4us per loop not including any goto's. 26738hz loop rate.

Again, if you have any doubts, check your .LST file generated by the assembler and manually count the number of instructions between the start and end of the loop.