1. Program to blink an LED
Since this is our first program, we will try to keep things simple. The following program will blink an LED on the board at a particular rate. A generic algorithm for such a program will be something like this: Step 1: Turn ON the LED Step 2: Wait for sometime Step 3: Turn OFF the LED Step 4: Wait for sometime Step 5: Goto Step1 To turn ON an LED on the iBOT board, we need to send logic 0 to that particular pin (since LEDs are connected in the active low configuration). In order to introduce a delay, we need to eat up some processor time. The simplest way to achieve this is to perform a NOP (No Operation) multiple times. And continuous looping can be achieved by either using the statement “goto” or more appropriately by using the while() loop. Now let’s see how we can put all of these things together in a C program.
// Program to blink an LED // #include<REG52MOD.h> //we include the necessary header file here void delay(unsigned int delay) //This a simple delay function using the //nested ‘for loop’ { unsigned int i,j; for(i=0;i<=1000;i++) for(j=0;j<=delay;j++); } void main(void) //main program begins here { while (1) //since there is no where to return //we put it in an infinite loop { RXD=0; //LED 1 is on pin RXD at PORT 3_1, we //turn it ON delay (20); //wait for a short time RXD=1; //turn the LED 1 OFF delay(20); //wait for a short time } }

We begin with including the header file for the microcontroller. Next we write the delay() function using the nested for loop. Don’t worry about the exact time of delay over here, you can experiment with different values and see what difference it makes. For example, replacing delay(20) with delay(60) will reduce the frequency while delay(10) would increase the frequency. You could also try and use all the LEDs and generate different patterns. Note: The LEDs are connected in an active-low configuration. Therefore, to turn ON an LED, we

TAG ROBOTICS www.tagonnet.com

//if pressed. let’s see how we can take inputs from a particular pin and execute actions based on its state. 4 and 5 as inputs since we have connected our switches to them while pin 0. turn ON LED 1. //if pressed. //if pressed. if pressed. Step 2: Check if Switch 2 is pressed. . turn ON LED 2.h> //we include the necessary header file here void main(void) //main program begins here { P3=0x3C. else. The algorithm would go something like this: Step 1: Check if Switch 1 is pressed. else. turn ON LED 4 } else P3_7=1. 2. turn ON LED 1 } else P3_0=1. . 3. 6 and 7 are the outputs where LEDs are connected. //else turn it OFF } } You will notice that we have written P3=0x3C at the very beginning of our program. turn it OFF. The simplest way to know the state (is it high or low?) of a particular pin is to poll it continuously. as seen in the previous example. Program using Switches and LEDs Now that we know how to turn on/off a particular pin of the microcontroller. 1. if pressed. //we put it in an infinite loop { if (P3_2==0) //check if Switch 1 is pressed { P3_0=0. //if pressed. This is done to set PORT 3’s pin 2. //else turn it OFF if (P3_5==0) //check if Switch 4 is pressed { P3_7=0. we will be using switches as our inputs and LEDs as the outputs. In this example. (1 Input.tagonnet.need to send logic 0 and not logic 1. turn it OFF. //else turn it OFF if (P3_3==0) //check if Switch 2 is pressed { P3_1=0. //else turn it OFF if (P3_4==0) //check if Switch 3 is pressed { P3_6=0.com . . Lets see how a C program based on this algorithm would look like: // Program using Switches and LEDs // #include<REG52MOD. Step n: goto step 1. turn ON LED 3 } else P3_6=1. turn ON LED 2 } else P3_1=1. 0 Output) TAG ROBOTICS www. //initialize PORT 3 to 00111100 = 0x3C while (1) //since there is no where to return.

