![]() |
Real time clocks, out of the question?
Hullo everyone. I'm Evan Wilson, lead programmer for Team 2172, St. Edward High School.
Our robot is all crated and shipped off... with horrible loop-counting timekeeping. In the past, I've heard of creating a real time clock with the Timer1 module in the PIC controller. Is it humanly possible to create such a program? I DO know some PIC assembly, and certainly C. I assume this would be intensively assembly. Also, is there actually a crystal in the controller already? When reading the datasheet for the chip, it almost made it sound as if I'd need to do some whacked out solder job... I would like to have gotten this done for this year's competition, but seeing as we have no adult mentors (and I'm a junior with a bit of a tight schedule) we couldn't cram in the work for that. Anyways, thanks to anyone who could help, give links, etc. |
Re: Real time clocks, out of the question?
Hi, We too have had te same problem, in easyC there are a total of 6 timers, however, we haven't been able to get any of them to work, i believe timer 1 is used for the camera, but we cant seem to get any of the other timers to work. If any other teams have been able to get them to work, it would be extremely helpfull.
Thanks Malhon |
Re: Real time clocks, out of the question?
Well, in my case, I've used MPLAB the whole way. But does easyC actually have timers? I dug into the datasheet and found the Timer0 and Timer1 modules in the microcontroller and wasn't sure how to use them.
http://users.tkk.fi/~jalapaav/Electr...ock/index.html That seems a little helpful. |
Re: Real time clocks, out of the question?
Yes, EasyC does have timers, six of them. I believe we have used up to three at once, and they all work fine.
What exactly isn't working for your? Could you post your code? Thanks, Nathan |
Re: Real time clocks, out of the question?
Well, the problem is that I don't have any. Without EasyC, I just have the arm calculating based on loop counting. Multiples of 26.2 ms. There's no way I can switch to EasyC this year. I guess that would be a logical solution for next year.
Is there anyone here who has ever written a timer and interrupt routine for a real time clock? That's really the only thing I could worry about anyways. Otherwise I'll just have to stick with loop counting. |
Re: Real time clocks, out of the question?
Timers are simple to use, no soldering required.
IFI has a white paper on coding timers that goes into the bits and bytes of it all: http://www.ifirobotics.com/docs/time...004-jan-14.pdf For a description of the timer library calls see the MPLAB-C18-Libraries.pdf manual under C:\mcc18\doc. Here's a sample using the timer libraries: Code:
// In user_routines.cCode:
// In user_routines_fast.c |
Re: Real time clocks, out of the question?
Thank you very much. I hope my assembly serves me well.
I think I can dig up the differences in the new controller. Want to be a mentor... just kidding. |
Re: Real time clocks, out of the question?
Quote:
-Kevin |
Re: Real time clocks, out of the question?
Okay. Thanks very much Kevin and Mark. The last thing I need to make sure of is the value for the system clock. In the edu_clock code, it's specified as 10MHz. Does anyone know if there would be a difference in the PIC18F8722? I seem to remember reading something about 32.blablabla MHz somewhere. Or maybe it's changeable...
After this, I deserve to be put back into grade school if I can't do the math. THANKS |
Re: Real time clocks, out of the question?
It's the same 10MHz for the PIC18F8722 we use.
|
Re: Real time clocks, out of the question?
Quote:
|
Re: Real time clocks, out of the question?
There's a nice set of interface functions for using Timer1 here: http://www.chiefdelphi.com/forums/sh...ht=wallclock.c
|
Re: Real time clocks, out of the question?
The way Kevin's code sets up the interrupt is the best. The other example code posted/referenced all appear to reload the Timer register each time the interrupt is serviced, and the interrupt service latency then becomes an additive value to the timer's period. To some extent, only reloading the upper byte will help avoid the problem, but its easy to avoid the problem all together by just configuring the timer differently. An added bonus is that a few instruction cycles are removed from the ISR by not having to reload the register each time.
Another thing to remember here is that this microcontroller does not have a divide instruction, so including divide operations in an interrupt routine is forcing the microcontroller to execute a lot of instructions on a frequent basis. Some of the examples provided utilize variables wider than 8 bits, but I didn't notice any cautionary notes for routines that access these variables outside of the interrupt service routine. Because this micrcontroller operates on 8 bits at a time, if a routine were testing a 16 or 32 bit variable and an interrupt occurred in the middle of such a test, the ISR will end up invalidating the foreground operation that it interrupted. No amount of 'volatile' declarations will fix that problem. If the timer value absolutely MUST be greater than 8 bits, then the foreground operations that access this variable must temporarily disable the relevant timer interrupt prior to accessing or manipulating the variable, then re-enable the interrupt after the operation is complete. I tend to use timers that count down to zero because most microcontrollers are able to test for a zero condition in a single instruction, whereas testing for a specific value tends to use two or more instructions... and this is something which is true for the PIC18. In other words: Code:
if (a) {Code:
if (a>59) {As such I tend to write timer ISR's this way: Code:
Code:
Code:
Code:
|
Re: Real time clocks, out of the question?
I wrote some code that starts uses the timer mentioned above that turns on an LED 1:30 seconds into the operator controller part.
Our team has a "Heads up display" we used last year for shooting, we remade it this year but this was the only use we could come up with. |
Re: Real time clocks, out of the question?
Alright. I have another question about how the timer variables work in all of these. I've used storage classes/qualifiers occasionally but I don't understand the significance of using, say, extern and volatile to store the timer variables. Could anyone shed some light on this?
|
| All times are GMT -5. The time now is 11:06. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi