Servo Control using Atmel 89S52                 Submitted by               Mayank Awasthi                B.Tech(ECE)      ...
ACKNOWLEDGEMENTFirst of all, we would like to express our sincere thanks to our guide Mr. SusmitSen for his intellectual g...
CONTENTS I.     Servo Motors ………………................... II.    Atmel 89S52 Microcontrollers ……….. III.   Keil code ……………………...
SERVO MOTORS      A Servo is a small device that incorporates a three wire DC motor, a      gear train, a potentiometer, a...
Servos are constructed from three basic pieces; a motor, a potentiometer(variable resister) that is connected to the outpu...
Servos are controlled by sending them a pulse of variable width. The controlwire is used to send this pulse. The parameter...
Another parameter that varies from servo to servo is the turn rate. This is the   time it takes from the servo to change f...
ATMEL 89S52 MICROCONTROLLERThe AT89S52 is a low-power, high-performance CMOS 8-bit microcontroller with8K bytes of in-syst...
Clock and Instruction Cycle :In 8051, one instruction cycle consists of twelve (12) clock cycles. Instructioncycle is some...
Timer Mode control (TMOD) Special Function Register:TMOD register is not bit addressable.TMODAddress: 89 HVarious bits of ...
Timer/ Counter control logic:                        Fig 8.1 Timer/Counter Control LogicTimer control (TCON) Special funct...
Timers can operate in four different modes as follows:Timer Mode-0:In this mode, the timer is used as a 13-bit UP counter ...
Fig 8.5 Operation of Timer in Mode 2Timer Mode-3:Timer 1 in mode-3 simply holds its count. The effect is same as setting T...
external interrupt flags are cleared on branching to Interrupt Service Routine(ISR), provided the interrupt is negative ed...
Interrupt Priority register (IP)  0                low priority  1              high priority                             ...
KEIL CODE*********************************************************************For 11.0592 MHz crystal 1 machine cycleFor m...
TL0=0xCB;}servo=0;TR0=0;}void main(void){TMOD=0x11;ET1=1;ET0=1;TH1=0xB7;TL1=0xFE;TR1=1;EA=1;while(1){}}                  x...
REFERENCEShttp://www.robokits.comhttp://www.thinkindialab.comhttp://www.asel.udel.edu/roboticshttp://www.asel.udel.edu/rob...
Upcoming SlideShare
Loading in …5
×

Pdpm,mayank awasthi,jabalpur,i it kanpur, servo motor,keil code

