微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 关于PWM调速问题

关于PWM调速问题

时间:10-02 整理:3721RD 点击:
这是原程序不知道为什么不能调速,在下只是个刚刚开始学习单片机的菜鸟。我用的是ln298驱动。程序中的PWM定义实际是ln298中的两个ena。还请大神们指点。

#include<reg51.h>                                
#define uchar unsigned char
#define uint  unsigned int  
sbit M1A = P0^3;                               
sbit M1B = P0^4;                               
sbit M2A = P0^5;               
sbit M2B = P0^6;                               
sbit RightIR=P3^6;                        //右侧传感器
sbit LeftIR=P3^4;                         //左侧传感器
sbit FontIR=P3^7;                         //前传感器
sbit  PWM=P0^2;
sbit  PWM2=P0^7;
uchar count;
/*******************************T0,T1定时 *******************************/
void T0_init(void)
{
         TMOD=0x11;
        TH1=(65536-200)/256;  
  TL1=(65536-200)%256;
        TH0=(65536-500)/256;  
  TL0=(65536-500)%256;  
   ET0=1;  
   TR0=1;
   EA=1;
         ET1=1;
         TR1=1;
}
/*****T0,PWM调速*******/
void time0(void) interrupt 1
{  TH0=(65536-500)/256;  
  TL0=(65536-500)%256;   
  count++;  
  if(count<=200) PWM=0;  
  else PWM=1;
  if(count>=500)
   count=0;
        if(count<200) PWM2=0;  
  else PWM2=1;
  if(count>=500)
   count=0;
}

void tingzhi()                                                                                                                //停止
{
   M1A=0;                                   
   M1B=0;                                   
   M2A=0;                                   
   M2B=0;
}
void qianjin()                                                                                                                //前进
{
   M1A=1;                                   
   M1B=0;                                   
   M2A=1;                                   
   M2B=0;
}
void houtui()                                                                                                                //后退
{
   M1A=0;                                   
   M1B=1;                                   
   M2A=0;                                   
   M2B=1;
}
void zuozhuan()                                                                                                        //左转
{

   M1A=0;                                   
   M1B=1;                                   
   M2A=1;                                   
   M2B=0;
}
void youzhuan()                                                                                                        //右转
{
   M1A=1;                                   
   M1B=0;                                   
   M2A=0;                                   
   M2B=1;
}
void delay(unsigned int i)  //定时
{                        uint j=0;
        for(;i>0;i--)
        {                for(j=0;j<125;j++)
                        {;}
        }
}
/*****T1中断******/
void        zhixian()        interrupt        3
{       
        if(FontIR==0&&LeftIR==0&&RightIR==1)
                        youzhuan();
               
        if(FontIR==0&&LeftIR==1&&RightIR==0)
                        zuozhuan();
                       
        if(FontIR==1&&LeftIR==0&&RightIR==1)
                        qianjin();
}
                       
void main()                               //主程序
{
T0_init;
while(1)
{if(FontIR==0&&LeftIR==0&&RightIR==1)
                        youzhuan();
               
        if(FontIR==0&&LeftIR==1&&RightIR==0)
                        zuozhuan();
                       
        if(FontIR==1&&LeftIR==0&&RightIR==1)
                        qianjin();
                        }
                       
       
}

帮你顶!
帮你顶!
帮你顶!
帮你顶!

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top