Teach you how to control mg995 servo with 51 MCU

Publisher:科技狂人Latest update time:2020-08-13 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This is the prototype of my car, which is not yet completed. I have been stuck on the servo control part, but I finally got it done today. Next, I will talk about all the parts I paid attention to and share my experience with you:

1 The microcontroller, servo, and power supply share a common ground;
2 Use two voltage regulator chips for power supply to separate the power supply;
3 The servo signal end is not directly connected to the microcontroller IO port, so I connected a 10K resistor in series. That is, after this step, I can control my servo.
 

I have heard many people say that the signal end can be controlled by connecting it directly to the IO port, but I have never been able to do it. Today, I connected a 10K resistor in series and unexpectedly found that I can control the servo.


So I posted this to share with you, and at the same time thank those who helped me think about it, thank you~~

Those who need a clearer circuit can leave a message, if I see that there are many people who need it, I will upload it~~~

Based on the public's request for information, then I will post my reference information~ However, these are not the circuits of my car, they are just references, you can also refer to them, but my post is mainly to share with you the problem of servo control, because I found that many people will have this question, so I will control it and post this post to communicate with you, the following is the circuit diagram of the car I referred to and a simple program, as well as
a framework for my power supply~~   
 (The car is being improved...)


Source program:


#include

#define uint unsigned int         

#define uchar unsigned char


sbit P1_0=P1^0; //PWM signal output terminal                 

void Init_t0(void); //Sub-function declaration               

void main()  

{         

        P1_0=0; //Set the initial PWM output to high level                        

        Init_t0();

        while(1); //wait for interrupt

}


void Init_t0(void)

{   

        TMOD=0x01; //Set timer 0 to work in mode 1 and then start the TR bit

        TH0=(65536-1500)/256;

        TL0=(65536-1500)%256; //1.5ms high level

        EA=1;

        ET0=1;

        TR0=1;         

}


/*******************Interrupt service routine***********************/  


void PWM0() interrupt 1 //Timer 0 interrupt, generate square wave

{  

        if(P1_0==1) //If the previous level is 1, the next low level time is 18.5ms

        {  

                TH0=(65536-18500)/256; // Tried t1=1500; t0=20000-t1; but the servo always squeaks. I guess: the calculation takes time.

                TL0=(65536-18500)%256;

        }

        else //If the previous level is 0, the next high level is 1.5ms

        {

                TH0=(65536-1500)/256;

                TL0=(65536-1500)%256;

        }

        P1_0=~P1_0;

}



Reference address:Teach you how to control mg995 servo with 51 MCU

Previous article:Successfully challenged 89C52 to drive WS2812
Next article:51 MCU outputs 3-way PWM to drive RGB LED source program

Latest Microcontroller Articles
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号