⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arm7_stepmotor.txt

📁 ARM7驱动步进电机的程序 ARM7驱动步进电机的程序
💻 TXT
字号:
 标签: 步进电机  程序  

ARM7驱动步进电机的程序
///////////////////////////////////////////////////////////////////////////////////
//文件名:motor.c
//
//////////////////////////////////////////////////////////////////////////////////
#include "stepmotor.h"

/*******************************************************
name :main
function : API 接口 调试 
********************************************************/
int main()
{
    StepmotorPort_Init() ;
     
    Stepmotor_Run(ATICLOCK,STEPMOTORREVBASE+300) ;    
    
    while(1) ;
    return 0 ;
}
///////////////////////////////////////////////////////////////////////////////////
//文件名:stepmotor.c
//////////////////////////////////////////////////////////////////////////////////
#include "stepmotor.h"

void __irq Stepmotor_Int(void) ;

//步进电机变量结构体
struct _STEPMOTOR
{
    uint8  dir ; //转动方向
    uint16 rev ; //转速
    uint8  count ;
};

struct _STEPMOTOR stepmotor ;

/****************************************************************
name : StepmotorPort_Init
function :步进电机端口初始化
*****************************************************************/
void StepmotorPort_Init()
{
    PINSEL2 &= 0xFFFFFFCF ;
    IO2DIR  |= 0x00780000 ; //P2.19 P2.20 P2.21 P2.22 FOR OUT
    IO2SET  |= 0x00780000 ;
}

/****************************************************************
name : StepmotorInt_Init
function : 步进电机中断初始化
parameter:
            rev : 步进电机转速 ,rev值越大,步进电机转动越快
*****************************************************************/
void StepmotorInt_Init( uint16 rev )
{    
    T1MCR = 0x600 ;
    T1MR3 = ( Fpclk/rev ) ; //
    
    T1PR = 0 ;
    T1PC = 0 ;
    
    T1TC  = 0 ;
    T1TCR = 0x01 ;
            
    VICIntSelect &= 0xFFFFFFDF ;
    VICVectCntl0 = 0x25 ;
    VICVectAddr0 = (int)Stepmotor_Int ; 
    VICIntEnable |= 0x00000020 ;
} 

//////////////////////////////////////////////////////
//                  API interface function       
//        rundir :步进电机转动方向 1: 0:
//      runrev :转动速度
///////////////////////////////////////////////////////
/*****************************************************************
name : Stepmotor_Run
function : API interface function  
parameter: 
           rundir :步进电机转动方向
           runrev :步进电机转速
******************************************************************/
void Stepmotor_Run( uint8 rundir , uint16 runrev )
{
    stepmotor.dir = rundir ;
    stepmotor.rev = runrev ;    
    stepmotor.count = 0 ;
    
    StepmotorInt_Init( stepmotor.rev ) ;
    
}

/****************************************************************
name : Stepmotor_RunDir
function : 控制步进电机方向
*****************************************************************/
void Stepmotor_RunDir()
{
    if(stepmotor.dir == ATICLOCK )
    {
        if( stepmotor.count == 0 )
        {
            IO2SET = DEASILRUN ;
        }
        else
        {
            IO2SET = ( ANTICLOCKRUN << (stepmotor.count-1) ) ;
        }
        IO2CLR = ( ANTICLOCKRUN << stepmotor.count ) ;
    }
    
    if(stepmotor.dir == DEASIL )
    {
        if( stepmotor.count == 0 )
        {
            IO2SET = ANTICLOCKRUN ;
        }
        else
        {
            IO2SET = ( DEASILRUN >> (stepmotor.count-1) ) ;
        }
        
        IO2CLR = ( DEASILRUN >> stepmotor.count ) ;
    }
            
    stepmotor.count ++ ;
    
    if( stepmotor.count == 4 )
    {
        stepmotor.count = 0 ;
    }        
            
}

/****************************************************************
name : Stepmotor_Int
function :中断处理
*****************************************************************/
void __irq Stepmotor_Int()
{    
    Stepmotor_RunDir() ;    
    
    T1IR = 0x08 ; //复位中断
    VICVectAddr=0x00 ;
}


///////////////////////////////////////////////////////////////////////////////////
//文件名:stepmotor.h
//
//////////////////////////////////////////////////////////////////////////////////
#ifndef _stepmotor_h
#define _stepmotor_h

#include "config.h"

////////////////////////////////////////////////////
//宏定义
/////////////////////////////////////////////////////
#define ANTICLOCKRUN     0x00080000   //逆时针运行值
#define DEASILRUN           0x00400000   //顺时针运行值

#define ATICLOCK         0            //逆时针转动标志
#define DEASIL           1            //顺时针转动标志
 
#define STEPMOTORREVBASE 1           //步进电机基准转速


//////////////////////////////////////////////////////
//                 API interface function       
//        rundir :步进电机转动方向 ; runrev:转动速度
//////////////////////////////////////////////////////
void Stepmotor_Run( uint8 rundir , uint16 runrev ) ;

void StepmotorPort_Init(void) ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -