Servo microcontroller introduction tutorial
1. Servo preparation
Servo is a must-learn module for the introduction of single-chip mechanical control. In some robot joints, servo is also used as control. Learning and proficient in using servo is our further understanding of single-chip microcomputer.
Therefore, we can not always use the development board to learn, but expand some electronic modules to help us understand the microcontroller more deeply. So you need to buy the servo by yourself.
The control principle of the small servo and the large servo is almost the same. Considering the economical and reasonable use, we choose the small servo as an introductory learning and simple use.
The common small servo model is "9g servo", and its appearance is roughly as follows:
However, I think this type of servo is cheap and commonly used by everyone, but this type of servo is far less useful than metal servos, so I still recommend that you choose to buy metal servos that are similar in size to the picture above but are slightly more expensive. The servo model used in this tutorial is shown in the figure below:
2. Servo control principle
The servo used in this tutorial is 9g in size, and the model is "FS90MG" as an example module.
The acting angle of this kind of servo is 0~180 degrees, that is, the servo propeller can be stuck in a certain angle at will. Due to the power supply, we use brute force to move the servo propeller. Only when the servo is powered off, the servo propeller is difficult to move. The oar will let us twist at will.
The servo has three wires. In addition to the two power wires for power supply, there is also a signal wire. Because we are using a 5V voltage power servo, the power wire can be directly connected to the +5V and GND of the microcontroller. If the signal wire is connected, To an IO port of the single-chip microcomputer, the IO port controls the angle of the servo paddle by outputting PWM and stays at any position.
The period of this PWM is 20ms, and the high level time is between 0.5ms~2.5ms to control the staying position of the servo paddle angle.
0.5ms---------0 degrees.
1.0ms---------45 degrees.
1.5ms---------90 degrees.
2.0ms---------135 degrees.
2.5ms---------180 degrees.
We connect the power cord of the servo, and then connect the signal line to P1.7, download the code, and the servo propeller will rotate to a fixed angle after power on and stay there. We want to twist the servo propeller. It is more laborious.
3. Code
#include <reg52.h>
#include <function.h>//
领英推荐
#include <timer.h> //
sbit PWMOUT = P1^7; //Servo signal line pin
??void main()
{
???LED_Init();//Initialize the LED hardware module
???EA = 1; //Close the main interrupt switch
???TIM0_Init(100,9);//timing 0.1ms, 9 is fine-tuning to make the timing accuracy higher
???while(1);
}
??void TIM0_IRQHandler() interrupt 1
{
???static u8 pwm=0;
???TH0 = T0RH; //Reload the reload value
???TL0 = T0RL;
???pwm++;
???if(pwm>=200)pwm=0; //pwm changes between 0 and 199 at an interval of 0.1ms, with a period of 20ms
??if(pwm<10)PWMOUT=1;//The high level lasts for 1ms in the PWM with a period of 20ms, and the low level lasts for 19ms, and the servo propeller stays at the position of 45 degrees.
???else PWMOUT=0;
}
We draw the waveform
After the power is off, we twist the servo propeller to another angle position, and then power on, observe the experimental phenomenon of the servo propeller to understand the working principle of this small module.