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

📄 5502_motor.c

📁 TMS320VC5502的直流电机驱动程序验证
💻 C
字号:
/******************************************************************************/
/*  Copyright 2004 by SEED Electronic Technology LTD.                         */
/*  All rights reserved. SEED Electronic Technology LTD.                      */
/*  Restricted rights to use, duplicate or disclose this code are             */
/*  granted through contract.                                                 */
/*	MODULE NAME... DEC5502_IO    							    		  	  */
/*	FILENAME...... 5502_MOTOR.c												  */
/*	PROJECT....... Program and configure operation on IO		  			  */
/*	DESCRIPTION:This is an example of programming and configure IO of DEC5502 */ 
/*  作者:韩敬                                                                */    
/*	版本:1.0                                                                 */
/*  时间:2006-08-18                                                          */
/******************************************************************************/

#include <csl.h>
#include <csl_pll.h>
#include <csl_emif.h>
#include <csl_chip.h>
#include <stdio.h>

#include "comm.h"
#include <stdio.h>
#include <csl_irq.h>
#include <csl_gpt.h>

/*FLASH的EMIF设置*/
EMIF_Config MyEmifConfig = {
EMIF_GBLCTL1_RMK(					// EMIF Global Control Register 1
  EMIF_GBLCTL1_NOHOLD_HOLD_ENABLED,	// Hold enable
  EMIF_GBLCTL2_EK2HZ_HIGHZ,			// EMIF_GBLCTL1_EK1HZ_EK1ENHigh-Z control
  EMIF_GBLCTL1_EK1EN_ENABLED		// ECLKOUT1 Enable
  ),
EMIF_GBLCTL2_RMK(					// EMIF Global Control Register 2
  EMIF_GBLCTL2_EK2RATE_1XCLK,		// ECLKOUT2 Rate
  EMIF_GBLCTL2_EK2HZ_HIGHZ,			// EMIF_GBLCTL2_EK2HZ_EK2ENEK2HZ = 0, ECLKOUT2 is driven with value specified by EKnEN during
  EMIF_GBLCTL2_EK2EN_DISABLED		// ECLKOUT2 Enable (enabled by default)
  ), 
EMIF_CE1CTL1_RMK(					// CE1 Space Control Register 1
  EMIF_CE1CTL1_TA_OF(3),			// Turn-Around time
  EMIF_CE1CTL1_READ_STROBE_OF(6),	// Read strobe width
  EMIF_CE1CTL1_MTYPE_16BIT_ASYNC,	// Access type
  EMIF_CE1CTL1_WRITE_HOLD_MSB_LOW,	// Write hold width MSB bitHIGH
  EMIF_CE1CTL1_READ_HOLD_OF(3)		// Read hold width
  ),
EMIF_CE1CTL2_RMK(					// CE1 Space Control Register 2
  EMIF_CE1CTL2_WRITE_SETUP_OF(4),	// Write setup width
  EMIF_CE1CTL2_WRITE_STROBE_OF(10),	// Write strobe width
  EMIF_CE1CTL2_WRITE_HOLD_OF(2),	// Write hold width
  EMIF_CE1CTL2_READ_SETUP_OF(2)		// Read setup width
  ),
EMIF_CE0CTL1_RMK(					// CE0 Space Control Register 1
  EMIF_CE0CTL1_TA_DEFAULT,
  EMIF_CE0CTL1_READ_STROBE_DEFAULT,
  EMIF_CE0CTL1_MTYPE_DEFAULT,
  EMIF_CE0CTL1_WRITE_HOLD_MSB_DEFAULT,
  EMIF_CE0CTL1_READ_HOLD_DEFAULT
  ),
EMIF_CE0CTL2_RMK(					// CE0 Space Control Register 2
  EMIF_CE0CTL2_WRITE_SETUP_DEFAULT,
  EMIF_CE0CTL2_WRITE_STROBE_DEFAULT,
  EMIF_CE0CTL2_WRITE_HOLD_DEFAULT,
  EMIF_CE0CTL2_READ_SETUP_DEFAULT
  ),
EMIF_CE2CTL1_RMK(					// CE2 Space Control Register 1
  EMIF_CE2CTL1_TA_DEFAULT,			// Not use for SDRAM (asynchronous memory types only)
  EMIF_CE2CTL1_READ_STROBE_DEFAULT,	// Read strobe width
  EMIF_CE2CTL1_MTYPE_32BIT_SDRAM,	// 32-bit-wide SDRAM
  EMIF_CE2CTL1_WRITE_HOLD_DEFAULT,	// Write hold width
  EMIF_CE2CTL1_READ_HOLD_DEFAULT	// Read hold width
  ),
EMIF_CE2CTL2_RMK(					// CE2 Space Control Register 2
  EMIF_CE2CTL2_WRITE_SETUP_DEFAULT,	// Write setup width
  EMIF_CE2CTL2_WRITE_STROBE_DEFAULT,// Write strobe width
  EMIF_CE2CTL2_WRITE_HOLD_DEFAULT,	// Write hold width
  EMIF_CE2CTL2_READ_SETUP_DEFAULT	// Read setup width
  ),
EMIF_CE3CTL1_RMK(					// CE3 Space Control Register 1
  EMIF_CE3CTL1_TA_OF(3),			// Turn-Around time
  EMIF_CE3CTL1_READ_STROBE_OF(6),	// Read strobe width
  EMIF_CE3CTL1_MTYPE_16BIT_ASYNC,	// Access type
  EMIF_CE3CTL1_WRITE_HOLD_MSB_LOW,	// Write hold width MSB bitHIGH
  EMIF_CE3CTL1_READ_HOLD_OF(3)	
  ),
EMIF_CE3CTL2_RMK(					// CE3 Space Control Register 2
  EMIF_CE3CTL2_WRITE_SETUP_OF(4),	// Write setup width
  EMIF_CE3CTL2_WRITE_STROBE_OF(10),	// Write strobe width
  EMIF_CE3CTL2_WRITE_HOLD_OF(2),	// Write hold width
  EMIF_CE3CTL2_READ_SETUP_OF(2)		// Read setup width
  ),
EMIF_SDCTL1_RMK(					// SDRAM Control Register 1
  EMIF_SDCTL1_TRC_OF(6),			// Specifies tRC value of the SDRAM in EMIF clock cycles.
  EMIF_SDCTL1_SLFRFR_DISABLED		// Auto-refresh mode
  ),
EMIF_SDCTL2_RMK(					// SDRAM Control Register 2
  0x11,								// 4 banks,11 row address, 8 column address
  EMIF_SDCTL2_RFEN_ENABLED,			// Refresh enabled
  EMIF_SDCTL2_INIT_INIT_SDRAM,
  EMIF_SDCTL2_TRCD_OF(1),			// Specifies tRCD value of the SDRAM in EMIF clock cycles
  EMIF_SDCTL2_TRP_OF(1)				// Specifies tRP value of the SDRAM in EMIF clock cycles
  ),
0x61B,								// SDRAM Refresh Control Register 1
0x0300,								// SDRAM Refresh Control Register 2
EMIF_SDEXT1_RMK(					// SDRAM Extension Register 1
  EMIF_SDEXT1_R2WDQM_1CYCLE,
  EMIF_SDEXT1_RD2WR_3CYCLES,
  EMIF_SDEXT1_RD2DEAC_1CYCLE,
  EMIF_SDEXT1_RD2RD_1CYCLE,
  EMIF_SDEXT1_THZP_OF(1),			// tPROZ2=2
  EMIF_SDEXT1_TWR_OF(0),			//
  EMIF_SDEXT1_TRRD_2CYCLES,
  EMIF_SDEXT1_TRAS_OF(4),
  EMIF_SDEXT1_TCL_2CYCLES
  ),
EMIF_SDEXT2_RMK(					// SDRAM Extension Register 2
  EMIF_SDEXT2_WR2RD_0CYCLES,
  EMIF_SDEXT2_WR2DEAC_1CYCLE,
  0,
  EMIF_SDEXT2_R2WDQM_1CYCLE
  ),
EMIF_CE1SEC1_DEFAULT,				// CE1 Secondary Control Register 1
EMIF_CE0SEC1_DEFAULT,				// CE0 Secondary Control Register 1
EMIF_CE2SEC1_DEFAULT,				// CE2 Secondary Control Register 1
EMIF_CE3SEC1_DEFAULT,				// CE3 Secondary Control Register 1
EMIF_CESCR_DEFAULT					// CE Size Control Register								
  };

