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

📄 mc_dev_board.c

📁 采用dspic30f2010的变频器核心驱动程序,程序可以实现正转/反转,频率设定,保护等
💻 C
📖 第 1 页 / 共 2 页
字号:
				PDC1 = PTPER - Speed;
				PDC2 = PTPER + Speed;
				}
			else
				{
				PDC1 = PTPER + Speed;
				PDC2 = PTPER - Speed;
				}
			}     // end if(Mode.BDC)
		
*/				
		// This is the main loop code that responds to a fault event
		// signalled by the fault ISR.  Here, we just turn on a status
		// LED to let the user know that the fault occurred.
		// The fault condition is reset when Button 1 is pressed.
		if(Flags.PWMFault)
			{
			LED1 = 1;
			}
		
		Flags.MediumEvent = 0;
		}		//end if(MediumEvent)
	
	//-----------------------------------------------------------------
	// Slow event handler executes every 100msec
	//-----------------------------------------------------------------
	if(Flags.SlowEvent)
		{
		// These statements check to see if any of the buttons are pressed.
		// If so, a software flag is set so the button press can be debounced.
		if(!BUTTON1)	Flags.Button1 = 1;
		if(!BUTTON2)	Flags.Button2 = 1;
//		if(BUTTON3)	Flags.Button3 = 1;
		if(!BUTTON4)	Flags.Button4 = 1;
	
		// Button #1 resets a fault event.
		// If button #1 is pressed, a reset signal will be sent to the
		// power module to clear any fault LEDs that are lit.  Also, 
		// the PWM channels are re-enabled.
			
		if(Flags.Button1)
			{
			// Wait until the button is released before doing anything.
			if(BUTTON1)
				{
				if(Flags.PWMFault)
					{
					// Reset the speed variable.  Save the old speed for use
					// in the restart profile.
					OldSpeed = Speed;
					Speed = 0;
					if(Mode.BDC)
						{
						// Set the duty cycles back to 50%.
						PDC1 = PTPER;
						PDC2 = PTPER;
						PDC3 = PTPER;
						}
					else
						{
						PDC1 = 0;
						PDC2 = 0;
						PDC3 = 0;
						}
						
					Flags.Restart = 1;
					// Turn the PWM output back on that was disabled in the FLTA ISR		
					OVDCON = 0x3F00;
					// Turn off the status LED
					LED1 = 0;
					// Reset the power module.
//					FAULT_RESET = 1;
					Nop();
					Nop();
					Nop();
//					FAULT_RESET = 0;
					// Clear the PWM fault flag
					Flags.PWMFault = 0;
					}
				// Clear the button status flag.
				Flags.Button1 = 0;	
				}
			}
		
		// Button2 turns the voltage boost on and off
		if(Flags.Button2)
			{
			LED2 = 1;
			if(BUTTON2)
				{
				LED2 = 0; 
                              
 //               IEC2bits.PWMIE = !IEC2bits.PWMIE;
                IFS2bits.FLTAIF = 0;
                Flags.StarStop= !Flags.StarStop;
                if(!Flags.StarStop) OVDCON=0;
                else OVDCON=0x3F00;
				// Clear the button status flag.
				Flags.Button2 = 0;
				}
			}
		
		// Button3 doesn't do anything for this application
//		if(Flags.Button3)
//			{
//			LED3 = 1;
//			if(!BUTTON3)
//				{
//				LED3 = 0;
//				Flags.Button3 = 0;
//				}
//			}
		
		// Button #4 is used to set the direction of the motor.			
		if(Flags.Button4)
			{
			// Wait until the button is released before doing anything.
			// Start a direction change if one is not already in progress
			// and the speed setting is non-zero.
			if(BUTTON4  && !Flags.DirChange && Speed)
				{
//				LED4 = 1;
				// Set the direction change flag.
				Flags.DirChange = 1;
				// Tell the motion profile to decelerate the motor
				Flags.Accelerate = 0;
				// Save the current Speed setting
				OldSpeed = Speed;
				// Clear the button status flag
				Flags.Button4 = 0;
				}
			}
		Flags.SlowEvent = 0;
		}		// end if(SlowEvent)
   	}			// end while(1)
}				// end main

//---------------------------------------------------------------------

