www.pudn.com > BLDC_Sensorless_FOC.rar > Main.c, change:2015-08-07,size:6176b


/** 
 * @cond 
 *********************************************************************************************************************** 
 * 
 * Copyright (c) 2015, Infineon Technologies AG 
 * All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the 
 * following conditions are met: 
 * 
 *   Redistributions of source code must retain the above copyright notice, this list of conditions and the  following 
 *   disclaimer. 
 * 
 *   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 
 *   following disclaimer in the documentation and/or other materials provided with the distribution. 
 * 
 *   Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote 
 *   products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 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 THE COPYRIGHT HOLDER OR 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. 
 * 
 **********************************************************************************************************************/ 
/*****************************************************************************/ 
/** BLDC: Motor Drive with Sensorless FOC for the QBL4208-61-04-013 motor   **/ 
/*****************************************************************************/ 
/** Press MON to start/stop the motor                                       **/ 
/*****************************************************************************/ 
 
/******************************************************************************* 
**                      Includes                                              ** 
*******************************************************************************/ 
#include "Main.h" 
#include <Adc1.h> 
#include <Adc2.h> 
#include <Bdrv.h> 
#include <Ccu6.h> 
#include <csa.h> 
#include <Gpt12e.h> 
#include <Scu.h> 
#include <int.h> 
#include <mon.h> 
#include <wdt1.h> 
#include "Mat.h" 
#include "Emo_RAM.h" 
 
/******************************************************************************* 
**                      Private Macro Definitions                             ** 
*******************************************************************************/ 
 
/******************************************************************************* 
**                      Private Function Declarations                         ** 
*******************************************************************************/ 
static void Main_lStartMotor(void); 
static void Main_lStopMotor(void); 
 
/******************************************************************************* 
**                      Globale Variable Definitions                          ** 
*******************************************************************************/ 
 
/******************************************************************************* 
**                      Private Variable Definitions                          ** 
*******************************************************************************/ 
static uint8 bMotorRun; 
/******************************************************************************* 
**                      Private Constant Definitions                          ** 
*******************************************************************************/ 
 
/******************************************************************************* 
**                      Global Function Definitions                           ** 
*******************************************************************************/ 
/** \brief Executes main code. 
 * 
 * \param None 
 * \return None 
 * 
 */ 
int main(void) 
{ 
  SCU_Init(); 
  ADC2_Init(); 
  CCU6_Init(); 
  Bdrv_Init(); 
  Adc1_Init(); 
  GPT12E_Init(); 
  CSA_Init(); 
	MON_Init(); 
  Emo_Init(); 
  INT_Init(); 
	 
	bMotorRun = 0; 
	 
	/* set your desired mechanical speed of the motor */ 
	Emo_setspeedreferenz(1000); 
	 
  while (1) 
  {  
		 
		/* Watchdog Service */ 
		WDT1_Service(); 
 
		/* wait for MON button pressed */ 
		if (MON_Debounce(0u) == 0u) 
		{ 
			if (bMotorRun == 0u) 
			{ 
				/* start the motor to run up to the desired speed */ 
				Main_lStartMotor(); 
				bMotorRun = 1u; 
			} 
			else 
			{ 
				/* stop the motor  */ 
				Main_lStopMotor(); 
				bMotorRun = 0u; 
			} 
			/* wait until MON button is released */ 
			while (MON_Debounce(1u) == 0u) {} 
		} 
		 
  } 
} /* End of main() */ 
 
/******************************************************************************* 
**                      Private Function Definitions                          ** 
*******************************************************************************/ 
static void Main_lStartMotor(void) 
{ 
  uint32 Error; 
   
  Error = EMO_ERROR_NONE; 
  if(Error == EMO_ERROR_NONE) 
  { 
    Error = Emo_StartMotor((uint32)1u); 
  } 
  if(Error == EMO_ERROR_NONE) 
  { 
  }   
  else /* Error */      
  { 
     
  }   
} /* End of Main_lStartMotor */ 
 
 
static void Main_lStopMotor(void) 
{ 
  uint32 Error; 
   
  Error = Emo_StopMotor();  
 
  if(Error != EMO_ERROR_NONE) 
  { 
     
  }   
} /* End of Main_lStopMotor */ 
 
/* In case the VCP supply fails the motor stops */ 
void BDRV_Diag_Supply(void) 
{ 
	Main_lStopMotor(); 
} 
 
/* in case an over current is detected on one of   ** 
** the MOSFETs then the motor will be switched off */ 
void BDRV_Diag(void) 
{ 
	Main_lStopMotor(); 
}