?? dtk_bpd_motor.c
字號:
/*SEED-DTK BPD */
/*SEED耿升輝*/
/*2004.12*/
#include "DSP28_Device.h"
#include "comm.h"
#include "ext_inf.h"
/*幀長度的設定*/
typedef struct _UartForDec5416{
unsigned int Length;
unsigned int Type;
unsigned int Mutul;
unsigned int Data[DataLongth];
unsigned int Check;
}UartForDec5416, *PuartForDec5416;
PuartForDec5416 precieve =0;
PuartForDec5416 psend=0;
unsigned int uart[0x104] ={0};
unsigned int uarts[0x104] = {0};
unsigned int uartsdata = 0;
unsigned int uarta_recbuff[1040];///*串口A接收緩沖區*/
unsigned int uartb_recbuff[1040];/*串口B接收緩沖區*/
int uart_s=0;
unsigned int sys_statbuff[32];
int uart_recive(void);
int uart_send(void);
unsigned int i,j;
void sys_reset(void);
unsigned int CommandSave=0;
unsigned int DcMotorChangeConv=0;
unsigned int step = 0;
unsigned int steplong = 0;
unsigned int steplongsave = 0;
unsigned int localstep =0;
unsigned int dcmotorrun = 0;
volatile unsigned int dcdircontrl = 0;
unsigned int stepruncode = 0;
unsigned int stepspeed = 10;
unsigned int stepspeedsave = 0;
unsigned int dcruncode = 0;
unsigned int stepnumber = 0;
#define DCMOTORFORWARD 0x2
#define DCMOTORINVESE 0x1
#define DCMOTORBRAKE 0x03
#define DCMOTORHALT 0x0
#define MOTORBUFF 0
#define MPLUSCHANGE 1
#define MPLUSSET 2
#define MPLUSCOUNT 3
#define MRATEBASE 4
#define MOTOPWMC 5
#define MOTORPWMZ 6
/*定義擴展總線存儲器空間頁地址寄存器地址為0x004020*/
volatile unsigned int* p_ceselect=(volatile unsigned int *)0x004020;
volatile unsigned int* p_cntlreg=(volatile unsigned int *)0x004018;
volatile unsigned int* p_stepportaddr=(volatile unsigned int *)0x080001;
volatile unsigned int* p_cntlbaseaddr=(volatile unsigned int *)0x080003;
volatile unsigned int write_data =0;
volatile unsigned int motorset[6] ={0,0,0,10,0,0};
volatile unsigned int motorstatus[10];
PMotorConfig pmotor = 0;
#define SELECTCE3 0x6
#define SELECTCE0 0x0
/*電機驅動碼*/
/*全步正轉*/
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 motorstephalf[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 motorstephalfn[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};
/********************************************************************************/
/* */
/* 函數定義:void _dcmotor_control(uint portdata) */
/* 功 能:系統設置 */
/* */
/* 入口參數: A---- 要寫入的數據 */
/* 出口參數: 無 */
/* 資源使用:AR0 */
/* */
/********************************************************************************/
void dcmotor_control(unsigned int data);
/********************************************************************************/
/* */
/* 函數定義:void _dcmotor_pluswithset(uint portdata) */
/* 功 能:系統設置 */
/* */
/* 入口參數: A---- 要寫入的占空比 */
/* 出口參數: 1、占空比未重設 */
/* 0、占空比已重設 */
/* 資源使用:AR0 */
/* */
/********************************************************************************/
int dcmotor_pluswithset(unsigned int data);
/********************************************************************************/
/* */
/* 函數定義:void _dcmotor_start(uint portdata) */
/* 功 能:系統設置 */
/* */
/* 入口參數: A---- 要寫入的占空比 */
/* 出口參數: 1、占空比未重設 */
/* 0、占空比已重設 */
/* 資源使用:AR0 */
/* */
/********************************************************************************/
void dcmotor_start();
/********************************************************************************/
/* */
/* 函數定義:void _dcmotor_out(uint data) */
/* 功 能:系統設置 */
/* */
/* 入口參數: A---- enable the PWM out */
/* 出口參數: NONe */
/* 資源使用:AR0 */
/* */
/********************************************************************************/
void dcmotor_out(unsigned int data);
/********************************************************************************/
/* */
/* 函數定義:void _stepmotor_set(uint portdata) */
/* 功 能:系統設置 */
/* */
/* 入口參數: A---- 要寫入的拍數 */
/* 出口參數: 無 */
/* 資源使用:AR0 */
/* */
/********************************************************************************/
void stepmotor_set(unsigned int data);
/********************************************************************************/
/* */
/* 函數定義:void _delay(uint portdata) */
/* 功 能:系統設置 */
/* */
/* 入口參數: A---- 要寫入的占空比 */
/* 出口參數: NO */
/* */
/********************************************************************************/
void delay(unsigned int data);
void stepdelay(unsigned int dcmotorrun);
void timer_dcmotor(void);
void change_time_dcmotor(void);
void main(void)
{
/*初始化系統*/
InitSysCtrl();
/*關中斷*/
DINT;
IER = 0x0000;
IFR = 0x0000;
/*初始化PIE中斷*/
InitPieCtrl();
/*初始化PIE中斷矢量表*/
InitPieVectTable();
/*初始化SCIA寄存器*/
InitSci();
for(i= 0; i<0x104;i++)
{
uart[i] = 0x5555;
uarts[i] = 0x5555;
}
for(i=0;i<32;i++)
{
sys_statbuff[i]=0;//系統標志buff清0
}
CommandSave=0;
#if SCIA_INT
/*設置中斷服務程序入口地址*/
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TXAINT = &SCITXINTA_ISR;
PieVectTable.RXAINT = &SCIRXINTA_ISR;
EDIS; // This is needed to disable write to EALLOW protected registers
/*開中斷*/
IER |= M_INT9;
#endif
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
LED1_ON;
*LED3=0;
/*實驗主控程序*/
psend = (PuartForDec5416)(&uarts[0]);
precieve = (PuartForDec5416)(&uart[0]);
pmotor = (PMotorConfig)(&motorset[0]);
/*通知主機,系統準備好*/
psend->Length = FRAMLONGTH;
psend->Type = UARTCOMMAND;
psend->Data[0] = INITOVER;
psend->Mutul = UARTCONT;
uart_send();
for(;;)
{
uart_s =uart_recive();
/* 返回值 0:接受完成 */
/* 1:數據未準備好 */
/* 3: 較驗出錯 */
/*接收從主機的數據*/
precieve = (PuartForDec5416)(&uart[0]);
switch(precieve->Data[0])
{
/*電機設置*/
case MTRCONFIG:
for( i = 1; i<7 ; i++)
{
motorset[i-1] = precieve->Data[i];
}
precieve->Data[0] = 0;
if(dcruncode == 1)
{
precieve->Data[0] = DCMTRRUN;
break;
}
if(stepruncode == STPMTRRUN)
{
precieve->Data[0] = STPMTRRUN;
}
break;
/*直流電機運行*/
case DCMTRRUN:
/*設置電機的工作方式*/
if(pmotor->DcDir == CLOCKWISE)
{
dcdircontrl = 1;
}
else
{
dcdircontrl = 2;
}
write_data=dcdircontrl;
*p_ceselect = SELECTCE3;//打開ce3空間
*p_cntlbaseaddr=write_data;
/*設置電機的轉速*/
motorstatus[MPLUSSET]=pmotor->DcSpd*0.7+150;
motorstatus[MPLUSCOUNT]=pmotor->DcSpd*0.7+150;
motorstatus[MOTORPWMZ]=0;
/*啟動電機*/
motorstatus[MRATEBASE]=500;
motorstatus[MOTOPWMC]=500;
*p_cntlreg=3;//cntl0=1
/*使能電機*/
dcmotorrun = 1;
precieve->Data[0] = 0;
if(stepruncode == STPMTRRUN)
{
precieve->Data[0] = STPMTRRUN;
}
dcruncode = 1;
break;
/*直流電機反向*/
case DCMTRRVS:
/*設置電機的工作方式*/
if(pmotor->DcDir == CLOCKWISE)
{
pmotor->DcDir = ANTICLOCKWISE;
}
else
{
pmotor->DcDir = CLOCKWISE;
}
precieve->Data[0] = DCMTRRUN;
break;
/*直流電機剎車*/ //命令有錯位,這里是反向命令。
case DCMTRBRK:
/*設置電機的工作方式*/
if(pmotor->DcDir == CLOCKWISE)
{
pmotor->DcDir = ANTICLOCKWISE;
}
else
{
pmotor->DcDir = CLOCKWISE;
}
precieve->Data[0] = DCMTRRUN;
break;
/*直流電機停*/
case DCMTRSLWDWN:
write_data=DCMOTORHALT;
*p_ceselect = SELECTCE3;//打開ce3空間
*p_cntlbaseaddr=write_data;
dcmotorrun = 0;
dcruncode =0;
precieve->Data[0] = 0;
break;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -