标题: 控制电机转动程序 [打印本页]

作者: mark127    时间: 2018-6-27 12:42
标题: 控制电机转动程序
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int  
#define uchar unsigned char
sbit YELLOW=P0^7;
sbit GREEN=P2^0;
sbit RED=P2^1;  
sbit BLUE =P2^2;
sbit ENA=P2^7;
sbit ENB=P3^0;  
sbit IN1 = P2^3;
sbit IN2 = P2^4;
sbit IN3 = P2^5;
sbit IN4 = P2^6;
uchar cycle=100;
uchar speed=80;
   void Delay(uint t)            //延时函数
    {   
         int i,j;
         for(i=110;i>0;i--)   
         for(j=t;j>0;j--);
        }
   void t0(void) interrupt 1
   {
     if(cycle>100)
            cycle=0;
         if(speed<cycle)
            ENA=ENB=0;       
                else
                ENA=ENB=1;
                cycle++;
    }
    void main()             //主函数
        {  
           EA=1;
           ET0=1;
       TMOD=0X02;
           TH0=0XA3;
           TL0=0XA3;
           TR0=1;      
           Delay(5);   
           P1=0XFF;
    while(1)   
         {
          if(P1==0XFE)            //前进
           {
                         GREEN=0;  
                  IN1 = 0;  //电机转动函数  
                  IN2 = 1;
                  IN3 = 0;  
                  IN4 = 1;
                GREEN=1
           }
           if(P1==0XFD)            //后退
           {
      RED=0;                          
                  IN1 = 1;  //电机转动函数  
                  IN2 = 0;
                  IN3 = 1;  
                  IN4 = 0;
      RED=1;                 
          }
          if(P1==0XFB)            //左转
           {
      BLUE =0;                         
                  IN1 = 0;  //电机转动函数  
                  IN2 = 0;
                  IN3 = 0;  
                  IN4 = 1;
                        BLUE =1;
    }
           if(P1==0XF7)           //右转
           {
      YELLOW=0;                         
                  IN1 = 0;  //电机转动函数  
                  IN2 = 1;
                  IN3 = 0;  
                  IN4 = 0;
                YELLOW=1;
                  }
           if(P1==0XEF)           //停止
           {
                 
                         IN1 = 0;  //电机转动函数  
                  IN2 = 0;
                  IN3 = 0;  
                  IN4 = 0;
           }
     }
         }         







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1