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

📄 main.c

📁 该程序是ST7MC驱动三洋压缩机(交流)
💻 C
字号:
/**************** (c) 2004  STMicroelectronics **********************
     
PROJECT  : 3-phase AC induction motor drive Software Library
COMPILER : COSMIC / METROWERKS

MODULE  :  main.c
VERSION :  1.0.0

CREATION DATE : April 2004

AUTHOR :	V. Onde / Microcontroller Division Applications
			Consumer & Micro Group

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

DESCRIPTION :   Main Routine
              
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

MODIFICATIONS :

 ******************************************************************************
 THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. ST MICROELECTRONICS
 SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES
 WITH RESPECT TO ANY CLAIMS ARISING FROM USE OF THIS SOFTWARE.
*******************************************************************************

******************************************************************************/

// Standard types definitions and ST7 specific macros
#include "lib.h"
// General purpose peripheral function prototypes
#include "ports.h"
#include "adc.h"
#include "pwmart.h"
#include "sci.h"
// ST7 specifics related functions prototypes
#include "ST7_Misc.h"
// Motor control specific functions prototypes
#include "mtc.h"		/* Peripheral specifics */
#include "acmotor.h"	/* AC motor related */
#include "config.h"		/* AC motor library configuration keys */
#include "MainParam.h"	/* Motor control parameters used in functions called from main.c */
// ST7FMC peripherals Hardware Registers declaration
#define __DEFINE_REGISTERS_STVD7_INCLUDE__
#include "ST7FMC2N6.h"


///////////////////////////////////////////////////////////////////////////////

/* Private typedefs (for demo purposes only) */
typedef enum
{
IDLE, START, RUN, STOP, BRAKE, WAIT, FAULT
} SystStatus_t;

/* Private variable (for demo purposes only) */
static SystStatus_t State;

/* Private functions (for demo purposes only) */
static void DoMotorControl( void );
static SystStatus_t StartMotor( void );
static void	CheckPowerStage(void);
static void	SendSystemDataRS232(void);

void main(void)
{

   ST7_IntPrioritySetUp();
	PORTS_Init();
	ADC_Init();
	ART_Init();						// PWM ART provided general purpose time bases
	ACM_Init();
	#ifdef ENABLE_RS232
		//	Init SCI for hyperterminal: 19200bps/8 data bit/no parity/1 stop bit
		SCI_Init(SCI_DEFAULT_PARAM1,SCI_DEFAULT_PARAM2); 
		SCI_Select_Baudrate(SCI_PR_13 + SCI_TR_2 + SCI_RR_2);
		SCI_Mode(SCI_TX_ENABLE);
	#endif

	ART_SetMainTimeBase(10);		// 100ms timebase for LED Toggle

	EnableInterrupts();

	ART_Start();					// Time bases are now available
	State = IDLE;
    
	// Flash LEDs to indicate proper MCU start-up
	PORTS_GreenLedOn();
	while (!ART_IsMainTimeElapsed());
	PORTS_RedLedOn();
	while (!ART_IsMainTimeElapsed());
	PORTS_GreenLedOn();

	// Main Loop
	while(1)
	{
		switch ( State )
		{
			case IDLE:	if ( PORTS_KeyScan() == TRUE )	// Start motor
						{
							#ifdef CLOSED_LOOP
								ACM_InitSoftStart(START_FREQ);
							#else	/* OPEN_LOOP: Read start-up frequency on trimmer */
								ACM_InitSoftStart_OL(100 + (ADC_GetRV1()*10));
							#endif
							State = START;
						}
						break;

			case START:	State = StartMotor();
						break;

			case RUN:	if ( PORTS_KeyScan() == TRUE )	// Stop motor
						{
							State = STOP;
						}
						else
						{
							DoMotorControl();
						}
						break;

			case STOP:	{
								u16 BrakingTorque;

							BrakingTorque = BRAKE_DUTY_CYCLE;
							if (BrakingTorque == 0)
							{
								ART_SetTimeOutDuration(1000);
								MTC_DisableMCOutputs();			// PWM is disabled on MCO outputs
								State = WAIT;
							}
							else
							{
								MTC_StartBraking( BrakingTorque );
								ART_SetTimeOutDuration(BRAKE_DURATION);
								State = BRAKE;
							}
						}
						break;

			case BRAKE: if (!ART_IsTimeOutElapsed())
						{
							MTC_Brake();
						}
						else
						{
							MTC_StopBraking();
							ART_SetTimeOutDuration(1000);
							State = WAIT;
						}
						break;

			case WAIT:	if (ART_IsTimeOutElapsed())
						{
							State = IDLE;
						}
						break;

			case FAULT:	
			default:	MTC_DisableMCOutputs();			// PWM is disabled on MCO outputs
						State = FAULT;
						break;
		}

		// Verify that inverter is operating properly
		CheckPowerStage();

		// LED management
		if (ART_IsMainTimeElapsed())
		{
        	switch ( State )
			{
				case IDLE:	PORTS_GreenLedOn();
							break;
				case START:
				case RUN:	
				case STOP:
				case BRAKE:
				case WAIT:  PORTS_RedLedOn();
							break;
				case FAULT:	
				default:	PORTS_RedLedToggle();
							break;
			}
		}

		#ifdef ENABLE_RS232
			SendSystemDataRS232();
		#endif

	}	// End of while(1)
} // main loop


