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

📄 samotor.c

📁 利用DSP进行直流电机控制的程序
💻 C
字号:


#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"   // DSP281x Examples Include File

// Prototype statements for functions found within this file.
interrupt void cpu_timer0_isr(void);
void Delay(unsigned int nTime);

void Gpio_select(void);
void error(int);
void program_stop();
void Gpio_PortA(void);
void Gpio_PortB(void);
void Gpio_PortF(void);
void Gpio_PortDEG(void);
char ConvertScanToChar(unsigned char cScanCode);


#define T46uS		0x0d40
#define SCANCODE_0 0x70
#define SCANCODE_1 0x69
#define SCANCODE_2 0x72
#define SCANCODE_3 0x7A
#define SCANCODE_4 0x6B
#define SCANCODE_5 0x73
#define SCANCODE_6 0x74
#define SCANCODE_7 0x6C
#define SCANCODE_8 0x75
#define SCANCODE_9 0x7D
#define SCANCODE_Del 0x49
#define SCANCODE_Enter 0x5A
#define SCANCODE_Plus 0x79
#define SCANCODE_Minus 0x7B
#define SCANCODE_Mult 0x7C
#define SCANCODE_Divid 0x4A
#define SCANCODE_Num 0x77

Uint16 var1 = 0;
Uint16 var2 = 0;
Uint16 var3 = 0;
Uint16 test_count = 0;
Uint16 Test_flag = 0;
Uint16 Test_var  = 0;
Uint16 Test_status[32];

Uint16 PASS_flag = 0;
unsigned int uWork;

int jishu=0;
unsigned int uWork,nCount;
unsigned int pwm1[8]={ 0x8e,0x8c,0x8d,0x89,0x8b,0x83,0x87,0x86 };
unsigned int pwm2[8]={ 0x86,0x87,0x83,0x8b,0x89,0x8d,0x8c,0x8e };
int nAddStep,nStep;

void main(void)
{   
	int nCount=0;
    char cKey;
	unsigned int nScanCode;
	

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
   InitSysCtrl();

// Step 2. Initalize GPIO: 
// This example function is found in the DSP281x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example  


// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts 
   DINT;

// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.  
// This function is found in the DSP281x_PieCtrl.c file.
   InitPieCtrl();
   
// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt 
// Service Routines (ISR).  
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP281x_DefaultIsr.c.
// This function is found in DSP281x_PieVect.c.
   InitPieVectTable();

// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.  
   EALLOW;  // This is needed to write to EALLOW protected registers
   PieVectTable.TINT0 = &cpu_timer0_isr;
   EDIS;    // This is needed to disable write to EALLOW protected registers

// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
// InitPeripherals(); // Not required for this example

   /////InitCpuTimers();   // For this example, only initialize the Cpu Timers
   	CpuTimer0.RegsAddr = &CpuTimer0Regs;
	// Initialize timer period to maximum:	
	////CpuTimer0Regs.PRD.all  = T46uS*9*6/5;
	CpuTimer0Regs.PRD.all  = 0x8fff;

	// Initialize pre-scale counter to divide by 1 (SYSCLKOUT):	
	CpuTimer0Regs.TPR.all  = 0;
	CpuTimer0Regs.TIM.all  = 0;
	CpuTimer0Regs.TPRH.all = 0;
	// Make sure timer is stopped:
	CpuTimer0Regs.TCR.bit.TSS = 1;
	CpuTimer0Regs.TCR.bit.SOFT = 1;
	CpuTimer0Regs.TCR.bit.FREE = 1;
	// Reload all counter register with period value:
	CpuTimer0Regs.TCR.bit.TRB = 1;
	CpuTimer0Regs.TCR.bit.TIE = 1;
	// Reset interrupt counters:
	CpuTimer0.InterruptCount = 0;	         


    //StartCpuTimer0();

// Step 5. User specific code, enable interrupts:


// Enable CPU INT1 which is connected to CPU-Timer 0:
   IER |= M_INT1;

// Enable TINT0 in the PIE: Group 1 interrupt 7
   PieCtrlRegs.PIEIER1.bit.INTx7 = 1;

// Enable global Interrupts and higher priority real-time debug events:
   EINT;   // Enable Global interrupt INTM
   ERTM;   // Enable Global realtime interrupt DBGM
   nStep=0; nAddStep=1;
   *(int *)0x108000=0x80;		// 初始化ICETEK-CTR
   *(int *)0x108000=0x0;
   *(int *)0x108000=0x80;
   *(int *)0x108007=0;			// 关闭东西方向的交通灯
   *(int *)0x108007=0x40;	// 关闭南北方向的交通灯
   
   *(int *)0x108007=0xc2;//开步进电机
   uWork=*(int *)0x108002;
   StartCpuTimer0();
   while (1)
	{
	 if ( nCount==0 )
		{
			nScanCode=*(int *)0x108001;
			nScanCode&=0x0ff;
			uWork=*(int *)0x108002;
			//*(int *)0x108007=0x0c0;
			if ( nScanCode!=0 )
			{
				if ( nScanCode==9 )	break;
				else
				{
					cKey=nScanCode;
				}
				cKey=nScanCode;
				if ( cKey!=0 )
				{
					if ( cKey==4 )	nAddStep=1;
					else if ( cKey==6 )	nAddStep=-1;
				}
			}
		}
	}

} 


