AT89C51 single chip DC motor PWM speed control program

Publisher:喜茶我要七分糖Latest update time:2017-09-02 Source: elecfansKeywords:AT89C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This is a PWM speed control program for DC motors with AT89C51 MCU. The program can be directly used in AT89C52, AT89S51, AT89S51, STC89C51, STC89C52 MCUs. The MCU crystal uses 11.0592M. The DC motor is controlled by L298 integrated circuit. The frequency of the generated PWM is about 91Hz. The pins of L298 have been marked in the program. You can draw the schematic diagram by yourself. I won't give it here. The following is the source program.  

#include[reg52.h> //Please replace ' [ ' with "<", the same below. Otherwise, an error will occur during compilation.

#include [intrins.h>  //

#define uchar unsigned char  

#define uint unsigned int  

sbit en1=P2^0;    

sbit en2=P2^1;    

sbit s1=P2^2;    

sbit s2=P2^3;    

sbit s3=P2^4;    

sbit s4=P2^5;    

fly t=0;    

fly m1=0;    

uchar m2=0;    

float tmp1,tmp2;  

 

void motor(uchar index, char speed)  

{  

if(speed>=-100 && speed<=100)  

{  

  if(index==1)  

  {  

   m1=abs(speed);  

   if(speed<0)  

   {  

    s1=0;  

    s2=1;  

   }  

   else  

   {  

    s1=1;  

    s2=0;  

   }  

  }  

  if(index==2)  

  {  

   m2=abs(speed);  

   if(speed<0)  

   {  

    s3=0;  

    s4=1;  

   }  

   else  

   {  

    s3=1;  

    s4=0;  

   }  

  }  

}  

}  

void delay(uint j)  

{  

for(j;j>0;j--);  

}  

void main()  

{  

char i;  

TMOD=0x02;  

TH0=0x9B;  

TL0=0x9B;  

EA=1;  

ET0=1;  

TR0=1;  

while(1)  

{  

  for(i=0;i<=100;i++)  

  {  

   motor(1,i);  

   motor(2,i);  

   delay(5000);  

  }  

  for(i=100;i>0;i--)  

  {  

   motor(1,i);  

   motor(2,i);  

   delay(5000);  

  }  

  for(i=0;i<=100;i++)  

  {  

   motor(1,-i);  

   motor(2,-i);  

   delay(5000);  

  }  

  for(i=100;i>0;i--)  

  {  

   motor(1,-i);  

   motor(2,-i);  

   delay(5000);  

  }  

}  

}  

void timer0() interrupt 1  

{  

if(t==0)  

{  

  tmp1=m1;  

  tmp2=m2;  

}  

if(t

if(t

t++;  

if(t>=100) t=0;  

}  


Keywords:AT89C51 Reference address:AT89C51 single chip DC motor PWM speed control program

Previous article:Simulation of Motor Speed ​​Measurement System Based on AT89C51 Single Chip Microcomputer
Next article:51 single chip microcomputer automatic bell C language program

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

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号