📄 motor_dtk.c
字号:
/*********************************************************************************
* Motor.C v1.00 电机驱动主程序 *
* 版权(c) 2004- 北京合众达电子技术有限责任公司 *
* 设计者: 刘宁 *
**********************************************************************************/
#include "SEED-DSK2407.h"
#include "comm.h"
/*电机驱动码*/
/* 全步反转*/
unsigned int motorstepall[48] ={8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1,\
8,2,4,1};
/*全步正转*/
unsigned int motorstepalln[48]={8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2,\
8,1,4,2};
/*半步正转*/
unsigned int motorstephalfn[96] ={8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9,\
8,0xa,2,6,4,5,1,9};
/*半步反转*/
unsigned int motorstephalf[96]={8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa,\
8,9,1,5,4,6,2,0xa};
#define pwm_value 50
#define pwm_max 500
#define FRAMLONGTH 0x104
unsigned int dcmotor_level = 0;
unsigned int dcmotor_count = 0;
unsigned int step_point = 0;
unsigned int step_mode = 0;
unsigned int step_dir = 0;
unsigned int step_half = 0;
unsigned int step_speed = 0;
unsigned int dcmotor_dir = 0;
unsigned int dcmotor_speed = 0;
unsigned int step_count = 0;
unsigned int step_clockwise = 0;/* 初始值为0 ,顺时针 */
unsigned int command_change=0;
unsigned int command_judge=0;
unsigned int recieve_over = 0;
unsigned int recieve_count = 0;
unsigned int recieve_check = 0;
unsigned int recieve[0x208] = {0};
unsigned int frame_data[0x104] = {0};
void blink();
void timer1_set(unsigned int timer1_period);
void timer2_set(unsigned int timer2_period);
void dc_motor_clockwise();
void dc_motor_anticlockwise();
void dc_motor_stop();
void step_motor(unsigned int step);
void step_motor_stop();
/*void sci_set(); */
void SpiInit();
void uart_recieve();
void ledturnon();
void sys_reset(void);
void timer_init();
void main()
{
unsigned int i=0;
unsigned int j=0;
DINT;
/*wait-state*/
WSGR = 0xdb;
/* forbid watchdog*/
*WDCR = 0x68;
/*enable SpI */
*SCSR1 = 0x02c;
*IFR = 0x3f;
*IMR = 0x00;
/*WHEN THE MOTOR PROGRAM RUN ,TURN ON THE LED2*/
*MCRC &= 0xBFFF;
*PFDATDIR |=0x4040;
timer_init();
SpiInit();
/* sci_set(); */
timer1_set(0x1F);
timer2_set(0xFFFF);
EINT;
/*drive the LED blink*/
for(;;)
{
/* pc机通过uart控制电机*/
/* 接收未结束 ,保持原来的命令*/
while(recieve_over == 0)
{
if(command_change ==1)
{
ledturnon();
}
};
recieve_over = 0;
command_judge= frame_data[3];
switch(command_judge)
{
case LEDTURNON: /*0XAB00*/
ledturnon();
command_change =1;
break;
case LED1ON: /*0XAB01*/
LED = 0x01;
command_change =0;
break;
case LED2ON: /*0XAB02*/
LED = 0x02;
command_change =0;
break;
case LED3ON: /*0XAB03*/
LED = 0x04;
command_change =0;
break;
case LED4ON: /*0XAB04*/
LED = 0x08;
command_change =0;
break;
case LEDEND: /*0XAB05*/
LED = 0x00;
command_change =0;
break;
case STPMTRHALT: /*0XAA24*/
*IMR &= 0xFB;
step_count=0;
step_motor_stop();
break;
case STPMTRRUN: /*0XAA25*/
*IMR |= 0x04;
break;
case STPMTRRVS: /*0XAA26*/
if(step_dir==STEP_CLOCKWISE)
{
/* step_motor_stop();*/
step_point=0;
step_count=0;
step_dir=STEP_ANTICLOCK;
}
else
{
/* step_motor_stop(); */
step_point=0;
step_count=0;
step_dir=STEP_CLOCKWISE;
}
break;
case DCMTRRUN: /*0XAA28*/
*IMR |= 0x02;
break;
case DCMTRBRK: /*0XAA39*/
*IMR &= 0xFD;
dc_motor_stop();
break;
case DCMTRSLWDWN: /*0XAA3A*/
*IMR &= 0xFD;
dc_motor_stop();
break;
case DCMTRRVS: /*0XAA3B*/
/* *IMR |= 0x02;*/
if(dcmotor_dir==CLOCKWISE)
{
dcmotor_dir=ANTICLOCKWISE;
}
else
{
dcmotor_dir=CLOCKWISE;
}
break;
case MTRCONFIG: /*0XAA3C*/
/* *IMR &= 0xFD; */
dc_motor_stop();
step_mode = frame_data[4];
step_dir = frame_data[5];
step_half = frame_data[6];
step_speed = frame_data[7];
dcmotor_dir = frame_data[8];
dcmotor_speed = frame_data[9];
dcmotor_count = 0;
dcmotor_level = 0;
step_count=0;
step_point=0;
ledturnon();
/* *IMR |= 0x02; */
break;
case MOTOREND:
*IMR &= 0xF9;
dc_motor_stop();
step_motor_stop();
break;
case SYSRESET:
*IMR &= 0xF9;
timer_init();
dc_motor_stop();
step_motor_stop();
sys_reset();
/*asm(" B 768H ");*/
default:
break;
}
}
}
/*TIMER1 INTERRUPT,DC MOTOR CONTROL */
interrupt void c_int2(void)
{
DINT;
if((*PIVR & 0x27) == 0x27)
{
*EVAIFRA = 0x0080;
}
if(dcmotor_level == 0)
{
dcmotor_count++;
timer1_set(0x1F);
/* 电机方向判断 */
if(dcmotor_dir==CLOCKWISE)
{
dc_motor_clockwise();
}
if(dcmotor_dir==ANTICLOCKWISE)
{
dc_motor_anticlockwise();
}
/* 电平变低 */
if(dcmotor_count==dcmotor_speed)
{
dcmotor_level = 1;
}
}
if(dcmotor_level == 1)
{
if(dcmotor_count<500)
{
dcmotor_count++;
timer1_set(0x1F);
dc_motor_stop();
}
if(dcmotor_count==pwm_max)
{
dcmotor_level = 0;
dcmotor_count = 0;
}
}
*IFR &= 0x0002;
*IMR |= 0x01;
EINT;
}
/*TIMER2 INTERRUPT,STEP MOTOR CONTROL*/
interrupt void c_int3(void)
{
if((*PIVR & 0x2b) == 0x2b)
{
*EVAIFRB = 0x0001;
}
if(step_mode==ROTATE)
{
step_count++;
if(step_count==step_speed)
{
/* 全步正转*/
if(step_dir==STEP_CLOCKWISE && step_half==FULLSTEP)
{
step_motor(motorstepalln[step_point]);
step_point++;
if(step_point==48)
{
step_point=0;
}
}
/* 全步反转 */
if(step_dir==STEP_ANTICLOCK && step_half==FULLSTEP)
{
step_motor(motorstepall[step_point]);
step_point++;
if(step_point==48)
{
step_point=0;
}
}
/* 半步反转 */
if(step_dir==STEP_ANTICLOCK && step_half==HALFSTEP)
{
step_motor(motorstephalfn[step_point]);
step_point++;
if(step_point==96)
{
step_point=0;
}
}
/* 半步正转 */
if(step_dir==STEP_CLOCKWISE && step_half==HALFSTEP)
{
step_motor(motorstephalf[step_point]);
step_point++;
if(step_point==96)
{
step_point=0;
}
}
step_count=0;
}
}
if(step_mode==LOCATE)
{
step_count++;
if(step_count==step_speed)
{
/* 全步正转*/
if(step_dir==STEP_CLOCKWISE && step_half==FULLSTEP)
{
step_motor(motorstepall[step_point]);
step_point++;
if(step_point==step_speed)
{
step_point=0;
*IMR &= 0xFB;
step_motor_stop();
}
}
/* 全步反转 */
if(step_dir==STEP_ANTICLOCK && step_half==FULLSTEP)
{
step_motor(motorstepalln[step_point]);
step_point++;
if(step_point==step_speed)
{
step_point=0;
*IMR &= 0xFB;
step_motor_stop();
}
}
/* 半步反转 */
if(step_dir==STEP_ANTICLOCK && step_half==HALFSTEP)
{
step_motor(motorstephalfn[step_point]);
step_point++;
if(step_point==step_speed)
{
step_point=0;
*IMR &= 0xFB;
step_motor_stop();
}
}
/* 半步正转 */
if(step_dir==STEP_CLOCKWISE && step_half==HALFSTEP)
{
step_motor(motorstephalf[step_point]);
step_point++;
if(step_point==step_speed)
{
step_point=0;
*IMR &= 0xFB;
step_motor_stop();
}
}
step_count=0;
}
}
*IFR &= 0x0004;
EINT;
}
/*SPI INTERRUPT*/
interrupt void c_int1(void)
{
unsigned int i;
if((*PIVR & 0x05) == 0x05)
{
recieve[recieve_count] = *SPIRXBUF;
recieve_count++;
recieve_check=0;
if(recieve_count==FRAMLONGTH)
{
for(i=0;i<FRAMLONGTH;i++)
{
frame_data[i]=recieve[i];
}
for(i=0;i<FRAMLONGTH-1;i++)
{
recieve_check=recieve_check^frame_data[i];
}
if(recieve_check == frame_data[259])
{
recieve_count= 0;
recieve_over = 1;
}
SpiInit();
}
}
*IFR &=0x0001;
/* *SPISTS = 0x80; */
EINT;
}
/*sci set*/
/*void sci_set()
{
*MCRA |= 0x03;
*SCICCR = 0x07; /*loopback mode:0x17 ;normal:0x07 1 stop bit,odd parity,8 char bits*/
/* *SCICTL1 = 0x03;
*SCICTL2 = 0x02;
*SCIHBAUD = 0x01;
*SCILBAUD = 0x03; /*Baud Rate=19200 b/s (40 MHz SYSCLK)*/
/* *SCICTL1 = 0x23;
*IMR |= 0x01;
} */
void timer_init()
{
*IMR &= 0xF9;
*GPTCONA = 0x0000;
*EVAIMRA = 0x0000;
*EVAIMRB = 0x0000;
*EVAIFRA = 0x0080;
*EVAIFRB = 0x0001;
*T1CNT = 0;
*T1CON = 0;
*T2CNT = 0;
*T2CON = 0;
}
/*timer1 set */
void timer1_set(unsigned int timer1_period)
{
*GPTCONA = 0x0000;
*T1CNT = 0;
*T1CON = 0x1740;
*T1PR = timer1_period;
*T1CMPR = 0x3fff;
*EVAIFRA = 0x0080;
*EVAIMRA = 0x0080;
/* *IMR |= 0x0002; */
}
/*timer2 set */
void timer2_set(unsigned int timer2_period)
{
*GPTCONA = 0x0000;
*T2CNT = 0;
*T2CON = 0x1040;
*T2PR = timer2_period;
*T2CMPR = 0x3fff;
*EVAIFRB = 0x0001;
*EVAIMRB = 0x0001;
/* *IMR |= 0x0004;*/
}
void SpiInit(void)
{
*MCRB = 0x3c;
*SPICCR = 0x4f;
*SPICTL = 0x01;
*SPIBRR = 0x31;
*SPIPRI = 0x10;
*SPICCR = 0xcf;
*SPISTS = 0x80;
*IMR |= 0x01;
}
/*dc_motor clockwise turn*/
void dc_motor_clockwise()
{
*MCRA &= 0xCFFF; /*I0PB4,IOPB5*/
*PBDATDIR |= 0x3020;
*PBDATDIR &= 0xFFEF;
}
/*dc_motor anticlockwise turn*/
void dc_motor_anticlockwise()
{
*MCRA &= 0xCFFF;
*PBDATDIR |= 0x3010;
*PBDATDIR &= 0xFFDF;
}
/*dc_motor stop*/
void dc_motor_stop()
{
*MCRA &= 0xCFFF;
*PBDATDIR &= 0xFFCF;
}
/*step_motor run*/
void step_motor(unsigned int step)
{
switch(step)
{
case 8:
*MCRA &= 0xFC3F;
*PADATDIR |= 0xC000;
*PADATDIR &= 0xFF3F;
*PBDATDIR |= 0x0302;
*PBDATDIR &= 0xFFFE;
break;
case 4:
*MCRA &= 0xFC3F;
*PADATDIR |= 0xC000;
*PADATDIR &= 0xFF3F;
*PBDATDIR |= 0x0301;
*PBDATDIR &= 0xFFFD;
break;
case 2:
*MCRA &= 0xFC3F;
*PADATDIR |= 0xC080;
*PADATDIR &= 0xFFBF;
*PBDATDIR |= 0x0300;
*PBDATDIR &= 0xFFFC;
break;
case 1:
*MCRA &= 0xFC3F;
*PADATDIR |= 0xC040;
*PADATDIR &= 0xFF7F;
*PBDATDIR |= 0x0300;
*PBDATDIR &= 0xFFFC;
break;
default:
break;
}
}
/*step motor stop*/
void step_motor_stop()
{
*MCRA &= 0xFC3F;
*PADATDIR |= 0xC000;
*PADATDIR &= 0xFF3F;
*PBDATDIR |= 0x0300;
*PBDATDIR &= 0xFFFC;
}
/*LED BLINK DELAY*/
void DELAY()
{
unsigned int j = 0;
int i = 0;
for(i = 0; i <0xff;i++)
{
for(j = 0;j<0x3ff;j++)
{
asm("NOP");
}
}
}
void ledturnon()
{
LED = 0x01;
DELAY();
LED = 0x02;
DELAY();
LED = 0x04;
DELAY();
LED = 0x08;
DELAY();
}
void sys_reset(void)
{
WSGR = 0;
*WDCR = 0x08;
for(;;){};
/*
asm(" B 768H "); */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -