51 MCU IO simulates PWM to control the steering of the servo

Publisher:数字思维Latest update time:2016-03-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The crystal oscillator in this example is 32M


#include
#define uint unsigned int

void drive();
void delay(uint);

sbit pwm=P2^0;


uint set; //set is the steering gear angle flag
unsigned char angle;  //angle is the steering gear PWM change angle


void Time0_Init()  //Interrupt initialization
{
 TMOD=0X01;
 IE=0X82;
 TH0=(65536-58)/256;   
 TL0=(65536-58)%6;
 TR0=1;
}

void main()   //Main function
{
set=0;
angle=23;  // Changing the value of angle can change the duty cycle of the servo

Time0_Init();
 
  for(;;)
   {
     drive();
    
}


void drive()  //Motor drive routine, change duty cycle
{
 
 angle=12; 
 delay(800);

 angle=52;  
 delay(800);
  
 angle=135;  
 delay(800);
}

void delay(uint x)
  {
  uint i,j;
  for(i=x;i>0;i--)
    for(j=110;j>0;j--);
  }

void T0_time()interrupt 1  //100HZ,10MS
{
 
 TH0=(65536-58)/256;   
 TL0=(65536-58)%6;
 
 if(set   pwm=1;
 else
 pwm=0;
 set++;
 if(set>385) set=0;   //Servo duty cycle

 

}

Reference address:51 MCU IO simulates PWM to control the steering of the servo

Previous article:80C51 MCU Program (1)
Next article:Detailed explanation of 8051 microcontroller instruction definition - AJMP addr11 (4)

Latest Microcontroller Articles
  • 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)
    Since development under LINUX is still quite troublesome, is there a more convenient and simple development method under WINDOWS? The answer is yes. Of course, it is not a development tool like ADS, because it ...
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
  • Learn ARM development(18)
Change More Related Popular Components
Guess you like

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号