📄 motor.c
字号:
/******************************************************************************
*
* (c) copyright Freescale Semiconductor Hong Kong Ltd. 2004
* ALL RIGHTS RESERVED
*
*******************************************************************************
** THIS CODE IS ONLY INTENDED AS AN EXAMPLE FOR DEMONSTRATING THE FREESCALE **
** MICROCONTROLLERS. IT HAS ONLY BEEN GIVEN A MIMIMUM LEVEL OF TEST. IT IS **
** PROVIDED 'AS SEEN' WITH NO GUARANTEES AND NO PROMISE OF SUPPORT. **
*******************************************************************************
*
* FILE: motor.c REVISION 0.2
*
* DESCRIPTION: Application code is placed here.
* An example to provide support for EVB912H256 motor driver demo
* Program to control the motor module to generate the PWM for
* step motors in driving full step/micro step and all motors
* related feature tests
*
*
* COMPILER: Metrowerks VERSION: ADS v1.2 (initial version)
* VERSION: ADS v2.0 (Demo alpha/beta version)
* VERSION: ADS v3.1 (Demo version)
*
* UPDATED HISTORY:
*
* REV YYYY.MM.DD AUTHOR DESCRIPTION OF CHANGE
* --- ---------- ------ ---------------------
* 0.0 2002.07.15 Kenny Lam Initial version
* 0.1 2002.09.12 Kenny Lam Demo alpha version
* 0.2 2004.04.12 Kenny Lam Demo beta version
* 0.3 2006.01.22 Kenny Lam Demo version
*
******************************************************************************/
/* Freescale is not obligated to provide any support, upgrades or new */
/* releases of the Software. Freescale may make changes to the Software at */
/* any time, without any obligation to notify or provide updated versions of */
/* the Software to you. Freescale expressly disclaims any warranty for the */
/* Software. The Software is provided as is, without warranty of any kind, */
/* either express or implied, including, without limitation, the implied */
/* warranties of merchantability, fitness for a particular purpose, or */
/* non-infringement. You assume the entire risk arising out of the use or */
/* performance of the Software, or any systems you design using the software */
/* (if any). Nothing may be construed as a warranty or representation by */
/* Freescale that the Software or any derivative work developed with or */
/* incorporating the Software will be free from infringement of the */
/* intellectual property rights of third parties. In no event will Freescale */
/* be liable, whether in contract, tort, or otherwise, for any incidental, */
/* special, indirect, consequential or punitive damages, including, but not */
/* limited to, damages for any loss of use, loss of time, inconvenience, */
/* commercial loss, or lost profits, savings, or revenues to the full extent */
/* such may be disclaimed by law. The Software is not fault tolerant and is */
/* not designed, manufactured or intended by Freescale for incorporation */
/* into products intended for use or resale in on-line control equipment in */
/* hazardous, dangerous to life or potentially life-threatening environments */
/* requiring fail-safe performance, such as in the operation of nuclear */
/* facilities, aircraft navigation or communication systems, air traffic */
/* control, direct life support machines or weapons systems, in which the */
/* failure of products could lead directly to death, personal injury or */
/* severe physical or environmental damage (High Risk Activities). You */
/* specifically represent and warrant that you will not use the Software or */
/* any derivative work of the Software for High Risk Activities. */
/* Freescale and the Freescale logos are registered trademarks of Freescale */
/* Semiconductor Inc. */
/*****************************************************************************/
#include <hidef.h>
#include <math.h>
#include <stdlib.h>
#include "cluster_includes.h"
#include "H256Port.h"
#include "Const.h"
#include "GlobalVar.h"
#define STEP0 0
#define STEP1 24
#define STEP2 48
#define STEP3 72
#define STEP4 96
#define SPEEDOMAXSTEP 8000
#define TACHOMAXSTEP 8000
#define FUELMAXSTEP 1450
#define TEMPMAXSTEP 1400
#define MAXFREQ_SPDR Eclock/350
#define MINFREQ_SPDR Eclock/2
#define MAXFREQ_TACH Eclock/200
#define MINFREQ_TACH Eclock/2
#define CONFACTOR 32640 //convertion factor= 32640=(64*2*255/8M)*8M
#define MAXDUTY_FUEL (long)(((float)89*(float)CONFACTOR)/100) //89/%
#define MINDUTY_FUEL (long)(((float)4588*(float)CONFACTOR)/10000) //45.88%
#define MAXDUTY_TEMP (long)(((float)84*(float)CONFACTOR)/100) //84%
#define MINDUTY_TEMP (long)(((float)2196*(float)CONFACTOR)/10000) //21.96%
#define NO_OF_DEMO 18
const char SinTbl4[48]={0,6,11,17,22,29,35,42,48,54,62,70,\
77,84,91,97,102,107,112,116,119,123,126,127,\
128,128,126,124,121,117,112,107,102,97,91,84,\
77,70,62,54,48,42,35,29,22,17,11,6};
const char CosTbl4[48]={128,128,128,127,126,125,123,121,119,116,112,107,\
102,97,91,84,77,70,62,54,46,35,24,13,\
2,9,20, 31,42,52,62,70,77,84,91,97,\
102,107,112,116,119,121,123,125,126,127,128,128};
const char *MotorType[4]={"\n\nSpeedometer\r",
"\n\nTachometer\r",
"\n\nFuel meter\r",
"\n\nTemperature meter\r"
};
const char *MotorStepLimit[4]={"(0-8000)=\r",
"(0-8000)=\r",
"(0-1450)=\r",
"(0-1400)=\r"
};
const char *MotorShortType[6]={"Spdr \r",
"Tach \r",
"Fuel \r",
"Temp \r",
"MOT1 \r",
"MOT2 \r"
};
#define SPEEDOMETERSPEED 3000 //((Eclock/2)*3)/1000/4
#define TACHOMETERSPEED 3000 //((Eclock/2)*3)/1000/4
#define FUELSPEED ((Eclock)*8*10)/1000/16
#define TEMPSPEED ((Eclock)*8*10)/1000/16
/****************************************************************************
* init_stepper1() -
*
* Description : Stepper 1 initialisation subroutine
* :
* Example : N/A
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : N/A
****************************************************************************/
void init_stepper1()
{
MCCTL0=0x00; //;fbus(Eclock)=0, FAST=0 (slow mode)
MCCTL1=0x80; //;RECIRC =1 (low side), disable interrupt for PWM motor
MCPERW=0x80; //;0x80=128 resoultion frequency = fbus(Eclock)/resoulation
// = 62.5K(8M/128)
MCCC0=0x90; //;Dual Full H-bridge mode,left aligned
MCCC1=0x90; //;Dual Full H-bridge mode,left aligned
MCDC0W=0x0; //;Duty cycle channel 0 for micro-stepping initialization
MCDC1W=0x0; //;Duty cycle channel 1 for micro-stepping initialization
// MCDC0H=0x90; //;Duty cycle channel 0 for fast mode (FAST=1)
// s=1 d10d9d8d7d6d5d4d3d2d1d0
// 0 0 0 0 1 0 0 0 0 0 0 =0x40 (50% DTY)
// MCDC1H=0x90; //;Duty cycle channel 1 for fast mode (FAST=1)
// s=1 d10d9d8d7d6d5d4d3d2d1d0
// 0 0 0 0 1 0 0 0 0 0 0 =0x40 (50% DTY)
MotorSpeed[SPEEDOMETER]=((Eclock/2)*3)/1000/2; //(375usec) 2666.7hz for micro-step
//111 hz for full step (24 micro-step)
MotorDemoStep[SPEEDOMETER]=3600; //for demo destination
MotorDemoStep1[SPEEDOMETER]=1000; //for demo destination
motor_flag[SPEEDOMETER]=DISABLE; //motor in disable status
MotorMaxStep[SPEEDOMETER]= SPEEDOMAXSTEP; //motor end limit
MotorMinStep[SPEEDOMETER]=0; //motor head limit
MotorMoveStatus[SPEEDOMETER]=IDLE;
MaxSpeedIndex[SPEEDOMETER]=30;
MTRDR[SPEEDOMETER]=CLOCKWISE;
}
/****************************************************************************
* init_stepper2() -
*
* Description : Stepper 2 initialisation subroutine
* :
* Example : N/A
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : N/A
****************************************************************************/
void init_stepper2()
{
MCCC2=0x90; //;Dual Full H-bridge mode,left aligned
MCCC3=0x90; //;Dual Full H-bridge mode,left aligned
MCDC2W=0x000; //;Duty cycle channel 2 for micro-stepping initialization
MCDC3W=0x000; //;Duty cycle channel 3 for micro-stepping initialization
MotorSpeed[TACHOMETER]=((Eclock/2)*3)/1000/2; //(375usec) 2666.7hz for micro-step
MotorDemoStep[TACHOMETER]=3600; //for demo destination
MotorDemoStep1[TACHOMETER]=1200; //for demo destination
motor_flag[TACHOMETER]=DISABLE; //motor in disable status
MotorMaxStep[TACHOMETER]= TACHOMAXSTEP; //motor end limit
MotorMinStep[TACHOMETER]=0; //motor head limit
MotorMoveStatus[TACHOMETER]=IDLE;
MaxSpeedIndex[TACHOMETER]=60;
MTRDR[TACHOMETER]=CLOCKWISE;
}
/****************************************************************************
* init_stepper3() -
*
* Description : Stepper 3 initialisation subroutine
* :
* Example : N/A
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : N/A
****************************************************************************/
void init_stepper3()
{
MCCC4=0x90; //;Dual Full H-bridge mode,left aligned
MCCC5=0x90; //;Dual Full H-bridge mode,left aligned
MCDC4W=0x9060; //;Duty cycle channel 5
MCDC5W=0x9060; //;Duty cycle channel 6
MotorSpeed[FUEL]=(unsigned long)((Eclock)*8*10)/1000/16; //0.05sec 200hz for micro-step
//0.1085hz for full step
MotorHBSpeed[FUEL]=(unsigned int)(MotorSpeed[FUEL]>>16);
MotorDemoStep[FUEL]=1500; //for demo destination
MotorDemoStep1[FUEL]=0; //for demo destination
motor_flag[FUEL]=DISABLE; //motor in disable status
MotorMaxStep[FUEL]= FUELMAXSTEP; //motor end limit
MotorMinStep[FUEL]=0; //motor head limit
MotorMoveStatus[FUEL]=IDLE;
MTRDR[FUEL]=CNTCLOCKWISE;
}
/****************************************************************************
* init_stepper4() -
*
* Description : Stepper 4 initialisation subroutine
* :
* Example : N/A
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : N/A
****************************************************************************/
void init_stepper4()
{
MCCC6=0x90; //;Dual Full H-bridge mode,left aligned
MCCC7=0x90; //;Dual Full H-bridge mode,left aligned
MCDC6W=0x9060; //;Duty cycle channel 6
MCDC7W=0x9060; //;Duty cycle channel 7
MotorSpeed[TEMP]=(unsigned long)((Eclock)*8)/1000/16; //0.0167sec 60hz for micro-step
//0.1085hz for full step
MotorHBSpeed[TEMP]=(unsigned int)(MotorSpeed[TEMP]>>16);
MotorDemoStep[TEMP]=1500; //for demo destination
MotorDemoStep1[TEMP]=0; //for demo destination
motor_flag[TEMP]=DISABLE; //motor in disable status
MotorMaxStep[TEMP]= TEMPMAXSTEP; //motor end limit
MotorMinStep[TEMP]=0; //motor head limit
MotorMoveStatus[TEMP]=IDLE;
MTRDR[TEMP]=CLOCKWISE;
}
/****************************************************************************
* motor_dest() -
*
* Description : Move motor to the destination
* :
* Example : N/A
* Input : N/A
* Output : updated MotorFinalStep[?]
* Modify : N/A
* Return : N/A
****************************************************************************/
char motor_dest(unsigned int pos)
{
char data_flag;
data_flag=check_pos(pos);
if((MotorStatus[MotorTest]==OFF)&&(data_flag==OK))
{
MotorFinalStep[MotorTest]=(int)pos;
MotorMoveStatus[MotorTest]=RAMPUP;
MotorSpeedIndex[MotorTest]=0;
MotorStatus[MotorTest]=ON;
return(data_flag);
}
else
{
return(data_flag); //is busy or out of range
}
}
/****************************************************************************
* check_pos(unsigned int position) -
*
* Description : Check Motor destination within limit or not subroutine
* :
* Example : N/A
* Input : position
* Output :
* Modify : N/A
* Return : dataflag to identify ok/notok
****************************************************************************/
char check_pos(unsigned int position)
{
char dataflag;
if(MotorTest==SPEEDOMETER)
{
if((position<=SPEEDOMAXSTEP))
dataflag=OK;
else
dataflag=NOTOK;
}
else if (MotorTest==TACHOMETER)
{
if((position<=TACHOMAXSTEP))
dataflag=OK;
else
dataflag=NOTOK;
}
else if (MotorTest==FUEL)
{
if((position<=FUELMAXSTEP))
dataflag=OK;
else
dataflag=NOTOK;
}
else if (MotorTest==TEMP)
{
if((position<=TEMPMAXSTEP))
dataflag=OK;
else
dataflag=NOTOK;
}
else
dataflag=NOTOK;
return(dataflag);
}
/****************************************************************************
* motor_home() -
*
* Description : Disable demo and let motor to home position
* :
* Example : N/A
* Input : N/A
* Output : MotorMode[all]= NORMAL
* Modify : N/A
* Return : N/A
****************************************************************************/
void motor_home()
{
char i;
DisableInterrupts;
for (i=0;i<4;i++)
{
MotorMode[i]=TEST;
MotorFinalStep[i]=0; // zero position
MotorStatus[i]=ON;
MotorMoveStatus[i]=CONST; // const speed
MotorSpeedIndex[i]=MaxSpeedIndex[i];
DemoIndex[i]=0; // clear DemoIndex[x]
}
MotorSpeed[SPEEDOMETER]= SPEEDOMETERSPEED; // set motor speed for SPEEDOMETER
MotorSpeed[TACHOMETER]= TACHOMETERSPEED; // set motor speed for TACHOMETER
MotorSpeed[FUEL]= ((Eclock/2)*3)/1000/4; // set motor speed for FUEL and TEMP
MotorDemoFlag=DISABLE;
PORTS = 0xF0; // LED off
//PORTP = 0x3A; // LED off
EnableInterrupts;
}
/****************************************************************************
* motor_move(char direction) -
*
* Description : Move motor UP(clockwise)/DOWN(anti-clockwise)
* : step by MotorStep
* Example : N/A
* Input : N/A
* Output : MotorFinalStep[?]+ MotorStep
* Modify : N/A
* Return : ok/notok
****************************************************************************/
char motor_move(char direction)
{
char data_flag;
int distance;
if(direction==UP)
distance=MotorFinalStep[MotorTest]+MotorStep;
else
distance=MotorFinalStep[MotorTest]-MotorStep;
if(MotorStatus[MotorTest]==OFF)
{
data_flag=check_pos(distance);
if(data_flag==OK)
{
MotorFinalStep[MotorTest]=(unsigned int)distance;
MotorMoveStatus[MotorTest]=CONST;
MotorSpeedIndex[MotorTest]=0;
MotorStatus[MotorTest]=ON;
}
else
{
if(direction==UP)
MotorFinalStep[MotorTest]= MotorMaxStep[MotorTest];
else
MotorFinalStep[MotorTest]= MotorMinStep[MotorTest];
// moving step is out of range\n\r");
}
MotorStatus[MotorTest]=ON;
}
if(data_flag==OK)
return(DISABLE);
else
return(ENABLE);
}
//pb6--cs33993
//ps6--ck33993
//ps5--so33993
//ps4--si33993
unsigned long mc33993read(void)
{
unsigned long word33993=0;
unsigned char i;
PORTS&=0xbf;
PORTB&=0xbf;
for(i=0;i<24;i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -