PDA

View Full Version : tmr1 on 16f690



dan gill
- 25th November 2006, 07:38
I've just started playing with the tmr1 on the 16F690. I'm using the t1ckin for an async counter to measure rotational shaft speed. What I can't seem to get is how to determine when the timer stops incrementing, ie: when the shaft has stopped rotating or is slowing down. I have tried the count command, but I can't use it due to the long delay necessary for the count. I know I'm going to feel stupid when someone shows me, but I just have a block or something...I'm only using the tmr1L for the timer, since the initial shaft speed doesn't need to register over 60 revs per second.
I've been reseting the timer once it reaches 60 since I'm only interested when the speed of the shaft is less than 60. Does anyone have any ideas about how to tell when the timer has stopped incrementing???

Thanks,

Dan

Archangel
- 25th November 2006, 08:30
Hi Dan,
Looks like you are a new member, Welcome. As you probably have guessed this has been addressed here before. The forum has a pretty good search engine, on the toolbar at the top. Here is a link to get you started:
http://www.picbasic.co.uk/forum/showthread.php?t=4610&highlight=encoder
Do search for timers too.
Regards
JS

IF you need help after reading in these posts, post back and someone else or myself will help you, sayzer just taught me how to set these up last month.

dan gill
- 25th November 2006, 10:28
Thanks Joe for the help, but I'm still not getting it.

My entire code works perfectly for what I need, except for the timer. I can get it to count fine, but I have no idea when it has quit counting. I'm using a magnetic reed switch and rare earth magnet to drive the t1clkin from high to low and it increments the timer great, but I can't figure out the code to tell the Pic that the shaft is no longer rotating, and that the timer is no longer counting. It has become the bane of my program! Do you have any other ideas??? Thanks...


Dan

Archangel
- 25th November 2006, 10:55
Hi Dan,
Please post your code so the experts and a few of us novices can see what you are doing. Please tell us what pic you are using too.
JS

HenrikOlsson
- 25th November 2006, 11:09
Hi Dan,
One approach could be to read the TMR1L register at certain time intervals and if the value you get is below a certain setpoint you say that the shaft no longer turns.

Get value from counter...
Reset counter to 0...
Compare value to setpoint...
If lower then shaft is stopped or turning too slow...
Do other stuff...(this needs to take aprox. the same amount of time each loop.)
Perhaps pause for a while...
Do it all over again...

Or you could set up TMR0 as an interrupt source and get the value of the TMR1 counter in the interrupt service routine.

/Henrik Olsson.

dan gill
- 25th November 2006, 18:44
Ok guys,

I've tried the reset deal and the comparison deal. I'm driving an R/C servo, so I can't use any other pause other than the 50Hz update pause. I need the tmr1L to do everything in the background without upsetting the servo refresh rate. I used the COUNT command and it would work the best for what I'm doing if it didn't require such a long pause for the count. I spoke with Charles at MELABS and suggested the tmr1 use. Here's a snippet of the program code. I can't use the tmr0 either. I need the pinout. There is no room to use a larger chip either.

Chip is a 16F690


PROGRAMSET:

SEROUT LCD,R,[I,LINE4]
SEROUT LCD,R,["PROGRAMSET"]
A=0
high LED4 'TURN ON GREEN LED TO SHOW SYSTEM READY
FOR A= 1 TO 50 '1 SECOND TIME LOOP COUNTER FOR TMR1L
IF A=50 THEN GROUNDSPEED 'LOOP FOR CHECKING GROUNDSPEED
IF SW1=1 AND SW2=1 THEN RESET 'push switch 1 first
HIGH SERVO
PAUSEUS (1000+S)
LOW SERVO
PAUSE 16
NEXT A
GOTO PROGRAMSET



GROUNDSPEED: 'CHECKS GROUNDSPEED EVERY 1 SECOND

SEROUT LCD,R,[I,1]
SEROUT LCD,R,[I,LINE1]
SEROUT LCD,R,["GROUNDSPEED"]
SEROUT LCD,R,[I,LINE2]
SEROUT LCD,R,["TMR1L=",#TMR1L]
IF TMR1L>60 THEN TIMERRESET 'TMR1L RESETS IF SPEED GREATER THAN 60
HIGH SERVO
PAUSEUS (1000+S)
LOW SERVO
PAUSE 16

My program works wonderfully if I could just get the last of the code to work.
Do you guys think I could use a smaller reset and check it like Henrik has suggested. The only problem I can see with the method is what if the program happens to check the timer when it's resetting? That would give a false reading. The absolute bottom line is I must be positive of when the shaft is no longer rotating, or at the very least, moving very slowly. Once the shaft stops moving, I can tell the servo to move, but not before. I really am upset that I have over 3000 words of code and that I can't figure out what is probably only a few words to complete the program.

I really appreciate all the help. Thanks.

Dan

Archangel
- 25th November 2006, 22:23
Ok guys,

I've tried the reset deal and the comparison deal. I'm driving an R/C servo, so I can't use any other pause other than the 50Hz update pause. I need the tmr1L to do everything in the background without upsetting the servo refresh rate. I used the COUNT command and it would work the best for what I'm doing if it didn't require such a long pause for the count. I spoke with Charles at MELABS and suggested the tmr1 use. Here's a snippet of the program code. I can't use the tmr0 either. I need the pinout. There is no room to use a larger chip either.

Chip is a 16F690


PROGRAMSET:

SEROUT LCD,R,[I,LINE4]
SEROUT LCD,R,["PROGRAMSET"]
A=0
high LED4 'TURN ON GREEN LED TO SHOW SYSTEM READY
FOR A= 1 TO 50 '1 SECOND TIME LOOP COUNTER FOR TMR1L
IF A=50 THEN GROUNDSPEED 'LOOP FOR CHECKING GROUNDSPEED
IF SW1=1 AND SW2=1 THEN RESET 'push switch 1 first
HIGH SERVO
PAUSEUS (1000+S)
LOW SERVO
PAUSE 16
NEXT A
GOTO PROGRAMSET



GROUNDSPEED: 'CHECKS GROUNDSPEED EVERY 1 SECOND

SEROUT LCD,R,[I,1]
SEROUT LCD,R,[I,LINE1]
SEROUT LCD,R,["GROUNDSPEED"]
SEROUT LCD,R,[I,LINE2]
SEROUT LCD,R,["TMR1L=",#TMR1L]
IF TMR1L>60 THEN TIMERRESET 'TMR1L RESETS IF SPEED GREATER THAN 60
HIGH SERVO
PAUSEUS (1000+S)
LOW SERVO
PAUSE 16

My program works wonderfully if I could just get the last of the code to work.
Do you guys think I could use a smaller reset and check it like Henrik has suggested. The only problem I can see with the method is what if the program happens to check the timer when it's resetting? That would give a false reading. The absolute bottom line is I must be positive of when the shaft is no longer rotating, or at the very least, moving very slowly. Once the shaft stops moving, I can tell the servo to move, but not before. I really am upset that I have over 3000 words of code and that I can't figure out what is probably only a few words to complete the program.

I really appreciate all the help. Thanks.

DanHi Dan,
isn't there more to this code? Where are your variable declarations? Are there more subroutines? It's just really hard to guess what you have and harder to copy/paste into pbasic compiler so as to work on this for you, if parts are missing.
JS

dan gill
- 25th November 2006, 22:40
I started messing around with Henrik had said about resetting the timer. I ran the timer thru a one second loop and if the tmr1L>0 then I reset the timer.
I then ran a counter to count up only when tmr1L was zero. The only way to have it be zero, was for the timer to quit counting. I used a count up to 3 just to be sure it wasn't just counting really slow and it works great. It may not be perfect, but it get the rev/second so close that it really doesn't matter for my application. Thanks guys for all the suggestions. you helped me a lot.

Dan

Archangel
- 25th November 2006, 22:58
Hi Dan,
glad you got it. Check your private mail too!