×
单片机 > 单片机程序设计 > 详情

二相四线步进电机的单片机源程序 用L298N模块驱动

发布时间:2020-06-17 发布时间:
|

在网上找了很久资料,都没有直接的源代码去驱动二相四线步进电机。本人编写了一些基本的代码,再加上L298N模块,就可以轻松驱动啦。
 

单片机源程序如下:

#include "reg52.h"


//电机IO

#define GPIO_MOTOR P1

//sbit F1 = P1^0;

//sbit F2 = P1^1;

//sbit F3 = P1^2;

//sbit F4 = P1^3;

//按键IO


sbit K1=P3^0;

sbit K2=P3^1;

sbit K3=P3^2;

sbit K4=P3^3;


                                                                                                                                                                    

sbit a0=P1^0;                          

sbit a1=P1^1;            

sbit b0=P1^3; 

sbit b1=P1^4;


void right(unsigned int y);

void left(unsigned int x); 

void Delay(unsigned int t);

unsigned char Direction,Speed;

void motor();

/*******************************************************************************

* 函 数 名         : main

* 函数功能                   : 主函数

* 输    入         : 无

* 输    出         : 无

*******************************************************************************/


void main(void) 

        unsigned char i;        

   Speed=10;


  while(1)

        {

                         

          if(K1==0)                //检测按键K1是否按下

                {

                        Delay(50);        //消除抖动

                        if(K1==0)

                        {

                                Direction=1;

                        }

                        while((i<200)&&(K1==0))         //检测按键是否松开

                        {

                                Delay(50);

                                i++;

                        }

                        i=0;

                }



                if(K2==0)                //检测按键K1是否按下

                {

                        Delay(50);        //消除抖动

                        if(K2==0)

                        {

                                Direction=2;

                        }

                        while((i<200)&&(K2==0))         //检测按键是否松开

                        {

                                Delay(50);

                                i++;

                        }

                        i=0;

                }        




                 if(K3==0)                //检测按键K1是否按下

                {

                        Delay(100);        //消除抖动

                        if(K3==0)

                        {

                                Speed--;

                                if(Speed<=3)

                                Speed=4;

                        }

                        while((i<200)&&(K2==0))         //检测按键是否松开

                        {

                                Delay(1);

                                i++;

                        }

                        i=0;

                }

         

                if(K4==0)                //检测按键K1是否按下

                {

                        Delay(100);        //消除抖动

                        if(K4==0)

                        {

                                Speed++;

                        }

                        while((i<200)&&(K4==0))         //检测按键是否松开

                        {

                                Delay(100);

                                i++;

                        }

                        i=0;

                }        


          motor();

  


        }

}






void motor()

{


if(Direction==1) 

        {

        a0=0;

        a1=1;

        b0=1;

        b1=1;

        Delay(Speed);

        a0=1;

        a1=0;

        b0=1;

        b1=1;

        Delay(Speed);

        a0=1;

        a1=1;

        b0=0;

        b1=1;

        Delay(Speed);

        a0=1;

        a1=1;

        b0=1;

        b1=0;

        Delay(Speed);

        }



if(Direction==2) 

                 

        {

        a0=1;

        a1=1;

        b0=1;

        b1=0;

        Delay(Speed);

        a0=1;

        a1=1;

        b0=0;

        b1=1;

        Delay(Speed);

        a0=1;

        a1=0;

        b0=1;

        b1=1;

        Delay(Speed);

        a0=0;

        a1=1;

        b0=1;

        b1=1;

        Delay(Speed);

        }



}




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
如何升级STM32单片机的代码