/*-----------------------------------------------------------------------------
ROUTINE Name : CheckPowerStage

Description : Verify that inverter power stage is running fine:
					heatsink temperature
					voltage bus value
					current limitation not triggerred
-----------------------------------------------------------------------------*/
void CheckPowerStage(void)
{

	if (ADC_CheckOverVoltage() || ADC_CheckOverTemp() || MTC_CheckEmergencyStop())
	{
		State = FAULT;
	}

}

/*-----------------------------------------------------------------------------
ROUTINE Name : SendSystemDataRS232

Description : upload internal data to the PC via RS232 and hyperterminal for 
debug purposes
-----------------------------------------------------------------------------*/
void SendSystemDataRS232(void)
{

	#ifdef CLOSED_LOOP

	  	SCI_PutString("S=");		// Speed Command
		PrintUnsignedInt(100 + (ADC_GetRV1()*10));	// Set by RV1 Trimmer
//	  	SCI_PutString(" S=");		// Stator frequency
//		PrintUnsignedInt(MTC_GetStatorFreq());
//  	SCI_PutString(" R=");		// Rotor frequency
//		PrintUnsignedInt(MTC_GetRotorFreq());
//	  	SCI_PutString(" V=");		// Voltage
//		PrintUnsignedInt(MTC_GetVoltage());
//		SCI_PutString(" Slp=");		// SlipCmd
//		PrintUnsignedInt(ACM_GetOptimumSlip( MTC_GetStatorFreq() ));
		#ifdef PI_PARAM_TUNING
		  	SCI_PutString(" Ki=");
			PrintUnsignedInt(Ki);
	  		SCI_PutString(" Kp=");
			PrintUnsignedInt(Kp);
		#endif

	#else	/* OPEN_LOOP */
	  	SCI_PutString(" Freq=");
		PrintUnsignedInt(MTC_GetStatorFreq());
	  	SCI_PutString(" Volt=");
		PrintUnsignedInt(MTC_GetVoltage());
//	  	SCI_PutString(" Brake=");
//		PrintUnsignedInt(ADC_GetRV3());

	#endif

	SCI_PutString("\n\r");	// Go to next line

}

/* ####################### CLOSED LOOP OPERATION ######################### */
/* ####################### CLOSED LOOP OPERATION ######################### */
/* ####################### CLOSED LOOP OPERATION ######################### */
/* ####################### CLOSED LOOP OPERATION ######################### */
/* ####################### CLOSED LOOP OPERATION ######################### */
#ifdef CLOSED_LOOP
/*-----------------------------------------------------------------------------
ROUTINE Name : DoMotorControl

Description : Performs closed loop control of the AC motor: the stator frequency 
and voltage and updated according to target Speed read on RV1 trimmer.
-----------------------------------------------------------------------------*/
void DoMotorControl( void )
{
		u16 FreqCmd, StatorFreq;
		u8 SlipCmd;


	if (MTC_GetRotorFreq() == 0)
	{
		State = FAULT;
	}

	if ( ART_IsRegPeriodElapsed() )
	{
			u8 NewVoltage;

		// Get current Stator frequency
		StatorFreq  = MTC_GetStatorFreq();

		// Read Trimmers
		FreqCmd = 100 + (ADC_GetRV1()*10);	// Set Speed between 10.0Hz and 266.0Hz
		#ifdef PI_PARAM_TUNING
			Ki = ADC_GetRV2();
			Kp = ADC_GetRV3();
		#endif
		
		SlipCmd = ACM_GetOptimumSlip( StatorFreq );

		NewVoltage = ACM_SlipRegulation( SlipCmd );

		if (( (FreqCmd+SlipCmd) > StatorFreq) && (MTC_GetSlip() < ACCEL_SLIP_LIMIT))
		{
			StatorFreq++;
		}

		if (( (FreqCmd+SlipCmd) < StatorFreq) && (MTC_GetSlip() > DECEL_SLIP_LIMIT))
		{
			StatorFreq--;
		}

		MTC_UpdateSine(NewVoltage, StatorFreq);
		
	}  /* End of if: regulation performed */

}

