51 single-chip microcomputer button to control the rotation of the servo

51 single-chip microcomputer button to control the rotation of the servo

---------------------------------------------------------------------------------------------------------------------------------

1. The connection between the steering gear and the microcontroller

2. Simple understanding of PWM wave

3. Program example

4. Difficulties in developing programs

---------------------------------------------------------------------------------------------------------------------------------

1. The connection between the steering gear and the microcontroller


    As shown in the figure, the positive pole of the servo is connected to the microcontroller vcc, and the negative pole is connected to gnd. The connection of the signal can be customized.

---------------------------------------------------------------------------------------------------------------------------------

2. Simple understanding of PWM wave

    The 51 single-chip microcomputer controls the forward and reverse rotation of the steering gear through buttons, which needs to be realized by PWM waves. This article only explains how to use PWM waves to control the rotation of the steering gear.

    For a general steering gear, the corresponding PWM wave cycle is 200ms, that is, 200ms output from the signal port of the microcontroller is a cycle. Assume that the time when the signal port outputs '1' is x ms, and the remaining time outputs '0', and the ratio of x/200 is the value of the duty cycle. The microcontroller controls the rotation of the steering gear by controlling the value of the duty cycle.

    For common servos, the value of the duty cycle has the following relationship with the corresponding angle:

    0.5ms/200ms = 0 degrees

    1.0ms/200ms = 45 degrees

    1.5ms/200ms = 90 degrees

    2.0ms/200ms = 135 degrees

    2.5ms/200ms = 180 degrees

    In the 51 single-chip microcomputer, the timer is used to time the time, and the rotation of the steering gear is controlled by outputting '1' or '0' through the signal output port.

---------------------------------------------------------------------------------------------------------------------------------

3. Program example

#include<reg52.h>  


sbit key1 = P3^2;  
sbit key2 = P3^3;
sbit pwm1 = P2^0;  


unsigned int target =12,percent = 0;  
  
void Timer0Initial();  
void initial_Timer();  
void delay(unsigned int x);  
void keyscan();  


void keyscan()  
{
delay(30);
if(key1 == 0)  
  {  
delay(10);  
    if(key1 == 0)  
    {  
if(target >= 5)  
target -= 1;   
    }    
  }  
  
  if(key2 == 0)  
  {  
delay(10);  
    if(key2 == 0)  
{  
if(target <= 25)  
target += 1;   
    }  
  }  

  
void initial()  
{  
key1 = 1;  
  key2 = 1;  
  initial_Timer();  
}  


void Timer0Initial()  
{  
  TH0 = (65536-65)/256;  
  TL0 = (65536-65)%256;  

  
void initial_Timer()  
{  
  EA = 1; 
  ET0 = 1; 
  TMOD = 0x01;
  Timer0Initial();  
  TR0 = 1;
}  
  
void delay(unsigned int x)  
{  
  unsigned int i,j;  
  for(i=x;i>0;i--)  
    for(j=110;j>0;j--); 



void main()  
{  
  initial();  
  while(1)  
  {  
    keyscan();  
  }  
}


void Timer0() interrupt 1 using 0  

percent+=1;
if(percent<=target)
pwm1=1;
else
pwm1=0;
if(percent==200)
percent=0;
  Timer0Initial();  

}

    Notes:

    P3^3 and P3^2 are independent buttons, and P2^0 is the output signal port.

---------------------------------------------------------------------------------------------------------------------------------

4. Difficulties in developing programs

    In the actual operation, there are often two situations that cause the program to fail to run normally:

    1> The program running speed does not match the speed of the steering gear

        In actual operation, since the rotation of the steering gear and the increase of the target in the program are not synchronized, this will lead to the release of the button, since the running speed of the program is much greater than the rotation speed of the steering gear, the value of the target has reached 25, but The servo goes straight to the 90-degree position, and will continue to rotate after releasing the button.

        The solution is to add a delay statement delay() in the keyscan function to make the program run slower, and make the rotation angle of the servo synchronize with the value of the target through multiple attempts.

    2> The interval from 0.5ms to 2.5ms does not make the servo turn from 0 degrees to 180 degrees

        Different steering gears may cause errors due to differences in production accuracy. The specific angle can be determined by adjusting the duty cycle of the PWM wave.

        For example, the actual PWM wave ms of the servo I use from 0 to 180 degrees is 6ms~27ms, and the actual PWM wave of my roommate's servo from 0 to 180 degrees is 4~18ms.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325450581&siteId=291194637