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

📄 speed.c

📁 3 phase motor driver source code with freescale MCU
💻 C
字号:
/*
 * Project:		 	3-PHASE AC DRIVE
 *
 * Microcontroller: Motorola MC68HC908MR24 
 *
 * Module:			SPEED.C
 * Revision/Date:	2.0 / February 2000 
 * Description:		This routine calculates required speed V_command	
 *					It is called by software timer in MAIN.C
 *
 * Compiler:		Hiware08 Compiler
 *
 * Author:			Radim VISINKA
 * Company:			MOTOROLA SPS
 *					Roznov System Application Laboratory 
 *					Roznov pod Radhostem, Czech Republic 
 *
 * ===================================================================
 *
 * Copyright (c):	MOTOROLA Inc.,1998, All rights reserved.
 *
 * ====================================================================
 * THIS SOFTWARE IS PROVIDED BY MOTOROLA RSAL "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL MOTOROLA RSAL OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */


/* DEFINITION_START */


/* Include Header Files */
#include "MR24IO.H"
#include "CONST.H"			/* file contains global constants and
							   definitions */


/* Assembler Subroutine Definitions */

/* Constant Definitions */

	/* time table */
const unsigned int Time_table[10] = 
{
6250 ,		/*  5 sec + 1.25 sec for ramping at 20 Hz/sec2*/
7500 ,		/*  5 sec + 2.5 sec */
7500 ,		/*  5 sec + 2.5 sec */
7500 ,		/*  5 sec + 2.5 sec */
15000 ,		/* 10 sec + 5   sec */
18000 ,		/* 10 sec + 8   sec */
10000 ,		/*  5 sec + 5   sec */
7500 ,		/*  5 sec + 2.5 sec */
20000 ,		/* 15 sec + 5   sec */
0xffff  	/* STOP */ 
};


	/* speed table - format 8.8 */
const signed int Speed_table[10] =
{
0x1900 	,	/*   25 Hz */
0xE700	,	/*  -25 Hz */
0x1900	,	/*   25 Hz */
0xE700	,	/*  -25 Hz */
0x6400 	,	/*  100 Hz */
0x9c00	,	/* -100 Hz */
0x1900	,	/*   25 Hz */
0xE700 	,	/*  -25 Hz */
0x6400	,	/*  100 Hz */
0x0000		/*   0 Hz - STOP*/	 

}; 


/* Global Variables (External) - 8 bit */
extern unsigned char Mcs_status;	/* Status register of MCS */
extern unsigned char Fault_flags;	/* 8 bit fault flag register */
extern unsigned char Om_mode;		/* Operational Mode register */
extern unsigned char Amplitude;		/* 0 to 127 gives 0 to 100% modulation*/
extern unsigned char Dcb_voltage;	/* AD value of DC Bus voltage */


/* Global Variables (External) - 16 bit */
extern signed int V_command;		/* command speed */
extern signed int V_com_actual;		/* actual ramp speed */
extern signed int Table_inc;	   	/* increment value for wave pointer update*/
extern signed int V_pi;				/* output of PI controller */


/* Local Variables - 8 bit */
unsigned char Speed_ptr = 0;	/* pointer for speed/time table */
unsigned char Start_cnt = 0;	/* counter for switch-on time of START button*/
unsigned char Ad_pointer = 0;	/* AD conversion pointer*/
unsigned char Pot_voltage = 0;	/* AD value of potentiometer voltage */
unsigned char Flags = 0; 		/* 8 bit flag register for speed routine */
#define SWITCH_INI_FL 	0x10  	/* start/stop initialisation flag */

unsigned char Flt_rec_led_cnt = 0; 
								/* flash constant of fault LED */
 
/* Local Variables - 16 bit */
unsigned int Time_reg = 0;		/* time counter for demo speed/time profile */
unsigned int Gfr_counter = 0;	/* general fault recovery counter */
			  

/* DEFINITION_END */