check various states of the robot. //else turn it in other direction (0000 0010) } } Similarly. 4. LCDs are not as easy to use as the LEDs but they are not very difficult either. see the parameters as you feed them and also as a handy debugging tool. whenever a switch is pressed. else. turn motor in one direction.com . we will receive logic 0 on that particular pin and not logic 1. turn motor in one //direction (0000 0001) } else P2=0x02. Program using a Sensor and a DC Motor Let’s try and make our next program more animated. Step 3: goto step 2 One IR proximity sensor is connected to PORT 1’s pin 0 and one motor is connected to M1 (pin 0 and 1 of PORT 2) // Program using Sensor and DC Motor // #include<REG52MOD.h header files in the C:\Keil\C51\INC folder before you use it. //initialize PORT 2 as output while (1) //since there is no where to return. And the ‘cool’ factor cannot be understated aswell. if active. //we put it in an infinite loop { if (P1_0==0) //check if sensor at PORT1. In this example. 3. Therefore. turn it in other direction. Program using the LCD module LCD is one of the most multipurpose module a robot can have.h> //we include the necessary header file here void main(void) //main program begins here { P1=0xff. TAG ROBOTICS www. It can be used to display information. we have written a small library for all the LCD functions. To make things simpler for you.tagonnet. iBOT uses a 2x16 backlight display that is compatible with the universally accepted Hitachi format.h and delay.Note: The switches are connected in an active-low configuration. Here’s the algorithm: Step 1: Initialize the ports Step 2: Check the state of the sensor. we will control the direction of the motor based on the state of a sensor. //initialize PORT 1 as input P2=0x00. All you need to do is to add the LCD. 1 inactive P2=0x01.0 is active { //0 active. //if active. you can write a code to turn both the motors and use both sensors together.

first refer to the datasheet of 89V51RD2 microcontroller and have a look at the UART section and the SCON (serial port control) register configurations. In order to do this.com . Note: LCD row 1 address starts at 0x00 and row 2 starts at 0x40 but with the MSB set. //Initialize the LCD module LCD_STRING("TRI"). Program to communicate with the PC via UART In this section we will learn how to make your iBOT communicate with the PC through the UART channel. where xxx xxxx is the address of character in binary.h> #include<delay. • LCD_WRITE(‘T’) Used to write any character T to the LCD. In the following program.Let’s look at the LCD functions that we have at our disposal: • LCD_INIT() Initializes the LCD in 2 line 4 bit mode with blinking cursor. Eg: To put cursor on 3 character line 1 LCD_CMD(0x83).Places cursor on line2 To place the cursor on a particular character box it is addressed as follows: Command = LCD_CMD(0b1XXX XXXX). • LCD_STRING(“Xplore Robotics”) Used to write a string on the LCD (max 16 characters in length) • LCD_CMD(X) To give commands to the LCD.h> //Include the necessary header files #include<delay. the microcontroller will send a “start” string to the PC at the beginning of the execution and simultaneously accept data coming from the PC (anything that you type on the keyboard) and display in on to the onboard LCD module. To put cursor on 5 character line 2 LCD_CMD(0xC5). //Move the cursor to second line LCD_STRING("HELLO WORLD !!"). //Send a string to display while(1){} //Loop Infinitely } 5. //Program using to communicate with the PC via UART// #include<REG52MOD. the addresses are 0x80 and 0xC0 respectively. X can be as follows PUTLINE1 – Places cursor on line1 PUTLINE2 . Here’s a sample program for using the LCD module with the help of the above functions: // Program using the LCD module// #include<REG52MOD.h> TAG ROBOTICS www.h> #include<LCD.h> void main (void) { LCD_INIT().tagonnet. • LCD_CLEAR() Clears LCD screen. //Send a string to display LCD_CMD(PUTLINE2).

