51 single chip microcomputer drives servo motor program

Publisher:行者无疆1978Latest update time:2015-05-19 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Except for a few enhanced versions of 51 microcontrollers, many do not have PWM. Therefore, to control servo motors with 51, software simulation of PWM is often required. I have seen some codes that output PWM waves through delay, and some use timers, but those codes are more troublesome to drive multi-channel servo circuits. The following code generates a reminder every 0.5ms through timer 0. The program can return the value to the servo motor signal pin by calling the DJ() function in the large loop. The DJ() function can determine the rotation angle, but because the timer is set to a 0.5ms interrupt, this determines that the rotation angle can only be -45, -90, 0, 45, 90. If you want to improve the accuracy, you need to set the initial value of the timer and change the num value of the function to achieve it.
#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.
Reference address:51 single chip microcomputer drives servo motor program

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

A current mode PWM monolithic buck regulator circuit design
    AP2420 is a current mode PWM monolithic buck regulator with an input voltage range of 2.5V to 6V, an output current of up to 2A, and an output voltage as low as 0.6V. Since the total current of the expansion board and the core board will not exceed 2A, using AP2420 as the power regulator of the microcontroller cor
[Power Management]
A current mode PWM monolithic buck regulator circuit design
Basic principles of PWM DC/DC converter evolution
The evolution criterion of the basic PWM DC/DC converter is: without considering the input/output reference potential relationship, the input/output voltage relationship remains unchanged, and the main circuit relationship remains unchanged, that is, equivalent conversion. Under this basic criterion, the evolution o
[Power Management]
Basic principles of PWM DC/DC converter evolution
STM32 timer--Toggle mode realizes 2-way pwm phase shift
Here’s how it works: The following code implements the use of TIM3's CH3 and CH4 to output two phase-shifted PWMs with a phase shift angle of 225 degrees. void TIM3_PWMShiftInit(void) { TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct; GPIO_InitTypeDef  GPIO_InitStruct; TIM_OCInitTypeDef TIM_OCInitStruct;   /**
[Microcontroller]
Flyback PWM DC/DC Converter
The flyback PWM converter (single-ended isolated PWM DC/DC converter) is shown in Figure (b). It is actually an isolated (dual-winding) Buck Boost converter. The flyback converter circuit features a simple circuit and the least number of components. It is generally used in low power (such as 100W) and multi-channel
[Power Management]
Flyback PWM DC/DC Converter
Timer simulates PWM output (three codes introduced)
1 Introduction PWM, the English name Pulse Width Modulation, is the abbreviation of pulse width modulation. It modulates the width of a series of pulses to output the required waveform (including shape and amplitude) and digitally encodes the analog signal level, that is, it adjusts the change of signal, energy, etc.
[Microcontroller]
Timer simulates PWM output (three codes introduced)
Research on high-power ultrasonic power supply system based on PWM technology
    Abstract: In view of the need for high-power ultrasonic transducer devices based on new rare earth functional materials and the need for supporting ultrasonic power supplies with excellent performance, this paper introduces the composition, working principle and application of a high-power ultrasonic power supply
[Power Management]
Analysis of the main power circuit design of three-phase PWM inverter
With the development of power electronics technology,  the application of inverters has penetrated into various fields, and generally requires inverters to have high-quality output waveforms. The quality of inverter output waveforms mainly includes two aspects, namely steady-state accuracy and dynamic performanc
[Power Management]
Analysis of the main power circuit design of three-phase PWM inverter
STM8S TIM2 output PWM initialization function
Refer to the online examples to summarize the initialization function #define TIM2_CLK 16000000  void  timer2_Init(char channal, ulong hz,int pwm) {     TIM2_ARRH = (TIM2_CLK/hz)/256;     TIM2_ARRL = (TIM2_CLK/hz)%256;     switch (channal)     {     case 1:{          TIM2_CCMR1 |= 0x70;          TIM2_CCER1 |= 0x0
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号