void Setup(void)
{
// Initialize system variables and flags.
Speed = 0;
OldSpeed = 0;
SlowEventCount = SLOW_EVENT_PERIOD;
MediumEventCount = MEDIUM_EVENT_PERIOD;
Mode.ACIM = 0;
Mode.ACIMTest = 0;
Mode.BLDC = 0;
Mode.BDC = 0;

// Initialize PORTs

//PORTA = 0;				
PORTB = 0;				// Initialize PORTs
PORTC = 0;
PORTD = 0;
PORTE = 0;
//PORTG = 0;
TRISB = 0xFFF3;
TRISC = 0xFFFF;
TRISD = 0xFFFF;			
TRISE = 0xFFFF;

LED1 = 1;
LED2 = 1;
//LED3 = 1;
//LED4 = 1;
Delay(50000);
LED1 = 0;
LED2 = 0;
//LED3 = 0;
//LED4 = 0;
Delay(50000);

	LED2 = 1;
//	while(BUTTON2);
	Delay(50000);
//	SetDDRamAddr(0x40);
//	putsXLCD("ACIM Mode 2     ");
	Mode.ACIMTest = 1;
	LED2 = 0;

// Initialize the LCD display
//OpenXLCD(FOUR_BIT&LINES_5X7);
//SetDDRamAddr(0);
//putsXLCD("dsPIC30F MC Demo");
//while(BusyXLCD());
//SetDDRamAddr(0x40);
//putsXLCD("S1-S4 slct mode ");

// Wait for a button to be pressed.
/*while(!BUTTON1 && !BUTTON2 && !BUTTON4); 
Delay(10000);

// Get software mode
if(BUTTON1 && !BUTTON2 && !BUTTON4)
	{
	LED1 = 1;
	while(BUTTON1);
	Delay(50000);
	Mode.ACIM = 1;
//	SetDDRamAddr(0x40);
//	putsXLCD("ACIM Mode 1     ");
	LED1 = 0;
	}
else if(!BUTTON1 && BUTTON2 && !BUTTON4)
	{
	LED2 = 1;
	while(BUTTON2);
	Delay(50000);
//	SetDDRamAddr(0x40);
//	putsXLCD("ACIM Mode 2     ");
	Mode.ACIMTest = 1;
	LED2 = 0;
	}
//else if(!BUTTON1 && !BUTTON2 && BUTTON3 && !BUTTON4)
//	{
//	LED3 = 1;
//	while(BUTTON3);
//	Delay(50000);
//	SetDDRamAddr(0x40);
//	putsXLCD("BLDC Mode       ");
//	Mode.BLDC = 1;
//	LED3 = 0;
//	}
else if(!BUTTON1 && !BUTTON2 && BUTTON4)
	{
//	LED4 = 1;
	while(BUTTON4);
	Delay(50000);
//	SetDDRamAddr(0x40);
//	putsXLCD("Brush DC Mode   ");
	Mode.BDC = 1;
//	LED4 = 0;
	}
else
	{
	Delay(50000);
//	SetDDRamAddr(0x40);	
//	putsXLCD("no mode selected");
	}
*/
// Initialize PWM
if(Mode.ACIM || Mode.ACIMTest)
	{
	PTPER = 230;		// Value gives 16KHZ center aligned PWM at 7.38MIPS
	PDC1 = 0;
	PDC2 = 0;
	PDC3 = 0;
//	PDC4 = 0;
	PWMCON1 = 0x0077;	// Enable PWM 1,2,3 pairs for complementary mode
	DTCON1 = 0x000F;	// Value provides 2us dead time at 7.38 MIPS
//	DTCON2 = 0;
	FLTACON = 0x0007;	// Fault A enabled for latched mode on PWM1, 2, and 3
//	FLTBCON = 0;		// Fault B not used.
	OVDCON	= 0;	// Enable PWM1H,1L, 2H, 2L, 3L, 3H for PWM
	PTCON = 0x8002;		// Enable PWM for center aligned operation
	}
/*else if(Mode.BLDC)
	{
	PTPER = 460;		// Value gives 16KHZ edge aligned PWM at 7.38MIPS
	PDC1 = 0;
	PDC2 = 0;
	PDC3 = 0;
//	PDC4 = 0;
	PWMCON1 = 0x0777;	// Enable PWM 1,2,3 pairs for independent mode
	DTCON1 = 0;			// Dead time disabled
//	DTCON2 = 0;
	FLTACON = 0x0007;	// Fault A enabled for latched mode on PWM1, 2, and 3
//	FLTBCON = 0;		// Fault B not used.
	OVDCON	= 0x3F00;	// Enable PWM1H,1L, 2H, 2L, 3L, 3H for PWM
	PTCON = 0x8000;		// Enable PWM for edge aligned operation
	}
	
else if(Mode.BDC)
	{
	PTPER = 460;		// Value gives 16KHZ edge aligned PWM at 7.38MIPS
	PDC1 = PTPER;
	PDC2 = PTPER;
	PDC3 = 0;
//	PDC4 = 0;
	PWMCON1 = 0x0033;	// Enable PWM 1,2 pairs for complementary mode
	DTCON1 = 0x000F;	// Value provides 2us dead time at 7.38 MIPS
//	DTCON2 = 0;
	FLTACON = 0x0003;	// Fault A enabled for latched mode on PWM1, 2
//	FLTBCON = 0;		// Fault B not used.
	OVDCON	= 0x0F00;	// Enable PWM1H,1L, 2H, and 2L for PWM
	PTCON = 0x8000;		// Enable PWM for edge aligned operation
	}*/
else
	// If no mode is selected, the PWM timebase provides interrupt
	// generation only.  No PWM outputs are enabled.
	{
	PTPER = 460;		// Value gives 16KHZ edge aligned PWM at 7.38MIPS
	PTCON = 0x8000;		// Enable PWM for edge aligned operation
	}

IFS2bits.PWMIF = 0;	
IEC2bits.PWMIE = 1;	// Enable PWM interrupts.
IFS2bits.FLTAIF = 0;// Clear the fault A interrupt flag.
IEC2bits.FLTAIE = 1;// Enable interrupts for Fault A


// Initialize ADC

ADCON1 = 0;
ADCON2 = 0;
ADCON3 = 0;
ADPCFG = 0XFFCF;
ADCHS = 0x0005;
ADCON1bits.ADON = 1;

// Reset any active faults on the motor control power module.
//FAULT_RESET = 1;
//Delay(10);
//FAULT_RESET = 0;

// Configure Timer1, but don't enable interrupts
// Timer 1 is used to drive the PFC circuit
//TMR1 = 0;
//PR1 = 60;
//T1CON = 0x8000;
//IFS0bits.T1IF = 0;
//IEC0bits.T1IE = 0;

// Enable the driver IC on the motor control PCB
//PWM_OUTPUT_DISABLE = 0;
// Ensure PFC switch is off.
//PFC_FIRE = 0;
// Turn brake off.
//BRAKE_FIRE = 0;
// Ensure FLTA flag is cleared
IFS2bits.FLTAIF = 0;
}

