The basic operations of servo motor control have been discussed in interfacing servo with 8051. This project allows the servo motor to move to an angle specified by the user. The pulse train required to rotate the servo is produced by AT89C51 microcontroller. The desired angle of rotation is provided through a 4x3 keypad interfaced to the microcontroller. A 16x2 LCD is also connected with the microcontroller to display the angle of rotation entered by the user.
For basic operations and control of servo motor, refer interfacing servo with 8051. The first pin of port P1 (P1^0) of AT89C51 microcontroller is set as the output pin to provide control signal to the servo motor. Ports P0 and P2 are used to interface keypad and data pins of LCD, respectively. Ports P1^3, P1^4 and P1^5 are connected to RS, RW and EN pins of LCD, respectively. (For more details, refer LCD interfacing with 8051)
Fig. 2: Block Diagram of Servo Output Of Microcontroller
Before connecting to the control wire of servo, the output from the microcontroller (P1^0) is fed through a comparator IC (LM324) so that the signal is protected from any loss due to overloading.
When the circuit is powered, the LCD prompts the user to enter the angle of rotation. The user can enter the angle in degrees through keypad. The angle entered by the user is also displayed on the LCD screen. As the user presses ‘*’ button of keypad, the angle of rotation is accepted by the microcontroller and the servo moves to the user defined angular position. If an angle greater than 180° is entered, the difference of the input with 180° is considered as the angle of rotation.
The program uses one of the 8051 timers to generate a 50 µs delay. A timer function is written which produces delay in multiples of 50 µs. A variable is taken as the argument to this function that determines the multiples. Initially when the microcontroller executes program, a pulse with ON time of 700 µs (14 x 50 µs) followed by OFF time of 18 ms is generated. This shifts the servo horn to 0° angle.
The angle entered by the user is stored in a variable whose value is added to the argument of the timer function. For example, if the user enters 90, the argument of the timer function is increased by 90 and becomes 104 (90 + 14). The ON time for the next train of pulses then corresponds to multiple of 50 µs. In this case the ON time of the next train of pulses will be 5200 µs (104 x 50 usec) followed by OFF time of 18 ms. This maintains the servo at 90° angle until the user enters the next angle.
Project Source Code
###
// Program to rotate servo to user defined angular position // 0 degree = 700us // 180 degree = 5500us // Timer1 pulse after 50us -23 #include<reg51.h> #define dataport P2 // Data port for LCD #define key P0 // Port for Keypad #define port P1 sbit output = port^0; sbit rs = port^3; sbit rw = port^4; sbit en = port^5; sbit col1 = key^4; sbit col2 = key^5; sbit col3 = key^6; sbit row1 = key^0; sbit row2 = key^1; sbit row3 = key^2; sbit row4 = key^3; int count=0,time=0,check, digit[3]; void delay(unsigned int msec) // Function for delay { int i,j; for(i=0;i<msec;i++) for(j=0;j<1275;j++); } void lcd_cmd(unsigned char item) // Function to send command to LCD { dataport = item; rs= 0; rw=0; en=1; delay(1); en=0; return; } void lcd_data(unsigned char item) // Function to send one byte data to LCD { dataport = item; rs= 1; rw=0; en=1; delay(1); en=0; return; } void lcd_data_string(unsigned char *str) // Function to send string to LCD { int i=0; while(str[i]!='