//Set baud TH1=253. //Global interrupt enable } void TX(unsigned char dat) //Transmit Data { SBUF=dat. delay(10).tagonnet. //Transmit TX('a'). //Initialize LCD UART_INIT(). void UART_INIT(void) //UART Initialization { SBUF=0x00. Line Following Robot Time to actually build a robot! Let’s put everything that we learned till now in our next program. LCD_WRITE(RXED). There are many algorithms and sensor TAG ROBOTICS www.h> unsigned char RXED. //Initialize UART delay(1). //rate as 9600bps TR1=1. //Enable serial interrupts EA=1. //Reset receive data bit RXED=SBUF. //Receive Enable TMOD=0x2F. ES=1. //in mode 1 SM2=0.com . //Transmit TX('r'). You can find it in Start > Programs > Accessories > Communications > Hyper Terminal Step 1: Enter a name for the connection Step 2: Select the COM port and configure its properties 6. //Empty Serial Buffer SM0=0. //Reset transmit data bit } } void main(void) //Main program begins here { LCD_INIT().#include<LCD. //Display received character on LCD } if(TI==1) { TI=0. } void RXTX_int(void) interrupt 4 //Serial interrupt ISR { if(RI==1) { RI=0.TXED. //Transmit while(1){} } S t a r t After loading the program on to your iBOT controller. //Transmit TX('t'). REN=1. //Transmit TX('t'). //wait for a while TX('S'). //Set UART SM1=1. open the Hyper Terminal on your PC. Line following is one of the simplest task a robot can perform.

YES YES YES NO YES NO NO NO START Initialize Ports Is left line sensor active? Is right line sensor active? Turn right Turn Left Is left line sensor active? Go straight Is right line sensor active? Basically. //initialize PORT 2 as output (motors) while (1) //since there is no where to return. The following code is written to follow a ‘black’ line on a ‘white’ surface but it can be easily modified to follow a ‘white’ line on a black surface. TAG ROBOTICS www. // 0000 00 01 #define left 0x04. // LINE FOLLOWING ROBOT // //Right line sensor connected to PORT1. thus moving in a ‘zigzag’ path and eventually follow the line.tagonnet.1 //Right motor connected at M2 //Left motor connected at M1 #include<REG52MOD. //initialize PORT 1 as input (sensors) P2=0x00. // 0000 01 00 #define linesensor_right P1_0 #define linesensor_left P1_1 void main(void) //main program begins here { P1=0xff.0 //Left line sensor connected to PORT1. Here’s one of the simplest and fail safe line follower’s algorithm: In this configuration we use two line sensors.h> //we include the necessary header file here #define forward 0x05. // 0000 01 01 #define right 0x01.configurations designed to do this. the robot will always try to get the sensors on the line alternately.com .

h> #define forward 0x05. Obstacle Avoiding robot Obstacle avoiding behavior is a prerequisite in any mobile robotic application. // 0000 10 01 left motor = forward.3 //Right motor connected at M2 //Left motor connected at M1 #include<REG52MOD. turn left for a while Step 2: check if left ir proximity sensor is active. // 0000 01 10 left motor = backwards. } } if (linesensor_left==0) // check if left sensor has detected // a line { while(linesensor_right==1) //if detected.h> //we include the necessary header file here #include<delay. //else go forward } } } 7. // 0000 01 01 #define turnleft 0x06. //initialize PORT 2 as output (motors) TAG ROBOTICS www. Here’s a simple algorithm: Step 1: check if right ir proximity sensor is active. In this project we will learn to add such a behavior in our iBOT. turn right for a while Step 3: else move forward Step 4: goto step 1 // OBSTACLE AVOIDING ROBOT // //Right proximity sensor connected to PORT1. then turn left till 11 { //the right sensor comes on the line P2=left. // right motor = forward #define turnright 0x09.2 //Left proximity sensor connected to PORT1.//we put it in an infinite loop { if (linesensor_right==0) //check if right sensor has detected // a line (black surface) { while(linesensor_left==1) //if detected. // right motor = backwards #define obst_right P1_2 #define obst_left P1_3 void main(void) //main program begins here { P1=0xff. } } else { P2=forward. //initialize PORT 1 as input (sensors) P2=0x00. then turn right till { //the left sensor comes on the line P2=right. if active.tagonnet.com . if active.

} if (obst_left==0) //check if left sensor has detected an obstacle { P2=turnright. Now let’s see how we can have a behavior that uses both of these sensors together. turn left for some time delay(30). } else { P2=forward. In a typical sumo robotics competition. //if detected. A sumo robot fits this bill perfectly. //we put it in an infinite loop { if (obst_right==0) //check if right sensor has detected an obstacle { P2=turnleft. two robots compete against each other inside a sumo ring..tagonnet. Note: In this particular case. But the turns will not be precise since they will change as the battery drains.com . //else go forward } } } By changing the delay variable we can make the robot turn for some specific degrees after it detects an obstacle. Sumo Robot In the previous two examples we learned how to use the line sensing modules as well as the IR proximity modules. where the turns were turning by ‘stopping’ either of the motor. while turning. turn right for some time delay(30). The ring is a made up of a black surface with a white line around its circumference as shown. 8. The idea is to push the opponent out of the ring in the stipulated time frame. //if detected. the turning would be ‘in place’ i.while (1) //since there is no where to return. Inspired from the tradition ‘human’ sumo competitions held in Japan. robotics enthusiasts soon started having robotics sumo. Sumo robotics competitions are now held around the world under various weight and size ‘classes’ just like the real ones. A typical sumo robot flowchart: NO Start Initialize ports Is the opponent TAG ROBOTICS www.e. both the motors will run in opposite direction unlike the line follower.