/* Define and initialize the GPT module configuration structure  */
GPT_Config  MyGptConfig = {
0, 							//Emulation management register
0,		 					//GPIO interrupt control register
0,	 						//GPIO enable register
0,		 					//GPIO direction register
0,		 					//GPIO data register
//0xB9EF, 					//Timer period register 1
//0x05F5,	 				//Timer period register 2
//0xA300,                     //Timer period register 1, 1s的period register=1/(1/(3*10^8MHz))=3*10^8=0x11e1a300 
//0x11E1,                     //Timer period register 2
//0x93e0,                      //Timer period register 1, 0.001s的period register=0.001/(1/(3*10^8MHz))=3*10^5=0x493e0   
//0x0004,                      //Timer period register 2
0x7fff,                      //Timer period register 1, 
0x0000,                      //Timer period register 2
0,							//Timer period register 3
0, 							//Timer period register 4
GPT_GPTCTL1_RMK(			//Timer control register 1
  GPT_GPTCTL1_TIEN_NOT_GATED,
  GPT_GPTCTL1_CLKSRC_VBUS,  //internal
  GPT_GPTCTL1_ENAMODE_CONTINUOUS,
  GPT_GPTCTL1_PWID_INACTIVE_1CYCLE,//TSTAT goes inactive one timer clock cycle after the timer counter reaches the
                                   //timer period.
  GPT_GPTCTL1_CP_CLOCK_MODE,
  GPT_GPTCTL1_INVIN_DONT_INVERT_OUTPUT,//noninverted
  GPT_GPTCTL1_INVOUT_DONT_INVERT_OUTPUT//noninverted
  ), 			
GPT_GPTCTL2_RMK(			//Timer control register 2
  GPT_GPTCTL2_TIEN_NOT_GATED,
  GPT_GPTCTL2_CLKSRC_VBUS,
  GPT_GPTCTL2_ENAMODE_CONTINUOUS,
  GPT_GPTCTL2_PWID_INACTIVE_1CYCLE,
  GPT_GPTCTL2_CP_CLOCK_MODE,
  GPT_GPTCTL2_INVIN_DONT_INVERT_OUTPUT,
  GPT_GPTCTL2_INVOUT_DONT_INVERT_OUTPUT
  ), 			
GPT_GPTGCTL1_RMK(			//Global timer control register
  GPT_GPTGCTL1_PSC34_DEFAULT,
  GPT_GPTGCTL1_TIMMODE_DEFAULT,//Timer is in 64-bit general-purpose timer mode.
  GPT_GPTGCTL1_TIM34RS_NOT_IN_RESET,//Timer 3:4 is not in reset. Timer 3:4 can be used as a 32-bit timer.
  GPT_GPTGCTL1_TIM12RS_NOT_IN_RESET //Timer 1:2 is not in reset. Timer 1:2 can be used as a 32-bit timer.
  ) 	
};


