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

📄 user_interface.c

📁 电路原理图
💻 C
📖 第 1 页 / 共 2 页
字号:
		return;
	}

	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_RAM[0];
								ENABLE_INTERRUPTS;
								run_state=STARTING;
								interface_flags.EDIT_MENU=FALSE;
								interface_flags.RUN_FRAME=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;
								Write_Screen(&line1_reset_message[0],&line2_reset_message[0]);
								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;

	// 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.
	
	// Save direction of motor rotation
	control_flags.DIR=user_parameters_RAM[0];

	// 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_RAM[16])*ADC_GAIN;
	ltemp*=((unsigned long)user_parameters_RAM[26]);
	current_trip=(ltemp/(user_parameters_RAM[27]*10))+ibus_offset;

	// If using voltage control for starting
	if (user_parameters_RAM[40])
	{
		hold1_demand=(unsigned int)((unsigned long)user_parameters_RAM[4])*FULL_DUTY/100;
		hold2_demand=(unsigned int)((unsigned long)user_parameters_RAM[5])*FULL_DUTY/100;
		ramp_start_demand=(unsigned int)((unsigned long)user_parameters_RAM[8])*FULL_DUTY/100;
		ramp_end_demand=(unsigned int)((unsigned long)user_parameters_RAM[9])*FULL_DUTY/100;
	}
	else //Using current control assume scaling is in % of trip
	{
		ltemp=(unsigned long)(current_trip-ibus_offset);
		hold1_demand=(unsigned int)((ltemp*(unsigned long)user_parameters_RAM[4])/100);
		hold2_demand=(unsigned int)((ltemp*(unsigned long)user_parameters_RAM[5])/100);
		ramp_start_demand=(unsigned int)((ltemp*(unsigned long)user_parameters_RAM[8])/100);
		ramp_end_demand=(unsigned int)((ltemp*(unsigned long)user_parameters_RAM[9])/100);
	}	
	ramp_demand_delta = (signed int)(ramp_end_demand-ramp_start_demand);
	windmilling_demand = (unsigned int)((ltemp*(unsigned long)user_parameters_RAM[41])/100);
	
	// Calculate step rates in units of TIMER2 from 
	// user parameters in RPM ensuring no overflows occur
	ltemp=((unsigned long)user_parameters_RAM[6]*(unsigned long)user_parameters_RAM[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_RAM[25]);
	}
	else
	{
		ramp_start_rate=(unsigned int)(COUNTER_RATE/ltemp);
		ramp_start_speed=user_parameters_RAM[6];
	}
	ltemp=((unsigned long)user_parameters_RAM[7]*(unsigned long)user_parameters_RAM[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_RAM[7]-user_parameters_RAM[6]);
	// Also calculate step rate at which zero X detection enabled when using
	// acquistion method 1
	ltemp=((unsigned long)user_parameters_RAM[44]*(unsigned long)user_parameters_RAM[25])/20;
	if ((COUNTER_RATE/ltemp) > 65535)
		acquire1_enable_rate=65535;
	else
		acquire1_enable_rate=(unsigned int)(COUNTER_RATE/ltemp);

	// ramp time is used to hold user_parameters[10] so that
	// it can be directly referenced in some inline assembly 
	ramp_time=user_parameters_RAM[10];
	iloop_p_gain=(int)user_parameters_RAM[17];
	iloop_i_gain=(int)user_parameters_RAM[18];
	iloop_d_gain=(int)user_parameters_RAM[19];
	wloop_p_gain=(int)user_parameters_RAM[20];
	wloop_i_gain=(int)user_parameters_RAM[21];
	vloop_p_gain=(int)user_parameters_RAM[23];
	vloop_i_gain=(int)user_parameters_RAM[24];
		
	// 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;

	ltemp=((unsigned long)user_parameters_RAM[28])*ADC_GAIN;
	voltage_trip=(ltemp*(unsigned long)user_parameters_RAM[15])/((unsigned long)user_parameters_RAM[29]*10);
	voltage_demand=(ltemp*(unsigned long)user_parameters_RAM[22])/((unsigned long)user_parameters_RAM[29]*10);

	upper_tol=100+user_parameters_RAM[30];
	lower_tol=100-user_parameters_RAM[30];

	// 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_RAM[34];
	vph_yellow_threshold=vph_yellow+user_parameters_RAM[34];
	vph_blue_threshold=vph_blue+user_parameters_RAM[34];

	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.

PORTGbits.RG6 = PORTDbits.RD6;
PORTGbits.RG7 = PORTDbits.RD7;
PORTGbits.RG8 = PORTAbits.RA7;
PORTGbits.RG9 = PORTDbits.RD13;


	switch_states=(unsigned char)((~PORTG)>>6);
	
	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;
	}
	return;
}

void Write_Screen(unsigned const char *line1,unsigned const char *line2)
{
    while(BusyXLCD());              	// Wait if LCD busy						
	SetDDRamAddr(0x00);						// sets address to origin
    while(BusyXLCD());             		// Wait if LCD busy
	putsXLCD(line1);				
	
	while(BusyXLCD());              	// Wait if LCD busy
	SetDDRamAddr(0x40);					// Move To Second Line
	while(BusyXLCD());              	// Wait if LCD busy
	putsXLCD(line2);					
    return;
}