void speed (void)
  {
		
			
/*PORTE |= TEST; */		/* TEST pin =  1*/		
		
	/* INPUT SCAN STATE */

	if (Ad_pointer == 0x00)
	{
	  Pot_voltage = ADRL;	/* read AD result 8-bit */
	  Ad_pointer = 0x01;	/* set-up next conversion */
	  ADSCR = 0x01;			/* starts next conversion - Dcb_voltage */
							/* ADC channel 1 */
	}
	
	
	else   /* if (Ad_pointer == 0x01) */
	{
	  Dcb_voltage = ADRL;	/* read AD result - 8-bit */
	  Ad_pointer = 0x00;	/* set-up next conversion */
	  ADSCR = 0x00;			/* starts next conversion - Pot_voltage */
							/* ADC channel 0 */
	}


	/* OPERATIONAL MODE DISRIBUTION STATE */

	if (Om_mode & 0x04)			
	{

						/*  D E M O   O M  */
			
	  if (Mcs_status & START_STOP_FLG)	/* if run demo command accepted */
	  {
	    if (Time_reg == 0)	    /* if new speed data required */
	    {
	      Time_reg = (Time_table[Speed_ptr]);	/* load required time */
     	  V_command = (Speed_table[Speed_ptr]);	/* load required speed */
		  Speed_ptr += 1;
	      if (Time_reg == 0xffff)			/* if STOP command loaded */
	      {
		    Speed_ptr = 0; 					/* speed_ptr = 0 */
		    Mcs_status &= ~(START_STOP_FLG);		/* start_flag = 0 */
		    Time_reg = 0;					/* time_reg = 0 */
	      }  
	    }
	    else
	   	{					/* if required speed loaded */
	      Time_reg -= 1;	/* decrement time counter register */
	    }	    	    	  
	  }

	  else /* start_demo_flag = 0 */
	  {	  
	    if ((~PORTA) & 0x20)			/* if start switch = start */
	    {
	      if (Flags & SWITCH_INI_FL)   	/* if previous switch was = stop */
		  {
			if(Start_cnt == 5)			/* if start_cnt = 5 */
			{
			  Start_cnt = 0;
			  Mcs_status |= START_STOP_FLG;  	/* START flag = 1 */			  
			  Flags &= (~(SWITCH_INI_FL));  	/* SWITCH_INI_FL = 0 */
			}
			else 						/* if start_cnt < 5 */
			{
			Start_cnt += 1;			  	/* increase start_cnt */
			}
		  }
	    }
	    else						 	/* if start swich = stop */
	    {
	      Flags |= SWITCH_INI_FL;  		/* SWITCH_INI_FL = 1 */
	      Start_cnt = 0; 
	    } 
	  }
	}
	else  /* Om_mode = 0x00 */
	{


			/*    M A N U A L   O M     */
	  				  
	  if (Mcs_status & START_STOP_FLG)	/* if run demo command accepted */
	  {
		if (PORTA & 0x20)			/* if start switch = start */
		{
		  if (Pot_voltage > 0xf0)
			{
			  Pot_voltage = 0xf0;
			}
		  if (PORTA & 0x10)			  /* if forw/rew switch = forw. */
		  {
		    V_command = Pot_voltage	<< 7;
		  }
		  else							  /* if forw/rew switch = reverse */
		  {
			V_command = - (Pot_voltage << 7);
		  }	
		}
		else  							/* if start switch = stop */
		{
		  V_command = 0x00;	 
  		  Mcs_status &= ~(START_STOP_FLG);	  /* stop */		  
		}
	  }	 

	  else							/* if the motor is stopped */
	  {
	  	if (PORTA & 0x20)			/* if start switch = start */
	    {
	      if (Flags & SWITCH_INI_FL)   	/* if previous switch was = stop */
		  {
			if(Start_cnt == 5)			/* if start_cnt = 5 */
			{
			  Start_cnt = 0;
			  Mcs_status |= START_STOP_FLG;  	/* START flag = 1 */
			}
			else 						/* if start_cnt < 5 */
			{
			Start_cnt += 1;			  	/* increase start_cnt */
			}
		  }
	    }
	    else						 	/* if start swich = stop */
	    {
	      Flags |= SWITCH_INI_FL;  		/* SWITCH_INI_FL = 1 */
	      Start_cnt = 0; 
	    } 
	  }
	}


	/* FAULT DETECTION STATE */

	if (Dcb_voltage > DCB_VOLT_LIMIT)	/* checks overvoltage */
	{
	  Fault_flags |= OVER_VOLTAGE;
	}

	/* there can be placed a definition of other fault states */

	if ((Fault_flags & OVER_VOLTAGE) || (Fault_flags & OVER_CURRENT))	   /* in case of over voltage */
	{
	  if (Fault_flags & OVER_VOLTAGE)
	  {
	    Fault_flags &= (~(OVER_VOLTAGE));
		  								 /* over voltage flag is cleared*/
	    Gfr_counter = OV_FLT_REC_TIME; /* Over voltage fault recovery
	     								 time is loaded */
	  }  
	  else /* overcurrent */
	  {
	    Fault_flags &= (~(OVER_CURRENT)); 
		  								 /* over current flag is cleared*/
	    Gfr_counter = OC_FLT_REC_TIME; /* Over current fault recovery
	     								 time is loaded */	
	  }

	  PCTL1 |= 0xc0;				   /* disable MCPWM */
	  FTAC |= 0x04;					/* clears FFLAG2 - fault2 flag bit */

	  Mcs_status |= GF_FLAG;		 /* general fault flag is set*/
	  Mcs_status &= (~MCS_STOP);	
	  Mcs_status &= (~MCS_RUN);	
	  Mcs_status &= (~(GFR_FLAG)); 	 /* gen. fault recovery flag is cleared */
	  Mcs_status &= (~(START_STOP_FLG));	 /* Start/Stop flag is cleared */
	  Flags &= (~(SWITCH_INI_FL));
	  V_com_actual = 0;
	  V_command = 0;
	  Table_inc = 0;						
	}
	else
	{
	  if (Mcs_status & GF_FLAG)		/* if GF flag was set and 
	  										no faults are pending */
	  {
	    Mcs_status |= GFR_FLAG;		/* general fault recovery flag is set*/	
	    Mcs_status &= (~(GF_FLAG)); /* gen. fault recovery flag is cleared */		
	  }
	}
 
						
	/* RUN ENABLE STATE */
	
	if ((~(Mcs_status)) & GFR_FLAG)	 /* if no fault occured */
	{
	 if ((~(Mcs_status)) & GF_FLAG)
	 {
	  if (Mcs_status & START_STOP_FLG)
	  {
		PCTL1 &= 0x3f;						/* enable MCPWM */
		if ((V_command == 0) && (V_com_actual == 0))
		{
		  Mcs_status &= (~MCS_RUN);				/* stop state */
		  Mcs_status &= (~MCS_STAND_BY);
		  Mcs_status |= MCS_STOP;		
		}
		else
		{
		  Mcs_status &= (~MCS_STOP);			/* run state */
		  Mcs_status &= (~MCS_STAND_BY);
		  Mcs_status |= MCS_RUN;	
		}
	  }
	  else
	  {
	  	if (V_com_actual == 0)		 
		{
		  PCTL1 |= 0xc0;				   		/* disable MCPWM */
		  Mcs_status &= (~MCS_STOP);			/* stand_by state */
		  Mcs_status &= (~MCS_RUN);
		  Mcs_status |= MCS_STAND_BY;
		}
	  }
	 }
	}
	else					/* if fault recovery required */
	{
	  Gfr_counter--;
	  if (Gfr_counter == 0)
	  {
	   Mcs_status &= (~(GFR_FLAG));
	   Mcs_status |= MCS_STAND_BY;
	  } 	  			
	}


	/* LED DRIVER STATE */

	if (Mcs_status & GF_FLAG)  		/* General fault status */
	{
	  PORTC |= FAULT_LED;	/* FAULT_LED ON */
	  PORTC &= ~(MCS_LED);	/* STATUS_LED OFF*/
	  Flt_rec_led_cnt = FLT_REC_LED_FLASH;
	}	 						
	else
	{
	  if (Mcs_status & GFR_FLAG)		/* GFR: flash fault LED  */
	  {
		Flt_rec_led_cnt--;
		if (Flt_rec_led_cnt == 0)
		{
		  Flt_rec_led_cnt = FLT_REC_LED_FLASH;
		  if (PORTC & FAULT_LED)
		  {
		    PORTC &= ~(FAULT_LED);	/* FAULT_LED OFF */
		  }	
		  else
		  {
		  	PORTC |= FAULT_LED;	/* FAULT_LED ON */
		  }
		}
	  }
	  else
	  {
	  	PORTC &= ~(FAULT_LED);	/* FAULT_LED OFF */
		if (Mcs_status & MCS_STAND_BY)	 /* if STAND_BY status */
		{
		  PORTC &= ~(MCS_LED);	/* STATUS_LED OFF*/
		}
		else			  /* run or stop status */
		{		  
		  PORTC |= MCS_LED;	/* STATUS_LED ON */	
		}
	  }
	}
  }							


⌨️ 快捷键说明

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