volatile unsigned char* DEL2 = (volatile unsigned char *)0x600003;//直流电机
volatile unsigned char* DECCTL = (volatile unsigned char *)0x280001;//控制寄存器

#define  DCMOTORBRAKE   0x03
#define  DCMOTORHALT	0x0
#define  MPLUSSET       2
#define  MPLUSCOUNT     3
#define  MRATEBASE      4
#define  MOTOPWMC       5
#define  MOTORPWMZ      6
#define  MPLUSCHANGE    1//JIA
#define  MOTORBUFF      0

void delay(int period);
void dcmotor_out(unsigned int data);

volatile unsigned char * MO_DECCTL = (volatile unsigned char *)0x280001;//控制寄?
volatile unsigned int motorset[2] ={0,500};
//motorset[]  bit1-直流电机速度输入<500; bit0-直流电机运转方向:顺时针-0x0,逆时针-0x1; 
volatile unsigned int motorstatus[10];//MPLUSSET=2,MPLUSCOUNT=3,MOTORPWMZ=6,MRATEBASE=4,MOTOPWMC=5

unsigned int dcmotorrun = 0;
volatile unsigned int dcdircontrl = 0;
unsigned int dcruncode = 0;


#define TESTCOMMAND 2
/*1为电机配置;
2为直流电机运行;
3为直流电机反向运行;
4为直流电机停止;
5为系统复位*/

