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

📄 user_interface.c

📁 关于无传感器无刷电机的源程序,其芯片小,功能还行.
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********************************************************************
 *                                                                     *
 *                        Software License Agreement                   *
 *                                                                     *
 *    The software supplied herewith by Microchip Technology           *
 *    Incorporated (the "Company") for its dsPIC controller            *
 *    is intended and supplied to you, the Company's customer,         *
 *    for use solely and exclusively on Microchip dsPIC                *
 *    products. The software is owned by the Company and/or its        *
 *    supplier, and is protected under applicable copyright laws. All  *
 *    rights are reserved. Any use in violation of the foregoing       *
 *    restrictions may subject the user to criminal sanctions under    *
 *    applicable laws, as well as to civil liability for the breach of *
 *    the terms and conditions of this license.                        *
 *                                                                     *
 *    THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION.  NO           *
 *    WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING,    *
 *    BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND    *
 *    FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE     *
 *    COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,  *
 *    INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.  *
 *                                                                     *
  **********************************************************************/

 /**********************************************************************
 *                                                                     * 
 *    Author: Smart Power Soutions, LLP                                * 
 *                                                                     *
 *    Filename:       user_interface.c	                                *
 *    Date:           6/15/05                                          *
 *    File Version:   5.00                                             *
 *    Project
:        53                                                            *
 *    Drawing:        2                                                *
 *                                                                     *
 *    Tools used:    MPLAB C30 Compiler v 1.32                         *
 *                                                                     *
 *    Linker File:    p30f6010.gld                                     *
 *                                                                     *
 *                                                                     *
 ***********************************************************************
 *	Code Description
 *  
 *  This file contains all the code for implementing the user interface.
 *  The user interface functions are called from the slow event handler,
 *  which runs every 100 msec.
 *  Different screens are called depending on the operating state of the
 *  software.  The process_switches() function uses the debounced push
 *  buttons and determines the length of time the button has been 
 *  pressed for fast incrementing of the parameter. It also moves the 
 *  system between the various states according to the push buttons.
 *  The SaveToEEPROM() function saves a changed parameter to EEPROM.
 *  The process_parameters() function does any scaling or units
 *  conversion that is required for the stored parameters.
 **********************************************************************/


#include "general.h"
#include "hardware.h"
#include "defs.h"
#include "extern_globals.h"
#include "lcd_messages.h"
#include "xlcd.h"

/*********************************************************************/
struct parameter_data 
{
    	unsigned int min;		/*minimum allowed value */
    	unsigned int max;    /*maximum allowed value */
    	unsigned int med;		/*the medium rate parameter increment*/
		unsigned int fast;	/*the fast rate parameter increment*/
    	char *line1_msg;     /*line 1 parameter screen message */
		char *units_msg;		/*units message used for RHS line 2*/
    };

