Today, I started my first blog. I have been learning 51 single chip microcomputer for some time. This article mainly introduces the debugging of PWM.
This section mainly uses the steering gear to realize PWM debugging and help more beginners learn
Xi.
As a beginner, I used the steering gear SG90 when learning 51:
Here are the pictures:
This steering gear has three leads: power line, GND and potential control line; Therefore, we only need to connect it to the single chip microcomputer. In use, we need to understand the operating principle of the steering gear. The steering gear adjusts different positions through the PWM signal on the control line.
PWM (pulse width modulation), i.e. pulse width modulation, in inertial systems, the required analog parameters can be obtained equivalently by debugging the width of a series of pulses. It is often used in motor speed regulation, switching power supply and other fields.
Important parameters of PWM: (Ts is the cycle to complete a process, and Ton is the time to turn on the high level)
Frequency = 1/Ts
Duty cycle = Ton/Ts
Accuracy = duty cycle change step
The following is the duty cycle of our common steering gear commissioning:
According to the chart, we can know that a high and low level cycle of the steering gear is 20ms. During this period, we can freely debug the duty cycle and let the steering gear rotate to any angle we want. Here I only provide a few angles in the picture. If you want to try other angles, you can try to debug it yourself; Here I try to use the delay to debug the duty cycle of the steering gear. The code is complete. If you feel appropriate, you can learn and use it directly; You can also try to debug PWM through timer. The principle is basically the same. Timer debugging is also the most used and more convenient. Of course, I'm just a simple idea here. Through such simple debugging, we can try the breathing lamp, which is the most direct. After all, there is no need to connect external equipment.
Insert the code slice here sbit ServoPin = P2^7; //Control steering gear pin /*The square wave delay of the steering gear is directly in front of the trolley*/ void Delay1550us() //@11.0592MHz { unsigned char i, j; i = 3; j = 196; do { while (--j); } while (--i); } void Delay18450us() //@11.0592MHz { unsigned char i, j; _nop_(); i = 34; j = 16; do { while (--j); } while (--i); } /*Steering gear square wave delay to the right of trolley*/ void Delay600us() //@11.0592MHz { unsigned char i, j; _nop_(); i = 2; j = 15; do { while (--j); } while (--i); } void Delay19400us() //@11.0592MHz { unsigned char i, j; _nop_(); i = 35; j = 197; do { while (--j); } while (--i); } /*The square wave delay of the steering gear faces to the left of the trolley*/ void Delay17500us() //@11.0592MHz { unsigned char i, j; i = 32; j = 93; do { while (--j); } while (--i); } void Delay2500us() //@11.0592MHz { unsigned char i, j; i = 5; j = 120; do { while (--j); } while (--i); } void delays(unsigned int z)//Millisecond delay { unsigned int x,y; for(x = z; x > 0; x--) for(y = 114; y > 0 ; y--); } /*================================================= *Function name: ServoFront *Function: Pan Tilt rotates forward *Call: *Input: =================================================*/ void ServoFront() { char i; for(i=0;i<10;i++) { ServoPin = 1; Delay1550us(); ServoPin = 0; Delay18450us(); } delays(800); } /*================================================= *Function name: ServoLeft *Function: Pan Tilt rotates to the left *Call: *Input: =================================================*/ void ServoLeft() { char i; for(i=0;i<10;i++) { ServoPin = 1; Delay2500us(); ServoPin = 0; Delay17500us(); } delays(800); } /*================================================= *Function name: ServoFront *Function: Pan Tilt rotates to the right *Call: *Input: =================================================*/ void ServoRight() { char i; for(i=0;i<10;i++) { ServoPin = 1; Delay600us(); ServoPin = 0; Delay19400us(); } delays(800); } void main() { ServoFront(); //Steering gear forward (steering gear reset) }
I hope this article can help you!!!
At the same time, you can put forward your own suggestions and opinions, so that I can find my shortcomings.