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(); }
Previous article:51 single chip microcomputer car L298N pwm speed control serial port control button control
Next article:51 single chip matrix keyboard
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Gods, why do you remind me to add a semicolon in front? ?
- CMD file analysis in CCS
- What is UWB and why is it in my phone?
- GD32E231 incense machine control board
- SPI interface oled screen driver based on pic18f25k22 microcontroller
- Understanding of USB-Serial, USB to Serial, USB-UART, PL2303, CH340, etc.
- [RVB2601 Creative Application Development] Design of a Farmland Pest Control Device
- [RVB2601 Creative Application Development] 2. Light up the LED
- Level shifter reverse circuit
- Please advise, is it necessary to consider the safety power when calculating the LED resistance?