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

📄 user_interface.c

📁 关于无传感器BLDC使用DSPIC现实的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
{
	static unsigned char initializing_timer=20;
	static unsigned char previous_valid_switch_states=0xFF;
	static unsigned char key_hold_timer=0;
	static unsigned int param_increment=1;

	if (initializing_timer==1)	run_state=STANDBY;
	if (initializing_timer) initializing_timer--;



	if (((previous_valid_switch_states & 0x08)==FALSE) && (valid_switch_states & 0x08))
		interface_flags.S7_RISING=TRUE;
	else
		interface_flags.S7_RISING=FALSE;

	previous_valid_switch_states=valid_switch_states;


	switch(run_state)
	{
		case INITIALIZING: break;

		case STANDBY:	if (interface_flags.S7_RISING)
							{
								DISABLE_INTERRUPTS;
								control_flags2.ROTATION_CHECK=TRUE;
								control_flags2.WINDMILLING=FALSE;
								control_flags2.RETRY_FLAG=FALSE;
								control_flags.LOCK1=FALSE;
								control_flags.LOCK2=FALSE;
								control_flags.RAMP=FALSE;
								control_flags.SENSORLESS=FALSE;
								control_flags.ACQUIRE2=FALSE;
								control_flags.ACQUIRE1=FALSE;
								control_flags.DIR=user_parameters[0];
								ENABLE_INTERRUPTS;
								run_state=STARTING;
								interface_flags.EDIT_MENU=FALSE;
								interface_flags.RUN_FRAME=0;
								fault_count = 0;
							}
							if (interface_flags.S4_RISING)
								interface_flags.EDIT_MENU=TRUE;
							break;

		case STARTING:	if (interface_flags.S7_RISING)
							{
								DISABLE_FIRING;
								control_flags.SENSORLESS=FALSE;
								control_flags.ACQUIRE2=FALSE;
								IEC0bits.T1IE=FALSE;
								IEC0bits.T2IE=FALSE;
								run_state=STANDBY;
							}
							break;

		case RUNNING: 	if (interface_flags.S7_RISING)
							{
								DISABLE_FIRING;
								control_flags.SENSORLESS=FALSE;
								control_flags.ACQUIRE2=FALSE;
								IEC0bits.T1IE=FALSE;
								IEC0bits.T2IE=FALSE;
								run_state=STANDBY;
							}
							if (interface_flags.S4_RISING)
								interface_flags.RUN_FRAME= !interface_flags.RUN_FRAME;
							break;
		case FAULT:		if (interface_flags.S7_RISING)
							{
								trip_state=NO_TRIP;
								DISABLE_INTERRUPTS;
								control_flags.LOCK1=FALSE;
								control_flags.LOCK2=FALSE;
								control_flags.RAMP=FALSE;
								control_flags.SENSORLESS=FALSE;
								control_flags.ACQUIRE1=FALSE;
								control_flags.ACQUIRE2=FALSE;
								IEC0bits.T1IE=FALSE;
								IEC0bits.T2IE=FALSE;
								ENABLE_INTERRUPTS;
								period_measurement=1000;
								//FAULT_RESET=TRUE;
								//FAULT_RESET=FALSE;
								run_state=STANDBY;
							}
							if (interface_flags.S4_RISING)
								interface_flags.EDIT_MENU=TRUE;
							break;
		default:			break;
	}

	return;
}