Uint32 TestCommand;

/* Function/ISR prototypes */
interrupt void Timer1Isr(void);
/* Reference start of interrupt vector table   */
/* This symbol is defined in file, vectors.s55 */
extern void VECSTART(void);

/* Create a TIMER_Handle object for use with TIMER_open */
GPT_Handle 	 hGpt;
Uint16 EventId1;        // 定时器1所对应的事件ID号 
Uint16 i = 0;

main()
{
    /* Initialize CSL library - This is REQUIRED !!! */	
    CSL_init();
    
    /* PLL configuration structure used to set up PLL interface */
	// 主频为300Mhz
    PLL_setFreq(1, 0xF, 0, 1, 3, 3, 0);
    
    /*EMIF为全EMIF接口*/
    CHIP_RSET(XBSR,0x0001);
    
    /*初始化DSP的外部EMIF*/
    EMIF_config(&MyEmifConfig); 
    *DECCTL=0x40;
    
	/* Set IVPH/IVPD to start of interrupt vector table */
	IRQ_setVecs((Uint32)(&VECSTART));
	 
	/* Temporarily disable all maskable interrupts */
	IRQ_globalDisable();   
	
	/* Open Timer 1, set registers to power on defaults */
	/* And return handle of Timer 1 */
	hGpt = GPT_open(GPT_DEV1, GPT_OPEN_RESET);
	/* Get Event Id associated with Timer 1, for use with */
	/* CSL interrupt enable functions.                    */         

	EventId1=GPT_getEventId(hGpt);
	
	/* Clear any pending Timer interrupts */
	IRQ_clear(EventId1);
	
	/* Place interrupt service routine address at */
	/* associated vector location */
	IRQ_plug(EventId1,&Timer1Isr);
	
	/* Write configuration structure values to Timer control regs */ 
	GPT_config(hGpt, &MyGptConfig); 
	
	/* Enable Timer interrupt */
    	IRQ_enable(EventId1);
	
	/* Enable all maskable interrupts */
	IRQ_globalEnable();      
	
	/* Start Timer */
	GPT_start(hGpt);
    
#if TESTCOMMAND==1
    TestCommand =MTRCONFIG;//电机设置0xAA3c
#endif
#if TESTCOMMAND==2
	TestCommand =DCMTRRUN;//直流电机运行0xAA28
#endif
#if TESTCOMMAND==3
	TestCommand =DCMTRRVS;//直流电机反向0xAA3B
#endif
#if TESTCOMMAND==4
	TestCommand =DCMTRBRK;//直流电机刹车0xAA39
#endif
#if TESTCOMMAND==5
	TestCommand =SYSRESET;//系统复位0xAA05
#endif
	
	for(;;)
	{
		switch(TestCommand)
		{
			/*电机设置*/
			case MTRCONFIG:
				TestCommand=0;  //相当于初始化=0,实际下面的程序中TestCommand真正的赋值只有DCMTRRU
				if(dcruncode == 1)  //dcruncode控制直流电机的运行或停止标志位,运行=1,停止=0  
				{
					TestCommand=DCMTRRUN;
					break;	
				}
				break;
			
			/*直流电机运行*/
			case DCMTRRUN:
				/*设置电机的工作方式*/
				if(motorset[0] == CLOCKWISE)
				{
					dcdircontrl = 1;	
				}
				else
				{
					dcdircontrl = 2;
				}
				motorstatus[MPLUSCHANGE]=0;//dcmotor_control
				*DEL2=dcdircontrl;//dcmotor_control
				delay(1000);
				
				/*设置电机的转速*/
				
				if(motorstatus[MPLUSCHANGE]==0)
				{
					motorstatus[MPLUSSET]=motorset[1]*0.7+150;  //motorset[5]的值决定直流电机的转速
					motorstatus[MPLUSCOUNT]=motorset[1]*0.7+150;  //motorset[5]的值决定直流电机的转速
				    motorstatus[MOTORPWMZ]=0;
				}      
				/*启动电机*/			
				
    		    motorstatus[MRATEBASE]=500;  //MRATEBASE=4  	
				motorstatus[MOTOPWMC]=500;  //MOTOPWMC=5 
    		    
    		    *MO_DECCTL=0x41;
    		    //CHIP_FSET(ST1_55,XF,1);	/* 开直流电机 */
			    delay(1000);  
				/*使能电机*/
				dcmotorrun = 1;
				TestCommand=0;
				break;
			
			/*直流电机反向*/	
			case DCMTRRVS:
				/*设置电机的工作方式*/
				if(motorset[0] == CLOCKWISE)
				{
					motorset[0] = ANTICLOCKWISE;
					if(motorset[1]<300)
					{
						motorset[1]=motorset[1]+50;
					}
				}
				else
				{
					motorset[0] = CLOCKWISE;
				}
				TestCommand=DCMTRRUN;
				break;					
			/*直流电机刹车*/		
			case DCMTRBRK:
				motorstatus[MPLUSCHANGE]=0;
				*DEL2=DCMOTORBRAKE;
				dcmotorrun = 0;
				dcruncode =0;
				TestCommand=0;
				break;	

		
			/*系统复位*/
			case SYSRESET:
				motorstatus[MPLUSCHANGE]=0;
				*DEL2=DCMOTORBRAKE;
				TestCommand=0;
			
				break;
			default:
				break;
			
		}	
	
		/*直流电机输出*/
		dcmotor_out(dcmotorrun);		
	}
}



