example_280xepwmstep.c

来自「DSP2808例程。TMS320F2808DSP的各个模块的应用例程」· C语言 代码 · 共 317 行

C
317
字号
// TI File $Revision: /main/6 $
// Checkin $Date: April 8, 2005   15:53:32 $
//###########################################################################
//
// FILE:    Example_280xEpwmDeadBand.c
//
// TITLE:   Check PWM deadband generation
//
// ASSUMPTIONS:
//
//    This program requires the DSP280x header files.  
//
//    Monitor ePWM1 - ePWM3 on an Oscilloscope as described
//    below.
//
//       EPWM1A is on GPIO0
//       EPWM1B is on GPIO1
//
//       EPWM2A is on GPIO2
//       EPWM2B is on GPIO3
//
//       EPWM3A is on GPIO4
//       EPWM3B is on GPIO5
//
//    As supplied, this project is configured for "boot to SARAM" 
//    operation.  The 280x Boot Mode table is shown below.  
//    For information on configuring the boot mode of an eZdsp, 
//    please refer to the documentation included with the eZdsp,  
//
//       Boot      GPIO18     GPIO29    GPIO34
//       Mode      SPICLKA    SCITXDA
//                 SCITXB
//       -------------------------------------
//       Flash       1          1        1
//       SCI-A       1          1        0
//       SPI-A       1          0        1
//       I2C-A       1          0        0
//       ECAN-A      0          1        1        
//       SARAM       0          1        0  <- "boot to SARAM"
//       OTP         0          0        1
//       I/0         0          0        0 
//
//
//
// DESCRIPTION:
//
//    This example configures ePWM1, ePWM2 and ePWM3 for:
//    - Count up/down
//    - Deadband
// 
//    3 Examples are included:
//    * ePWM1: Active low PWMs
//    * ePWM2: Active low complementary PWMs
//    * ePWM3: Active high complementary PWMs
//
//    Each ePWM is configured to interrupt on the 3rd zero event
//    when this happens the deadband is modified such that
//    0 <= DB <= DB_MAX.  That is, the deadband will move up and
//    down between 0 and the maximum value. 
//
//
//    View the EPWM1A/B, EPWM2A/B and EPWM3A/B waveforms 
//    via an oscilloscope
//
//
//###########################################################################
// $TI Release: DSP280x V1.20 $
// $Release Date: September 19, 2005 $
//###########################################################################

#include "DSP280x_Device.h"     // DSP280x Headerfile Include File
#include "DSP280x_Examples.h"   // DSP280x Examples Include File

// Prototype statements for functions found within this file.

interrupt void epwm1_isr(void);
void InitEPwm1(void);
void Disable_PWM(void);
void Enable_PWM(void);
void EnableInterrupt(void);

Uint16 speed 		= 0;
Uint16 motor_cnt 	= 0;
Uint16 motor_timer0	= 0;
Uint16 motor_steptime = 0;
Uint16 motor_dir = 0;

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

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

// For this case just init GPIO pins for ePWM1, ePWM2, ePWM3
// These functions are in the DSP280x_EPwm.c file
   //InitEPwm1Gpio();
   //InitEPwm2Gpio();
   //InitEPwm3Gpio();
   InitEPwm1();
   
// 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 DSP280x_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 DSP280x_DefaultIsr.c.
// This function is found in DSP280x_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.EPWM1_INT = &epwm1_isr;
   //PieVectTable.EPWM2_INT = &epwm2_isr;
   //PieVectTable.EPWM3_INT = &epwm3_isr;      
   EDIS;    // This is needed to disable write to EALLOW protected registers

// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP280x_InitPeripherals.c
// InitPeripherals();  // Not required for this example
   
// Step 5. User specific code, enable interrupts
// Initalize counters:   
	speed = 0;
	if(speed)
	{
		motor_steptime = 9375/speed;
	}
	motor_timer0 = 0;
	motor_cnt = 0;

   EnableInterrupt();
   Enable_PWM();
   EPwm1Regs.CMPA.half.CMPA = 1000; // adjust duty for output EPWM1A

// Step 6. IDLE loop. Just sit and loop forever (optional):
   for(;;)
   {
       asm("          NOP");
	   if(speed)
		{
			motor_steptime = 9375/speed;
		}

   }

} 