com . //initialize PORT 2 as output (motors) while (1) //since there is no where to return. //initialize PORT 1 as input (sensors) P2=0x00.h> #define forward 0x05 // 0000 01 01 #define reverse 0x0A // 0000 10 10 #define turnleft 0x06 // 0000 01 10 left motor = backwards.h> //we include the necessary header file here #include<delay. TAG ROBOTICS www.3 //Right motor connected at M2 //Left motor connected at M1 #include<REG52MOD.2 //Left proximity sensor connected to PORT1. // right motor = forward #define turnright 0x09 // 0000 10 01 left motor = forward.on the left and me not on the edge? Is the right line sensor active? Is the opponent on the right and me on the edge? Is the opponent in front and me not on the edge? Is the right line sensor active? Turn left Turn right Go forward Turn backwards then turn left Turn backwards then turn right Go forward NO NO NO NO YES YES YES YES 16 // SUMO ROBOT // //Right line sensor connected to PORT1.tagonnet.0 //Left line sensor connected to PORT1.1 //Right proximity sensor connected to PORT1. // right motor = backwards #define linesensor_right P1_0 #define linesensor_left P1_1 #define obst_right P1_2 #define obst_left P1_3 void main(void) //main program begins here { P1=0xff.

//if detected. //if detected. P2=turnright.e. //if detected. go reverse for a while delay(40).. //then turn right for a while delay(30). //else go forward } } } TAG ROBOTICS www. //and go forward } else { P2=forward. //if detected..com .tagonnet. //if detected. go forward } //chase him and push him 17 if (linesensor_right==0 ) //check if right line sensor has // detected the ring { P2=reverse. P2=forward. towards the opponent) while ( obst_left==0 //check if left sensor has && obst_right==1 //detected the opponent && linesensor_right==1 //and make sure that the robot is && linesensor_left==1) //not on the edge { P2=turnleft. turn left } //(i. //then turn left for a while delay(30). go reverse for a while delay(40).e. //and go forward } if (linesensor_left==0 ) //check if left line sensor has //detected the ring { P2=reverse. P2=forward. P2=turnleft.//we put it in an infinite loop { while ( obst_right==0 //check if right sensor has && obst_left==1 //detected the opponent && linesensor_right==1 //and make sure that the robot is && linesensor_left==1) //not on the edge { P2=turnright. towards the opponent) while ( obst_left==0 //check if both sensors && obst_right==0 //have detected the opponent && linesensor_right==1 && linesensor_left==1) { P2=forward. turn right } //(i.