const struct parameter_data parameter_data [] =	{
/* Min, Max, Med delta, Fast delta, Line1 msg, Units msg */
{0,1,1,1,				"DIRECTION DEMAND"," FWD/BKWD"},//0
{0,3,1,1,				"  CONTROL MODE  ","         "},//1
{0,32767,10,100,		"Lock Pos.1  Time"," 10ms/bit"},//2
{0,32767,10,100,		"Lock Pos.2  Time"," 10ms/bit"},//3
{0,100,5,10,			"Lock Pos.1 Dem. "," % Demand"},//4
{0,100,5,10,			"Lock Pos.2 Dem. "," % Demand"},//5
{1,9999,10,100,		"Ramp Start Speed"," Revs/min"},//6
{1,9999,10,100,		"Ramp End   Speed"," Revs/min"},//7
{0,100,2,5,				"Ramp Start Dem. "," % Demand"},//8
{0,100,2,5,				"Ramp End   Dem. "," % Demand"},//9
{1,32767,10,100,		"Ramp Duration   "," 10ms/bit"},//10
{1,32767,10,100,		"Phase Adv. Start"," Revs/min"},//11
{1,32767,10,100,		"Phase Adv. Slope"," see docs"},//12
{1,32767,10,100,		"Stall Time Limit"," 10ms/bit"},//13
{1,32767,10,100,		"Over Speed Limit"," Revs/min"},//14
{1,32767,10,100,		"Over Volts Limit","0.1V/bit "},//15
{1,32767,10,100,		"Over Current Lim","0.1A/bit "},//16
{0,32767,100,500,		"Current P Gain  ","         "},//17
{0,32767,100,500,		"Current I Gain  ","         "},//18
{0,32767,100,500,		"Current D Gain  ","         "},//19
{0,9999,10,100,		"Speed P Gain    ","         "},//20
{0,9999,10,100,		"Speed I Gain    ","         "},//21
{0,32767,10,100,		"Voltage Demand  ","0.1V/bit "},//22
{0,32767,100,500,		"Volts P Gain    ","         "},//23
{0,32767,100,500,		"Volts I Gain    ","         "},//24
{2,48,2,4,				"No. Motor Poles "," Even   !"},//25
{1,3670,10,100,		"Current Scale X ","         "},//26
{1,6550,10,100,		"Current Scale / ","         "},//27
{1,3670,10,100,		"Volts Scale X   ","         "},//28
{1,6550,10,100,		"Volts Scale /   ","         "},//29
{1,100,5,20,			"Tolerance Check "," %       "},//30
{0,1,1,1,				"Auto Re-acquire "," ON / OFF"},//31
{0,255,1,2,				"Blanking Count  ","         "},//32
{0,255,1,2,				"Zero X Level Thd","         "},//33
{1,1023,1,2,			"Acquire Threshld"," ADC VAL "},//34
{0,255,1,2,				"Acquire Level Td","         "},//35
{0,32767,10,100,		"Rotation Timeout"," 10ms/bit"},//36
{1,100,2,10,			"Pot / for Duty  ","         "},//37
{1,100,2,10,			"Pot / for Currnt","         "},//38
{1,64,2,5,				"Pot X for Speed ","         "},//39
{0,1,1,1,				"Starting Control"," 1=V, 0=I"},//40
{0,100,5,10,			"Windmilling Dem."," % Demand"},//41
{1,32767,10,100,		"Braking Ramp T  "," 10ms/bit"},//42
{0,1,1,1,				"Acquire Method  "," see docs"},//43
{1,9999,10,100,		"ZeroX Enable Spd"," Revs/min"},//44
};

volatile unsigned int user_parameters_RAM[NO_PARAMETERS] =
{	FORWARDS,		//	Direction Demand 
	CLOSED_VOLTS,	// Speed Control Mode - See defs.h 
	50,				// First Lock Position Time in units of medium event (10ms) 
	50,				// Second Lock Position Time in units of medium event (10ms) 
	45,				// % Demand Used For Lock Position 1
	45,				// % Demand Used For Lock Position 2			
	10,				// Starting Speed for Ramp / RPM 
	2000,			// Finish Speed for Ramp / RPM	
	55,				// % Demand Used At Start of Ramp
	65,				// % Demand Used At End of Ramp	
	50, 			// Duration of starting ramp in units of medium event (10ms)
	1500,			// Phase Advance Start Speed in RPM 
	25,				// Phase Advance Slope in 1/1000th elec. degree / RPM	
	100,			// Stall Time Limit in units of medium event (10ms) 
	3500,			// Over Speed Trip in RPM	
	500,			//	Over Voltage Trip in 1/10ths V
	100,			// Over Current Limit in 1/10ths A 
	900,			// Current Loop P Gain 
	100,			// Current Loop I Gain 
	0,				// Current Loop D Gain 
	9000,			// Speed Loop P Gain 
	400,			// Speed Loop I Gain 
	490,			// Voltage Demand for Brake Chopper in 1/10ths V 
	10000,			// Voltage Loop P Gain 
	5000,			// Voltage Loop I Gain 
	10,				// Number of Motor Poles 
	100,			// Current Scaling X - see below
	539,			// Current Scaling / - see below
	100,			// Voltage Scaling X - see below
	1305,			// Voltage Scaling / - see below
	99,				// % Tolerance used for Lost Check - see below
	1,				// Auto-reaquire if lost ON/OFF
	2,				// Blanking Length used for zero X in no of ADC samples
	2,				// No. of samples required > or < VDC/2 before zero X checked for
	12,  			// ADC value used for rising edge detect for acquisition routine
	6,				// No of samples of VPH before rising edge detect done in acquisition
	5,				// Rotation Timeout in units of medium event (10ms)
	1,				// Divisor used to scale pot ADC reading to Duty Cycle
	32,				// Divisor used to scale pot ADC reading to I Dem in ADC
	3,				// X Used to Scale pot ADC reading to speed demand in rpm
	1,				// 1=Use Voltage Control for Starting, 0=Use Current Control
	20,				// % Demand Used For Windmilling Braking
	1,				// Duration of braking ramp in units of medium event (10ms)
	0,				// Use Method 1 (=0) or Method 2 (=1) for acquisition of position
	1500};			// Speed at Which ZeroX detection enabled when using method1 acqusition



