120
1 Components used in building a small automatic robot Robo-11 68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to control the operation of the robot. It has a 32KB memory, 21 analog input channels, 9 digital inputs, 9 digital outputs, and it drives 4 DC motors and 6 RC servo motors. Universal Plate Plate size is 160 x 60 mm. There are 341 3-mm size holes with a distance of 5 mm between each hole. Two plates are provided. Motor Gearbox Uses a 6-9 V and 180 mA DC motor with a ratio of 48:1; torque 4kgF/cm; Two sets are provided. Track There are 3 sizes. Four 8-Joint tracks; Four 10-Joint Tracks; and two 30-joint tracks. Angled Shaft Base Two pieces of each the long base and short base are provided. Metal Axel 4 mm in diameter and 100 mm in length. Four axels come in the set.

68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

1

Components used in building a small automatic robot Robo-11

68HC11 Micro-controller Activity BoardIt comes with a CX-4 cable to downloadthe program. This board is used to controlthe operation of the robot. It has a 32KBmemory, 21 analog input channels, 9digital inputs, 9 digital outputs, and itdrives 4 DC motors and 6 RC servo motors.

Universal PlatePlate size is 160 x 60 mm. Thereare 341 3-mm size holes with adistance of 5 mm between eachhole. Two plates are provided.

Motor GearboxUses a 6-9 V and 180 mA DCmotor with a ratio of 48:1;torque 4kgF/cm; Two sets areprovided.

TrackThere are 3 sizes. Four 8-Jointtracks; Four 10-Joint Tracks;and two 30-joint tracks.

Angled Shaft BaseTwo pieces of each the long baseand short base are provided.

Metal Axel4 mm in diameter and 100 mmin length. Four axels come in theset.

Page 2: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

2

WheelsThere are four different types which are MainTrack Wheel (2 Pieces), Large Track SupportWheel (2 pieces), Small Track Support Wheel (10pieces), and hubs (12 pieces).

Knobby PlateThis is used to attach the AX-11 with the universalplates.

Angled Joiner20 pieces of varied color joiners made from PVCplastic. They can be connected together or byusing screws and 3 mm nuts in installation.

Obtuse Joiner20 pieces of varied color 135 degree obtusejoiners made from PVC plastic. They can beconnected together or by using screws and 3 mmnuts in installation.

Strength Joiner20 pieces of varied color joiners made from PVCplastic. They can be connected together or byusing screws and 3 mm nuts in installation.

Nut and Screw SetThere are two 2 mm open-end screws, four 3 x 6mm screws, thirty 3x10 mm screws, four 3 x 15mm screws, four 3 x 25 mm screws, and thirty 3mm nuts.

Switch InputThe switch input is used to detect collision at logic“0”. Two sets along with the connecting cable areprovided.

IR ReflectorUsed to detect the perimeter, lines, and the wheelcode. The results are in voltage. Three sets alongwith the connecting cable are provided.

IR RangerMeasures distance ranging from 4 to 30 cm usinginfrared light. The results are in voltage.

Page 3: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

3

Start by putting together the two track wheels,which each wheel using one 30-joint track, one10-joint track, and two 8-joint track. Connect alltracks together to form one track wheel that issuitable for the size of the robot.

1

Attach the motor gearbox sets to the universalplate using a 3 x 6 mm and 3 x 10 mm screw.Position the Motor Gearbox sets as shown in thepicture below.

2

BUILDING THE ROBOT

Page 4: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

4

Take the long angled shaft base and attach it to the universalplate. In between the plate and the long angled shaft, place thestrength joiner. Screw them all in together using a 3 x 10 mmscrew and nut, positioning it at the 8th hole from the side wherethe motor was attached. The smooth side of the shaft baseshould be turned outwards as shown in the picture. Tighten thescrew and nut.

3

Flat plastic joiner

Turn the universal plate over to the side where the motors areattached. Attach the short angled shaft base at the end of theplate as shown in the picture.

4

Use a 3 x 10 mm screw and nut to attachthe short angled shaft base to theuniversal plate

Page 5: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

5

Turn the plate bottom up and insert the metal axel into the holesof the long angled shaft in the positions of 1, 4, and 7 (Countingfrom the side with the installed motor).

5

Place the small track supportwheels over the metal axelwith the front of the wheelsturned outwards. Insert thehubs over the wheels and useyour fingers to press intightly. Insert a metal axelinto the two short angledshaft bases. Then insert thelarge track support wheelsand hub to it.

6

Page 6: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

6

Attach the main track wheels to the motor axels with the 2 mmopen-end screws.7

Attach an angled joiner to the universal plate between the twomotor gearboxes with a 3 x 10 mm screw and a 3 mm nut. Thenattach two more at the Large track support wheels, which isopposite from the motors as shown in the picture.

8

Page 7: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

7

Take 3 more angled joiners and attach them to the otheruniversal plate as shown in the picture. The position of theangled joiners must be the same as the ones attached to theplate with the motor gearboxes in Step 8.

9

Insert a strength joiner into all 3 angled joiners to create asupport base for the plate with the motor gearbox sets. Thenplace the two universal plates together. This will make it easier toremove the robot structure for any internal modifications thatmay be needed.

10

Page 8: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

8

Then take the track wheels that were put together in step 1 andplace it over the supporting wheels on both sides of the robot.Make sure that both wheels are aligned with one another.

11

Place the knobby plate with thetwo-sided glue onto the top ofthe finished robot. Remove thesticker and place the AX-11board onto the glue surface. Thiswill make it easier to remove orshift the AX-11 board. Thenconnect the signal cable of theleft motor to exterior terminal M-0 and the signal cable of theright motor to the interiorterminal M-1.

12

Page 9: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

9

Preparing to Download the Program

Preliminary Preparations in Using Interactive Cwith the AX-11 board on the Robo-11 robot.

The preparations that we are about to talkabout is writing the main control program, or whatthey call the Firmware, into the memory of theAX-11 board. This will be done only once in thebeginning, or if the main program data disappears,or if the AX-11 board is unable to receive datafrom the operational program written by theprogrammer.

Turn on the POWER switch on the AX-11 board. If the voltage ofthe battery on the AX-11 is enough, the green PWR LED will be litbrightly. If not, the LED will be dimly displayed and the red BATTLED will indicate that the voltage level is low, as shown in FigureA1-1. Use the +12V DC Adaptor that came with the AX-11 boardas the power supply instead by connecting it to the outlet on theboard. Once power is supplied, the yellow CHARGE LED will light,and the red LED will disappear as shown in Figure A1-2.

1

Page 10: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

10

Take the AX-11 and connect it to the serial port of the computeras shown in Figure A1-3. The green SER LED will light, indicatingthat the connection of the AX-11 to the serial port of thecomputer was successful, and is ready for use.

2

Page 11: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

11

Problem Solving if the computerhas only one USB port

Use a USB port to analog port RS-232converter, in which we recommend theUCON-232 board. (www.inex.co.th)

Open the Interactive C programby going into Start → Program →Interactive C 4.30ax → InteractiveC for AX-11. A title window willappear for an instant beforechanging to the Select ControllerType window. Choose AX-11.

3

The Port Selection window willappear for you to choose theserial port of the computer that isto be used to communicate withthe AX-11 board. Choose the portyou want and click Connect now.

4

Go to the menu Tools → Downloadfirmware. A window will appear tochoose the serial port ofcommunication again. Once youhave chosen, click DownloadFirmware.

5

Page 12: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

12

A figure showing the steps indownloading the firmware tothe AX-11 board will appear.Start by connecting thedownload cable to the serialport and to the AX-11 board.Click Next when done.

If the green SER LED is lit andblinking, click Yes, it’sblinking.

A window will appear tellingyou to turn off the powerswitch. Turn off the POWERswitch and click Next.

A window will then appear foryou to press the STOP switchon the AX-11 board. While stillholding it down, turn on thepower of the AX-11 board asindicated in the picture. Thenclick Next.

9

A window will appear to displaythe status of the AX-11,indicating that the PWR andBATT LED should be off. Thenclick Both Lights Off >

10

If the PWR LED is lit, click PWR light still on!An alert message will appear saying it isunable to enter the download mode of theprogram, and to check the download cableagain before repeating the steps for download.

If the BATT LED is lit, click BATT light still on!An alert message will appear saying that thevoltage supply of the battery is low and needsto be recharged for at least another 30minutes before using it again.

Page 13: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

13

If everything is alright, awindow displaying theFirmware download status willappear. Once the download isdone, a beep will be heardfrom the AX-11 board, and thestatus window of theInteractive C program willdisplay the messageDownload Successful. Thedisplay of the AX-11 will showIC 4.30 on AX-11 ActivityBoard, ending with a blinkingheart. The Interactive Cprogram will then go to theInteraction window, which isused for testing the program.

The Robo-11 is now ready forprogram coding and operation.

8

Testing the Interactive C Program

After downloading the main controlprogram, or the firmware, the next stepis to write a basic function to test theoperation of the AX-11.

1Go to the Interaction window,type in the function

Printf (“Hello world !\n”);Then press Enter.

2 The monitor shows the resultsfrom the AX-11, displaying“Hello world!” on the topsentence. The display of theInteraction window will displaythe message

This means that the AX-11 boardcan now interact with theInteractive C program.

Page 14: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

14

Next we will create a simple Interactive C program to use with the AX-11 board.

1 Click New to create a newprogram folder

2 Type in the program below, andsave it as hello.ic

3 Click Download to download theprogram. A window will appearasking to save the file *.ic first.Here, the file is hello.ic. Awindow displaying the status ofthe download will then appear.

4 Run the program byMethod 1: Turn off and on thePOWER switch once to reset theAX-11 board.Method 2: Run the program byclicking Run Main on theInteractive C window. Thenchoose the Interaction window,the message below will appear

The message “Hello world!“will appear on the top of theAX-11 display screen.

Page 15: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

15

Things to be know when downloading the program to the AX-11

If the AX-11 and Interactive C program is used together continuously withoutturning off the program, the programmer would be able to download and test theprogram anytime, even when the POWER switch is turned off. This is because theprogram is stored in a non-volatile system memory.

If the Interactive C program is closed and reopenedagain while the AX-11 board is still on, and the firmwarestill operating, the communication between the AX-11and the Interactive C program must be reestablished.Choose the serial port used to communicate, and thenclick Connect Now. A window displaying the status ofdownloading the Interactive C library to the AX-11 boardwill appear as seen in the picture.

Then we will go to the Interaction window, causing the program that was saved inthe memory previously to have disappeared; therefore it must be downloadedagain.

This means that every time the Interactive C program is closed and openedagain, the user must always download the program he needs into the memoryagain. This is because the Interactive C program is a program that needs to beconnected to the hardware in order to check its status continuously. Therefore, ifthe communication is lost because it was turned off, the communication mustalways be reestablished at the beginning by downloading the program’s library tothe AX-11 board.

The AX-11 Power Supply

The AX-11 uses 8 serial Nickelmethus-hydridebatteries size AA with a voltage of 1.2V 1700mAHor above, therefore resulting in a supply of at least9.6 1700 mAH. The recommended time incharging the batteries is at least 10 to 15 hours.Low current is used to charge the batteries inorder to extend the battery life.

If the AX-11 board is fully charged, it can beused continuously for 2-4 hours, depending on thenumber of peripherals connected and the amountof voltage they use.

Page 16: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

16

Changing the Batteries

he batteries that come with the AX-11 board areechargeable nickelmethus-hydride, and can usedor approximately 1 year. Therefore, for highestfficiency, the batteries should be changed everyear using the following steps:

1 Prepare the new battery set and attach foamtape to the top of it.

2 Turn off the POWER switch and removed theadapter cable. Use a screwdriver to removethe 4 screws at the corners of the AX-11.

3 Use a flat-head screw driver (or a four-edgedscrewdriver depending on the type of screwsin the terminal block). Loosen the screws atthe terminal block with the cables of theoriginal battery set. Remove each cable anduse a pincher to cut the ends off. This is toprevent the occurrence of a short circuit.

4 Replace the original batteries with the newset, turning the side with the foam tapeupwards. Then peel about 5 mm of thebattery’s red cable (positive) off and connectit to the + of the terminal block. Use thescrewdriver to tighten the screws at theterminal block that hold the cable. Do thesame for the black cable (negative) of thebattery. This step is very important. It needsto be done one cable at a time to preventshort-circuit of the battery or with other partsof the AX-11 boards.

5 Then place the board back to its originalplace. Use the screwdriver to screw the 4screws at the corner of the AX-11 board backto the box. Connect the adapter to the AX-11board to charge the battery. This should take10 to 12 hours to charge the batteries for thefirst time. The AX-11 board should be readyfor use after this.

Page 17: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

17

/* Example for Robot basic movementHardware configuration- Motor left connected to DC Motor chanel M-0- Motor right connected to DC Motor chanel M-1 */#define pow 50 /* Configuration power drive motor */