void dcmotor_out(unsigned int data)
{
	if(data==0)
	{
		*MO_DECCTL=0x40;;	/* 关直流电机 */	
	}
	else 
	{
		if(motorstatus[MOTORPWMZ]==0)
		{
			*MO_DECCTL=0x41;	/*PWM波置高电平 */		
		}
		else
		{
			*MO_DECCTL=0x40;	/*PWM波置低电平 */	
		}
	}

}


void delay(int period)
{
    int i, j;
    
    for(i=0; i<period; i++)
    {
        for(j=0; j<0x1000; j++);
    }
}

/*定时器1的中断程序*/
interrupt void Timer1Isr(void)
{
	motorstatus[3]--;  //从此开始到IF语句结束为_timer_dcmotor to pwm_sub
	if(motorstatus[3]==0)
	{
		motorstatus[6]=1;  //在dcmotor_out中将PWM波从高电平置低电平
	}
	motorstatus[5]--;  //从此开始到IF语句结束为pwm_sub
	if(motorstatus[5]==0)
	{
		motorstatus[3]=motorstatus[2];//给motorstatus[3]赋初值
		motorstatus[5]=motorstatus[4];//给motorstatus[5]赋初值
		motorstatus[6]=0;  //在dcmotor_out中将PWM波从低电平置高电平
	}		  	
}
/******************************************************************************\
* End of 5502_MOTOR.c
\******************************************************************************/

⌨️ 快捷键说明

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