interrupt void cpu_timer0_isr(void)
{  
   CpuTimer0.InterruptCount++;

   // Acknowledge this interrupt to receive more interrupts from group 1
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
   CpuTimer0Regs.TCR.bit.TIF = 1;
   CpuTimer0Regs.TCR.bit.TRB = 1;
   *(int *)0x108007=pwm2[nStep];
   nStep+=nAddStep;
   if ( nStep<0 )	nStep=7;
   else if ( nStep>7 )	nStep=0;
   nCount++;nCount%=64;
   StopCpuTimer0();
   Delay(6);
   *(int *)0xc0000 ^=0x1;
   StartCpuTimer0();
}

void Delay(unsigned int nDelay)
{
	int ii,jj,kk=0;
	for ( ii=0;ii<nDelay;ii++ )
	{
		for ( jj=0;jj<64;jj++ )
		{
			
			kk++;
		}
	}
}

void Gpio_PortA(void)
{    

// GPIO Test #2: 
// Configure Upper 8 bits of Port as inputs and lower 8 bits as outputs    
// Loop back bits [7:0] to bits [15:8]
// Don't set any input qualifier
 
    var1= 0x0000;       // sets GPIO Muxs as I/Os
    var2= 0x00FF;       // sets GPIO 15-8 DIR as inputs, 7-0 DIR as outputs
    var3= 0x0000;       // Don't set any input qualifier
    
    Gpio_select();    
    
    test_count = 0;
    Test_status[Test_var] = 0x0002;
    Test_var++;
    
    Test_status[Test_var] = 0xD0BE;      // Set the default value of status 
                                         // to "PASSED"
    GpioDataRegs.GPACLEAR.all = 0x00FF;  // Test Clear
    asm(" RPT #5 ||NOP");                             
    GpioDataRegs.GPASET.bit.GPIOA3=1;
 
}

void Gpio_PortB(void)
{    

// GPIO Test #2: 
// Configure Upper 8 bits of Port as inputs and lower 8 bits as outputs    
// Loop back bits [7:0] to bits [15:8]
// Don't set any input qualifier
 
    var1= 0x0000;       // sets GPIO Muxs as I/Os
    var2= 0x00FF;       // sets GPIO 15-8 DIR as inputs, 7-0 DIR as outputs
    var3= 0x0000;       // Don't set any input qualifier
    
    Gpio_select();    
    
    test_count = 0;
    Test_status[Test_var] = 0x0002;
    Test_var++;
    
    Test_status[Test_var] = 0xD0BE;      // Set the default value of status 
                                         // to "PASSED"
    GpioDataRegs.GPBCLEAR.all = 0x00FF;  // Test Clear
    asm(" RPT #5 ||NOP");                             
    GpioDataRegs.GPBSET.bit.GPIOB4=1;
 
}

void Gpio_select(void)
{
     EALLOW;
     
    GpioMuxRegs.GPAMUX.all=var1;   // Configure MUXs as digital I/Os or
    GpioMuxRegs.GPBMUX.all=var1;   // peripheral I/Os
    GpioMuxRegs.GPDMUX.all=var1;
    GpioMuxRegs.GPFMUX.all=var1;         
    GpioMuxRegs.GPEMUX.all=var1; 
    GpioMuxRegs.GPGMUX.all=var1;
                                        
    GpioMuxRegs.GPADIR.all=var2;   // GPIO PORTs  as output
    GpioMuxRegs.GPBDIR.all=var2;   // GPIO DIR select GPIOs as output 
    GpioMuxRegs.GPDDIR.all=var2;
    GpioMuxRegs.GPEDIR.all=var2;        
    GpioMuxRegs.GPFDIR.all=var2; 
    GpioMuxRegs.GPGDIR.all=var2;

    GpioMuxRegs.GPAQUAL.all=var3;  // Set GPIO input qualifier values
    GpioMuxRegs.GPBQUAL.all=var3;   
    GpioMuxRegs.GPDQUAL.all=var3;
    GpioMuxRegs.GPEQUAL.all=var3;
 
    EDIS;
     
}     

char ConvertScanToChar(unsigned char cScanCode)
{
	char cReturn;
	
	cReturn=0;
	switch ( cScanCode )
	{
		case SCANCODE_0: cReturn='0'; break;
		case SCANCODE_1: cReturn='1'; break;
		case SCANCODE_2: cReturn='2'; break;
		case SCANCODE_3: cReturn='3'; break;
		case SCANCODE_4: cReturn='4'; break;
		case SCANCODE_5: cReturn='5'; break;
		case SCANCODE_6: cReturn='6'; break;
		case SCANCODE_7: cReturn='7'; break;
		case SCANCODE_8: cReturn='8'; break;
		case SCANCODE_9: cReturn='9'; break;
//		case SCANCODE_F1: 
		case SCANCODE_Plus: cReturn='+'; break;
//		case SCANCODE_F2: 
		case SCANCODE_Minus: cReturn='-'; break;
	}
	 
	return cReturn;
}


//===========================================================================
// No more.
//========================================

⌨️ 快捷键说明

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