void main(){ao(); // All off motor every channelprintf(“Press Start!\n”); // Display message on LCDstart_press(); // Wait until Press start keywhile(1) // Infinite loop{run_fd(2.0); // Robot forward 2 secrun_bk(2.0); // Robot backward 2 sec}}void turn_left(float spin_time){motor(0,-pow); // Motor0 backward for pow define valuemotor(1,pow); // Motor1 forward for pow define valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward for pow define valuemotor(1,-pow); // Motor1 backward for pow define valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward for pow define valuemotor(1,pow); // Motor1 forward for pow define valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward for pow define valuemotor(1,-pow); // Motor1 backward for pow define valuesleep(delay); // Delay set by parameter delay}

ROBO-11 Testing

After the Robo-11 has been put together, we will next write a program to test thefunctioning of the motor to see whether it is working together properly and if it isready for its future operations or not. A simple program will be downloaded for therobot to move forward 2 seconds, move backwards 2 seconds, and continue to repeatthese steps.

Type in the following program code and download it to the Robo-11

Page 18: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

18

/* Example for Robot movement, program to near square movementHardware configuration- Motor left connected to DC Motor chanel M-0- Motor right connected to DC Motor chanel M-1 */#define pow 40 /*Configuration power drive motor*/

void main(){ao(); // All off motor every channelprintf(“Press Start!\n”); // Display message on LCDstart_press(); // Wait until Press start keywhile(1) // Infinite loop{run_fd(2.0); // Robot forward 2 secturn_left(1.0); // Robot backward 1 sec}}

Testing It

Place the Robo-11 on the floor and then turn on the POWER switch. The LCDscreen displays the message Press Start. Press START on the AX-11.

The robot will move forward for 2 seconds, using only 50% of its power. Observethe motor LED on the AX-11 board. Both must be green. Then the robot will movebackwards for 2 seconds. Observe the motor LED on the AX-11 that shows thefunctionality of the motors. Both will turn to red.

If you don’t get these results, switch the motor cable’s terminalcharge on the AX-11 until you get the correct results. Use this motorconnection for future operations.

Robo-11 Test (2)

From Test (1), the next step would be to include additional conditionsfor the robot so that the robot can move in the direction or waydesired. In this test, the Robo-11 is told to move in a square-likeshape.

Type in the following program code and download it to the Robo-11

Page 19: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

19

void turn_left(float spin_time){motor(0,-pow); // Motor0 backward for pow define valuemotor(1,pow); // Motor1 forward for pow define valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward for pow define valuemotor(1,-pow); // Motor1 backward for pow define valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward for pow define valuemotor(1,pow); // Motor1 forward for pow define valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward for pow define valuemotor(1,-pow); // Motor1 backward for pow define valuesleep(delay); // Delay set by parameter delay}

Testing It

Place the Robo-11 on the floor and then turn on the POWERswitch. The LCD screen displays the message Press Start.Press START on the AX-11. The robot will move forward for 2seconds, using only 50% of its power. Then the robot willturn left for 2 seconds and continue doing so. It can beobserved that the robot is moving in a square-likemovement.

How much the Robo-11 Standard is able to move in a perfectsquare depends on many factors, such as the current batterylevel, the current supplied to the motor, movement time,and the friction at the wheels of the robot.

Page 20: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

20

1. If the downloaded program does not work.

Solve by• Check the download cable by checking if the SER LED is green.• Check the power supply by checking if the BATT LED is red. If it is

lighted bright it means that the battery is low. Use the external powerfrom the DC adapter +12V 500 mA

• Download the firmware again.

2. If the AX-11 cannot communicate with theInteractive C program.

Solve by• Check the power supplied to the AX-11 by checking if the PWR LED is

green.• Check the download cable by checking if the SER LED is green.• Check the power supply by checking if the BATT LED is red. If it is

lighted bright it means that the battery is low. Use the external powerfrom the DC adapter +12V 500 mA

• If everything is ok, download the firmware again.

3. If the firmware can not be downloaded

Solve by• Check the power supplied to the AX-11 by checking if the PWR LED is

green.• Check the download cable by checking if the SER LED is green.• Check the power supply by checking if the BATT LED is red. If it is

lighted bright it means that the battery is low. Use the external powerfrom the DC adapter +12V 500 mA

• If it is still not working, send the robot back to the manufacturer ordistributor to check its functionality

Basic Problem Solving

Page 21: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

21

IC 4 Programmers Manual

IntroductionInteractive C (IC for short) is a C language consisting of a compiler (with interactivecommand-line compilation and debugging) and a run-time machine language module.IC implements a subset of C including control structures (for, while, if, else), local andglobal variables, arrays, pointers, structures, 16-bit and 32-bit integers, and 32-bit floatingpoint numbers.

IC works by compiling into pseudo-code for a custom stack machine, rather thancompiling directly into native code for a particular processor. This pseudo-code (or p-code) is then interpreted by the run-time machine language program. This unusualapproach to compiler design allows IC to offer the following design tradeoffs:

• Interpreted execution that allows run-time error checking. For example, IC does arraybounds checking at run-time to protect against some programming errors.

• Ease of design. Writing a compiler for a stack machine is significantly easier thanwriting one for a typical processor. Since IC's p-code is machine-independent,porting IC to another processor entails rewriting the p-code interpreter, rather thanchanging the compiler.

• Small object code. Stack machine code tends to be smaller than a native coderepresentation.

• Multi-tasking. Because the pseudo-code is fully stack-based, a process's state isdefined solely by its stack and its program counter. It is thus easy to task-switch simplyby loading a new stack pointer and program counter. This task-switching is handledby the run-time module, not by the compiler.

Since IC's ultimate performance is limited by the fact that its output p-code is interpreted,these advantages are taken at the expense of raw execution speed.

IC 4 was written by Randy Sargent of the KISS Institute for Practical Robotics. Randywas assisted by Mark Sherman. Portions of the code and the libraries are based onthe public distribution of IC 2.8 written by Randy Sargent, Anne Wright and FredMartin.

Page 22: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

22

Using IC When IC is running and has a connection to a compatible processor board such as theHandy Board or RCX, C expressions, function calls, and IC commands may be typed in thecommand entry portion of the interaction window.

For example, to evaluate the arithmetic expression 1 + 2, type in the following: 1 + 2;When this expression is entered from the interaction window, it is compiled by the consolecomputer and then downloaded to the attached system for evaluation. The connectedboard then evaluates the compiled form and returns the result, which is printed on thedisplay section of console interaction window.

To evaluate a series of expressions, create a C block by beginning with an open curlybrace { and ending with a close curly brace }. The following example creates a localvariable i and prints 10 (the sum of i + 7) to the board's LCD screen: {int i=3; printf("%d", i+7);}

Page 23: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

23

IC Interface

Both new (unsaved) and saved files can be opened for editing in IC. A row of tabs lists thefiles that have been opened. Clicking a file's tab activates it for editing. The first tab for theinterface is always the interaction window.

The File button has standard entries for New, Open, Close, Save, Save As, Print, and Exit.Under File - Save As, if no file name extension is supplied, IC automatically saves with the".ic" extension.

To download the active file, simply click the download button. The active file will also besaved, unless it is new, in which case the user is prompted for a "save as" file name. Remark:a preprocessor command #use has been added to IC to specify any other saved files(personal libraries) that need to be downloaded along with the active file [Note: #use isquite different from the #include prepreocessor command of standard C environments.#include is not implemented for reasons given later in the section describing the IC-preprocessor.]

If a downloaded program does not do what is intended, it may corrupt the p-codeinterpreter, particularly if pointers are being employed. The interface provides an optionunder the Settings button for downloading the firmware to reinitialize the board.

When there is a connection to a board and the downloaded programs include "main",then "main" can be executed using the Run Main button. The Stop button will haltexecution of the attached system.

Under the Tools button, among other options, are ones for listing downloaded files, globalvariables, and functions (including library functions).

The interface provides additional capabilities for program entry/edit, minor adjustment tothe display, and for setting up the serial interface to a board.

C programs are automatically formatted and indented. Keywords, library functions,comments, and text strings are high-lighted with color unless this feature is turned off.

IC does parenthesis-balance-highlighting when the cursor is placed to the right of any rightparenthesis, bracket, or brace.

Page 24: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

24

The main() Function

After functions have been downloaded to a board, they can be invoked from IC so longas the board is connected. If one of the functions is named main(), it can be run directlyfrom the interface as noted earlier, and otherwise will be run automatically when theboard is reset.

Note: to reset the Handy Board without running the main() function (for instance, whenhooking the board back to the computer), hold down the board's Start button whileactivating the board. The board will then reset without running main().

IC versus Standard C

The IC programming language is based loosely on ANSI C. However, there are majordifferences.

Many of these differences arise from the desire to have IC be "safer" than standard C. Forinstance, in IC, array bounds are checked at run time; for this reason, arrays cannot beconverted to pointers in IC. Also, in IC, pointer arithmetic is not allowed.

Other differences are due to the desire that the IC runtime be small and efficient. Forinstance, the IC printf function does not understand many of the more exotic formattingoptions specified by ANSI C.

Yet other differences are due to the desire that IC be simpler than standard C. This is thereason for the global scope of all declarations.

In the rest of this document, when we refer to "C", the statement applies to both IC andstandard C. When we wish to specify one or the other, we will refer to either "IC" or"standard C". When no such qualifiers are present, you should assume that we are talkingabout IC.

Page 25: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

25

A Quick C Tutorial

Most C programs consist of function definitions and data structures. Here is a simple Cprogram that defines a single function, called main./* Simple example IC Programmer's Manual */ void main() { printf("Hello, world!\n"); // Something simple }The expression

/* <text> */forms a multi-line or bracketed comment. In contrast, text that starts with "//" forms a singleline comment, which continues only to the end of the line. Comments are ignored by ICwhen the program is compiled.All functions must have a return type. Since main does not return a value, it uses void, thenull type, as its return type. Other types include integers (int) and floating point numbers(float). This function declaration information must precede each function definition.

Immediately following the function declaration is the function's name (in this case, main).Next, in parentheses, are any arguments (or inputs) to the function. main has none, but anempty set of parentheses is still required.

After the function arguments is an open curly-brace {. This signifies the start of the actualfunction code. Curly-braces signify program blocks, or chunks of code.

Next comes a series of C statements. Statements demand that some action be taken. Ourdemonstration program has a single statement, a printf (formatted print). This will print themessage "Hello, world!" to the LCD display. The \n indicates end-of-line. The printfstatement ends with a semicolon (;). All C statements must be ended by a semicolon.Beginning C programmers commonly make the error of omitting the semicolon that isrequired to end each statement.

Page 26: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

26

The main function is ended by the close curly-brace }.

Let's look at an another example to learn some more features of C. The following codedefines the function square, which returns the mathematical square of a number.int square(int n) { return(n * n); }The function is declared as type int, which means that it will return an integer value.Next comes the function named square, followed by its argument list in parentheses.square has one argument, n, which is an integer. Notice how declaring the type of theargument is done similarly to declaring the type of the function.

When a function has arguments declared, those argument variables are valid within the"scope" of the function (i.e., they only have meaning within the function's own code).Other functions may use the same variable names independently.

The code for square is contained within the set of curly braces. In fact, it consists of a singlestatement: the return statement. The return statement exits the function and returns thevalue of the C expression that follows it (in this case "n * n").

Except where grouped by parentheses, expressions are evaluated according to a set ofprecedence rules associated with the various operations within the expression. In this case,there is only one operation (multiplication), signified by the "*", so precedence is not anissue.

Let's look at an example of a function that performs a function call to the square program.float hypotenuse(int a, int b) { float h; h = sqrt((float)(square(a) + square(b))); return(h); }

Page 27: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

27

This code demonstrates several more features of C. First, notice that the floating pointvariable h is defined at the beginning of the hypotenuse function. In general, whenever anew program block (indicated by a set of curly braces) is begun, new local variables maybe defined.The value of h is set to the result of a call to the sqrt function. It turns out that sqrt is a built-inIC function that takes a floating point number as its argument.

We want to use the square function we defined earlier, which returns its result as an integer.But the sqrt function requires a floating point argument. We get around this typeincompatibility by coercing the integer sum (square(a) + square(b)) into a float bypreceding it with the desired type, in parentheses. Thus, the integer sum is made into afloating point number and passed along to sqrt.

The hypotenuse function finishes by returning the value of h.

This concludes the brief C tutorial.

Data Objects

Variables and constants are the basic data objects in a C program. Declarations list thevariables to be used, state what type they are, and may set their initial value.

Page 28: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

28

Variables

Variable names are case-sensitive. The underscore character is allowed and is often usedto enhance the readability of long variable names. C keywords like if, while, etc. may notbe used as variable names.

Functions and global variables may not have the same name. In addition, if a localvariable is named the same as a function or a global variable, the local use takesprecedence; ie., use of the function or global variable is prevented within the scope of thelocal variable.

Declaration In C, variables can be declared at the top level (outside of any curly braces) or atthe start of each block (a functional unit of code surrounded by curly braces). Ingeneral, a variable declaration is of the form:

<type> <variable-name>; or<type> <variable-name>=<initialization-data>;

In IC, <type> can be int, long, float, char, or struct <struct-name>, and determinesthe primary type of the variable declared. This form changes somewhat whendealing with pointer and array declarations, which are explained in a later section,but in general this is the way you declare variables.

Local and Global Scopes

If a variable is declared within a function, or as an argument to a function, its bindingis local, meaning that the variable has existence only within that function definition. Ifa variable is declared outside of a function, it is a global variable. It is defined for allfunctions, including functions which are defined in files other than the one in whichthe global variable was declared.

Page 29: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

29

Variable Initialization

Local and global variables can be initialized to a value when they are declared. Ifno initialization value is given, the variable is initialized to zero.

All global variable declarations must be initialized to constant values. Local variablesmay be initialized to the value of arbitrary expressions including any global variables,function calls, function arguments, or local variables which have already beeninitialized.

Here is a small example of how initialized declarations are used. inti=50; /* declare i as global integer; initial value 50 */ longj=100L; /* declare j as global long; initial value 100 */ intfoo() { intx; /* declare x as local integer; initial value 0 */ longy=j; /* declare y as local integer; initial value j */ }Local variables are initialized whenever the function containing them is executed.Global variables are initialized whenever a reset condition occurs. Reset conditionsoccur when:

1. Code is downloaded;

2. The main() procedure is run;

3. System hardware reset occurs.

Page 30: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

30

Persistent Global Variables

A special persistent form of global variable, has been implemented for IC. Apersistent global variable may be initialized just like any other global variable, but itsvalue is only initialized when the code is downloaded and not on any other resetconditions. If no initialization information is included for a persistent variable, its valuewill be initialized to zero on download, but left unchanged on all other resetconditions.

To make a persistent global variable, prefix the type specifier with the keywordpersistent. For example, the statement persistent int i=500;creates a global integer called i with the initial value 500.Persistent variables keep their state when the board is turned off and on, when mainis run, and when system reset occurs. Persistent variables will lose their state whencode is downloaded as a result of loading or unloading a file. However, it is possibleto read the values of your persistent variables in IC if you are still running the same ICsession from which the code was downloaded. In this manner you could read thefinal values of calibration persistent variables, for example, and modify the initialvalues given to those persistent variables appropriately.

Persistent variables were created with two applications in mind:

• Calibration and configuration values that do not need to be re-calculated onevery reset condition.

• Robot learning algorithms that might occur over a period when the robot isturned on and off.

Page 31: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

31

Constants Integer Constants Integers constants may be defined in decimal integer format (e.g., 4053 or -1),hexadecimal format using the "0x" prefix (e.g., 0x1fff), and a non-standard but usefulbinary format using the "0b" prefix (e.g., 0b1001001). Octal constants using the zeroprefix are not supported.

Long Integer Constants

Long integer constants are created by appending the suffix "l" or "L" (upper- or lower-case alphabetic L) to a decimal integer. For example, 0L is the long zero. Either theupper or lower-case "L" may be used, but upper-case is the convention forreadability.

Floating Point Constants

Floating point numbers may use exponential notation (e.g., "10e3" or "10E3") or maycontain a decimal period. For example, the floating point zero can be given as "0.","0.0", or "0E1", but not as just "0". Since the board has no floating point hardware,floating point operations are much slower than integer operations, and should beused sparingly.

Characters and String Constants

Quoted characters return their ASCII value (e.g., 'x').

Character string constants are defined with quotation marks, e.g., "This is a characterstring.".

NULL

The special constant NULL has the value of zero and can be assigned to andcompared to pointer or array variables (which will be described in later sections). Ingeneral, you cannot convert other constants to be of a pointer type, so there aremany times when NULL can be useful.

For example, in order to check if a pointer has been initialized you could compare itsvalue to NULL and not try to access its contents if it was NULL. Also, if you had adefined a linked list type consisting of a value and a pointer to the next element, youcould look for the end of the list by comparing the next pointer to NULL.

Page 32: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

32

Data Types IC supports the following data types:

16-bit Integers

16-bit integers are signified by the type indicator int. They are signed integers, andmay be valued from -32,768 to +32,767 decimal.32-bit Integers

32-bit integers are signified by the type indicator long. They are signed integers, andmay be valued from -2,147,483,648 to +2,147,483,647 decimal.

32-bit Floating Point Numbers

Floating point numbers are signified by the type indicator float. They haveapproximately seven decimal digits of precision and are valued from about 10^-38to 10^38.

8-bit Characters

Characters are an 8-bit number signified by the type indicator char. A character'svalue typically represents a printable symbol using the standard ASCII charactercode, but this is not necessary; characters can be used to refer to arbitrary 8-bitnumbers.

Pointers

IC pointers are 16-bit numbers which represent locations in memory. Values inmemory can be manipulated by calculating, passing and dereferencing pointersrepresenting the location where the information is stored.

Arrays

Arrays are used to store homogenous lists of data (meaning that all the elements ofan array have the same type). Every array has a length which is determined at thetime the array is declared. The data stored in the elements of an array can be setand retrieved in the same manner as for other variables.

Page 33: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

33

Structures

Structures are used to store non-homogenous but related sets of data. Elements of astructure are referenced by name instead of number and may be of any supportedtype.

Structures are useful for organizing related data into a coherent format, reducing thenumber of arguments passed to functions, increasing the effective number of valueswhich can be returned by functions, and creating complex data representationssuch as directed graphs and linked lists.

Pointers The address where a value is stored in memory is known as the pointer to that value. It isoften useful to deal with pointers to objects, but great care must be taken to insure that thepointers used at any point in your code really do point to valid objects in memory.Attempts to refer to invalid memory locations could corrupt your memory. Most computingenvironments that you are probably used to return helpful messages like 'SegmentationViolation' or 'Bus Error' on attempts to access illegal memory. However, you won't have thissafety net on the board you are connecting to. Invalid pointer dereferencing is very likelyto go undetected, and will likely render invalid your data, your program, or even thepcode interpreter.

Pointer Safety

In past versions of IC, you could not return pointers from functions or have arrays ofpointers. In order to facilitate the use of structures, these features have been added to thecurrent version. With this change, the number of opportunities to misuse pointers haveincreased. However, if you follow a few simple precautions you should do fine.

First, you should always check that the value of a pointer is not equal to NULL (a specialzero pointer) before you try to access it. Variables which are declared to be pointers areinitialized to NULL, so many uninitialized values could be caught this way.

Page 34: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

34

Second, you should never use a pointer to a local variable in a manner which could causeit to be accessed after the function in which it was declared terminates. When a functionterminates the space where its values were being stored is recycled. Therefore not onlymay dereferencing such pointers return incorrect values, but assigning to those addressescould lead to serious data corruption. A good way to prevent this is to never return theaddress of a local variable from the function which declares it and never store thosepointers in an object which will live longer than the function itself (a global pointer, array, orstruct). Global variables and variables local to main will not move once declared and theirpointers can be considered to be secure.

The type checking done by IC will help prevent many mishaps, but it will not catch allerrors, so be careful.

Pointer Declaration and Use

A variable which is a pointer to an object of a given type is declared in the same manneras a regular object of that type, but with an extra * in front of the variable name.

The value stored at the location the pointer refers to is accessed by using the * operatorbefore the expression which calculates the pointer. This process is known as dereferencing.

The address of a variable is calculated by using the & operator before that variable, arrayelement, or structure element reference.

There are two main differences between how you would use a variable of a given typeand a variable declared as a pointer to that type.

For the following explanation, consider X and Xptr as defined as follows:

Page 35: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

35

long X; long *Xptr;• Space Allocation -- Declaring an object of a given type, as X is of type long,

allocates the space needed to store that value. Because an IC long takes four bytesof memory, four bytes are reserved for the value of X to occupy. However, a pointerlike Xptr does not have the same amount of space allocated for it that is needed foran object of the type it points to. Therefore it can only safely refer to space whichhas already been allocated for globals (in a special section of memory reserved forglobals) or locals (temporary storage on the stack).

• Initial Value -- It is always safe to refer to a non-pointer type, even if it hasn't beeninitialized. However pointers have to be specifically assigned to the address of legallyallocated space or to the value of an already initialized pointer before they are safeto use.

So, for example, consider what would happen if the first two statements after X and Xptrwere declared were the following: X=50L; *Xptr=50L;The first statement is valid: it sets the value of X to 50L. The second statement would bevalid if Xptr had been properly initialized, but in this case it has not. Therefore, thisstatement would corrupt memory.Here is a sequence of commands you could try which illustrate how pointers and the * and& operators are used. It also shows that once a pointer has been set to point at a place inmemory, references to it actually share the same memory as the object it points to: X=50L; /* set the memory allocated for X to 50 */ Xptr=&X; /* set Xptr to point to memory address of X */ printf("%d ",*Xptr); /* dereference Xptr; value at address is 50 */ X=100L; /* set X to the value 100 */ printf("%d ",*Xptr); /* dereference again; value is now 100 */ *Xptr=200L; /* set value at address given by Xptr to 200 */ printf("%d\n",X); /* check that the value of X changed to 200 */Passing Pointers as Arguments Pointers can be passed to functions and functions can change the values of the variablesthat are pointed at. This is termed call-by-reference; a reference, or pointer, to a variable isgiven to the function that is being called. This is in contrast to call-by-value, the standardway that functions are called, in which the value of a variable is given the to functionbeing called.

Page 36: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

36

The following example defines an average_sensor function which takes a port number anda pointer to an integer variable. The function will average the sensor and store the result inthe variable pointed at by result.

Prefixing an argument name with * declares that the argument is a pointer. void average_sensor(int port, int *result) { int sum = 0; int i; for (I = 0; I < 10; i++) sum += analog(port); *result = sum/10; }Notice that the function itself is declared as a void. It does not need to return anything,because it instead stores its answer in the memory location given by the pointer variablethat is passed to it.The pointer variable is used in the last line of the function. In this statement, the answersum/10 is stored at the location pointed at by result. Notice that the * is used to assign avalue to the location pointed by result.

Returning Pointers from Functions Pointers can also be returned from functions. Functions are defined to return pointers bypreceeding the name of the function with a star, just like any other type of pointerdeclaration. int right,left; int *dirptr(int dir) { if (dir==0) { return(&right); } if (dir==1) { return(&left); } return(NULL); }The function dirptr returns a pointer to the global right when its argument dir is 0, a pointerto left when its argument is 1, and NULL" if its argument is other than 0 or 1.

Page 37: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

37

Arrays

IC supports arrays of characters, integers, long integers, floating-point numbers, structures,pointers, and array pointers (multi-dimensional arrays). While unlike regular C arrays in anumber of respects, they can be used in a similar manner. The main reasons that arrays areuseful are that they allow you to allocate space for many instances of a given type, sendan arbitrary number of values to functions, and provide the means for iterating over a setof values.

Arrays in IC are different and incompatible with arrays in other versions of C. Thisincompatibility is caused by the fact that references to IC arrays are checked to insure thatthe reference is truly within the bounds of that array. In order to accomplish this checking inthe general case, it is necessary that the size of the array be stored with the contents of thearray. It is important to remember that an array of a given type and a pointer to the sametype are incompatible types in IC, whereas they are largely interchangeable in regular C.

Declaring and Initializing Arrays

Arrays are declared using square brackets. The following statement declares an array often integers: int foo[10];In this array, elements are numbered from 0 to 9. Elements are accessed by enclosing theindex number within square brackets: foo[4] denotes the fifth element of the array foo(since counting begins at zero).Arrays are initialized by default to contain all zero values. Arrays may also be initialized atdeclaration by specifying the array elements, separated by commas, within curly braces. Ifno size value is specified within the square brackets when the array is declared butinitialization information is given, the size of the array is determined by the number ofelements given in the declaration. For example,

Page 38: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

38

int foo[]= {0, 4, 5, -8, 17, 301};creates an array of six integers, with foo[0] equaling 0, foo[1] equaling 4, etc.If a size is specified and initialization data is given, the length of the initialization data maynot exceed the specified length of the array or an error results. If, on the other hand, youspecify the size and provide fewer initialization elements than the total length of the array,the remaining elements are initialized to zero.

Character arrays are typically text strings. There is a special syntax for initializing arrays ofcharacters. The character values of the array are enclosed in quotation marks: char string[]= "Hello there";This form creates a character array called string with the ASCII values of the specifiedcharacters. In addition, the character array is terminated by a zero. Because of this zero-termination, the character array can be treated as a string for purposes of printing (forexample). Character arrays can be initialized using the curly braces syntax, but they willnot be automatically null-terminated in that case. In general, printing of character arraysthat are not null-terminated will cause problems.Passing Arrays as Arguments When an array is passed to a function as an argument, the array's pointer is actuallypassed, rather than the elements of the array. If the function modifies the array values, thearray will be modified, since there is only one copy of the array in memory.

In normal C, there are two ways of declaring an array argument: as an array or as apointer to the type of the array's elements. In IC array pointers are incompatible withpointers to the elements of an array so such arguments can only be declared as arrays.

As an example, the following function takes an index and an array, and returns the arrayelement specified by the index: int retrieve_element(int index, int array[]) { return array[index]; }Notice the use of the square brackets to declare the argument array as a pointer to anarray of integers.

Page 39: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

39

When passing an array variable to a function, you are actually passing the value of thearray pointer itself and not one of its elements, so no square brackets are used. void foo() { int array[10]; retrieve_element(3, array); }

Multi-dimensional Arrays

A two-dimensional array is just like a single dimensional array whose elements are one-dimensional arrays. Declaration of a two-dimensional array is as follows: int k[2][3];The number in the first set of brackets is the number of 1-D arrays of int. The number in thesecond set of brackets is the length of each of the 1-D arrays of int. In this example, k is anarray containing two 1-D arrays; k[0] is a 1-D array of color=blue>int of length 3; k[0][1] is ancolor=blue>int. Arrays of with any number of dimensions can be generalized from thisexample by adding more brackets in the declaration.Determining the size of Arrays at Runtime

An advantage of the way IC deals with arrays is that you can determine the size of arraysat runtime. This allows you to do size checking on an array if you are uncertain of itsdimensions and possibly prevent your program from crashing.

Since _array_size is not a standard C feature, code written using this primitive will only beable to be compiled with IC.

The _array_size primitive returns the size of the array given to it regardless of the dimensionor type of the array. Here is an example of declarations and interaction with the_array_size primitive:

Page 40: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

40

int i[4]={10,20,30}; int j[3][2]={{1,2},{2,4},{15}}; int k[2][2][2]; _array_size(i); /* returns 4 */ _array_size(j); /* returns 3 */ _array_size(j[0]); /* returns 2 */ _array_size(k); /* returns 2 */ _array_size(k[0]); /* returns 2 */Structures Structures are used to store non-homogenous but related sets of data. Elements of astructure are referenced by name instead of number and may be of any supported type.Structures are useful for organizing related data into a coherent format, reducing thenumber of arguments passed to functions, increasing the effective number of values whichcan be returned by functions, and creating complex data representations such asdirected graphs and linked lists.

The following example shows how to define a structure, declare a variable of structuretype, and access its elements. struct foo { int i; int j; }; struct foo f1; void set_f1(int i,int j) { f1.i=i; f1.j=j; } void get_f1(int *i,int *j) { *i=f1.i; *j=f1.j; }

Page 41: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

41

The first part is the structure definition. It consists of the keyword struct, followed by thename of the structure (which can be any valid identifier), followed by a list of namedelements in curly braces. This definition specifies the structure of the type struct foo. Oncethere is a definition of this form, you can use the type struct foo just like any other type. Theline struct foo f1;is a global variable declaration which declares the variable f1 to be of type struct foo.The dot operator is used to access the elements of a variable of structure type. In this case,f1.i and f1.j refer to the two elements of f1. You can treat the quantities f1.i and f1.j just asyou would treat any variables of type int (the type of the elements was defined in thestructure declaration at the top to be int).

Pointers to structure types can also be used, just like pointers to any other type. However,with structures, there is a special short-cut for referring to the elements of the structurepointed to. struct foo *fptr; void main() { fptr=&f1; fptr->i=10; fptr->j=20; }In this example, fptr is declared to be a pointer to type struct foo. In main, it is set to point tothe global f1 defined above. Then the elements of the structure pointed to by fptr (in thiscase these are the same as the elements of f1), are set. The arrow operator is used insteadof the dot operator because fptr is a pointer to a variable of type struct foo. Note that(*fptr).i would have worked just as well as fptr->i, but it would have been clumsier.Note that only pointers to structures, not the structures themselves, can be passed to orreturned from functions.

Page 42: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

42

Complex Initialization examples

Complex types -- arrays and structures -- may be initialized upon declaration with asequence of constant values contained within curly braces and separated by commas.

Arrays of character may also be initialized with a quoted string of characters.

For initialized declarations of single dimensional arrays, the length can be left blank and asuitable length based on the initialization data will be assigned to it. Multi-dimensionalarrays must have the size of all dimensions specified when the array is declared. If a lengthis specified, the initialization data may not overflow that length in any dimension or an errorwill result. However, the initialization data may be shorter than the specified size and theremaining entries will be initialized to 0. Following is an example of legal global and localvariable initializations: /* declare many globals of various types */ int i=50; int *ptr=NULL; float farr[3]={ 1.2, 3.6, 7.4 }; int tarr[2][4]={ { 1, 2, 3, 4 }, { 2, 4, 6, 8} }; char c[]="Hi there how are you?"; char carr[5][10]={"Hi","there","how","are","you"}; struct bar { int i; int *p; long j; } b={5, NULL, 10L}; struct bar barr[2] = { { 1, NULL, 2L }, { 3 } }; /* declare locals of various types */ int foo() { int x; /* local variable x with initial value 0 */ int y= tarr[0][2]; /* local variable y with initial value 3 */ int *iptr=&i; /* local pointer to integer which points to the global i */ int larr[2]={10,20}; /* local array larr with elements 10 and 20 */ struct bar lb={5,NULL,10L}; /* local variable of type struct bar with i=5 and j=10 */ char lc[]=carr[2]; /* local string lc with initial value "how" */ ... }

Page 43: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

43

Statements and Expressions Operators act upon objects of a certain type or types and specify what is to be done tothem. Expressions combine variables and constants to create new values. Statements areexpressions, assignments, function calls, or control flow statements which make up Cprograms.Operators Each of the data types has its own set of operators that determine which operations maybe performed on them.

Integer Operations

The following operations are supported on integers:

• Arithmetic. addition +, subtraction -, multiplication *, division /.

• Comparison. greater-than >, less-than <, equality ==, greater-than-equal >=, less-than-equal <=.

• Bitwise Arithmetic. bitwise-OR |, bitwise-AND &, bitwise-exclusive-OR ^, bitwise-NOT~.

• Boolean Arithmetic. logical-OR ||, logical-AND &&, logical-NOT !.When a C statement uses a boolean value (for example, if), it takes the integer zeroas meaning false, and any integer other than zero as meaning true. The booleanoperators return zero for false and one for true. Boolean operators && and || willstop executing as soon as the truth of the final expression is determined. Forexample, in the expression a && b, if a is false, then b does not need to beevaluated because the result must be false. The && operator therefore will notevaluate b.

Long Integers A subset of the operations implemented for integers are implemented for long integers:arithmetic addition +, subtraction -, and multiplication *, and the integer comparisonoperations. Bitwise and boolean operations and division are not supported.

Page 44: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

44

Floating Point Numbers

IC uses a package of public-domain floating point routines distributed by Motorola. Thispackage includes arithmetic, trigonometric, and logarithmic functions. Since floating pointoperations are implemented in software, they are much slower than the integeroperations; we recommend against using floating point if you are concerned aboutperformance.

The following operations are supported on floating point numbers:

• Arithmetic. addition +, subtraction -, multiplication *, division /.

• Comparison. greater-than >, less-than <, equality ==, greater-than-equal >=, less-than-equal <=.

• Built-in Math Functions. A set of trigonometric, logarithmic, and exponential functionsis supported. For details, go to the Library Function Descriptions. These functions areincluded among those itemized as "Math" functions.

Characters

Characters are only allowed in character arrays. When a cell of the array is referenced, it isautomatically coerced into a integer representation for manipulation by the integeroperations. When a value is stored into a character array, it is coerced from a standard 16-bit integer into an 8-bit character (by truncating the upper eight bits).

Assignment Operators and Expressions

The basic assignment operator is =. The following statement adds 2 to the value of a. a = a + 2;The abbreviated form a += 2;could also be used to perform the same operation. All of the following binary operatorscan be used in this fashion: + - * / % << >> & ^ |

Page 45: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

45

Increment and Decrement Operators The increment operator "++" increments the named variable. For example, the construction"a++" is equivalent to "a= a+1" or "a+= 1". A statement that uses an increment operator hasa value. For example, the statement a= 3; printf("a=%d a+1=%d\n", a, ++a);will display the text "a=3 a+1=4". If the increment operator comes after the named variable,then the value of the statement is calculated after the increment occurs. So the statement a= 3; printf("a=%d a+1=%d\n", a, a++);would display "a=3 a+1=3" but would finish with a set to 4. The decrement operator "--" isused in the same fashion as the increment operator.Data Access Operators

&A single ampersand preceding a variable, an array reference, or a structure elementreference returns a pointer to the location in memory where that information is beingstored. This should not be used on arbitrary expressions as they do not have a stableplace in memory where they are being stored.

*A single * preceeding an expression which evaluates to a pointer returns the valuewhich is stored at that address. This process of accessing the value stored within apointer is known as dereferencing.

[<expr>]An expression in square braces following an expression which evaluates to an array(an array variable, the result of a function which returns an array pointer, etc.) checksthat the value of the expression falls within the bounds of the array and referencesthat element.

.A dot between a structure variable and the name of one of its fields returns the valuestored in that field.

->An arrow between a pointer to a structure and the name of one of its fields in thatstructure acts the same as a dot does, except it acts on the structure pointed at by itsleft hand side. Where f is a structure of a type with i as an element name, the twoexpressions f.i and (&f)->i are equivalent.

Page 46: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

46

Precedence and Order of Evaluation The following table summarizes the rules forprecedence and associativity for the C operators. Operators listed earlier in the table havehigher precedence; operators on the same line of the table have equal precedence.Operator Associativity () [] left to right

! ~ ++ -- - (<type>) right to left

* / % left to right

+ - left to right

<< >> left to right

< <= > >= left to right

== != left to right

& left to right

^ left to right

| left to right

&& left to right

|| right to left

= += -= etc. right to left

, left to right

Page 47: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

47

Control Flow

IC supports most of the standard C control structures. One notable exception is the switchstatement, which is not supported.

Page 48: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

48

Statements and Blocks

A single C statement is ended by a semicolon. A series of statements may be groupedtogether into a block using curly braces. Inside a block, local variables may be defined.Blocks may be used in place of statements in the control flow constructs.

If-Else

The if else statement is used to make decisions. The syntax is: if (<expression>) <statement-1> else <statement-2><expression> is evaluated; if it is not equal to zero (e.g., logic true), then <statement-1> isexecuted.The else clause is optional. If the if part of the statement did not execute, and the else ispresent, then <statement-2> executes.

While

The syntax of a while loop is the following: while (<expression>) <statement>while begins by evaluating <expression>. If it is false, then <statement> is skipped. If it is true,then <statement> is evaluated. Then the expression is evaluated again, and the samecheck is performed. The loop exits when <expression> becomes zero.One can easily create an infinite loop in C using the while statement: while (1) <statement>For

The syntax of a for loop is the following: for (<expr-1>;<expr-2>;<expr-3>) <statement>The for construct is equivalent to the following construct using while: <expr-1>; while (<expr-2>) { <statement> <expr-3>; }

Page 49: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

49

Typically, <expr-1> is an assignment, <expr-2> is a relational expression, and <expr-3> is anincrement or decrement of some manner. For example, the following code counts from 0to 99, printing each number along the way: int i; for (i = 0; i < 100; i++) printf("%d\n", i);

Break Use of the break statement provides an early exit from a while or a for loop.

Page 50: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

50

LCD Screen Printing

IC has a version of the C function printf for formatted printing to the LCD screen.

The syntax of printf is the following: printf(<format-string>, <arg-1> , ... , <arg-N>);This is best illustrated by some examples.Printing Examples

Example 1: Printing a message

The following statement prints a text string to the screen. printf("Hello, world!\n");In this example, the format string is simply printed to the screen. The character \n at theend of the string signifies end-of-line. When an end-of-line character is printed, the LCDscreen will be cleared when a subsequent character is printed. Thus, most printf statementsare terminated by a \n.

Example 2: Printing a number

The following statement prints the value of the integer variable x with a brief message. printf("Value is %d\n", x);The special form %d is used to format the printing of an integer in decimal format.

Example 3: Printing a number in binary

The following statement prints the value of the integer variable x as a binary number. printf("Value is %b\n", x);The special form %b is used to format the printing of an integer in binary format. Only thelow byte of the number is printed.

Page 51: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

51

Example 4: Printing a floating point number

The following statement prints the value of the floating point variable n as a floating pointnumber. printf("Value is %f\n", n);The special form %f is used to format the printing of floating point number.

Example 5: Printing two numbers in hexadecimal format printf("A=%x B=%x\n", a, b);The form %x formats an integer to print in hexadecimal.

Formatting Command Summary Format Command Data TypeDescription

%d int decimal number

%x int hexadecimal number

%b int low byte as binary number

%c int low byte as ASCII character

%f float floating point number

%s char array char array (string)

Page 52: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

52

Special Notes

• The final character position of the LCD screen is used as a system "heartbeat." Thischaracter continuously blinks between a large and small heart when the board isoperating properly. If the character stops blinking, the board has failed.

• Characters that would be printed beyond the final character position are truncated.

• When using a two-line display, the printf() command treats the display as a singlelonger line.

• Printing of long integers is not presently supported.

Page 53: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

53

Preprocessor

The preprocessor processes a file before it is sent to the compiler. The IC preprocessorallows definition of macros, and conditional compilation of sections of code. Usingpreprocessor macros for constants and function macros can make IC code more efficientas well as easier to read. Using #if to conditionally compile code can be very useful, forinstance, for debugging purposes.

The special preprocessor command #use has been included to allow programs to cause aprogram to download to initiate the download of stored programs that are not in the IClibrary. For example, suppose you have a set of stored programs in a file named "mylib.ic",some of which you need for your current program to work. /* load my library */ #use "mylib.ic"

void main() { char s[32] = "text string wrapping badly\n"; fix (s); /* apply my fix function to s and print it */ printf(s); }Preprocessor Macros Preprocessor macros are defined by using the #define preprocessor directive at the start ofa line. A macro is local to the file in which it is defined. The following example shows how todefine preprocessor macros. #define RIGHT_MOTOR 0 #define LEFT_MOTOR 1 #define GO_RIGHT(power) (motor(RIGHT_MOTOR,(power))) #define GO_LEFT(power) (motor(LEFT_MOTOR,(power))) #define GO(left,right) {GO_LEFT(left); GO_RIGHT(right);} void main() { GO(0,0); }

Page 54: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

54

Preprocessor macro definitions start with the #define directive at the start of a line, andcontinue to the end of the line. After #define is the name of the macro, such asRIGHT_MOTOR. If there is a parenthesis directly after the name of the macro, such as theGO_RIGHT macro has above, then the macro has arguments. The GO_RIGHT and GO_LEFTmacros each take one argument. The GO macro takes two arguments. After the nameand the optional argument list is the body of the macro.Each time a macro is invoked, it is replaced with its body. If the macro has arguments, theneach place the argument appears in the body is replaced with the actual argumentprovided.

Invocations of macros without arguments look like global variable references. Invocationsof macros with arguments look like calls to functions. To an extent, this is how they act.However, macro replacement happens before compilation, whereas global referencesand function calls happen at run time. Also, function calls evaluate their arguments beforethey are called, whereas macros simply perform text replacement. For example, if theactual argument given to a macro contains a function call, and the macro instantiates itsargument more than once in its body, then the function would be called multiple times,whereas it would only be called once if it were being passed as a function argumentinstead.

Appropriate use of macros can make IC programs and easier to read. It allows constantsto be given symbolic names without requiring storage and access time as a global would.It also allows macros with arguments to be used in cases when a function call is desirablefor abstraction, without the performance penalty of calling a function.

Page 55: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

55

Conditional compilation

It is sometimes desirable to conditionally compile code. The primary example of this is thatyou may want to perform debugging output sometimes, and disable it at other times. TheIC preprocessor provides a convenient way of doing this by using the #ifdef directive. void go_left(int power) { GO_LEFT(power); #ifdef DEBUG printf("Going Left\n"); beep(); #endif }In this example, when the macro DEBUG is defined, the debugging message "Going Left"will be printed and the board will beep each time go_left is called. If the macro is notdefined, the message and beep will not happen. Each #ifdef must be follwed by an#endif at the end of the code which is being conditionally compiled. The macro to bechecked can be anything, and #ifdef blocks may be nested.Unlike regular C preprocessors, macros cannot be conditionally defined. If a macrodefinition occurs inside an #ifdef block, it will be defined regardless of whether the #ifdefevaluates to true or false. The compiler will generate a warning if macro definitions occurwithin an #ifdef block.

The #if, #else, and #elif directives are also available, but are outside the scope of thisdocument. Refer to a C reference manual for how to use them.

Page 56: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

56

Comparison with regular C preprocessors

The way in which IC deals with loading multiple files is fundamentally different from the wayin which it is done in standard C. In particular, when using standard C, files are compiledcompletely independently of each other, then linked together. In IC, on the other hand, allfiles are compiled together. This is why standard C needs function prototypes and externglobal definitions in order for multiple files to share functions and globals, while IC does not.

In a standard C preprocessor, preprocessor macros defined in one C file cannot be used inanother C file unless defined again. Also, the scope of macros is only from the point ofdefinition to the end of the file. The solution then is to have the prototypes, externdeclarations, and macros in header files which are then included at the top of each C fileusing the #include directive. This style interacts well with the fact that each file is compiledindependent of all the others.

However, since declarations in IC do not file scope, it would be inconsistent to have apreprocessor with file scope. Therefore, for consistency it was desirable to give IC macrosthe same behavior as globals and functions. Therefore, preprocessor macros have globalscope. If a macro is defined anywhere in the files loaded into IC, it is defined everywhere.Therefore, the #include and #undef directives did not seem to have any appropriatepurpose, and were accordingly left out.

The fact that #define directives contained within #if blocks are defined regardless ofwhether the #if evaluates to be true or false is a side effect of making the preprocessormacros have global scope.

Other than these modifications, the IC preprocessor should be compatible with regular Cpreprocessors.

Page 57: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

57

The IC Library File

Library files provide standard C functions for interfacing with hardware on the robotcontroller board. These functions are written either in C or as assembly language drivers.Library files provide functions to do things like control motors, make tones, and input sensorsvalues.

IC automatically loads the library file every time it is invoked. Depending on which board isbeing used, a different library file will be required. IC may be configured to load differentlibrary files as its default; IC will automatically load the correct library for the board you'reusing at the moment.

Separate documentation covers all library functions available for the Handy Board andRCX; if you have another board, see your owner's manual for documentation.

To understand better how the library functions work, study of the library file source code isrecommended; e.g., the main library file for the Handy Board is named lib_hb.ic.

For convenience, commonly a description of commonly used library functions follows.

Page 58: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

58

Commonly Used IC Library Functions start_button(); /* returns 1 if button is pressed, otherwise 0 */

stop_button(); /* returns 1 if button is pressed, otherwise 0 */

digital(); /* returns 0 if the switch attached to the port is open and returns 1 if the switch is closed. Digital ports are numbered 7-15. Typically used for bumpers or limit switches. */

analog(); /* returns the analog value of the port (a value in the range 0-255). Analog ports on the handy board are numbered 2-6 and 16-23. Light sensors and range sensors are examples of sensors you would use in analog ports (only on Handy Board). */

knob(); /* returns an int between 0 and 255 depending on knob position */

sleep(<float_secs>); /* waits specified number of seconds */

beep(); /* causes a beep sound */

tone(<float_frequency>, <float_secs>) /* plays at specified frequency for specified time (seconds) */

printf(<string>, <arg1>, <arg2>, ... ); /* prints <string>. If the string contains % codes then the <args> after the string will be printed in place of the % codes in the format specified by the code. %d prints a decimal number. %f prints a floating point number. %c prints a character, %b prints an integer in binary, %x prints an integer in hexadecimal. */

motor(<motor_#>, <speed>) /* controls the motors. <motor_#> is an integer between 0 and 3 (1 less for RCX). <speed> is an integer between -100 and 100 where 0 means the motor is off and negative numbers run the motor in the reverse direction */

Page 59: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

59

fd(<motor_#>); /* turns on the motor specified (direction is determined by plug orientation */

bk(<motor_#>); /* turns on the motor specified in the opposite direction from fd */

off(<motor_#>); /* turns off the motor specified */

ao(); /* turns all motor ports off */Processes Processes work in parallel. Each process, once it is started, will continue until it finishes oruntil it is killed by another process using the kill_process(<process_id)>); statement. Eachprocess that is active gets 50ms of processing time. Then the process is paused temporarilyand the next process gets its share of time. This continues until all the active process havegotten a slice of time, then it all repeats again. From the user's standpoint it appears that allthe active processes are running in parallel.

Processes can communicate with one another by reading and modifying global variables.The globals can be used as semaphores so that one process can signal another. ProcessIDs may also be stored in globals so that one process can kill another when needed.

Up to 4 processes initiated by the start_process() library function can be active at any time.

The library functions for controlling processes are:start_process(<function_name>(<arg1>, <arg2>, . . .)); /* start_process returns an integer that is the <process_id> and starts the function <function_name> as a separate process */

defer(); /* when placed in a function that is used as a process this will cause that process to give up the remainder of its time slice whenever defer is called */

kill_process(<process_id>); /* this will terminate the process specified by the <process_id> */

Page 60: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

60

Encoders (Handy Board only) The enable_encoder() library function is used to start a process which updates thetransition count for the encoder specified. The encoder library functions are designed forsensors connected to (digital) ports 7,8,12,13. The corresponding <encoder#> values are0,1,2,3. Every enabled encoder uses a lot of the HB's processor -- so don't enable anencoder unless you are going to use it, and never put an enable statement inside of aloop.enable_encoder(<encoder#>); /* turns on the specified encoder (either 0,1,2, or 3 which are plugged into digital ports 7, 8, 12, 13 respectively). This should be done only once - never enable an already enabled encoder. If an encoder is not enabled, read_encoder will always return 0. */

disable_encoder(<encoder#>) /* turns off the specified encoder */

reset_encoder(<encoder#>) /* sets the specified encoder value to 0 */

read_encoder(<encoder#>) /* returns an int that is the current value of the specified encoder */

Page 61: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

61

Controlling the DC Motors

To Control the DC Motors, Please ensure that the moors are connected to the MIII Mainboard before programming.

Procedure- Connect the DC Motors to M-0 and M-1 channel on the Main Board.- Make sure the Interactive C Program is opened. Select a NEW Program or continue

with your existing Program.- The Following Sample Code, “DC-CODE”, shows how you can control the DC Motors.- After you have typed / inserted the code, click on the “RUN MAIN” button.- Observe the Motor Indicator on the LCD Screen.

RESULTS

Observe and you will see the following:- GREEN LED Lights Up.- Motor Turns in 1 direction for 2 Seconds- RED LED Lights Up- Motor Turns in faster by 30% in another direction for 2 Seconds- This Operation continues in an endless loop

Page 62: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

62

DC-CODE

IMPORTANT COMMANDS

While ( ) {….} “A” represents loops, 1 = INFINITE LOOPS“…..” represents the code inside

Motor ( , ); “B” represents the board motor number“C” represents the % of power

Sleep ( ); “D” represents the number of seconds to wait.D

CB

A

void main(){ while(1) // Infinite loop { motor(0,50); // Motor0 forward at 50% of maximum power motor(1,50); // Motor1 forward at 50% of maximum power sleep(2.0); // Delay 2 sec

motor(0,-80); // Motor0 backward at 80% of maximum power motor(1,-80); // Motor1 backward at 80% of maximum power sleep(2.0); // Delay 2 sec }}

/* Example for drive motor *//*Hardware configuration - Motor left connected to DC Motor channel M-0 - Motor right connected to DC Motor channel M-1*/

COMMENTS

MAIN PROG

Page 63: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

63

Controlling RC-Servo Motors

The Set of servo motors provided inthe package can also beprogrammed to be run with the mainboard. The main board’s capabilitycan allow up to 6 servo motors.

In Interactive C, we can use 2commands. They are

init_expbd_servos( ); “A” represents ON or OFF 1 for ON, 0 for OFF

servo = ; “B” represents the board servo number, from 0to 5.”C” represents the Servo DRIVE VALUE (Power ).

Procedure

- Connect the RC Servo motor to Channel 0 on the main board.- BE CAREFUL OF THE COLOR CODE.- WHITE GOES TO S- RED to +- BLACK to –- Make sure the Interactive C Program is opened. Select a NEW Program or continue

with your existing Program.- The Following Sample Code, “SERVO-CODE”, shows how you can control the Servo

Motors.- After you have typed / inserted the code, click on the “RUN MAIN” button.- Observe the Servo Motor’s Operations.

CB

A

Page 64: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

64

SERVO CODE

IMPORTANT COMMANDS

Init_expbd_servos ( ); “A” represents either 1 or 0. 1 is to turn servo motors ON. 0 is to OFF servo motors.

servo = ; “B” represents the Servo Channel. “C” represents the Drive Value ( POWER ).

CB

A

void main(){ int i=600; // Declare and define servo drive value init_expbd_servos(1); // Enable servo motor driver while(!stop_button()) // Check STOP switch pressed { servo0 = i; // Drive servo motor ch-0 with servo drive value sleep(1.0); // Delay 1 sec i+=100; // Increase value step = 100 if(i>4200) // Check angle>180 deg { i=600; // Set to origin } } init_expbd_servos(1); // Disable servo motor driver beep(); // Sound beep}

/* Servo motor test*//*Hardware configuration - servo motor connected to ch-0*/

COMMENTS

MAIN PROG

Page 65: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

65

Controlling the SWITCH Sensor

The Main Board allows expandability of up to 9 DIGITAL Inputs.

Procedure- Connect the Switch sensor to DI-5 ( Digital Input 5) on the Main Board.- Make sure the Interactive C Program is opened. Select a NEW Program or continue

with your existing Program.- The Following Sample Code, “SWITCH-CODE”- After you have typed / inserted the code, click on the “RUN MAIN” button.- Observe the LCD Screen on the Main Board.

Page 66: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

66

SWITCH CODE

IMPORTANT CODES

=digital( ); “A” Represents Value of Digital Input.“B” Represents Channel / Port number.

beep(); This tells the Main Board to play a BEEP

printf(“ ”); “C” Represents what to be displayed onthe LCD Screen

C

B

void main(){ int result; // Declare keep input from touch sensor while(1) // Infinite loop { result = digital(15); // Keep value from DI-15 channel if(result==1) // Check Touch sensor pressed { printf("Your press!\n"); // Display message for pressed beep(); // Sound beep sleep(1.0); // Delay 1 sec for display message } else { printf("----------\n"); // Display when Touch sensor not pressed sleep(0.1); // Delay 0.1 sec for display message } }}

/* Touch sensor test*//*Hardware configuration - Touch sensor connected to DI-15*/

COMMENTS

MAIN PROG

Page 67: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

67

FIRE DETECTION / LIGHT DETECTION

The Main Board allows 21 AnalogINPUTS for analog sensors whichconverts physical signals to DC Volts.Light sensors convert physical Lightdensity into DC Voltage. If it is dark,it’s lower in voltage. If it is Bright,it’s higher in voltage which mayexceed 5V.

Procedure- Connect the Light Sensor to

AI-31 (Analog Input 31)- Make sure the Interactive C Program is opened. Select a NEW Program or continue

with your existing Program.- The Following Sample Code, “LIGHT-CODE”- After you have typed / inserted the code, click on the “RUN MAIN” button.- Turn the Main Board off and on again to reset the board.- Try these in LIGHT and DARK surroundings.- Observe the LCD Screen on the Main Board.

Page 68: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

68

LIGHT CODE

IMPORTANT CODES

=analog( );“A” Represents Value of Analog Input.“B” Represents Channel / Port number.

BA

void main(){ int value; // Declare keep input from touch sensor // { value = analog(31); // Keep value from AI-31 channel printf("Light %d\n",value); // Display Reflect value sleep(0.1); // Delay 0.1 sec for display }}

/*Light Sensor test *//*Hardware configuration - Light Sensor connected to AI-31*/

COMMENTS

MAIN PROG

Page 69: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

69

GP2D12 Readings ( Package & Additional )

The GP2D12 Module sensorsare also Analog and thereforeuse the same ports as the lightsensors.

Procedure- Connect the GP2D12 Sensor to AI-6.- Make sure the Interactive C Program is opened. Select a NEW Program or continue

with your existing Program.- The Following Sample Code, “GP2D12-CODE”- After you have typed / inserted the code, click on the “RUN MAIN” button.- Place an object in-front of the sensor.- Observe the LCD Screen on the Main Board.- Move the object nearer and further from the sensor and observe the LCD Screen

again.

Page 70: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

70

GP2D12 CODE

#define base -19float dis_table[118]={

/*distance value*//*ADC value*/

32.0,/*19*/ 30.1,/*20*/ 29.5,/*21*/ 28.6,/*22*/ 27.5,/*23*/26.1,/*24*/ 25.3,/*25*/ 24.5,/*26*/ 23.7,/*27*/ 23.2,/*28*/22.3,/*29*/ 22.0,/*30*/ 21.1,/*31*/ 20.5,/*32*/ 20.0,/*33*/19.3,/*34*/ 19.1,/*35*/ 18.4,/*36*/ 17.9,/*37*/ 17.5,/*38*/17.1,/*39*/ 16.5,/*40*/ 16.2,/*41*/ 15.8,/*42*/ 15.6,/*43*/15.5,/*44*/ 15.3,/*45*/ 14.8,/*46*/ 14.2,/*47*/ 13.8,/*48*/13.6,/*49*/ 13.3,/*50*/ 13.1,/*51*/ 12.8,/*52*/ 12.6,/*53*/12.4,/*54*/ 12.2,/*55*/ 12.0,/*56*/ 11.8,/*57*/ 11.7,/*58*/11.6,/*59*/ 11.6,/*60*/ 11.5,/*61*/ 11.4,/*62*/ 11.3,/*63*/10.85,/*64*/ 10.6,/*65*/ 10.3,/*66*/ 10.2,/*67*/ 10.0,/*68*/9.8,/*69*/ 9.7,/*70*/ 9.5,/*71*/ 9.3,/*72*/ 9.2,/*73*/9.0,/*74*/ 8.9,/*75*/ 8.8,/*76*/ 8.6,/*77*/ 8.4,/*78*/8.3,/*79*/ 8.2,/*80*/ 8.0,/*81*/ 7.85,/*82*/ 7.7,/*83*/7.6,/*84*/ 7.5,/*85*/ 7.3,/*86*/ 7.2,/*87*/ 7.15,/*88*/7.1,/*89*/ 7.0,/*90*/ 6.9,/*91*/ 6.8,/*92*/ 6.7,/*93*/6.6,/*94*/ 6.5,/*95*/ 6.4,/*96*/ 6.35,/*97*/ 6.3,/*98*/6.2,/*99*/ 6.15,/*100*/ 6.1,/*101*/ 6.0,/*102*/ 5.9,/*103*/5.85,/*104*/ 5.8,/*105*/ 5.7,/*106*/ 5.65,/*107*/ 5.6,/*108*/5.55,/*109*/ 5.45,/*110*/ 5.4,/*111*/ 5.4,/*112*/ 5.3,/*113*/5.2,/*114*/ 5.1,/*115*/ 5.0,/*116*/ 5.0,/*117*/ 4.9,/*118*/4.9,/*119*/ 4.8,/*120*/ 4.8,/*121*/ 4.7,/*122*/ 4.7,/*123*/4.6,/*124*/ 4.6,/*125*/ 4.5,/*126*/ 4.5,/*127*/ 4.4,/*128*/4.4,/*129*/ 4.3,/*130*/ 4.3,/*131*/ 4.2,/*132*/ 4.2,/*133*/4.1,/*134*/ 4.1,/*135*/ 4.0/*136*/ };

/* GP2D120 test*//*Hardware configuration - GP2D120 connected to AI-6*/

COMMENTS

DEFINITION

Page 71: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

71

void main(){ int an; // Keep analog value from sensor while(1) // Infinite loop { an = analog(6); // Read analog value from sensor if(an>=19 && an<=136 ) // Distance interval 10-80 cm { printf("dis%f cm adc = %d \n",dis_table[an+base],an);// Show distance value } else { printf(" Out of Range adc = %d\n",an);// Show mesage out of Range

} sleep(1.0); // Delay before read new cycle }}

MAIN PROG

Page 72: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

72

SRF04 Readings ( Additional )

The SRF04 has Signals, on input andthe other output For every output,there is an input for checking.

Procedure- Connect the Echo Input of the SRF04 to Digital Input Capture C-1 (IC1)- Connect the Trigger Pulse output of the SRF04 to the Digital Input 9 (IN9)- Make sure the Interactive C Program is opened. Select a NEW Program or continue

with your existing Program.- The Following Sample Code, “SRF04-CODE”- After you have typed / inserted the code, click on the “RUN MAIN” button.- Place an object in-front of the sensor.- Observe the LCD Screen on the Main Board.- Move the object nearer and further from the sensor and observe the LCD Screen

again.

Page 73: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

73

SRF04 CODE

#use "srf04_lib.ic" /* Include library srf04_lib.ic*/

void main(){ int result; // Declare keep input from touch sensor sonar_init(); // Initial sonar sensor while(1) // Infinite loop { result= sonar_sample(); // Keep distance value if (result != -1 ) // Check result value printf("Distance %d cm\n", result); // Display distance cm scale else printf("Out of Range\n"); // Display massage when out of range sleep(0.3); // Delay 0.3 sec for display }}

/* Sonar sensor test*//*Hardware configuration - echo pulse pin connected to IC1 - trigger pulse pin connected to IN-9*/

COMMENTS

MAIN PROG

Page 74: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

74

Add senses to the Robo-11 standard so thatit can detect and move along a line, by usingthe IR Reflector Sensor.

1 Using the Robo-11 standard robot,remove the top structure that has the AX-11 attached.

2 Turn the robot upside down. Install the10 mm plastic spacer using a 3 x 25 mmscrew screwed into the universal plateaccording to the positions as shown in thepicture ( positions 5,2 and 5,9 : the firstset of numbers refer to the longitudepositions from the leftmost side while thesecond set is the horizontal positionsform the top down. )

3 Take two IR Reflector Sensors (ZX-03)and place them onto the spacers, twistingthe screw through the spacer and sensorand screwing it tightly together with a 3mm nut. Do the same for both sets, andset the robot upright.

Building Type 1

Page 75: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

75

/* Example for Robo-11 liner1 : Black line DetectionHardware configuration- Motor left connected to DC Motor chanel M-0- Motor right connected to DC Motor chanel M-1- ZX-03 left connected to AI-31- ZX-03 Right connected to AI-17 */

#define pow 50 /*Configuration power drive motor 50% */#define ref 70 /*Configuration analog reference 70 */int left=0,right=0; // To keep input analog value

void main(void){ao();while(!stop_button()) // Check STOP button pressed in process{printf(“Press start!\n”); // Show messagewhile(!start_button()); // Check START button pressed to runprintf(“ Track Line\n”); // Show message for workingwhile(!stop_button()) // Check STOP button pressed in process{left = analog(31); // Read left sensor dataright = analog(17); // Read right sensor dataif((left>ref)&&(right>ref) //check out line{run_fd(0.01); // Direct forward} else if((left<ref)&&(right>ref)) // check left sensor in line{turn_right(1.5); // Turn right 1.5 sec} else if((left>ref)&&(right<ref)) // check right sensor in line{turn_left(1.5); // Turn left 1.5 sec}

4 Replace the top structure that has theAX-11 board on it back onto the bottompart. Then connect the left IR ReflectorSensor (looking down from the top) toterminal Al-31 and the right one toterminal Al-17 of the AX-11

The Robo-11 Liner1 is now complete andready to search and detect lines.

Robo-11 Liner1 Test

After the Robo-11 Liner1 has been put together, we will then write a test program totest the operation of the Robo-11 Liner1 by having it detect a black line and movebackwards and change directions once it detects the line.

Type in the following program code and download it to the Robo-11 liner1.

Page 76: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

76

else if((left<ref)&&(right<ref)) //check 2 sensor in line{turn_left(1.5); // Turn left 1.5 sec}}printf(“Stop..\n”); // Show message off all motorao(); // Off all motor for end programbeep(); // Sound beep signal for end program}}void turn_left(float spin_time){motor(0,-pow); // Motor0 backward for pow define valuemotor(1,pow); // Motor1 forward for pow define valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward for pow define valuemotor(1,-pow); // Motor1 backward for pow define valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward for pow define valuemotor(1,pow); // Motor1 forward for pow define valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward for pow define valuemotor(1,-pow); // Motor1 backward for pow define valuesleep(delay); // Delay set by parameter delay}

TIPS ON THE IR REFLECTOR SENSOR

The heart of this sensor circuit is the sensor that detects reflectionsfrom infrared light. It consists of the Infrared LED which emitsinfrared light to the surface. Photo-transistors will then receive thereflected infrared lights. If no infrared light is received, the OUTterminal will have low voltage when measured. In the case that itreceives infrared light, whether low or high current passes throughthe photo-transistor depends on the intensity of the light receivedwhich in turn varies according to the distance of the reflection.(Sensor TCRT5000 can be used at a distance of 0.1 – 1.5centimeters). Therefore, 0.5 – 5V can be measured at the OUTterminal, and the AX-11 will get a value of 10 to 255.

Page 77: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

77

Create the test field by cutting two 20 cm long and 1 in wideblack tape and placing it 50 cm apart on a flat surface.

Testing It

Place the Robo-11 Liner1 on the floor and then turn on thePOWER switch. The LCD screen displays the message PressStart. Press START on the AX-11. The robot will moveforward using only 50% of its power, while using the IRReflector Sensor to detect the black line. The robot willimmediately change directions if it detects a black line, thedirection depending on the operation of the sensors.

If the left sensor detects the line first, the robot will turnright. However, if the right sensor detects the line first, it willturn left. If both sensors detect a line, the robot will also turnleft.

How accurate the Robo-11 Liner1 detects the black linedepends on the chosen decision variable, whether is definedas the black line or the white surface. In the test program,the ref parameter is defined as 80. This value may bechanged depending on the test area, which may have moreor less light. Other factors may include the distance from thesensor to the floor, or if the test field is modified to havewhite lines on a black surface instead.

Page 78: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

78

Building Type 2

1 The Robo-11 Liner2 will have 3 IRReflector sensors installed, enabling amore efficient detection. Using the Robo-11 standard robot, remove the topstructure that has the AX-11 attached.

2 Turn the robot upside down. Install the10 mm plastic spacer using a 3 x 25 mmscrew screwed into the universal plateaccording to the positions (5,2), (8,6)and (5,9). The first set of numbers refersto the longitude positions from theleftmost side while the second set is thehorizontal positions form the top down.

3 Place the IR Reflector Sensors (ZX-03)onto the spacers, twisting the screwthrough the spacer and sensor andscrewing it tightly together with a 3 mmnut. Do the same for all sets, and set therobot upright.

4 Replace the top structure with the AX-11board back onto the bottom part. Thenconnect the left IR Reflector Sensor(looking down from the top) to terminalAl-31 and the right one to terminal Al-17of the AX-11

Page 79: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

79

Conditions in testing the Line Sensor

The programmer needs to test reading the values from the line sensor, in this case,detecting a black line and white surface. Then he must calculate the value that isable to differentiate between the reflection from the black line and the white fieldsurface. The program can be written as following:

void main(){

while(1) // Endless loop{printf(“L=%dM=%d R=%d\n”,analog(31),analog(25),analog(17));// Reading all sensorssleep(0.1); // Display delay}

The test results in the following:

When the black line is detected, the converted analog signals to digital signals willgive a value of 40 to 60.

When the white surface is detected, the converted analog signals to digital signalswill give a value of 40 to 60.

Therefore, 80 is chosen as the reference value for the black line detection. Thus, ifthe value read from the line sensor is less than 80, the sensor will consider it to beon a black line. But if the value read is more than 80, than the sensor is consideredto be on the white surface area.

When the Robot Moves

Results of all sensors are as following:

Left Line Sensor Central LineSensor

Right Line Sensor

Detected whitesurface. The valueread from input Al-31 was more than80.

Detected the blackline. The valueread from input Al-25 was less than80.

Detected whitesurface. The valueread from input Al-17 was more than80.

When the robot operates in this scenario, the robot mustbe controlled so that it moves forward and delays briefly.

Scenario 1: Robot moves along the line

Page 80: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

80

Scenario 2: The robot moves rightwards away of the line

Scenario 3: The robot moves leftwards away of the line.

Scenario 4: The robot meets an intersection

Results of all sensors are as following:

Left Line Sensor Central Line Sensor Right Line SensorDetected the blackline. The value readfrom input Al-31 wasless than 80.

Detected the blackline. The value readfrom input Al-25 wasmore than/less than80.

Detected whitesurface. The valueread from input Al-17 was more than80.

When the robot operates in this scenario, the robot must becontrolled so that it turns left slowly and delays briefly.

Results of all sensors are as following:

Left Line Sensor Central Line Sensor Right Line SensorDetected whitesurface. The valueread from input Al-31 was more than80.

Detected the blackline. The value readfrom input Al-25 wasmore than/less than80.

Detected whitesurface. The valueread from input Al-17 was less than 80.

When the robot operates in this scenario, the robot must becontrolled so that it turns right slowly and delays briefly.

Results of all sensors are as following:

Left Line Sensor Central Line Sensor Right Line SensorDetected the blackline. The value readfrom input Al-31 wasless than 80.

Detected the blackline. The value readfrom input Al-25 wasless than 80.

Detected the blackline. The value readfrom input Al-17 wasless than 80.

When the robot operates in this scenario, the robot has manyoptions to choose from whether it is to move forward, turn left, orturn right.

Page 81: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

81

/* Example for Robot track line- Motor left connected to DC Motor chanel M-0- Motor right connected to DC Motor chanel M-1 */

#define pow 50 /*Configuration power drive motor*/#define ref 80 /*Configuration analog reference */int left=0,right=0,mid=0; // For keep input analog value

void main(void){ao();while(!stop_button()) // wait STOP button pressed in process{printf(“Press start!\n”); // Show begining messagewhile(!start_button()); // Wait START button pressed to runprintf(“ Track Line\n”); // Show working messagewhile(!stop_button()) // Wait STOP button pressed in process{left = analog(31); // Read left sensor datamid = analog(25); // Read middle sensor dataright = analog(17); // Read right sensor dataif((left>ref)&&(mid<ref)&&(right>ref) // Check in line{run_fd(0.01); // Direct forward} else if((left<ref)&&(mid>ref)&&(right>ref)) // Check over right{turn_left(0.1); // Turn left for backing to line} else if((left>ref)&&(mid<ref)&&(right<ref)) // Check in line{turn_right(0.3); // Turn right for backing to line} else if((left>ref)&&(mid>ref)&&(right<ref)) // Check over left{turn_right(0.1); // Turn right for backing to line} else if((left<ref)&&(mid<ref)&&(right>ref)) // Check cross line{turn_left(0.3); // Turn left for backing to line}else if((left<ref)&&(mid>ref)&&(right<ref)) // Check between cross line{turn_left(0.3); // Turn left for backing to line}else // If out of condition{run_fd(0.01); // Direct forward}}

Robo-11 Liner2 Test

After the Robo-11 Liner2 has been put together, we will next write a test programto test the operation of the Robo-11 Liner2 by having it detect and move forwardalong a black line.

Type in the following program code and download it to the Robo-11 Liner2.

Page 82: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

82

printf(“Stop..\n”); // Show finish messageao(); // Off all motor for ending programbeep(); // Sound beep signal for ending program}}

void turn_left(float spin_time){motor(0,-pow); // Motor0 backward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(delay); // Delay set by parameter delay}

Testing It

Place the Robo-11 liner2 over the black line on the floor (provided in the set) andthen turn on the POWER switch. The LCD screen displays the message PressStart. Press START on the AX-11. The robot will move forward using only 50% ofits power. At the same time, the display screen will show the message “TrackLine”. When the intersection is located, the robot will always decide to turn left andwill only stop operating when the STOP switch is pressed.

How accurately the Robo-11 Liner2 detects the black line depends on the chosendecision variable, whether it is defined as the black line or the white surface,similar to the Robo-11 Liner1. It also depends on choosing the appropriate speedlevel. If it moves too fast, it may swing out of its course.

Page 83: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

83

This robot was further developed from theRobo-11 standard by adding switches that willdetect obstacles via collision.

BUILDING THE ATTACKER

1 Using the Robo-11 Standard robot (or theRobo-11 Liner2), take a strength joinerand attach it to the front of the robot witha 3 x 10 screw and 3 mm nut as shown inthe picture. It should be twisted outwardand installed at approximately 45degrees.

2 Next, connect an obtuse joiner to the endof that strength joiner.

3 At the other end of the obtuse joiner fromstep 2, attach an angled joiner. Thisangled joiner will then be used to connectthe switch.

Page 84: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

84

4 Take the switch and install it to that robot arm that was built instep 3, by using a 3 mm spacer, 3 x 10 screw, and 3 mm nutand screwing them in together tightly.

5 Then proceed to build the robot arm for the other side, usingthe same steps. When finished, connect the signal cable of theleft switch to terminal Dl-15 and the signal cable of the rightswitch to terminal Dl-10 of the AX-11 board. The Robo-11Attacker is now finished and ready to be tested.

Page 85: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

85

ABOUT THE SWITCH CIRCUIT

Pressing the switch results in two occurrences.

When the switch is not pressed, let the results be logic “1”When the switch is pressed, let the results be logic “0”, and LED1 light up.

Since the switch can give two results, it is considered to be a digital inputcomponent. Therefore, in connecting it to the AX-11 board, it must be connected tothe digital input terminals D1 which has 9 channels altogether, D1-0 to D1-8.

In addition, it can also use the LED display on the switch as a digital output byconnected it to the digital output terminal D0 which also has 9 channels. The LEDon the switch will turn on or off depending on the logic “0” or “1” being sent. Whenlogic “0” is sent, the LED will light (This method of use is not recommended forbeginners)

Caution: When the LED display on theswitch is used as an output, theswitch must not be used or pressedor else the switch terminal ports maybe damaged.

Robo-11 Attacker Test

The next step would be to write a test program to test theoperation of the Robo-11 Attacker by having it detect objects bytouch or collision and then to change directions to avoid thatobject.

Type in the following program code and download it to theRobo-11 Attacker.

Page 86: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

86

/* Example for object detection by Touch sensor- Motor left connected to DC Motor chanel M-0- Motor right connected to DC Motor chanel M-1- Touch sensor left side connected to DI-15- Touch sensor right side connected to DI-10 */#define pow 50 /*Configuration power drive motor*/void main(){int obj_left,obj_right; // For keep result detect objectprintf(“Press Start!\n”); // Display messagestart_press(); // Wait until START button pressedprintf(“Robot Move...\n”); // Show running messagewhile(!stop_button()) // Infinite loop{obj_left = digital(15); // Keep left sensor detectionobj_right = digital(10); // Keep right sensor detectionif(obj_left==0 && obj_right==0) // In case both sensor don’t detect{run_fd(0.1); // Forward 0.1 second}else if(obj_left==0 && obj_right==1)

// In case right sensor detect object only{run_bk(0.5); // Backward 0.5 secturn_left(0.5); // Turn left 0.5 sec}else if(obj_left==1 && obj_right==0) // In case left sensor detect object{run_bk(0.5); // Backward 0.5 secondturn_right(0.5); // Turn right 0.5 second}else if(obj_left==1 && obj_right==1) // In case both sensor detect object{run_bk(0.5); // Backward 0.5 secondturn_right(1.0); // Turn right 1.0 second}}ao(); // Off motor all channelbeep(); // Sound alarmprintf(“Stop...\n”); // Show ending message}void turn_left(float spin_time){motor(0,-pow); // Motor0 backward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(delay); // Delay set by parameter delay}

Page 87: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

87

Testing It

Place the Robo-11 Attacker on the floor (provided inthe set) and then turn on the POWER switch. The LCDscreen displays the message Press Start. PressSTART on the AX-11. The robot will move forwardusing only 50% of its power. At the same time, thedisplay screen will show the message “Robot Move”.

When the robot runs into an obstaclewith the left arm:The robot will move backwards for 0.5 seconds andturn right for 0.5 seconds. Then it will continue tomove forward.

When the robot runs into an obstaclewith the right arm:The robot will move backwards for 0.5 seconds andturn left for 0.5 seconds. Then it will continue tomove forward.

The robot will stop when the STOP switch is pressed.

The Robo-11 Attacker uses a switch to enable digitalcommunication with the sensor. When the switch ispressed, or activated, the digital signal “0” will besent. With the proper coding, the Robo-11 Attackerwill be able to avoid the obstacle and will also havethe capability for Maze Solving if additional sensorsare installed.

Page 88: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

88

Adding the Infrared Ranger Sensor GP2D120 tothe Robo-11 Standard robot enables it to detectobstacles at a distance without having to collideinto or touch the object.

BUILDING THE RANGER

1 Using the Robo-11 Standard robot (or theRobo-11 Liner2 or Attacker), take anangled joiner and attach it to the left andright front of the robot with a 3 x 10screw and 3 mm nut as shown in thepicture. (If the Robo-11 Attacker is usedand modified, the position of the switcharm might need to be changed).

2 Next, insert a strength joiner verticallyinto both angled joiners.

3 Take two more angled joiners and attachit to the other ends of the strength joiner.

Page 89: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

89

4 Then attach another angled joiner to theend of the angled joiner in step 3.

5 Install the GP2D120 module to the end ofthe angled joiner from step 4. The armsmight need to be pulled out a bit forinstallation. Use a 3 x 10 screw and two 3mm nuts to install the GP2D120 to thearm from steps 1-4. Then connect thesignal cable of the module to the inputterminal A1-6 of the AX-11 board.

Now the Robo-11 Ranger robot is ready tomeasure the range and detect obstacles at adistance without having to make any contactwith the object.

GP2D12 and GP2D120 Infrared Ranger and Sensor Module

• Measures the range using Infrared light• Can measure a distance of 10 to 80 cm for the

GP2D12 and 4 to 30 cm for the GP2D120• Uses 4.5-5 V voltage and 33 mA• Provides 0.4 – 2.4 V output range at +5V

supply.

The GP2D12/GP2D120 are infrared sensormodules that have 3 terminals, which are thevoltage input (Vcc), ground (GND), and thevoltage output (Vout). The values from theGP2D12 must be read after the initiating period ofthe module, which usually lasts around 32 – 52.9ms. After this time, the voltage output can bemeasured.

For the GP2D120, the output at the 30 cmdistance and +5V input is within the 0.25 to 0.55V range, with the average being 0.4V. Thus, theoutput variation within the 4 to 30 cm range is2.25V + 0.3V.

Warning for the GP2D12 and GP2D120signal cable.

Since the GP2D12 and GP2D120 havedifferent terminal layouts than that of theAX-11 board, even though they look thesame, a special signal cable was made forthe two modules. The cable is alreadyconnected to the modules. All the user needsto do is connect this cable to the AX-11board. The user must not remove this cableat all times and MUST NOT switch theGP2D120 module cable with the cable ofother sensor modules.

Page 90: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

90

Robo-11 Ranger Test

In writing the program to test the Robo-11Ranger robot, we emphasize on thecommunication with the GP2D120 module todetect and measure the distance between theobject and the robot so that it can avoid theobstacle without having any physical contact.

Type in the following program code anddownload it to the Robo-11 Ranger.

Page 91: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

91

/* Example for infrared detector sensor by GP2D120- Motor left connected to DC Motor chanel M-0- Motor right connected to DC Motor chanel M-1- GP2D120 connected to AI-6 */#use “gp2d120_lib.ic” /* gp2d120_lib.ic */#define pow 50 /* Set 50% motor power */void main(){float result;printf(“Press Start!\n”); // Show begining messagestart_press(); // Wait START button pressedprintf(“Robot Move...\n”); // Show running messagewhile(!stop_button()) // Infinite loop{result = distance(); // Get distance valueif (result <= 20.0 && result != -1.0 ) // Check distance <=20 ?

// if TRUE escape{run_bk(1.0); // Backword 1 secondturn_left(0.5); // Turn left 0.5 second}else{run_fd(0.1); // Forward 0.1 second}}ao(); // Off motor all channelbeep(); // Sound alarmprintf(“Stop...\n”); // Show ending message}void turn_left(float spin_time){motor(0,-pow); // Motor0 backward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(delay); // Delay set by parameter delay}

Page 92: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

92

Testing It

Place the Robo-11 Ranger on the floor and then turn onthe POWER switch. The LCD screen displays themessage Press Start. Press START on the AX-11. Therobot will move forward using only 50% of its power. Atthe same time, the display screen will show the message“Robot Move”.

When the robot moves closer to theobstacle and is within the specifieddistance :The Robo-11 Ranger robot will move backwards 1second and turn left for 1 second. Then it will moveforward.

The specified range to detect objects in this test is 20cm. .

The robot will stop when the STOP switch is pressed.

The Robo-11 Ranger is another example of analogcommunication with the sensor. The value read from thesensor is converted to digital data and processed furtherto provide results. The results in this case is the distancemeasured from the sensor to the object.

Page 93: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

93

Additional sensors and components to measurethe movement distance are added so that the usercan control the robot to move within a specifieddistance more accurately.

BUILDING THE SMARTMOVER

1 Using the Robo-11 standard robot,remove the structure of the robot that isattached to the top of the AX-11 board.On the bottom part, attach the encoderwheel sticker to the two main trackwheels.

2 Take an angled joiner and attach it to thebase plate at the positions of 16,3 and16,9 (counting from the side opposite ofthe motor gear set) with a 3 x 10 mmscrew and a 3 mm nut. The right angleside should be turned outwards as shownin the picture

3 Attach a 25 mm copper tube to theangled joiner with a 3 x 6 mm screw. Thecopper tubes should be facing outwardsas shown in the picture. Do the same forboth sides. .

Page 94: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

94

4 Take the ZX-21 code wheel reflectorsensor and attach it to the other end ofthe copper tube. The infrared reflectorsensor should be turned inwards to facethe main track wheel. Screw togetherwith a 3 x 6 mm screw and repeat thesame for both sides.

5 In installing the ZX-21, the infraredreflector should be placed in the maintrack wheels with a distance of 2 to 3 mmaway from the code wheel sticker.Practically, if the sensor is attached to thecopper tube as in step 5, the distancewill be just right.

6 Then place the top structure that wasremoved back onto the AX-11 board.Connect the left signal cable from the ZX-21 to terminal IN-8, and the right signalcable to terminal IN-7 of the AX-11board. The Robo-11 SmartMover is nowcomplete.

Page 95: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

95

• Used with the 18 color tab code wheel sticker; each color making a 20 degreeangle.

• The output is a pulse signal which can be connected directly to a micro-controller.

The heart of this sensor module is the infrared reflector sensor and the codewheel sticker. The infrared light that is emitted from the sensor will reflect on thecode wheel sticker. If it hits the black area, the photo-transistor will receive littlereflected light, causing logic “1” to be sent to the digital receiver which consists oftwo notgets. However, if is it reflected onto a white or silver area, more reflectionwill occur and the photo-transistor will operate well, sending logic “0”. When thewheel turns, alternate reflections like these will occur, causing pulse outputsignals. One complete round of the wheel will give 9 pulse and 18 signals.

ZX-21 Code Wheel Reflector Sensor

Measuring distance with theEncoder Wheel Sensor

The ZX-21 Encoder Wheel sensor will work in accordancewith the code wheel sticker by detecting the infraredreflections from the code wheel sticker attached to the robot.The code wheel sticker has a diameter of 3.5 cm, with 9alternating black and silver tab areas each; altogetherresulting in 18 color tabs. If the arc of one color area isdefined as s and d is the 3.5 cm diameter, thus

The circumference of the outer wheel is

2πr = (2*3.14*3.5/2) = 10.99cm

Distance s (one step range) has a value of

πd/18 = (3.14*3.5)/18 = 0.61cm

Page 96: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

96

/*encoder_lib.ic*//* Hardware Configuration- Encoder left connect to port IN-8- Encoder right connect to port IN-7 */int LEFT =1; // Define for encoder left side channelint RIGHT =0; // Define for encoder right side channelint FORWARD = 1; // Define for drive robot forwardint BACKWARD = 2; // Define for drive robot backwardint SPIN_LEFT = 3; // Define for drive robot spin rightint SPIN_RIGHT = 4; // Define for drive robot spin leftint TURN_LEFT = 5; // Define for drive robot turn leftint TURN_RIGHT = 6; // Define for drive robot turn rightint POWER = 40; // Define power for drive robot and initial at 40%float STEP=0.0; // Variable for keep Global step

//——————— Function for reset encoder left and right——————//void reset_encoder_all(){reset_encoder(LEFT); // Reset Encoder left side(IN-8)reset_encoder(RIGHT); // Reset Encoder right side(IN-7)}//——————— Function for initial Encoder before working —————//void encoder_init(){enable_encoder(LEFT); // Enable count Encoder left side(IN-8)enable_encoder(RIGHT); // Reset count Encoder right side(IN-7)reset_encoder_all(); // Reset twice Encoder}//——————— Function for read step for left/right side Encoder —//int read_step(int ch){read_encoder(ch); // Read count Encoder by channel referencereturn(read_encoder(ch)); // Return read count value}

The ZX-21 will count 1 every time the wheel code area shiftsposition to the next area tab, whether it is from black tosilver (white) or silver (white) to black. The change of wheelcodes our caused when the wheel turns. When the countchanges or increases by 1, it means that the robot hasmoved 1 step, or a distance of 0.61 centimeters.

Encoder_lib.ic Library

To make it convenient in using the ZX-21 Encoder Wheel andthe AX-11 board with the Interactive C program, a library forthe wheel encoder to use when measuring the movementrange for various operations of the robot was prepared. Inthis library, you will find a complete set of functions thatinvolve the encoder wheel.

Page 97: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

97

//——————— Function for read total step ————————————//float totel_step(){return(STEP); // Return Global step count}//——————— Function for read total distance ——————————//float total_dist(){return(STEP*0.61); // Return Global distance count}//——————— Function for clear total step count —————————//void clear_dist_all(){STEP = 0.0; // Clear Global step count value}//——————— Function for count step by reference —————————//void count_step(int sel_move,int step,int sel_count){int flag=0,left,right;reset_encoder_all(); // Reset twice Encoderif(sel_move==FORWARD) // Condition forwardFD();else if(sel_move==BACKWARD) // Condition backwardBK();else if(sel_move==SPIN_LEFT) // Condition spin leftS_LEFT();else if(sel_move==SPIN_RIGHT) // Condition spin rightS_RIGHT();else if(sel_move==TURN_LEFT) // Condition turn leftT_LEFT();else if(sel_move==TURN_RIGHT) // Condition turn rightT_RIGHT();while(!flag) // Loop for check count step{left = read_step(LEFT); // Read step leftright = read_step(RIGHT); // Read step rightif(sel_count==LEFT && left>=step) // Encoder left side complete?flag = 1; // Set flag when count completeelse if(sel_count==RIGHT && right>=step) // Encoder right side complete?flag = 1; // Set flag when count complete}ao(); // Off all motorif(sel_move==FORWARD || sel_move==BACKWARD)

// Condition update Global step count{STEP = STEP + (float)read_step(sel_count);

// Update step count if drive robot forward or backward}reset_encoder_all(); // Reset twice Encoder}//——————— Function for distance counting by reference ——————//void count_dist(int a,int b,int c){count_step(a,(b*100)/61,c); // Calculate distance to step count and// drive robot in this result}//——————— Robot forward driving function ——————————//void FD(){motor(0,POWER); // Motor 0 forwardmotor(1,POWER); // Motor 1 forward}

Page 98: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

98

//——————— Robot backward driving function ——————————//void BK(){motor(0,-POWER); // Motor 0 backwardmotor(1,-POWER); // Motor 1 backward}//——————— Robot spin left function ——————————//void S_LEFT(){motor(0,-POWER); // Motor 0 backwardmotor(1,POWER); // Motor 1 forward}//——————— Robot spin right function ——————————//void S_RIGHT(){motor(0,POWER); // Motor 0 forwardmotor(1,-POWER); // Motor 1 backward}//—————- Robot turn left function ——————————//void T_LEFT(){off(0); // Motor 0 breakmotor(1,POWER); // Motor 1 forward}//——————— Robot turn left function ——————————//void T_RIGHT(){off(1); // Motor 1 breakmotor(0,POWER); // Motor 0 forward}

Explanations of the functions

Two ZX-21 encoder wheel sensors are referenced when the usercalls upon the functions in the encoder_ib.ic library: the leftsensor which is connected to port IN-8 and the right sensor whichis connected to port IN-8 of the AX-11 board. The M-0 slot is alsoused to drive the left motor wheel of the robot and the M-1 todrive the right wheel.

In the beginning of the program, if the user wants to use thislibrary, he or she must call upon it by using the command #use“encoder_lib.ic”. In the first part of the program, and within themain function, the user must call the encoder_init function inorder to be able to start using both encoder wheels.

Page 99: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

99

1. reset_encoder_all function

Used to clear the count values from the encoder wheel sensor and startagain.

Function Format

Void reset_encoder_all ()

2. encoder_init function

Used to initiate the encoder wheel sensor. In writing a program, thisfunction must be called at the beginning of the main program.

Function formatVoid encoder_init()

3. read_step function

Used to read the count value from the encoder wheel sensor

Function formatInt read_step (int ch)

ParametersCh Used to choose the encoder wheel sensor that the user wants to read.

Define Left if you want to read the value from the left encoder wheelDefine Right if you want to read the value from the right encoder wheel

Return Value

The return value is the step count received from the encoder wheel specified.

Page 100: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

100

4. Total_step function

Used to read the cumulative values from the encoder wheel sensor bymeasuring the steps only when the robot moves forward and backwards. Thevariable STEP is used to store the cumulative steps.

Function formatFloat total_step ()

Return ValueThe return value is the cumulative step count read from the encoder

wheel.

5. Total_dist function

Used to read the cumulative distance from the encoder wheel sensor bymeasuring the distance only when the robot moves forward and backwards.Calculate with 1 step equal to 0.61 centimeters.

Function formatFloat total_dist()

Return ValueThe return value is the cumulative distance read from the encoder wheel.

6. clear_dist_all function

Used to clear the cumulative count.

Function formatVoid clear_dist_all()

Page 101: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

101

7. count_step function

Used to command the robot to move according to the number of stepsdefined.

Function formatVoid count_step (int sel_move, int step, int sel_count)

ParameterSel_move used to specify how the robot moves as following:

Forward for forward movementBacward for backward movementSpin_left To rotate leftSpin_right To rotate rightTurn_left To turn leftTurn_right To turn right

Step Used to define the number of steps that the user wantsSel_count Used to choose which encoder wheel sensor to read from, as

following

Left choose the left encoder wheelRight choose the right encoder wheel

8. count_dist function

Used to command the robot to move according to a specified distance;modified from the count_step function.

Function formatVoid count_dist (int a , int b, int c)

Page 102: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

102

Parameters

a Used to define the robot movement as following

Forward for forward movementBacward for backward movementSpin_left To rotate leftSpin_right To rotate rightTurn_left To turn leftTurn_right To turn right

b Used to define the distance the user wants in centimetersc Used to choose the encoder wheel sensor that is to be used, asfollowing

Left choose the left encoder wheel sensorRight choose the right encoder wheel sensor

9. FD function

Used to drive the robot forward

function format void FD()

10. BK function

Used to drive the robot backwards

function format void BK()

11. S_Left function

Used to make the robot to rotate left

Function format void S_LEFT()

Page 103: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

103

12. S_Right function

Used to make the robot to rotate right

Function format void s_right()

13. T_Left function

Used to make the robot turn left

Function format T_LEFT ()

14. T_RIGHT function

Used to make the robot turn right

Function format T_RIGHT ()

15. POWER variable

Use to define the driving power of the robot, with values from 0% to 100%. If the value is not specified in the program, the initial value is 40% (POWER = 40) as defined in the library.

Page 104: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

104

Robo-11 SmartMover Test

The test program for the Robo-11 SmartMover robot can beseparated into two tests. The first is to let the robot move a specifieddistance, which is the basic test for the robot and the encoder wheelsensor. Then add another operation for the robot to follow by letting itmove in the shape of a character from the alphabet. This shows howthe encoder wheel sensor can be used in more complex situations tomake the robot’s turns and changes of direction more accurate.

60 cm-mover: Drive the Robo-11 SmartMover forward for 60centimeters

Type in the following program and download it into the Robo-11SmartMover

/*Example for Robot samrt movement by reflective encoder sensor and codewheel *//* - Encodder pulse left connected to IN-8- Encodder pulse right connected to IN-7 */

#use “encoder_lib.ic” /*Include Library file encoder_lib.ic*/void main(){encoder_init(); // Initial EncoderPOWER = 50; // Set power at 50%printf(“Press Start!\n”); // Show begining messagewhile(1) // Infinite loop{while(!start_button()); // Wait START button pressedprintf(“\n”); // Clear LCD displaycount_dist(FORWARD,60,RIGHT); / Forward 60 cm reference with

// Encoder right sideprintf(“STEP %f\n”,total_dist()); // Display step count value}}

Page 105: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

105

Testing It

Place the Robo-11 SmartMover on the floor and then turn on the POWER switch.The LCD screen displays the message Press Start. Press START on the AX-11.The robot will move forward using only 50% of its power for a distance of 60centimeters and stops, as well as displaying the distance moved. If the STARTswitch is pressed again, the robot moves forward for another 60 centimeters,stops, and displays the cumulative distance which it moved. The value will haveincreased from the original value by 60 cm.

For this program, the line #use “encoder_lib.ic” must be used to call upon thefunctions in the encoder_lib.ic library that will be used with the ZX-21 encoderwheel sensor. Then initiate the encoder wheel sensor by using the functionencoder_init, followed by defining the motor power to 50% of its maximumpower. Next, a message appears on the LCD awaiting the START switch to bepressed. The robot will then move forward for a distance of 60 centimeters andstops. The robot will display the cumulative distance on the LCD. If the STARTswitch is pressed again, the robot will operate in the same way, while thecumulative value that is displayed will increase by 60 centimeters each time.

U-mover: Driving the Robo-11 SmartMover in aU-Shape

Type in the following program and download it to the Robo-11 SmartMover

/*Example-2 for Robot samrt movement by reflective encoder sensor and codewheel */#use “encoder_lib.ic” /*Include Library file encoder_lib.ic*/void main(){encoder_init(); // Initial Encoderprintf(“Press Start!\n”); // Show begining messagewhile(1) // Infinite loop{while(!start_button()); // Wait START button pressedclear_dist_all(); // Clear total distanceprintf(“\n”); // Clear displaycount_dist(FORWARD,60,RIGHT); // Robot forward 60 cm reference by

// Encoder right sidecount_step(SPIN_RIGHT,18,LEFT); // Robot spin right 18 step reference

// by Encoder left sidecount_dist(FORWARD,60,RIGHT); // Robot forward 60 cm

// reference by Encoder right sidecount_step(SPIN_RIGHT,18,LEFT); // Robot spin right 18 step

// reference by Encoder left sidecount_dist(FORWARD,60,RIGHT); // Robot forward 60 cm

// reference by Encoder right side}}

Page 106: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

106

Testing It

Place the Robo-11 SmartMover on the floor and then turnon the POWER switch. The LCD screen displays themessage Press Start. Press START on the AX-11. Therobot will move forward using only 50% of its power for adistance of 60 centimeters and rotate right. Then it willmove forward for another 60 centimeters, rotate rightagain, and move another 60 centimeters. It will continueto repeat these actions. If the user looks down from a topview, he will see that the robot’s movements are of thatsimilar to the English alphabet U.

However, in this program, if the robot needs to make a 90degree turn to get the desired direction, the user maysometimes have to use the count_step or count_distfunction. In this test, to get a 90 degree turn, thefollowing values must be specified count_step(SPIN_RIGHT, 18, LEFT); In the actual results, the robotmay not be able to rotate 90 degrees exactly due to thefact that the rotational axis may not always be stable. Tofix this problem, test by letting the robot rotate atdifferent step values until it gets the desired angle.

Page 107: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

107

With the ZX-08* digital sensor, the robot has yetanother means to detecting obstacles by infraredlight without making any contact with the object.

* Additional component that does not comewith the set

BUILDING THE OBJECTOR

1 Using the Robo-11 Standard robot (or theRobo-11 Liner2), take a strength joinerand attach it to the right corner at thefront of the robot with a 3 x 10 screw and3 mm nut as shown in the picture. Do notscrew in tightly yet.

2 Attach an obtuse joiner onto the strengthjoiner, with the obtuse angle facinginwards.

3 Next, place a strength joiner to the end ofthe obtuse joiner. You will get a roboticarm to place the sensor.

Page 108: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

108

4 Twist the arm to the right, making anangle of 45 degrees with the robot. Thenscrew in the screw and nut togethertightly.

5 Install the ZX-08 sensor to the strengthjoiner at the end of the robot arm with a3 x 10 mm screw and 3 mm nut. Screw intightly.

6 Create another robotic arm to installanother ZX-08 by using steps 1-4.

7 Install the ZX-08 to the end of the roboticarm that was just built. Then connect thesignal cables as following:

(1) Connect Tx at the left of the ZX-08 toterminal OUT-0

(2) Connect Rx at the left of the ZX-08 toterminal Dl010

(3) Connect Tx at the right of the ZX-08 toterminal OUT-1

(4) Connect Rx at the right of the ZX-08to terminal Dl-11

The Robo-11 Objector is now complete andready for the test program.

Page 109: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

109

ZX-08 Infrared objector

This sensor has both the infrared light receiver andtransmitter within itself. The ZX-08 sensor can detectobstacles at a maximum distance of 6 centimeters. TheRx signal cable must be connected to the digital input (Dlor IN), while the Tx must be connected to any digitaloutput (OUT) on the AX-11 board. Once the signal cablesare connected, sending logic “1” to Tx will make theinfrared LED on the ZX-08 module light up. If there is anobstacle blocking in front, the infrared light will sendreflect that object back to the infrared receiver, causing alogic “0” to be sent to the AX-11 input.

Page 110: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

110

/* Example for Robot infrared detector sensor by called ZX-08- Motor left connected to DC Motor channel M-0- Motor right connected to DC Motor channel M-1- TX of ZX-08 left side connected to OUT-0- TX of ZX-08 right side connected to OUT-1- RX of ZX-08 left side connected to DI-10- RX of ZX-08 right side connected to DI-11 */#define pow 50 /*Configuration power drive motor*/

void main(){int obj_left,obj_right; // For keep result detect objectprintf(“Press Start!\n”); // Show begining messagestart_press(); // Wait START button pressedprintf(“Robot Move...\n”); // Show running messageset_digital_out(0); // Send TX ir signal to left sensorset_digital_out(1); // Send TX ir signal to right sensorwhile(!stop_button()) // Infinite loop{obj_left = digital(10); // Get detection result from left sensorobj_right = digital(11); // Get detection result from right sensorif(obj_left==0 && obj_right==0) // If left and right do not detect object{run_fd(0.1); // Forward 0.1 second}else if(obj_left==0 && obj_right==1) // If right sensor detect object{run_bk(0.5); // Backward 0.5 secondturn_left(0.5); // Turn left 0.5 second}else if(obj_left==1 && obj_right==0) // If left sensor detect object only{run_bk(0.5); // Backward 0.5 secondturn_right(0.5); // Turn right 0.5 second}else if(obj_left==1 && obj_right==1) // If both sensor detect object{run_bk(0.5); // Backward 0.5 secondturn_right(1.0); // Turn right 1.0 second}}

Robo-11 Objector Test

The next step would be to write a test program to test theoperation of the Robo-11 Objector by having it detectobjects by not touching or colliding with the objects andthen to change directions to avoid that object.

Type in the following program code and download it to theRobo-11 Objector.

Page 111: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

111

ao(); // Off motor all channelbeep(); // Sound alarmprintf(“Stop...\n”); // Show ending message}void turn_left(float spin_time){motor(0,-pow); // Motor0 backward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(delay); // Delay set by parameter delay}

Testing It

Place the Robo-11 Objector on the floor and then turn on the POWER switch. TheLCD screen displays the message Press Start. Press START on the AX-11. The robotwill move forward using only 50% of its power. At the same time, the display screenwill show the message “Robot Move”.

When the left sensor finds an object:The Robo-11 Objector robot will move backwards for 0.5 seconds and turn right for0.5 seconds. Then it will continue to move forward.

When the right sensor finds an object:The Robo-11 Objector robot will move backwards for 0.5 seconds and turn left for0.5 seconds. Then it will continue to move forward.

The robot will stop when the STOP switch is pressed.

Page 112: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

112

Increase the Robo-11 robot’s capability indetecting objects at a far distance without anymaking contact by using the SRF04* Ultra SonicSensor

* Additional component that does not comewith the set.

BUILDING THE SONAR ROBOT

1 Using the Robo-11 Standard robot (or theRobo-11 Liner2), take an angled joinerand attach it to the front of the robot witha 3 x 10 screw and 3 mm nut at position1, 6 as shown in the picture. Let the rightangle face outwards and screw togethertightly.

2 Take another angled joiner and attach itto the angled joiner from step 1. Makesure the other end is facing the left of therobot as shown in the picture.

3 Then take another angled joiner andattach it to the angled joiner from step 2.. Make sure the other end faces the frontof the robot at shown in the picture.

Page 113: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

113

4 Yet another angled joiner is taken andattached to the angled joiner from step 3.This time, let the right angle side face thefront of the robot.

5 Take the SRF04 module and attach it tothe ADX-SRF04 board. Then install it tothe angled joiner from step 4 using a 3mm plastic spacer, 3 x 10 mm screw, and3 mm nut. Attach the ADX-SRF04 to thetop right corner (looking in from the frontof the robot and SRF04 module)

6 Connect the signal cables of the ADX-SRF04board to the ports on the AX-11 board asfollowing:

a. Connector ECHO to terminal IC1 of theAX-11

b. Connector PULSE to terminal IN-9 ofthe AX-11

The Robo-11 Sonar is now complete andready to be tested.

Page 114: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

114

/*Example for object detection of Robo-11 by sonar sensor *//* Hardware configuration- ECHO pin connected to IC1- TRIGGER PULSE pin connected to IN-9- Motor left connected to DC Motor chanel M-0- Motor right connected to DC Motor chanel M-1 */#use “srf04_lib.ic” /* Include library srf04_lib.ic */#define pow 50 /*Configuration power drive motor*/void main(){int result; // Define variable for storing distance valueao(); // Off all motorssonar_init(); // Initial sonar sensorprintf(“Press Start!\n”); // Show begining messagewhile(!start_button()); // Wait START button pressedbeep(); // Drive beep soundwhile (!stop_button()) // Check STOP button pressed{result = sonar_sample(); // Get and store distance valueif (result <= 20 && result >= 0 ) // Check distance <=20 and >=0 ?// if TRUE escape

SRF04 ULTRASONIC SENSOR

• +5V power supply and 30 mA currency• Uses a 40 kHz ultrasonic receiver and transmitter• Measures a distance of 3 cm – 3m• 10 micro-second pulse signal• Returns a pulse signal in which the pulse width is

proportional to the measured distance.• Small in size: 43 mm x 20 mm x 17 mm (w x l x h)

The SRF04 will send the ultrasonic frequency signal outand then measure the time for the signal to reflect back.The output from the SRF04 is in the form of a pulse, inwhich the width is proportional to the distance of thedetected object. The ultrasonic frequency of the SRF04 is40 kHz.

Robo-11 Sonar Test

The test program of the Robo-11 Sonar will emphasize on communicating with theSRF04 module in order to detect and measure the distance between the robot andan object. The results that were measured will then be used to determine themovements of the robot so that it can avoid the obstacles without having anycontact with the object.

Type in the following program code and download it to the Robo-11 Sonar.

Page 115: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

115

{run_bk(0.5); // Backword 0.5 secondturn_left(0.5); // Turn left 0.5 second}else{run_fd(0.1); // Forward 0.1 second}}beep(); // Sound beepao(); // Off all motors}void turn_left(float spin_time){motor(0,-pow); // Motor0 backward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(delay); // Delay set by parameter delay}

Testing It

Place the Robo-11 Sonar on the floor and then turn on the POWER switch. The LCDscreen displays the message Press Start. Press START on the AX-11. The user willhear a beep sound and then robot will move forward using only 50% of its power.

When the robot moves closer to the obstacle and is within the specified distance :The Robo-11 Sonar robot will move backwards for 0.5 second and rotate left for 0.5seconds. Then it will move forward.

The specified range to detect obstacles in this test is 20 cm.

The robot will stop when the STOP switch is pressed. You will hear the beep againbefore it stops.

The Robo-11 Sonar is an example of using special communication with the sensor byusing pulse counts, which is different from the GP2D120 module which returns avoltage value, or the ZX-08 module which returns basic logic signals.

Page 116: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

116

Increase the capability for the Robo-11 to locate aposition and direction by using the CMPS03Electronic Compass module which uses a magneticfield sensor in locating a particular direction.

BUILDING THE COMPASSOR

1 Using the Robo-11 Standard robot (or theRobo-11 Liner2), take an angled joinerand attach it to the front of the robot witha 3 x 10 screw and 3 mm nut at position1, 6 as shown in the picture. Let the rightangle face outwards and screw togethertightly.

2 Take a strength joiner and attach it to theother end of the angled joiner fromstep 1.

3 Take another strength joiner and attach itto the other end of the strength joinerfrom step 2.

Page 117: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

117

4 Yet another strength joiner is taken andattached to the strength joiner from step3

5 Take an angled joiner and attach it to theother end of the strength joiner from step4, making sure the end of the joiner facesthe front of the robot. The structure isnow ready for the CMPS03 to be installed.

6 Attach the CMPS03 module to theADX-CMPS03 board and then installit onto the structure built in steps 1-5. Use a plastic spacer, a 3 x 10 mmscrew, and a 3 mm nut, and screwthem in together tightly.

Next connect the signal cable fromterminal PWM of the ADX-CMPS03 toterminal Dl-14 of the AX-11 board.The Robo-11 Compassor is nowcomplete.

Page 118: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

118

CMPS03 Electronic Compass Module

• Uses a +5V power supply and 20 mA currency• Uses two of Phillip’s KMZ51 magnetic field sensors• 0.1 degree accuracy, with approximately 3-4 degree error rate.• Pulse output with a width of 1 to 37 ms and 0.1 ms increment rate.• I2C Bus system output which sends data in two formats: 0 to 255 and

0 to 3599.

Robo-11 Compassor Test

Adjust the Reference direction for the Robo-11 Compassor robot

In the following preparations, the direction of the CMPS03 ElectronicCompass module will be defined and adjusted so that it can be used as themain reference value. The value will then be stored in the internal memoryof the CMPS03, which will be done only once. This reference data will not beerased even if there is no power supplied to the CMPS03.

1. Turn the power switch of the Robo-11 Compassor on.2. Place the robot on an empty field and face it towards the north. Press

the switch on the ADX-CMPS03 once and let it go.3. Then turn the robot so that it is facing east. Press the switch on the

ADX-CMPS03 once more and let it go.4. Then turn the robot to face south. Press the switch on the ADX-

CMPS03 once more and let it go.5. Last, turn the robot to face west. Press the switch on the ADX-

CMPS03 once more and let it go. Now the CMPS03 compass modulecan tell directions accurately.

Writing the test program for the Robo-11 Compassor

In this test program for the Robo-11 Compassor, we will imagine that therobot is placed in a very large field with a smooth surface, making it unableto check its movement by touch or by its reflector sensors. The onlymethod left is to use the electronic compass module to detect the magneticfield and use it to determine which direction it should move next.

Type in the following program and download it to the Robo-11 Compassorrobot.

Page 119: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

119

/*Example for Robot movement control by compass sensor *//* Hardware configuration- PWM channel from adapter board(CMPS03) connected to DI-14- Motor left connected to DC Motor chanel M-0- Motor right connected to DC Motor chanel M-1 */#use “cmps03_pwm_lib.ic” /* Include library srf04_lib.ic */#define pow 40 /*Configuration power drive motor*/

void main(){long result; // Definf variable for storing angle valueao(); // Off all motorsCMPS03_set_channel(14); // Initial CMPS-03 sensor used DI-14printf(“Press Start!\n”); // Show begining messagewhile(!start_button()); // Wait START button pressedbeep(); // Sound beepwhile (!stop_button()) // Check STOP button pressed{result = read_angle(); // Keep angle valueprintf(“Angle %d\n”,result); // Display angleif (result > 0L && result <= 40L) // Check angle value <=40 ?

// if TRUE escape{run_fd(0.05); // Forward 0.05 second}else{turn_left(0.05); // Turn left 0.05 second}}beep(); // Sound beepao(); // Off all motors}void turn_left(float spin_time){motor(0,-pow); // Motor0 backward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void turn_right(float spin_time){motor(0,pow); // Motor0 forward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(spin_time); // Delay set by parameter spin_time}void run_fd(float delay){motor(0,pow); // Motor0 forward with pow valuemotor(1,pow); // Motor1 forward with pow valuesleep(delay); // Delay set by parameter delay}void run_bk(float delay){motor(0,-pow); // Motor0 backward with pow valuemotor(1,-pow); // Motor1 backward with pow valuesleep(delay); // Delay set by parameter delay}

Page 120: 68HC11 Micro-controller Activity Board Universal Plate Motor ......68HC11 Micro-controller Activity Board It comes with a CX-4 cable to download the program. This board is used to

120

Testing It

Place the Robo-11 Compassor on the test field and then turn onthe POWER switch. The LCD screen displays the message PressStart. Press START on the AX-11. The user will hear a “beep”sound. The robot will then locate its position to see which angleor direction it is facing by reading the values from the CMPS03module. It will also display the results (Angle xxx: where xxx isthe angle value read). If the angle is not equal to the specifiedangle, the robot will rotate left in intervals of 0.05 seconds untilit finds the angled desired. The robot will then proceed to moveforward using only 40% of its power. The robot will continue tocheck the angle value as it moves to ensure that it is moving inthe right direction. The desired direction is at an angle of 0 to40 degrees.

The robot will stop when the STOP switch is pressed. The userwill hear a beep before it stops.

The Robo-11 Compassor is an interesting example of developingsmall automatic robots that can move on its own, with the helpof efficient modules such as the CMPS03, a widely used sensorin the RoboCup Junior competition, or the International FootballRobot Junior Championship. This is because the competitionfield usually does not have any reference points for the robot todetect its position, and thus the electronic compass module isvery helpful.

The values read from the CMPS03 module is the measuredwidth of the PWM signal, which is another example of signalprocessing that is different from all the other sensors.