1,421 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,421
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Pdpm,mayank awasthi,jabalpur,i it kanpur, servo motor,keil code

  1. 1. Servo Control using Atmel 89S52 Submitted by Mayank Awasthi B.Tech(ECE) PDPM IIITDM, Jabalpur M. Tech, IIT Roorkee (Communication Systems) Contact no. 9410321979 Under the supervision of Mr. Susmit Sen Senior Research Engineer Centre for Robotics IIT Kanpur i
  2. 2. ACKNOWLEDGEMENTFirst of all, we would like to express our sincere thanks to our guide Mr. SusmitSen for his intellectual guidance, continuous interest, generous support, infinitepatience, and constant encouragement throughout this work. He has devoted hisvaluable time to discuss this project, his expertise and broad knowledge inMechatronics & Robotics played a major role in the realization of this work. Weappreciate Mr. Susmit Sen for his confidence boosting start up to our projectwork and encouragement in creative endeavors.We especially appreciate the company of our classmates, seniors, workshop &lab assistants and who have made useful to this work by way of discussions andsuggestions from time to time. Group Member Mayank Awasthi B.Tech(ECE) PDPM IIITDM, Jabalpur Contact no. 9336441681 ii
  3. 3. CONTENTS I. Servo Motors ………………................... II. Atmel 89S52 Microcontrollers ……….. III. Keil code ……………………………….. IV. References …………………………… iii
  4. 4. SERVO MOTORS A Servo is a small device that incorporates a three wire DC motor, a gear train, a potentiometer, an integrated circuit, and an output shaftbearing. Of the three wires that stick out from the motor casing, one is forpower, one is for ground, and one is a control input line. The shaft of theservo can be positioned to specific angular positions by sending a codedsignal. As long as the coded signal exists on the input line, the servo willmaintain the angular position of the shaft. If the coded signal changes, thenthe angular position of the shaft changes. A very common use of servos is in Radio Controlled models like cars,airplanes, robots, and puppets. They are also used in powerful heavy-dutysail boats. Servos are rated for Speed and Torque. Normally there are twoservos of the same kind, one geared towards speed (sacrificing torque), andthe other towards torque (sacrificing speed). A good example of this is theHS-625MG servo and the HS-645MG servo. Servos come in different sizes but use similar control schemes and areextremely useful in robotics. The motors are small and are extremely powerfulfor their size. It also draws power proportional to the mechanical load. Alightly loaded servo, therefore, doesn’t consume much energy.A typical Servo looks like a rectangular box with a motor shaft coming out ofone end and a connector with three wires out of the other end. The threewires are the power, Control, and Ground. Servos work with voltagesbetween 4 and 6 volts. The control line is used to position the servo. Theservo motor comes in different sizes, which affect the overall size of theservo. The gears of a servo vary from servo to servo. Inexpensive servoshave plastic gears, and more expensive servos have metal gears which aremuch more rugged but wear faster. The potentiometer of a servo is thefeedback device. The electronics of a servo are pretty much the same in allservos, but the output shaft bearing of a servo has either a plastic on plasticbearing that will not take much side load or a metal on metal bearings thatstand up better under extended use, or ball bearings which work best. Wehighly recommend ball bearing servos if your application demands heavy sideloads. iv
  5. 5. Servos are constructed from three basic pieces; a motor, a potentiometer(variable resister) that is connected to the output shaft, and a control board.The potentiometer allows the control circuitry to monitor the current angle ofthe servo motor. The motor, through a series of gears, turns the output shaftand the potentiometer simultaneously. The potentiometer is fed into the servocontrol circuit and when the control circuit detects that the position is correct,it stops the motor. If the control circuit detects that the angle is not correct, itwill turn the motor the correct direction until the angle is correct. Normally aservo is used to control an angular motion of between 0 and 180 degrees. It isnot mechanically capable (unless modified) of turning any farther due to themechanical stop build on to the main output gear.The amount of power applied to the motor is proportional to the distance itneeds to travel. So, if the shaft needs to turn a large distance, the motor willrun at full speed. If it needs to turn only a small amount, the motor will run at aslower speed. This is called proportional control.How Do Servos Work ? v
  6. 6. Servos are controlled by sending them a pulse of variable width. The controlwire is used to send this pulse. The parameters for this pulse are that it has aminimum pulse, a maximum pulse, and a repetition rate. Given the rotationconstraints of the servo, neutral is defined to be the position where the servohas exactly the same amount of potential rotation in the clockwise direction asit does in the counter clockwise direction. It is important to note that differentservos will have different constraints on their rotation but they all have aneutral position, and that position is always around 1.5 milliseconds (ms).The angle is determined by the duration of a pulse that is applied to thecontrol wire. This is called Pulse width Modulation. The servo expects to seea pulse every 20 ms. The length of the pulse will determine how far the motorturns. For example, a 1.5 ms pulse will make the motor turn to the 90 degreeposition(neutral position). When these servos are commanded to move they will move to the positionand hold that position. If an external force pushes against the servo while theservo is holding a position, the servo will resist from moving out of thatposition. The maximum amount of force the servo can exert is the torquerating of the servo. Servos will not hold their position forever though; theposition pulse must be repeated to instruct the servo to stay in position. When a pulse is sent to a servo that is less than 1.5 ms the servo rotatesto a position and holds its output shaft some number of degreescounterclockwise from the neutral point. When the pulse is wider than 1.5 msthe opposite occurs. The minimal width and the maximum width of pulse thatwill command the servo to turn to a valid position are functions of each servo.Different brands, and even different servos of the same brand, will havedifferent maximum and minimums. Generally the minimum pulse will be about1 ms wide and the maximum pulse will be 2 ms wide. vi
  7. 7. Another parameter that varies from servo to servo is the turn rate. This is the time it takes from the servo to change from one position to another. The worst case turning time is when the servo is holding at the minimum rotation and it is commanded to go to maximum rotation. This can take several seconds on very high torque servos.HS-322HD Servo Motors Specifications:This servo comes with mounting hardware, mounting grommets, and 4 servohorns. The HS-322HD servo has heavy duty gears for smoother operation andlonger life when compared to normal servos. This servo has a Hitech/JRconnector which mates directly with a 0.1" 3-pin header. The servo spline has 24teeth and mates with Hitec compatible accessories. Specifications Voltage Operating Speed Output Torque Weight Range 0.19sec/60 degrees at 3kg.cm (41.6oz.in) at 43.0g4.8V - 6V 4.8V 4.8V (1.51oz)Wire Color MeaningOn all Hitec servos the Black wire is ground, the Red wire (center wire) ispower, and the yellow (third) wire is signal. vii
  8. 8. ATMEL 89S52 MICROCONTROLLERThe AT89S52 is a low-power, high-performance CMOS 8-bit microcontroller with8K bytes of in-system programmable Flash memory. The on-chip Flash allowsthe program memory to be reprogrammed in-system or by a conventionalnonvolatile memory programmer.By combining a versatile 8-bit CPU with in-system programmable Flash ona monolithic chip, the Atmel AT89S52 is a powerful microcontroller whichprovides a highly-flexible and cost-effective solution to many embedded controlapplications.The AT89S52 provides the following standard features: 8K bytes of Flash, 256bytes of RAM, 32 I/O lines, Watchdog timer, two data pointers, three 16-bittimer/counters, a six-vector two-level interrupt architecture, a full duplex serialport, on-chip oscillator, and clock circuitry.PIN Configuration Pin configuration of ATMEL 89S52 viii
  9. 9. Clock and Instruction Cycle :In 8051, one instruction cycle consists of twelve (12) clock cycles. Instructioncycle is sometimes called as Machine cycle by some authors. Fig 5.2 : Instruction cycle of 8051In 8051, each instruction cycle has six states (S 1 - S 6 ). Each state has twopulses (P1 and P2)I/O Port ConfigurationEach port of 8051 has bidirectional capability. Port 0 is called truebidirectional port as it floats (tristated) when configured as input.Port-1, 2, 3 are called quasi bidirectional port.Timers / Counters8051 has two 16-bit programmable UP timers/counters. They can be configuredto operate either as timers or as event counters. The names of the two countersare T0 and T1 respectively. The timer content is available in four 8-bit specialfunction registers, viz, TL0,TH0,TL1 and TH1 respectively.In the "timer" function mode, the counter is incremented in every machine cycle.Thus, one can think of it as counting machine cycles. Hence the clock rate is1/12 th of the oscillator frequency.In the "counter" function mode, the register is incremented in response to a 1 to 0transition at its corresponding external input pin (T0 or T1). It requires 2 machinecycles to detect a high to low transition. Hence maximum count rate is 1/24 th ofoscillator frequency.The operation of the timers/counters is controlled by two special functionregisters, TMOD and TCON respectively. ix
  10. 10. Timer Mode control (TMOD) Special Function Register:TMOD register is not bit addressable.TMODAddress: 89 HVarious bits of TMOD are described as follows -Gate: This is an OR Gate enabled bit which controls the effect of onSTART/STOP of Timer. It is set to one (1) by the program to enable the interruptto start/stop the timer. If TR1/0 in TCON is set and signal on pin is high thenthe timer starts counting using either internal clock (timer mode) or externalpulses (counter mode). It is used for the selection of Counter/Timer mode.Mode Select Bits:M1 and M0 are mode select bits. x
  11. 11. Timer/ Counter control logic: Fig 8.1 Timer/Counter Control LogicTimer control (TCON) Special function register:TCON is bit addressable. The address of TCON is 88H. It is partly related toTimer and partly to interrupt. Fig 8.2 TCON RegisterThe various bits of TCON are as follows.TF1: Timer1 overflow flag. It is set when timer rolls from all 1s to 0s. It is clearedwhen processor vectors to execute ISR located at address 001BH.TR1: Timer1 run control bit. Set to 1 to start the timer / counter.TF0: Timer0 overflow flag. (Similar to TF1)TR0: Timer0 run control bit.IE1: Interrupt1 edge flag. Set by hardware when an external interrupt edge isdetected. It is cleared when interrupt is processed.IE0: Interrupt0 edge flag. (Similar to IE1)IT1: Interrupt1 type control bit. Set/ cleared by software to specify falling edge /low level triggered external interrupt.IT0: Interrupt0 type control bit. (Similar to IT1) xi
  12. 12. Timers can operate in four different modes as follows:Timer Mode-0:In this mode, the timer is used as a 13-bit UP counter as follows. Fig. 8.3 Operation of Timer on Mode-0The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit count.Upper 3bits of TLX are ignored. When the counter rolls over from all 0s to all 1s, TFXflag is set and an interrupt is generated.The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bitis 0, the counter continues counting up. If TR1/0 bit is 1 and Gate bit is 1, thenthe operation of the counter is controlled by input. This mode is useful tomeasure the width of a given pulse fed to input.Timer Mode-1:This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit mode. Fig 8.4 Operation of Timer in Mode 1Timer Mode-2: (Auto-Reload Mode)This is a 8 bit counter/timer operation. Counting is performed in TLX while THXstores a constant value. In this mode when the timer overflows i.e. TLX becomesFFH, it is fed with the value stored in THX. For example if we load THX with 50Hthen the timer in mode 2 will count from 50H to FFH. After that 50H is againreloaded. This mode is useful in applications like fixed time sampling. xii
  13. 13. Fig 8.5 Operation of Timer in Mode 2Timer Mode-3:Timer 1 in mode-3 simply holds its count. The effect is same as setting TR1=0.Timer0 in mode-3 establishes TL0 and TH0 as two separate counters. Fig 8.6 Operation of Timer in Mode 3Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3while TR0 and TF0 are available to Timer-0 lower 8 bits(TL0).Interrupts:8051 provides 5 vectored interrupts. They are - 1. 2. TF0 3. 4. TF1 5. RI/TIOut of these, and are external interrupts whereas Timer and Serial portinterrupts are generated internally. The external interrupts could be negativeedge triggered or low level triggered. All these interrupt, when activated, set thecorresponding interrupt flags. Except for serial interrupt, the interrupt flags arecleared when the processor branches to the Interrupt Service Routine (ISR). The xiii
  14. 14. external interrupt flags are cleared on branching to Interrupt Service Routine(ISR), provided the interrupt is negative edge triggered. For low level triggeredexternal interrupt as well as for serial interrupt, the corresponding flags have tobe cleared by software by the programmer.Each of these interrupts can be individually enabled or disabled by setting orclearing the corresponding bit in the IE (Interrupt Enable Register) SFR. IEcontains a global enable bit EA which enables/disables all interrupts at once.Interrupt Enable register (IE): Address: A8HEX0 interrupt (External) enable bitET0 Timer-0 interrupt enable bitEX1 interrupt (External) enable bitET1 Timer-1 interrupt enable bitES Serial port interrupt enable bitET2 Timer-2 interrupt enable bitEA Enable/Disable allSetting 1 Enable the corresponding interruptSetting 0 Disable the corresponding interruptPriority level structure:Each interrupt source can be programmed to have one of the two priority levelsby setting (high priority) or clearing (low priority) a bit in the IP (Interrupt Priority)Register. A low priority interrupt can itself be interrupted by a high priorityinterrupt, but not by another low priority interrupt. If two interrupts of differentpriority levels are received simultaneously, the request of higher priority level isserved. If the requests of the same priority level are received simultaneously, aninternal polling sequence determines which request is to be serviced. Thus,within each priority level, there is a second priority level determined by the pollingsequence, as follows. xiv
  15. 15. Interrupt Priority register (IP) 0 low priority 1 high priority xv
  16. 16. KEIL CODE*********************************************************************For 11.0592 MHz crystal 1 machine cycleFor microcontroller is 1.085usecthus for 20 msec we require count of 18433thus count for timer65535-18433 = 47102 = B7FE hfor 1ms = FC65 h …. 0 degreefor 2ms = F8CB h …. 180 degree *///Here Servo is connected to P0.6//Switches are connected at P1.5 and P1.6#include<atmel89x52.h>sbit servo=P0^6;void timer1_ovf(void) interrupt 3 // timer 1 for 20ms{TH1=0xB7;TL1=0xFE;servo=1;TR0=1;}void timer0_ovf(void) interrupt 1 // timer 0 for various shaft position{if(P1^5==1){TH0=0xFC; //0 degree shaft positionTL0=0x65;}else if(P1^6==1){TH0=0xFA; // 90 degree shaft positionTL0=0x99;}else if(P1^5==1&&P1^6==1){TH0=0xF8; //180 degree shaft position xvi
  17. 17. TL0=0xCB;}servo=0;TR0=0;}void main(void){TMOD=0x11;ET1=1;ET0=1;TH1=0xB7;TL1=0xFE;TR1=1;EA=1;while(1){}} xvii
  18. 18. REFERENCEShttp://www.robokits.comhttp://www.thinkindialab.comhttp://www.asel.udel.edu/roboticshttp://www.asel.udel.edu/roboticshttp://www.google.comhttp://www.asel.udel.edu/roboticshttp://www.asel.udel.edu/resna-sig13http://www.esnips.com xviii

×