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

📄 dtk_bpd_motor.c

📁 dsp2812的电机调速例子程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*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 + -