|
|
|
![]() |
|
|||||||
|
||||||||
|
|
Thread Tools | Rate Thread | Display Modes |
|
#13
|
||||
|
||||
|
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) {
do_something();
}
Code:
if (a>59) {
do_something();
}
As such I tend to write timer ISR's this way: Code:
// 1ms timer tick ISR
if (Timer1ms) {
--Timer1ms;
}
if (!(--Timer10)) {
Timer10 = 10;
if (Timer10ms) {
--Timer10ms;
}
if (!(Timer100)) {
Timer100 = 10;
if (Timer100ms) {
--Timer100ms;
}
if (!(Timer1000)) {
Timer1000 = 10;
if (Timer1s) {
--Timer1s;
}
}// !Timer1000
} // !Timer100
} // !Timer10
}
Code:
Timer10ms = 100; // Start Timer, 100 * 10ms = 1 second
// elsewhere
if (!(Timer10ms)) { // if timer expired
do_something();
}
Code:
if (Timer) { // if timer is running
if (!(--Timer)) { // decrement, and if it hits zero
Event_Flag = 1; // set event flag
}
}
Code:
Event_Flag = 0; // make sure event flag is cleared
Timer = 100; // start timer
// elsewhere
if (Event_Flag) {
do_something();
}
|
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Networkable real-time game simulation for 'Aim High'! test it out now | Bongle | General Forum | 11 | 19-03-2006 23:10 |
| Real time photos from the buckeye regional | Greg Needel | Regional Competitions | 4 | 25-03-2004 12:46 |
| Robot motion after the time has run out. | Randy Ai | Rules/Strategy | 1 | 06-01-2003 17:17 |
| Real Time Scoring | archiver | 2000 | 2 | 24-06-2002 00:09 |
| real time chat | SharkBite | CD Forum Support | 4 | 07-02-2002 21:22 |