PDA

View Full Version : Reading Timers



kevj
- 27th August 2007, 01:53
I'm trying to get my head around what should be a pretty simple concept - using timers. I've been reading this forum for the better part of the last few days and still can't find anything that really spells it out.

I've seen tons of other posts (several on the first page) asking timer questions - and the PBP manual is conspicously sparce when talking about them - I think the community could use a good tutorial that really spells it out in simple terms so there's a clear understanding. Timers seem to require a certain amount of tribal knowledge - TONS written using them, but very little really 'about' them.

Without writing 8 pages of details about the big picture, here are the very specific things I'm trying to do (the only part I don't understand, the rest of my application is super straight forward).

1. All timer resolutions need to be as short as possible - the 2uS I see published running at 20mhz should be just right, but no slower. The actual PIC doesn't matter right now as I'm still prototyping - once I understand this question I'lll be able to select the right part for my use.

2. The ONLY thing this PIC will do in this part of the program is watch for an input and wait for a clock to run out - that's it.

3. I want to basically do the following paraphrased code:

START TIMER to run out / overflow in exactly 3000 microseconds
Loop:
Check pin 1 for input (if input: Gosub - ActionHandler)
Check if the clock has run out (check timer if timer over flow is > 0)
Loop

This way when the pin I'm watching changes - I jump imediately (within a couple of clock cycles I'd expect) to my ActionHandler

I would really like the ActionHandler to be able to "read" the timer's current position and stop it (or reset it as I no longer need it) and ideally be able to tailor it's response to the period of time within the above timing period the event occured.

So ideally I end up with:

- The clock runs for 3ms without input, catches the "time out" at the end, does something else, then repeats

- Or, during that 3ms, an input is almost immediatley acted upon and I'm also able to see how far into the duration of the 3ms the event occured.


My confusion:

I don't know how to just "read the timer". Or "start the timer" or "stop the timer" or "reset the timer".

I found this post which was somewhat helpful:
http://www.picbasic.co.uk/forum/showthread.php?t=4092

I've been digging in the PBP manual, reading the greek of the Pic data sheet and just getting frustrated looking for the answer. :(

I guess I'm looking for an actual code example of:

Start Timer
Stop Timer
Reset Timer
Read Timver Value

... and if I could do that without writing any assembly, that would be great. I'd like to learn that stuff eventually, but I'm running out of time on this project and I know it would work fine if I could just do the above, and unfortuantely I just don't have the time to learn ASM right now.


And to get the resolution I'm looking for, it looks like I have to read a 16 bit value, which Paul Borgmieier was very helpful in the above post.

And also, as long as I'm confused and asking silly questions:

What does "CCP" mean? The above post mentions it, I see it all over the forum and the Pic data sheet only quotes it once as "CCP1" in the section with the rest of the greek talking about the timer - so it's got something to do with timers but I just can't figure out what. I can't find it defined anywhere.

That's the real frustration here - I see so many terms and I'm trying to see them all used enough to figure out what they really mean and how to use them for my own purpose without asking questions like this. :-p



Thanks huge! Sorry to be such a downer with such an attitude - I planned to get all my code blocked up this weekend and I spent the last several days just trying to figure out how timers work. Arugh. @#$#@$#.

kevj
- 27th August 2007, 07:52
I finally stumbled onto some understanding this evening when I finally began to understand what the registers were all about. I found this great set of tuts here:

http://www.hobbyprojects.com/pic_tutorials/tutorial1.html

Pretty well step by step explanation of the PIC and simple ASM - that whole ASM thing is actually pretty easy. I can see it getting mind boggling to accomplish anything worthwhile, but one step at a time - it makes sense.

Anyway, after reading the above, the data sheet for the PIC makes a lot more sense. I'm sure I'll still have Q's on all this later this week, but now I finally see how to start and stop the timers.

I'm still not sure on the whole multiple bytes for the 16 bit timer, how to read each and act on them. Any info apprecaited otherwise I'll just keep trying to figure it out on my own.


Thanks!!

kevj
- 27th August 2007, 08:19
Woah - just found this thread:

http://www.picbasic.co.uk/forum/showpost.php?p=31066&postcount=3

From Joe S. - he gives credit elsewhere, but I think I get it now. It's almost 1am on Monday morning and after beating my head on this all weekend, I think I finally get it. lol.

:)