|
Module#6: Interactive C WHY INTERACTIVE C?The microcontroller driven handy-board had to be controlled usingsoftware. The usual method of doing this is writing assembly languageroutines for the handy board. Assembly language consists of mnemonic codes which are cumbersome to learn.A simple program written in a high level language would require many assembly language instructions in assembly language. Assembly language for .The 6811 is different from the assembly language of the 8085 which is taught at most Universities. We therefore decided to forgo the idea of using assembly language. Our next option was C language. C is a mid level language and enables the programmer to manipulate data as a collection of bits. This enabled programs written in C to easily interface with the hardware.However, it must be noted that C language in wide use today is the C language for Intel based microprocessors.We needed a C language for the 6811.We had two options:
This is a type of compiler which generates hex code file for the 6811 when the C program is compiled.The 6811 C crosscompiler from ImageCraft had already been used by many handy board users.However, there was a freeware version available which could not perform many functions and did not support some data types.For example the freeware version did not support floating point numbers.We could have obtained a registered version but we did not have timeto order a compiler from Imagecraft and so decided to go for the second option.
We have used the RS232 interface to load the object code from the PC to the micro-controller's RAM, since we have not used the EEPROM to store the program. Usage of assembly language would make programming cumbersome, and it would require in-depth knowledge of the assembly of 8085. Moreover, debugging would be tedious and dynamic management of the I/P and O/P positions would be rendered difficult. IC, being a high level language, provides us with more flexibility to program the MC68HC11A1FN to handle various situations efficiently. IC is basically a compiler that translates a program written in Interactive C into a pseudo-code, which is further interpreted into the machine code of the specified microprocessor. Thus, IC has the advantages of runtime error checking, ease of design and multitasking. Considering the following features, we decided to use IC. FEATURES OF INTERACTIVE C IC compiles the C program into pseudocode for a stack based machinerather than machine language code for the particular processor. The pcodeis then interpreted by a run time machine language module and executed.This offers the following advantages: 1) Interpreted execution allows runti0me error checking and prevents thesystem from crashing.For example IC allows array bounds at runtime checking which is not done in C for the Intel microprocessors. 2) It is not necessary to write seperate IC compiler for different processor if IC program is to be ported to another system. Only the interpreter is to replaced. 3) The pcode occupies lesser memory than the correponding machine code. 4) Since the pcode is fully stack based, the process's status is definedsolely by the contents of the program counter and the stack pointer. Thusmultitasking can easily be achieved by loading a new memory address in theprogram counter and the stack pointer. This is handled by the runtime module and not the IC compiler. IC has the disadvantage that pcode is being interpreted and this slows downthe system a bit. However the advantages outweigh this disadvantage. Q UICK STARTHere are the steps to getting the rover started with IC:- 1) Connect the Handy Board to the host comp0uter through the interface/charger board.The interface/charger board is connected to the PC COM1 or COM2 port using a straight through modem cable (not a NULL modem cable).The interface/charger board is connected to the Handy Board using a 4 wire telephone cable. 2) The Handy Board should be started in download mode by pressing the STOP button while switching the Handy Board on. The green and red LED pair located near the STOP button light up and then go off. This implies that the board is in download mode.The STOP button is then released. 3) The appropriate downloading software is run on the host computer and the pcode_hb.s19 is downloaded. 4) The Handy Board is reset by switching the Handy Board on and off.The piezobeeper should beep once and the LCD should display the IC welcome message. 5) IC should be run on the host computer. D OWNLOADERS FOR THE 6811:The IC software system consists of the following two primary components:- 1) The 6811 downloader software used to download the 6811 operating program on the Handy Board. 2) The IC application used to compile and download IC programs to the Handy Board. OVERVIEW The 6811 downloaders are general purpose applications for downloading Motorola hex file (also called s19 record) into the Handy Board's memory.Each line of the hex file contains ASCII-encoded binary data indicating what data is to be loaded where into the Handy Board's memory.For use with Interactive C, the program named "pcode_hb.s19" must be present in the Handy Board. The task of downloaders, then, is simply to initialize the Handy Board's memory with the contents of this file. An additional purpose of the donwnloaders is to program the 6811's"CONFIG" register. The CONFIG register determines the nature of the 6811memory map. For use with interactive C, the config register must be set toa value of 0x0c, which allows the 6811 to access the Handy Board's 32Kstatic RAM memory in its entirety. Some downloaders automatically program the CONFIG register; others require a special procedure to do so. It should noted that CONFIG register needs to be programmed only once. It is then set into memory unless the power supply to the RAM is disrupted. For MS-DOS : Two downloaders are available for MS-DOS machines.dl, by Randy Sargent and dlm, by Fred Martin DL: dl is compatible only with A series of the 6811 and it automaticallysets the CONFIG register on download. The following statement must be entered for downloading dl pcode_hb.s19 -port com1/com2 DLM: dlm is compatible with both A and E series but the CONFIG register has to beset manually. dlm pcode_hb.s19 -256 is the command used for A series (our case) . INITIALIZE BOARD 'Initialize Board' features automatic programming of the CONFIGregister,but it is compatible with only the A series. It comes with two versions- one using a modem port and other the printer port. Inorder to get 'Initialize Board' to use the Handy Board's pcode_hb.s19 file, one must edit its STR resources to name this file. Then it can be used by just double clicking on the application icon. U SING ICBefore using IC it has to configured. This is performed by typing 'ic -config' at the operating system command prompt.This lets us set theeditor to be used, the path to the library file (for the Handy Board), thename of the library file, the port to which the Handy Board is connected. After configuring IC, ic should be typed at the OS prompt to startIC. IC will check whether the Handy Board is connected to the host computer.Then, the library files will be downloaded and IC will give the command line prompt.IC can be started with the name of a C file to compile. When running and attached to a 6811 system, C expressions, functioncalls, and IC commands may be types at the "C>" prompt.All C expressions must end with a semicolon. Eg:- Typing 1 + 2; at the C> prompt will return <int> 3 It should be noted that this expression is compiled, downloaded to the Handy Board and evaluated by the 6811 and not the host computer. To evaluate a series of expressions, the expressions should be entered enclosed within brace brackets({}). I C COMMANDSIC has the following commands: 1. load: The command load <file(s)> compiles and loads the named file. The board must be attached for this to work. IC looks first in the local directory and in the IC library path for files. Several files may be loaded into IC at once, memory permitting, allowing programs to be defined in multiple files. It is necessary to enter the multiple files simultaneously if they form a single program. 2. unload: The unload <file(s)> unload the named file from memory andredownloads the remaining files. 3. list: The command list files displays the names of all files presentlyloaded into IC. The command list functions displays the presently definedfunctions.The command list globals displays the names of all currently defined global variables. 4. kill_all: This command kills all the currently running processes. 5. ps: ps prints the status of currently running processes on the screen.6. edit: The command edit <filename> brings up a system editor to allowediting of a file.This command is most useful on a single-tasking operatingsystem like MS-DOS. 7. run: If IC is running on a MS-DOS system, this command opens a shell to execute MS-DOS commands. 8. help: The command help displays a help screen of IC commands. 9. quit: The command quit exits IC. CTRL-C can also be used. THE main() FUNCTION After functions have been downloaded to the Handy Board, they can be invoked from the IC prompt. If one of the functions is named main(), it will automatically be run when the Handy Board is reset. To reset the Handy Board without running the main() function (for instance, when hooking the board back to the computer), hold down the START button when turning on the Handy Board. The board will reset without running main(). OUTPUT CONTROL a. MOTORS Motor ports are numbered from 0 to 4(for the Handy Board). When the motor is set in forward direction, green LED corresponding to that motorstarts glowing and when set in backward direction, red LED starts glowing. void fd(int m) : Turns the motor on in forward direction eg:-fd(0);void bk(int m) : Turns the motor on in backward direction eg:-bk(0); void off(int m) : Turns off motor m. eg:- off(1); void alloff() Turns off all the motors. ao is short form for alloff. void ao() void motor(int m,int p) : Turns on motor m at power level p. Power levels range from 100 for full on forward power to -100 for full on backward power. Power level adjsutment is performed by pulse width modulation. b. SENSOR INPUTDIGITAL INPUT: Int digital(int p) : Returns the value of the sensor in sensor port p, as a true/false value( 1 for true and 0 for false).p can vary from 7 to 16.True (1) is reported when the sensor is active low(i.e. an obstacle is detected). void set_digital_threshold(int p,int thresh) : Sets threshold value for digital readings on analog port p to thresh.Threshold should lie between 0 thru 255. If the value read from the analogsensor is greater than thresh, 0 is returned else 1 is returned.The threshold value is considered to be persistent.On download threshold are initialised to 127 on download. ANALOG INPUT : Int analog(int p): Returns value of sensor port numbered p. Result is integer between 0 and 255. If analog() function is called with a digital port number, 255 is returned if reading is 1 else 0 is returned. U SER BUTTON AND KNOBSThe Handy Board has two buttons and a knob whose value can be read by user programs.
int stop_button(): Returns value of button labelled STOP: 1 if pressed and 0 if released.
Returns value of button labelled START. void stop_press() : waits for STOP button to be pressed, then released. Then issues a short beep and returns.The code for stop press is as follows:
void start_press(): Like stop press, but for the START button.
int knob(): Returns the position of a knob as a value from 0 to 255. S HAFT ENCODER ROUTINESShaft encoders can be used to count the number of times a wheel spins, or in general the number of digital pulses seen by an input.Two types of shaft encoders- optical and magnetic are in general use.Shaft encoders are connected to ports 0 and 1.These inputs require the input timer capture feature of the 6811, hence they cannot be connected to any other port. Processing time is only spent when a pulse is being recorded,and even very fast pulses can be recorded.Shaft encoders are an optional feature and not part of the Handy Board, hence the library routines needed to use them are stored in seperate files called encodex_hb.icb, where x represents port no.encode0_hb.icb and encode1_hb.icb were the files used by us. The encoding software keeps a running count of the number of pulseseach enabled encoder has seen. The number of counts is set to zero when a channel is first enabled and on reset. The number of counts is kept in a variable that is only 16 bits long, therefore the variable will overflow if number of counts increase beyond 32767 and the value will become negative. The various functions for the encoders are as follows: void enable_encoder(int encoder): Enables the encoder to start counting and initializes counter to zero. By default the encoders are in the disabled state and have to be enabled before use. void disable_encoder(int encoder): Disables the given encoder and prevents it from counting. If an encoder is not in use, it should be disabled to save processing time. void reset_encoder(int encoder): Resets the counter of the given encoder to zero. For an enabled encoder this is a more efficient way of reseting the encoder. void read_encoder(int encoder): Returns the number of pulses counted by the given encoder from the time of last reset. TIME COMMANDS System code keeps track of time passage in milliseconds. The timevariables are implemented using the long integer data type. Standard functions allow use floating point variables when using the timing functions. void reset_system time(): Resets the count of system time to zero milliseconds. long mseconds(): Returns the count of system time in milliseconds. Time count is reset by hardware reset (i.e., pressing reset switch on board) or thefunction reset_system_time(). mseconds() is implemented as a C primitive(not as a library function). float seconds(): Returns the count of system time in seconds, as a floating point number.Resolution is one millisecond.
void sleep(float sec): Waits for an amount of time equal to or slightly greater than "sec" seconds. sec is a floating point number.
void msleep(long msec): Waits for an amount of time equal to or greater than msec milliseconds.msec is a long integer.
E RROR HANDLINGThere are two types of errors that can happen when working with IC:compile-time errors and run-time errors. Compile-time errors occur during the compilation of the source file. They are indicative of mistakes in the C source code.Typical compile-time errors result from incorrect syntax or mis-matching of data types. Run-time errors occur while a program is running on the board. Theyindicate problems with a valid C form when it is running. A simple example would be a divide-by-zero error. Another example might be running out ofstack space, if a recursive procedure goes too deep in recursion. COMPILE-TIME ERRORS: When compiler errors occur, an error message is printed to the screen. All compile-time errors must be fixed before a file can be downloaded to the board. RUN-TIME ERRORS: When a run-time error occurs, an error message is displayed on the LCD screen indicating the error number. If the board is hooked up to IC when the error occurs, a more verbose error message is printed on the terminal. LIST FILES If a program consists of multiple files, a list file may be created.The list file contains the names of the files constituting a program. Thisfile has the extension lis. When this file is given as an argument with the load command, all files mentioned inside the lis file are downloaded. E XTRA DOCUMENTATION :IC software : http://el.www.media.mit.edu/groups/el/projects/handy-board/software/index.html I/O routines for PC to Handy Board communication: http://el.www.media.mit.edu/projects/handy-board/faq/index.html#serial Students at Cambridge had built a similar rover.Their web site : http://www.engr.trinity.edu/~rover All software questions are answered at: http://el.www.media.mit.edu/projects/handy-board/faq/ |
1999 IEEE VESIT Student Branch Hits :
Webmaster : Saumitra Mohan Das |