![]() |
|
|
DPRG
Events
Shoptalk
Support the DPRG
We need your help to keep going! Click the button to find out how you can help support our work!
Search DPRG Web Site
Member Login
Website design and hosting by NCC |
Theory of Operation for SR04 System Timerby David P. Anderson09 March 1999
Introduction This document attempts to outline the system software in a more detailed fashion, with an emphasis on the software operating environment and principles, hopefully in a generic manner that is applicable to other micro-controllers and development environments. The micro-controller used for SR04 is a Motorola HC6811 running in an M.I.T. 6.270 board, similar to the commercially available "Handy Board". The software is written in "C" using the ImageCraft "icc11" compiler. The operating system is based on the "ic" environment created at M.I.T. for their introductory robotics course. An excellent resource is the book Mobile Robots by Jones, Flynn & Seiger.
I. The System Timer
Free Running Timer
B. Resolution SYSTEM_TICK = .001 second High bandwidth sensors like sonar, audio, and video will require their own high-speed clocks, typically implemented as special purpose hardware, from a few tens of kilohertz for audio to megahertz for video.
C. Precision The SR04 system uses an unsigned 32 bit integer as the basic system clock. This overflows every 2^32 = 4294967296 ticks, or: 4294967.296 seconds = 71582.788 minutes = 1193.04 hours = 49.7 days So overflow occurs about every 50 days. Fancier clocks can be implemented, including actual time-of-day/month/year schemes. In practice none of our robots have ever been left running anywhere near 50 days. One day is more typical, so the 32 bit clock has been sufficient. This is also convenient because many "C" compilers, such as the icc11 used to implement SR04, provide a 32 bit unsigned integer as a standard data type, either as an "int" or in this case as a "long".
D. Implementation Higher level robotic functions that need to use the timer then simply read this integer, which is accessed like any other C variable. The HC6811 code to produce this function is in two parts. An initialization subroutine sets up the timer whenever the system is reset. The system clock interrupt then runs continuously. Here are the initialization and interrupt code fragments, written in C:
#include <hc11.h> /* include file of HC11 register definitions */
unsigned long sysclock; /* 32 bit global 1 khz system clock variable */
void system_service_init() /* initialize the TOC4 interrupt */
/* do this at startup */
{
sysclock = 0; /* reset the system clock */
TMSK1 |= 0x10; /* enable TOC4 */
TFLG1 = 0x10; /* clear TOC4 flag */
TCTL1 &= 0xF3; /* disable output pin */
}
#pragma interrupt_handler system_service /* tell c to compile rti */
/* instead of rts */
void system_service()
{
TFLG1 = 0x10; /* reset TOC4 interrupt */
TOC4 += 2000; /* delay another 2000 "E" cycles */
sysclock += 1; /* increment the system clock */
}
Notice the system_service() routine is set to next interrupt after 2000 "E" cycles. This particular microprocessor runs at two megahertz, so one millisecond equals 2000 microprocessor clock cycles. One other initialization procedure must be done in order to get this all working. The address of the system_service() routine must be plugged into the HC6811 interrupt vector table. This is located at either 0xbfd6 or 0xffd6, depending on the mode of the microprocessor. On the HC6811 a global interrupt enable instruction must also be executed to allow interrupts to function. Refer to the HC11 documentation for the specifics.
E. Testing
void sysclock_test()
{
unsigned long extern sysclock;
while(1) {
printf("%d\n",sysclock);
}
}
This will produce a continuously incrementing count, depending on how long your printf() function takes to execute, which should increase by 1000 every second. When you have this working, you're in business and ready to proceed to a few utilities that simplify coding of delay and time-dependent I/O routines.
Home | I. System Timer | II. Timer Utilities |
Copyright © 1984 - 2013 Dallas Personal Robotics Group. All rights reserved.