void Edit_Screen(void)
{
	unsigned char lcd_char[5];

   while(BusyXLCD());              	// Wait if LCD busy
   SetDDRamAddr(0x00);					// Sets address to origin
   while(BusyXLCD());             	// Wait if LCD busy
	putsXLCD((unsigned const char *)parameter_data[param].line1_msg);
						
   while(BusyXLCD());              	// Wait if LCD busy
	SetDDRamAddr(0x40);					// Move To Second Line
	while(BusyXLCD());             	// Wait if LCD busy
	uint_to_string(new_param_value,&lcd_char[0]);
	putsXLCD(&lcd_char[0]);
	while(BusyXLCD());              	// Wait if LCD busy
	putsXLCD((unsigned const char *)"  ");			
	while(BusyXLCD());              	// Wait if LCD busy
	putsXLCD((unsigned const char *)parameter_data[param].units_msg);

   return;
}

void Run_Screen(void)
{
	unsigned int temp;
	unsigned char lcd_char[7];
	unsigned long ltemp;
	
	while(BusyXLCD());              	// Wait if LCD busy
   SetDDRamAddr(0x00);					// Sets address to origin
	
	if (interface_flags.RUN_FRAME==0)
	{
		while(BusyXLCD());
		putsXLCD((unsigned const char *)"RUN  DEMAND=");
		while(BusyXLCD());
		temp=((unsigned long)filtered_pot*100)/1023;
		uint_to_string(temp,&lcd_char[0]);
		putsXLCD(&lcd_char[2]);				// Note offset in address as
		while(BusyXLCD());					// demand only ever 100% max
		putsXLCD((unsigned const char *)"%");

		ltemp=((unsigned long)filtered_vdc*10);
		ltemp*=((unsigned long)user_parameters_RAM[29]);
		temp=ltemp/((unsigned long)user_parameters_RAM[28]*ADC_GAIN);
		while(BusyXLCD());              	// Wait if LCD busy
		SetDDRamAddr(0x40);					// Move To Second Line
		while(BusyXLCD());
		uint_to_string(temp,&lcd_char[0]);
		// Add in decimal point as Vdc scaling gives 100's of mV
		lcd_char[6]='\0';						// Null terminate
		lcd_char[5]=lcd_char[4];			// Shift decimal fraction down
		lcd_char[4]='.';						// Add in decimal point
		putsXLCD(&lcd_char[1]);				// Note offset in address
		while(BusyXLCD());					// As VDC always less than 999V
		putsXLCD((unsigned const char *)"V  ");
		uint_to_string(filtered_rpm,&lcd_char[0]);
		while(BusyXLCD());
		putsXLCD(&lcd_char[0]);
		while(BusyXLCD());
		putsXLCD((unsigned const char *)"RPM");
	}
	else
	{
		while(BusyXLCD());
		putsXLCD((unsigned const char *)"RUN ");
		while(BusyXLCD());
		temp=user_parameters_RAM[1];
		putsXLCD(&lcd_mode_msg[temp][0]);
		
		// Calculate phase advance in 1/10s of an electrical degree
		ltemp=((unsigned long)(phase_advance-TIME_CORRECTION)*1800);
		temp=ltemp/period_measurement;
		// Clamp to 30 degrees. phase_advance variable can be > 30
		// but commutation code limits it to 30.
		if (temp>300)	temp=300;
		while(BusyXLCD());              	// Wait if LCD busy
		SetDDRamAddr(0x40);					// Move To Second Line
		while(BusyXLCD());
		uint_to_string(temp,&lcd_char[0]);
		// Add in decimal point as phase advance calculated in
		// 1/10ths of a degree
		lcd_char[6]='\0';						// Null terminate
		lcd_char[5]=lcd_char[4];			// Shift decimal fraction down
		lcd_char[4]='.';						// Add in decimal point
		putsXLCD(&lcd_char[2]);				// Note offset in address
		while(BusyXLCD());					// As advance always less than 30
		putsXLCD((unsigned const char *)"degs");
		uint_to_string(filtered_rpm,&lcd_char[0]);
		while(BusyXLCD());
		putsXLCD(&lcd_char[0]);
		while(BusyXLCD());
		putsXLCD((unsigned const char *)"RPM");
	}
	return;
}
	




void uint_to_string(unsigned int value,unsigned char *display_str)
{

	unsigned char i , zero_flag; 

	display_str[0] = '0';
	while (value >= 10000)
   {
   	display_str[0] ++;
    	value -= 10000;
   }

	display_str[1] = '0';
	while (value >= 1000)
   {
   	display_str[1] ++;
   	value -= 1000;
   }

	display_str[2] = '0';
	while (value >= 100)
   {
   	display_str[2] ++;
   	value -= 100;
   }

	display_str[3] = '0';
	while (value >= 10)
   {
   	display_str[3] ++;
   	value -= 10;
   }

	display_str[4] = '0'+ value;

   /* Now blank off any leading zeros */

	zero_flag = TRUE;
	for (i=0;i<4;i++)
   {
    	if ((zero_flag) && (display_str[i] == '0'))	   
      	display_str[i]  = ' ';        
    	else
        	zero_flag = FALSE;
   }
	display_str[5]='\0';
	return;
}

⌨️ 快捷键说明

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