#include"reg52.h"
unsigned int num=0;
//Servo
sbit DJ1=P3^6;
sbit DJ2=P3^7; /*Pin definition*/
int DJ(int a); //Function declaration
timerinit() //Timer interrupt initialization
{
TMOD=0x01; //Set timer 0 to working mode 1
EA=1; //Open total interrupt
ET0=1; //Timer 0 interrupt enable
TH0 = (65536-500)/256; //The initial value makes the timer overflow in 0.5 milliseconds
TL0 = (65536-500)%256;
TR0=0;
}
main()
{
timerinit();
while(1)
{
}
}
timer() interrupt 0 //An interrupt occurs every 0.5ms. After 20ms, the timer is reset to 0 and counts again
{
num++;
DJ1=DJ(3);
DJ2=DJ(3);
if(num>=40)
{
num=0;
TH0 = (65536-500)/256; //The initial value makes the timer overflow in 0.5 milliseconds
TL0 = (65536-500)%256;
return;
}
}
int DJ(char a) //When a=3, the position of the servo is 0 degrees, a=2, a=1 respectively correspond to -45 -90 degrees, a=4, a=5 correspond to 45 90 degrees
{
if(num>=a||num>=5)
return 0;
if(num>=1)
return 1;
}
Due to the time constraints, I have not tested the feasibility of the program (usually there is no problem, haha). If I have time, I will continue to improve the program and add the function of controlling the rotation speed of the servo. If you want to make a robot, you should get a professional driver board, so that debugging will be very intuitive and convenient, reducing many unnecessary troubles.
Previous article:51 MCU universal infrared remote control decoding program
Next article:C program for microcontroller to control 18-way servos
Recommended ReadingLatest update time:2024-11-16 21:34
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Intelligent Control Technology of Permanent Magnet Synchronous Motor (Written by Wang Jun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Ek314 video test problem
- [TI millimeter wave radar evaluation]_3_mmwave_studio usage problems
- MM, be careful, your secret to sleeping has been discovered!
- Is CD4051 VEE negative voltage problem?
- Motion and Environmental Sensor Development Board X-NUCLEO-IKS01A3 Related Materials
- Gaoyun Cloud Source Editor causes cursor movement when using Chinese
- How to reduce the cost of Bluetooth
- Why can elderly devices costing only a few hundred yuan measure body temperature, but smart phones costing thousands of yuan cannot?
- [Silicon Labs BG22-EK4108A Bluetooth Development Evaluation] Part 2: Understanding the Reading and Writing of Bluetooth BLE LED Light Services
- About the problem of HAL library external interrupt entering interrupt service twice