Analysis of the servo program using 51 single-chip microcomputer

Publisher:温馨生活Latest update time:2018-06-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A servo, also known as a servo motor, is an electromechanical structure with a closed-loop control system. A servo is mainly composed of a housing, a circuit board, a coreless motor, gears, and a position detector. Its working principle is that the controller sends a PWM (pulse width modulation) signal to the servo, which is processed by the IC on the circuit board to calculate the direction of rotation, and then drives the coreless motor to rotate, and transmits the power to the swing arm through the reduction gear. At the same time, the position detector (potentiometer) returns a position signal to determine whether it has reached the set position. Generally, the servo can only rotate 180 degrees. The servo has 3 wires, brown for ground, red for positive power, and orange for signal line, but the colors of the wires of different brands of servos may be different, so you need to pay attention.

The rotation position of the servo is achieved by controlling the duty cycle of the PWM (pulse width modulation) signal. The period of the standard PWM (pulse width modulation) signal is fixed at 20ms. The positive pulse width of the duty cycle of 0.5~2.5ms corresponds to the rotation angle of the servo -90°~90° (i.e. 0~180 degrees). Note that due to different brands of servos, the pulse width parsed by their controllers is also different, so for the same signal, the rotation angles of servos of different brands are also different. The principle is: it has a reference circuit inside, which generates a reference signal with a period of 20ms and a width of 1.5ms. The control signal enters the signal modulation chip through the channel of the receiver to obtain a DC bias voltage. The DC bias voltage obtained is compared with the voltage of the potentiometer to obtain a voltage difference output. Finally, the positive and negative outputs of the voltage difference are output to the motor driver chip to determine the forward and reverse rotation of the motor. When the motor speed is constant, the potentiometer is driven to rotate through the cascade reduction gear, so that the voltage difference is 0 and the motor stops rotating.

   0.5ms------------0 degrees;

   1.0ms------------45 degrees;

   1.5ms------------90 degrees;

   2.0ms-----------135 degrees;

   2.5ms-----------180 degrees;

 

The procedure of using 11.0592MHZ 51 single chip microcomputer to drive the servo is as follows:

//Automatically rotate on power on  

  

#include  

  

unsigned char count; //0.5ms times mark  

  

sbit pwm =P1^0 ; //PWM signal output  

  

sbit jia =P2^4; // Angle increase button detection IO port  

  

sbit jian =P2^5; // Angle reduction key detection IO port  

  

unsigned char jd; //angle identifier  

  

   

  

sbit pwm1 =P0^0 ; //PWM signal output to oscilloscope, used to detect PWM wave  

  

void delay(unsigned char i)//delay  

  

{  

  

  unsigned char j,k;  

  

  for(j=i;j>0;j--)  

  

    for(k=125;k>0;k--);  

  

}  

  

void Time0_Init() //Timer 0 initialization  

  

{  

  

//After checking the initial value of timer 0 with an oscilloscope, the 11.0592MHZ crystal oscillator enters the interrupt with a timing of 0.5ms and the initial value is as follows  

  

TH0 = (65536-445)/256;  

  

TL0 = (65536-445)%256;  

  

   

TMOD = 0x01; //Timer 0 works in mode 1     

  

IE = 0x82; //IE=0x82=1000 0010 is equivalent to EA=1, open the general interrupt ET0=1, open the timer 0 interrupt  

  

  TR0=1; //Start timer 0  

  

}  

  

void Time0_Int() interrupt 1 //interrupt program  

  

{  

  

//Reinstall initial value  

  

TH0 = (65536-445)/256;  

  

TL0 = (65536-445)%256;  

  

   

  

    if(count< jd)  

  

{  

  

 pwm=1; //It is indeed less than, PWM output high level  

  

 pwm1=pwm; //Connect to the io port of the oscilloscope to observe the PWM waveform } //Judge whether the 0.5ms times are less than the angle mark  

  

 //pwm=1; //It is indeed less than, PWM output high level  

  

    else  

  

{  

  

 pwm=0; //It is indeed less than, PWM output high level  

  

 pwm1=pwm; //Connect to the io port of the oscilloscope to observe the PWM waveform}  

  

    // pwm=0; // if greater than 0, the output is low level  

  

    count=(count+1); //0.5ms times plus 1  

  

    count=count%40; //The number of times is always kept at 40, that is, the cycle is kept at 20ms  

  

}  

  

   

  

//This canceled part is the program that controls the rotation of the servo through the buttons. Its function is to control the forward rotation through the jia button.  

  

//Control the reversal by pressing the jian key  

  

/*void keyscan() //key scan  

  

{  

  

   if(jia==0) //Is the angle increase button pressed?  

  

  {  

  

    delay(10); //press delay to eliminate jitter  

  

    if(jia==0) //actually pressed  

  

     {  

  

      jd++; //angle mark plus 1  

  

      count=0; //When the button is pressed, the 20ms cycle starts again  

  

      if(jd==6)  

  

        jd=5; //It is already 180 degrees, keep  

  

      while(jia==0); //Wait for the button to be released  

  

     }  

  

  }  

  

  if(jian==0) //Is the angle reduction button pressed?  

  

  {  

  

    delay(10);  

  

    if(jian==0)  

  

     {  

  

      jd--; //angle mark minus 1  

  

      count=0;  

  

      if(jd==0)  

  

        jd=1; //It is already 0 degrees, keep  

  

      while(jan==0);  

  

     }  

  

  }  

  

}*/  

  

   

void main()  

  

{  

  

//Power on, the servo will automatically reverse      

  

//It should be noted that each step is the setting of the delay function delay parameter. The setting of the delay function parameter here is not ideal      

  

while(1)  

  

{  

  

  for(jd=1;jd<6;jd++)  

  

{  

  

count=0;  

  

Time0_Init();  

  

delay(50000000);  

  

}  

  

delay(50000000);  

  

for(jd=6;jd>0;jd--)  

  

{  

  

count=0;  

  

Time0_Init();  

  

delay(50000000);  

  

}  

  

}  

  

}  


Reference address:Analysis of the servo program using 51 single-chip microcomputer

Previous article:Detailed explanation of 51 single chip microcomputer PWM dual servo control
Next article:Detailed explanation of 51 single chip microcomputer PWM program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号