// Current and Voltage Scaling X and / Parameters
// These are used to scale ADC readings into Amps or Volts as follows: 
// If you get 12.87 A/V for ibus or 12.87 V/V for vdc then
// X = 100 and / = 1287 is one possible combination

// For LV Power Module Use The Following values: 
// VX=100 V/=1305 i.e scaling is 13.05 V/V		 
// If LK11&12 Open 	IX=100 I/=539 i.e scaling is 5.39A/V 
// If LK11&12 Closed	IX=10  I/=119 i.e.scaling is 11.9A/V 

// For HV Power Module Use The Following values: 
// VX=10  V/910 i.e. scaling is 91.0 V/V 
// If LK11&12 Open	IX=100 I/=108 i.e scaling is 1.08A/V 
// if LK11&12 Closed IX=100 I/=239 i.e scaling is 2.39A/V 

// Tolerance for Lost Check Parameter 
// Every 60 electrical degrees a new zero crossing event should
// be detected. In order to determine if the system is lost, 
// a check is carried out comparing the time elapsed since the 
// last zero crossing and the one before that. 
// If the tolerance parameter is set to 25% then up to 25%
// variation between the two times is considered to be acceptable.
// If the system fails the check, then the system is lost.
// Some natural variation in the times is to be expected even at
// a constant speed due to timer and ADC resolution as well as 
// motor asymmetry. Speed changes will also result in variation.
// It is suggested that the parameter is not set to less than 25%
// as otherwise the system may be determined to lost just due to
// normal variations.
/*********************************************************************/



void screen_handler(void);
void process_switches(void);
void process_parameters(void);
void debounce_switches(void);
void Write_Screen(unsigned const char *,unsigned const char *);
void Edit_Screen(void);
void Run_Screen(void);
void uint_to_string(unsigned int, unsigned char *);

extern void erase_flash_row(unsigned int);
extern void program_flash(unsigned int, unsigned int);

struct interface_flags {
				unsigned EDIT_PARAM : 1;
				unsigned EDIT_MENU :	1;
				unsigned PARAM_CHANGING	: 1;
				unsigned S4_RISING :1;
				unsigned S5_RISING :1;
				unsigned S6_RISING :1;
				unsigned S7_RISING :1;
				unsigned RUN_FRAME :1;
				unsigned UNUSED : 8;
};

struct interface_flags interface_flags;

