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

📄 pumpctl.c

📁 单相电机变频驱动原码,采用DISPIC30芯片
💻 C
字号:
/****************************************************************
*
*		Microchip 16-bit Embedded Control Design Contest
*
*		Entry # MT2268
*
*		Spa Pump Controller
*
*****************************************************************
*
*		Main Routine
*
*****************************************************************/


#define MAIN

#include "PumpCtl.h"
#include "ADC.h"
#include "PWM.h"
#include "Serial.h"
#include "Periph.h"
#include "Inverter.h"
#include "PFC.h"


#define CLK_SW	// Switch clock to PLL mode after startup (per Rev A1 Errata #19)


#ifdef CLK_SW
_FOSCSEL (FRC);			// Select Fast RC osc without PLL
_FOSC (CSW_FSCM_ON & FRC_CLK_RANGE & OSC2_IO & HS_EC_DIS);	// Select FRC range
#else
_FOSCSEL (FRC_PLL);		// Select Fast RC osc with PLL
_FOSC (CSW_FSCM_OFF & FRC_CLK_RANGE & OSC2_IO & HS_EC_DIS);	// Select FRC range
#endif

#ifdef WDOG
_FWDT (FWDTEN_ON & WDTPRE_PR32 & WDTPOST_PS16);	// Set watchdog to 1/(32kHz/32/16)
#else
_FWDT (FWDTEN_OFF);								// Turn watchdog timer off
#endif


void Hardware_init (void);
void Software_init (void);
void Interrupt_init (void);


int main (void)
{

#ifdef CLK_SW
	// Switch to PLL mode after reset (per Rev A1 Errata #19)
	__builtin_write_OSCCONH (0x01);	// Select FRC + PLL mode
	__builtin_write_OSCCONL (0x01);	// Set OSWEN to switch to new mode
#endif

	sys_state = S_INIT;

	Hardware_init ();		// Initialize everything
	Software_init ();
	Interrupt_init ();

	sys_state = S_STARTUP;

	while (1) {

		Comm_run ();			// Do serial comm processing

		LED_run ();				// Do LED blink processing

		switch (sys_state) {

			case S_STARTUP:			// Just starting up

				Inrush_start ();			// Start inrush delay

				sys_state = S_INRUSH;
				break;

			case S_INRUSH:			// Waiting for bus caps to charge up

				if (Inrush_run () == 0) {	// Inrush delay complete

					PFC_start ();			// Start up PFC

					sys_state = S_PFC;
				}
				break;

			case S_PFC:				// Waiting for PFC to stabilize

				PFC_run ();

				if (pfc_state == P_RUNNING) {

					Inv_start ();			// Start up inverter

					sys_state = S_RUNNING;
				}

			case S_RUNNING:			// Normal running state

				PFC_run ();					// Do PFC processing
				Inv_run ();					// Do inverter processing

				if (line_loss)				// Check for AC power failure
					sys_fault |= FAULT_pwr_fail;

				break;

			case S_SHUTDOWN:		// Normal shutdown

				Inv_stop ();				// Stop inverter
				PFC_stop ();				// Stop PFC
				break;

			case S_FAULT:			// Fault shutdown

				if (sys_fault != 0) {		// Handle the fault

					sys_fault |= FAULT_ack;

					Inv_stop ();			// Stop inverter
					PFC_stop ();			// Stop PFC
				}
				else {						// Fault has been cleared

					PFC_start ();			// Restart PFC

					sys_state = S_PFC;		//  and go back around
				}
				break;

			default:
				break;
		}

		if ((sys_fault & 0xFF00) != 0)		// Hard faults
			sys_state = S_FAULT;
		else if ((sys_fault & 0x00FF) != 0)	// Recoverable faults
			sys_state = S_SHUTDOWN;

		WDOG_CLR;
	}

	return (0);
}


/*
	Initialize hardware peripherals
*/
void Hardware_init (void)
{

	IO_init ();
	PWM_init ();
	ADC_init ();
	Cmp_init ();
	Timer_init ();
	Serial_init ();

	PMD1 = 0x3088;		// Disable T2, T3, I2C, SPI
	PMD2 = 0x0103;		// Disable IC1, OC1, OC2
}


/*
	Initialize software variables
*/
void Software_init (void)
{
	sys_mode.w = 0xFFFF;		// Enable all mode flags
	PFC_init ();
	Inv_init ();
}


/*
	Initialize interrupts
*/
void Interrupt_init (void)
{

	_ADIP	= 6;		// Set int priorities
	_PSEMIP	= 5;
	_T1IP	= 3;
	IFS0 = 0;			// Clear all int flags
	IFS1 = 0;
	IFS2 = 0;
	_ADIE	= 1;		// Enable ints
	_PSEMIE	= 1;
	_T1IE	= 1;
}

⌨️ 快捷键说明

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