Use 51 single chip microcomputer to output PWM to control the servo

Publisher:EnchantingEyesLatest update time:2019-08-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A club in SCU's School of Electronics and Information holds a boat model competition every year.

Because I was lazy during last year's boat model competition, I bought a ready-made remote control (Tian Di Fei 6) to participate in the competition, and controlled the ESC and servo on the remote control boat I made. Recently, I feel like making my own remote control in this year's boat model competition, so let's start with the first step: use a single-chip microcomputer to control the servo (the principle of controlling the ESC is similar).


0x01.What is a servo?


        In fact, the steering gear is a servo motor that rotates a certain angle according to requirements. In my opinion, knowing these is enough~~

0x02.What is the use of servos?


        Take the model for example. If the boat wants to turn, one way it can be used is to change the angle of the rudder behind the boat to change the resistance of the water on both sides, so as to make the boat turn. This is also the origin of the term steering gear.


0x03.How to control?


        What is a PWM wave? It is actually a square wave with a frequency of 50Hz and a period of 20ms. In each period, the duty cycle of the high level is between 0.5ms and 2.5ms. 0.5ms represents 0 degrees, and 2.5ms represents 180 degrees. Other degrees can be directly converted according to the ratio* (this is a bit like the meaning of the map function in ARDUINO)


0x04. How to implement PWM?


        There are many ways to implement pulse width modulation waves. Many microcontrollers already have "natively supported" PWM output pins to output PWM waveforms.


        I use a more earthy method, as shown below:


This thing is used for output. It is a 89C52RC model microcontroller produced by STC. Its core is a 51 microcontroller, and the timer interrupt method is used to output pwm waves.




0x05. Implementation effect


       The P2^7 pin of the microcontroller outputs a PWM waveform, and the P2^4 and P2^5 pins are used to operate independent buttons to control the angle of the servo.




Using the KEIL 4 platform, the code is as follows:


Main function:






//GYZ

// 1 PWM waveform output, can control the servo and ESC

 

#include

 

//Pin position definition

sbit key1 = P2^5;

sbit key2 = P2^4; //Independent key pin

sbit pwm1 = P2^7;

 

//Global variable definition 

unsigned int target =6,percent = 0;

 

//Header file includes

#include

#include

#include

 

// Function declaration

void Timer0Initial();

void initial_Timer();

void delay(unsigned int x);

void keyscan();

 

 

void main()

{

initial();

while(1)

{

keyscan();

}

}



Independent key scanning


key.h


void keyscan()

{

if(key1 == 0)

{

delay(3);

if(key1 == 0)

{

if(target >= 7)

{

target -= 1;

}

while(key1 == 0)

{P1 = 0x00;}

}

P1 = 0xFF;

}

 

if(key2 == 0)

{

delay(3);

if(key2 == 0)

{

if(target <= 26)

{

target += 1;

}

while(key2 == 0)

{P1 = 0x00;}

}

P1 = 0xFF;

}

}//Key detection program

 

void initial()

{

key1 = 1;

key2 = 1;

P1 = 0xff;

initial_Timer();

}


Timer Program


Timer.h


//Timer Configuration

//Use 12Mhz crystal

//Theoretically, after the 12Mhz crystal is divided by 12

//Should be 100 counts and the time is 100us

//Measured by oscilloscope

//Select 65 times to generate an interrupt

 

void Timer0Initial()

{

TH0 = (65536-65)/256;

TL0 = (65536-65)%256;

} //This initial value causes an interrupt to occur every 0.1ms

 

void initial_Timer()

{

EA = 1;    // Enable general interrupt

ET0 = 1;    // Enable T0 interrupt

TMOD = 0x01; //Timer mode, T0 uses working mode 1

Timer0Initial();   //Install initial value, generate interrupt once every 0.1ms

TR0 = 1;   //Turn on T0 timer

}

 

void delay(unsigned int x)

{

unsigned int i,j;

for(i = 0;i< x; ++i)

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

;

}


Finally, the interrupted program


interrupt.h


void Timer0() interrupt 1 using 0

{

percent += 1;

if(percent == 200)

{

percent = 0;

pwm1 = 1; //Pull the pin high to maintain the frequency of 50hz

}

if(percent == target)

{

pwm1 = 0;

}

Timer0Initial();

 

}


Reference address:Use 51 single chip microcomputer to output PWM to control the servo

Previous article:51 single chip microcomputer car L298N pwm speed control serial port control button control
Next article:51 single chip matrix keyboard

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号