/*-----------------------------------------------------------------------------
ROUTINE Name : StartMotor

Description : Starts the motor in closed loop: if a minimum speed has not been
reached after a given period, the start-up is considered as failed.
-----------------------------------------------------------------------------*/
SystStatus_t StartMotor( void )
{
		SystStatus_t NextState;

	switch ( ACM_SoftStart(MIN_START_FREQ) )
	{
		case START_FAIL:
							NextState = STOP;
							break;

		case START_OK:	
							ACM_InitSlipFreqReg( ACM_GetOptimumSlip( MTC_GetStatorFreq() ) );
							NextState = RUN;
							break;

		case START_ONGOING:	
							NextState = START;
							break;

		default:			NextState = FAULT;
							break;
	}

	return ( NextState );
}
#endif

/* ####################### OPEN LOOP OPERATION ######################### */
/* ####################### OPEN LOOP OPERATION ######################### */
/* ####################### OPEN LOOP OPERATION ######################### */
/* ####################### OPEN LOOP OPERATION ######################### */
/* ####################### OPEN LOOP OPERATION ######################### */
#ifdef OPEN_LOOP
/*-----------------------------------------------------------------------------
ROUTINE Name : DoMotorControl

Description : Performs open loop control of the AC motor: the stator frequency 
and voltage and updated according to trimmer reading.
-----------------------------------------------------------------------------*/
void DoMotorControl( void )
{
		u8 NewVoltage, VoltCmd;
		u16 NewFreq, SlipFreq, FreqCmd;

	// Read Trimmers
	FreqCmd = 100 + (ADC_GetRV1()*10);
	VoltCmd = ADC_GetRV2();

	// Get actual values of volt and Freq
	NewVoltage = MTC_GetVoltage();
	NewFreq = MTC_GetStatorFreq();

	SlipFreq = MTC_GetSlip();

	if ( ART_Is_TimeInMsElapsed() )
	{
			u8 VMax;

		VMax = ACM_VoltageMaxAllowed(NewFreq);
		if ( VoltCmd > VMax )
		{
			VoltCmd = VMax;
		}

		if ( VoltCmd > NewVoltage ) NewVoltage++;	// Smoothly increase voltage
		if ( VoltCmd < NewVoltage ) NewVoltage--;	// Smoothly decrease voltage

		if ( FreqCmd > NewFreq ) NewFreq++;		// Smoothly increase frequency
		// Smoothly decrease frequency unless RotorFreq>=StatorFreq (risk of reactive current)
		// if no sensor available, RotorFreq will be 0 and SlipFreq=StatorFreq
		if ( (SlipFreq != 0) && ( FreqCmd < NewFreq ) ) NewFreq--;

	}
	
	MTC_UpdateSine(NewVoltage, NewFreq);

}

/*-----------------------------------------------------------------------------
ROUTINE Name : StartMotor

Description : Starts the motor in open loop: with the stator frequency read 
on the trimmer, increase the stator voltage from 0 to the limit set by the 
V/f curve. When Voltage reaches this limit the routine returns TRUE.
-----------------------------------------------------------------------------*/
SystStatus_t StartMotor( void )
{
		u8 StartVolt, MaxVolt;
		u16 StartFreq;

	// Read Trimmers
	StartFreq = 100 + (ADC_GetRV1()*10);
	StartVolt = ADC_GetRV2();

	// Verify that Start-up voltage is in line with V/f characteristic
	MaxVolt = ACM_VoltageMaxAllowed(StartFreq);
	if (StartVolt > MaxVolt)
	{
		StartVolt = MaxVolt;
	}
	
	if ( ACM_SoftStartOL(StartVolt) )
	{
		return(RUN);
	}
	else
	{
		return(START);
	}

}
#endif	/* OPEN_LOOP */
/*** (c) 2004  STMicroelectronics **************************** END OF FILE ***/

⌨️ 快捷键说明

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