void screen_handler(void)
{
	static unsigned char blink_counter=1;
	
	blink_counter++;

	if (blink_counter > 40)	blink_counter=0;

	switch (run_state)
	{
		case RUNNING:	Run_Screen();
							break;
		case FAULT:		
		case STANDBY:	
							process_parameters();
							
							
							if (interface_flags.EDIT_MENU)
							{
								if (interface_flags.EDIT_PARAM)
								{
									if (blink_counter>10) Edit_Screen();
									else	
									{
										if (!interface_flags.PARAM_CHANGING)
											Write_Screen(parameter_data[param].line1_msg, \
													 		"S6 STORE S7 EXIT");
										else	Edit_Screen();
									}
								}
								else
								{
									Write_Screen(parameter_data[param].line1_msg, \
													 		"S6 ALTER S7 EXIT"); 
								}
							}
							else
							{
								if (run_state==FAULT)
								{
									if (blink_counter>10)
										Write_Screen(&line1_fault_message[0],&line2_fault_message[trip_state-1][0]);
									else
										Write_Screen(&line1_fault_message[0], \
													 		"S4 EDIT S7 RESET");
								}
								else
									Write_Screen(&line1_standby_message[0],&line2_standby_message[0]);
							}
							break;

		case INITIALIZING: 	Write_Screen(&line1_boot_message[0],&line2_boot_message[0]);
									break;

		case STARTING:	Write_Screen(&line1_starting_message[0],&line2_starting_message[0]);
							break;
		default:			break;
	}
	return;
}



void process_switches(void)
{
	static unsigned char previous_valid_switch_states=0xFF;
	static unsigned char key_hold_timer=0;
	static unsigned int param_increment=1;

	if ((previous_valid_switch_states==valid_switch_states) \
		&& (interface_flags.EDIT_MENU==FALSE))	
	{
		previous_valid_switch_states=valid_switch_states;
		return;
	}

	if (((previous_valid_switch_states & 0x01)==FALSE) && (valid_switch_states & 0x01))
		interface_flags.S4_RISING=TRUE;
	else
		interface_flags.S4_RISING=FALSE;

	if (((previous_valid_switch_states & 0x02)==FALSE) && (valid_switch_states & 0x02))
		interface_flags.S5_RISING=TRUE;
	else
		interface_flags.S5_RISING=FALSE;

	if (((previous_valid_switch_states & 0x04)==FALSE) && (valid_switch_states & 0x04))
		interface_flags.S6_RISING=TRUE;
	else
		interface_flags.S6_RISING=FALSE;

	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;

	if ((interface_flags.EDIT_MENU==TRUE) && \
		 (interface_flags.EDIT_PARAM==FALSE))
	{
		if (interface_flags.S5_RISING)
		{
			if (param<(NO_PARAMETERS-1))	param++;
			else				param=0;
		}
		if (interface_flags.S4_RISING)
		{
			if (param>0)	param--;
			else				param=(NO_PARAMETERS-1);
		}
		if (interface_flags.S6_RISING)	
		{
			interface_flags.EDIT_PARAM=TRUE;
			new_param_value=user_parameters_RAM[param];
		}
		if (interface_flags.S7_RISING)	interface_flags.EDIT_MENU=FALSE;
		return;
	}
	if (interface_flags.EDIT_PARAM)
	{
		if (key_hold_timer > FAST_RATE_T)	param_increment=parameter_data[param].fast;
		else
		{
			if (key_hold_timer > MED_RATE_T) param_increment=parameter_data[param].med;
			else	param_increment=1;
		}

		if (valid_switch_states & 0x02)
		{
			if (new_param_value < (parameter_data[param].max - param_increment))
				new_param_value+=param_increment;
			else
				new_param_value=parameter_data[param].max;

			if (key_hold_timer<255)	key_hold_timer++;
		}

		if (valid_switch_states & 0x01)
		{
			if (new_param_value > (parameter_data[param].min + param_increment))
				new_param_value-=param_increment;
			else
				new_param_value=parameter_data[param].min;

			if (key_hold_timer<255)	key_hold_timer++;
		}
		if ((!(valid_switch_states & 0x01)) && (!(valid_switch_states & 0x02)))
		{
			key_hold_timer=0;
			interface_flags.PARAM_CHANGING=FALSE;
		}
		else	interface_flags.PARAM_CHANGING=TRUE;

		if (interface_flags.S6_RISING)
		{
			user_parameters_RAM[param]=new_param_value;
			process_parameters();
			param_increment=1;
			interface_flags.EDIT_PARAM=FALSE;
		}
		if (interface_flags.S7_RISING)	
		{

⌨️ 快捷键说明

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