interrupt void epwm1_isr(void)
{
	if(speed)
	{		
		if(motor_timer0 ++ >= motor_steptime)
		{
			motor_timer0 = 0;
			if(motor_dir)
			{
				if(motor_cnt-- < 1)
				{
					motor_cnt = 8;
				}
			}
			else
			{
				if(motor_cnt++ > 8)
				{
					motor_cnt = 1;
				}
			}

			switch (motor_cnt)
			{
				case 1:	
						GpioDataRegs.GPACLEAR.bit.GPIO0  = 1;
						GpioDataRegs.GPASET.bit.GPIO3  = 1;
						break;
				case 2:	
						GpioDataRegs.GPASET.bit.GPIO2  = 1;	
						break;
				case 3:	
						GpioDataRegs.GPACLEAR.bit.GPIO3  = 1;	
						break;
				case 4:	
						GpioDataRegs.GPASET.bit.GPIO1  = 1;	
						break;
				case 5:	
						GpioDataRegs.GPACLEAR.bit.GPIO2  = 1;	
						break;
				case 6:	
						GpioDataRegs.GPASET.bit.GPIO0  = 1;
						break;
				case 7:
						GpioDataRegs.GPACLEAR.bit.GPIO1  = 1;
						break;
				case 8:	
						GpioDataRegs.GPASET.bit.GPIO3  = 1;
						break;
				default:		;
			}
			/*if(motor_dir)
			{
				if(motor_cnt ++)
				{
					if(motor_cnt > 7)		// B --> AB
					{
						motor_cnt = 0;
						GpioDataRegs.GPASET.bit.GPIO3  = 1;
					}
					else if(motor_cnt > 6)	// BC --> B
					{
						GpioDataRegs.GPACLEAR.bit.GPIO1  = 1;
					}
					else if(motor_cnt > 5)	// C  --> BC
					{
					GpioDataRegs.GPASET.bit.GPIO2  = 1;
					}
					else if(motor_cnt > 4)	// DC --> C
					{
						GpioDataRegs.GPACLEAR.bit.GPIO0  = 1;
					}
					else if(motor_cnt > 3)	// D  --> DC
					{
						GpioDataRegs.GPASET.bit.GPIO1  = 1;
					}
					else if(motor_cnt > 2)	// AD --> D
					{
						GpioDataRegs.GPACLEAR.bit.GPIO3  = 1;
					}
					else if(motor_cnt > 1)	// A --> AD 
					{
						GpioDataRegs.GPASET.bit.GPIO0  = 1;
					}
				}
				else					// AB --> A
				{
					GpioDataRegs.GPACLEAR.bit.GPIO2  = 1;
					GpioDataRegs.GPASET.bit.GPIO3  = 1;
				}
			}
			else
			{
				if(motor_cnt ++)
				{
					if(motor_cnt > 7)		// D --> DA
					{
						motor_cnt = 0;
						GpioDataRegs.GPASET.bit.GPIO3  = 1;
					}
					else if(motor_cnt > 6)	// CD --> D
					{
						GpioDataRegs.GPACLEAR.bit.GPIO1  = 1;
					}
					else if(motor_cnt > 5)	// C  --> CD
					{
						GpioDataRegs.GPASET.bit.GPIO0  = 1;
					}
					else if(motor_cnt > 4)	// BC --> C
					{
						GpioDataRegs.GPACLEAR.bit.GPIO2  = 1;
					}
					else if(motor_cnt > 3)	// B  --> BC
					{
						GpioDataRegs.GPASET.bit.GPIO1  = 1;
					}
					else if(motor_cnt > 2)	// AB --> B
					{
						GpioDataRegs.GPACLEAR.bit.GPIO3  = 1;
					}
					else if(motor_cnt > 1)	// A --> AB 
					{
						GpioDataRegs.GPASET.bit.GPIO2  = 1;
					}
				}
				else					// DA --> A
				{
					GpioDataRegs.GPACLEAR.bit.GPIO0  = 1;
					GpioDataRegs.GPASET.bit.GPIO3  = 1;
				}
			}*/
		}
	}

   // Clear INT flag for this timer
    EPwm1Regs.ETCLR.bit.INT = 1;
   
   // Acknowledge this interrupt to receive more interrupts from group 3
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;

}



 

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

⌨️ 快捷键说明

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