// This function does the necessary calculations when a parameter
// value changes. Some parameters values are used directly, others
// form the basis for other variables but these need to be calculated.
void process_parameters(void)
{
	unsigned long ltemp,ltemp2;

	// If a value is missing from this switch statement this implies the
	// user parameter is used directly.
	// Note that parameters that affect other variables should also be in
	// this list e.g.if Voltage scaling changes, voltage demand and trips
	// need to be recalculated.
	switch (param)
	{
		case 0:	control_flags.DIR=user_parameters[0];
					break;
		case 4:	// If using voltage control for starting
					if (user_parameters[40])
						hold1_demand=(unsigned int)((unsigned long)user_parameters[4])*FULL_DUTY/100;
					else //Using current control assume scaling is in % of trip
					{
						ltemp=((unsigned long)(current_trip-ibus_offset));
						ltemp*=((unsigned long)user_parameters[4]);
						hold1_demand=(unsigned int)(ltemp/100);
					}	
					break;
		case 5:	// If using voltage control for starting
					if (user_parameters[40])
						hold2_demand=(unsigned int)((unsigned long)user_parameters[5])*FULL_DUTY/100;
					else //Using current control assume scaling is in % of trip
					{
						ltemp=(unsigned long)(current_trip-ibus_offset);
						ltemp*=((unsigned long)user_parameters[5]);
						hold2_demand=(unsigned int)(ltemp/100);
					}	
					break;
		case 8:	// If using voltage control for starting
					if (user_parameters[40])
						ramp_start_demand=(unsigned int)((unsigned long)user_parameters[8])*FULL_DUTY/100;
					else //Using current control assume scaling is in % of trip
					{
						ltemp=(unsigned long)(current_trip-ibus_offset);
						ltemp*=((unsigned long)user_parameters[8]);
						ramp_start_demand=(unsigned int)(ltemp/100);
					}
					ramp_demand_delta=(signed int)(ramp_end_demand-ramp_start_demand);
					break;

		case 9:	// If using voltage control for starting
					if (user_parameters[40])
						ramp_end_demand=(unsigned int)((unsigned long)user_parameters[9])*FULL_DUTY/100;
					else //Using current control assume scaling is in % of trip
					{
						ltemp=(unsigned long)(current_trip-ibus_offset);
						ltemp*=((unsigned long)user_parameters[9]);
						ramp_end_demand=(unsigned int)(ltemp/100);
					}
					ramp_demand_delta=(signed int)(ramp_end_demand-ramp_start_demand);
					break;

					// ramp time is used to hold user_parameters[10] so that
					// it can be directly referenced in some inline assembly 
		case 10: ramp_time=user_parameters[10];
					break;
		case 17:	iloop_p_gain=(int)user_parameters[17];
					break;
		case 18: iloop_i_gain=(int)user_parameters[18];
					break;
		case 19: iloop_d_gain=(int)user_parameters[19];
					break;
		case 20:	wloop_p_gain=(int)user_parameters[20];
					break;
		case 21:	wloop_i_gain=(int)user_parameters[21];
					break;
		case 23:	vloop_p_gain=(int)user_parameters[23];
					break;
		case 24:	vloop_i_gain=(int)user_parameters[24];
					break;
		case 6:	
		case 7:
		case 25:
		case 44: // Calculate step rates in units of TIMER2 from 
					// user parameters in RPM ensuring no overflows occur
					ltemp=((unsigned long)user_parameters[6]*(unsigned long)user_parameters[25])/20;
					// This check ensures that ramp_start_rate is calculated with no overflow
					if ((COUNTER_RATE/ltemp) > 65535)
					{	
						ramp_start_rate=65535;
						ramp_start_speed=COUNTER_RATE*20/(65535*(unsigned long)user_parameters[25]);
					}
					else
					{
						ramp_start_rate=(unsigned int)(COUNTER_RATE/ltemp);
						ramp_start_speed=user_parameters[6];
					}
					ltemp=((unsigned long)user_parameters[7]*(unsigned long)user_parameters[25])/20;
					// This check ensures that ramp_end_rate is calculated with no overflow
					if ((COUNTER_RATE/ltemp) > 65535)	
						ramp_end_rate=65535;
					else
						ramp_end_rate=(unsigned int)(COUNTER_RATE/ltemp);

					ramp_speed_delta=(int)(user_parameters[7]-user_parameters[6]);
					// Also calculate step rate at which zero X detection enabled when using
					// acquistion method 1
					ltemp=((unsigned long)user_parameters[44]*(unsigned long)user_parameters[25])/20;
					if ((COUNTER_RATE/ltemp) > 65535)
						acquire1_enable_rate=65535;
					else
						acquire1_enable_rate=(unsigned int)(COUNTER_RATE/ltemp);
					break;
		case 16:
		case 26:	
		case 27:	// Get value reported for ibus
					// At this stage assuming ibus=0 and have first valid sample
					// and so use this one for offset
					ibus_offset=ibus;
					ltemp=((unsigned long)user_parameters[16])*ADC_GAIN;
					ltemp*=((unsigned long)user_parameters[26]);
					current_trip=(ltemp/(user_parameters[27]*10))+ibus_offset;
					// Now calculate the current limits used when in CLOSED_CURRENT
					// speed control as 95% of the current trip levels
					pos_current_limit=(long)(current_trip-ibus_offset)*95/100;
					pos_current_limit*=16384L;
					neg_current_limit=-1*pos_current_limit;
					// Now recalculate starting parameters if using current control
					// for starting.
					if (user_parameters[40]==FALSE)
					{
						ltemp2=((unsigned long)(current_trip-ibus_offset));
						ltemp=((unsigned long)user_parameters[4])*ltemp2;
						hold1_demand=(unsigned int)(ltemp/100);
						ltemp=((unsigned long)user_parameters[5])*ltemp2;
						hold2_demand=(unsigned int)(ltemp/100);
						ltemp=((unsigned long)user_parameters[8])*ltemp2;
						ramp_start_demand=(unsigned int)(ltemp/100);
						ltemp=((unsigned long)user_parameters[9])*ltemp2;
						ramp_end_demand=(unsigned int)(ltemp/100);
						ramp_demand_delta=(signed int)(ramp_end_demand-ramp_start_demand);
					}
					ltemp2=((unsigned long)(current_trip-ibus_offset));
					ltemp=((unsigned long)user_parameters[41])*ltemp2;
					windmilling_demand=(ltemp/100);	
					break;
		case 15:
		case 22:
		case 28:
		case 29:	ltemp=((unsigned long)user_parameters[15])*ADC_GAIN;
					ltemp*=((unsigned long)user_parameters[28]);
					voltage_trip=ltemp/((unsigned long)user_parameters[29]*10);
					ltemp=((unsigned long)user_parameters[22])*ADC_GAIN;
					ltemp*=((unsigned long)user_parameters[28]);
					voltage_demand=ltemp/((unsigned long)user_parameters[29]*10);
					break;
		case 30: upper_tol=100+user_parameters[30];
					lower_tol=100-user_parameters[30];
					break;

		case 34: // Calculate acquision (method2) threshold values based
					// on user parameter and ADC value read during initialization.
					// This compensates for offset voltages due to ADC and power module.
					vph_red_threshold=vph_red+user_parameters[34];
					vph_yellow_threshold=vph_yellow+user_parameters[34];
					vph_blue_threshold=vph_blue+user_parameters[34];
					break;
		case 40:	if (user_parameters[40])
					{
						hold1_demand=(unsigned int)((unsigned long)user_parameters[4])*FULL_DUTY/100;
						hold2_demand=(unsigned int)((unsigned long)user_parameters[5])*FULL_DUTY/100;
						ramp_start_demand=(unsigned int)((unsigned long)user_parameters[8])*FULL_DUTY/100;
						ramp_end_demand=(unsigned int)((unsigned long)user_parameters[9])*FULL_DUTY/100;
					}
					else
					{
						ltemp2=((unsigned long)(current_trip-ibus_offset));
						ltemp=((unsigned long)user_parameters[4])*ltemp2;
						hold1_demand=(unsigned int)(ltemp/100);
						ltemp=((unsigned long)user_parameters[5])*ltemp2;
						hold2_demand=(unsigned int)(ltemp/100);
						ltemp=((unsigned long)user_parameters[8])*ltemp2;
						ramp_start_demand=(unsigned int)(ltemp/100);
						ltemp=((unsigned long)user_parameters[9])*ltemp2;
						ramp_end_demand=(unsigned int)(ltemp/100);
					}
					ramp_demand_delta=(int)(ramp_end_demand-ramp_start_demand);
					break;
		case 41: ltemp2=((unsigned long)(current_trip-ibus_offset));
					ltemp=((unsigned long)user_parameters[41])*ltemp2;
					windmilling_demand=(unsigned int)(ltemp/100);
					break;
		default:	break;
	}
	return;
} 

// This function does a simple switch debounce by not
// updating the global variable valid_switch_states
// unless all 4 push buttons have been in the same
// state for 3 calls of the function
void debounce_switches(void)
{
	static unsigned char oldest_switch_states=0;
	static unsigned char previous_switch_states=0;
	unsigned char switch_states;

	// The four push buttons are on PORTG6-9 but have pull
	// up resistors making a logic 0 equal to a button press
	// So we complement and shift them down to be aligned to 
	//	the bottom which will also effectively mask off all other bits.

	//switch_states=(unsigned char)((~PORTG)>>6);
	if (!PORTCbits.RC14)
		switch_states = 0x8;
	else switch_states = 0x0;
	if (switch_states!=previous_switch_states)
	{
		oldest_switch_states=previous_switch_states;
		previous_switch_states=switch_states;
		return;
	}

	if (previous_switch_states != oldest_switch_states)
	{
		oldest_switch_states=previous_switch_states;
		previous_switch_states=switch_states;
	}
	else
	{
		valid_switch_states=switch_states;
		oldest_switch_states=previous_switch_states;
		previous_switch_states=switch_states;

⌨️ 快捷键说明

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