//---------------------------------------------------------------------

int ReadADC(unsigned int channel)
{
int Delay;

if(channel > 0x000F) return(0);
ADCHS = channel;
ADCON1bits.SAMP = 1;
for(Delay = 0; Delay < 20; Delay++);
IFS0bits.ADIF = 0;
ADCON1bits.SAMP = 0;
while(!IFS0bits.ADIF);
return(ADCBUF0);

}

//---------------------------------------------------------------------
// The PWM ISR just sets a software flag to trigger SVM calculations
// in the main software loop.

void __attribute__((__interrupt__)) _PWMInterrupt(void)
{
SlowEventCount--;
if(SlowEventCount == 0)
	{
	Flags.SlowEvent = 1;
	SlowEventCount = SLOW_EVENT_PERIOD;
	}
MediumEventCount--;
if(MediumEventCount == 0)
	{
	Flags.MediumEvent = 1;
	MediumEventCount = MEDIUM_EVENT_PERIOD;
	}
Flags.PWMEvent = 1;
IFS2bits.PWMIF = 0;
}

//---------------------------------------------------------------------
// The FLTA ISR responds to events on the PWM fault pin.
// This ISR code just turns off all the PWM outputs via the OVDCON
// register and signals the main loop that a problem has occurred.

void __attribute__((__interrupt__)) _FLTAInterrupt(void)
{
// Keep all outputs disabled until we figure out what is going on!
OVDCON = 0;
//PFC_FIRE = 0;
// Signal a fault to the main loop.
Flags.PWMFault = 1;
// Clear the FLTA interrupt flag.
IFS2bits.FLTAIF = 0;
}

//---------------------------------------------------------------------
// The Timer1 ISR is used to drive the voltage boost circuit

//void __attribute__((__interrupt__)) _T1Interrupt(void)
//{
//if(++BoostCount > 4)
//	{
//	PFC_FIRE = 1;
//	BoostCount = 0;
//	}
//else 
//	PFC_FIRE = 0;
	
// Clear the Timer1 interrupt flag.
//IFS0bits.T1IF = 0;
//}

//---------------------------------------------------------------------
// This is a generic delay routine 

void Delay(unsigned int count)
{
unsigned int j;

for(j=0;j<count;j++)
	ClrWdt();

}


// end of file

⌨️ 快捷键说明

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