📄 mtc.c
字号:
/**************** (c) 2004 STMicroelectronics ********************************
PROJECT : 3-phase AC induction motor drive Software Library
COMPILER : COSMIC / METROWERKS
MODULE : mtc.c
VERSION : 1.0.0
CREATION DATE : April 2004
AUTHOR : V. Onde / Microcontroller Division Applications
Consumer & Micro Group
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DESCRIPTION : Low level motor control routines
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
MODIFICATIONS :
*******************************************************************************
THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. ST MICROELECTRONICS
SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES
WITH RESPECT TO ANY CLAIMS ARISING FROM USE OF THIS SOFTWARE.
******************************************************************************/
#include "lib.h" /* General purpose typedefs and macros */
#include "mtc.h" /* Public motor control peripheral function prototypes */
#include "MTCparam.h" /* Motor Control parameters */
#include "mtc_bits.h" /* MTC peripheral Hardware Registers declaration */
#include "pwmart.h" /* Software Timebase needed during braking */
#include "config.h" /* Software library configuration file */
// ST7FMC peripherals Hardware Registers declaration
#include "ST7FMC2N6.h"
/* --------------------------- Private typedefs ---------------------------- */
#ifdef __HIWARE__ /* Metrowerks Compiler */
typedef struct {
TwoBytes Capture;
u8 Prsc_Reg;
} SpeedMeas_s, * near PSpeedMeas_s;
#elif defined(__CSMC__) /* Cosmic Compiler */
typedef struct {
TwoBytes Capture;
u8 Prsc_Reg;
} SpeedMeas_s, * @tiny PSpeedMeas_s;
#endif
/* -------------------------- Private variables ---------------------------- */
// Some variables accessed in assembly located in page zero to decrease CPU load
// WARNING: if SensorPeriod structure is put outside Zero page, the declaration
// of the pointer PSpeedMeas_s has to be modified (remove near/@tiny)
#ifdef __HIWARE__ /* Metrowerks Compiler */
#pragma DATA_SEG SHORT _ZEROPAGE
volatile u8 SineMag;
volatile u16 SineFreq;
volatile u16 SineFreqBuf;
volatile s8 PhaseShift;
volatile u8 MTCStatus;
volatile u16 Phase;
volatile SpeedMeas_s SensorPeriod[SPEED_FIFO_SIZE];
volatile PSpeedMeas_s SpeedFIFO_Index;
#pragma DATA_SEG DEFAULT
#elif defined(__CSMC__) /* Cosmic Compiler */
@tiny volatile u8 SineMag;
@tiny volatile u16 SineFreq;
@tiny volatile u16 SineFreqBuf;
@tiny volatile s8 PhaseShift;
@tiny volatile u8 MTCStatus;
@tiny volatile u16 Phase;
@tiny volatile SpeedMeas_s SensorPeriod[SPEED_FIFO_SIZE];
@tiny volatile PSpeedMeas_s SpeedFIFO_Index; // Pointer initialized in MTC_InitPeripheral function
#endif
static u16 StatorFreq;
static u16 PrevStatorFreq;
static u16 BrakeDutyCycle;
static BOOL MCES_Status;
/* -------------------------- Private functions ---------------------------- */
u32 GetLastTachoPeriod(void);
u32 GetAvrgTachoPeriod(void);
/* --------------------------- Private macros ------------------------------ */
#define MTC_EnableClock() ( MCRA |= (1<<MCRA_CKE) )
#define MTC_DisableClock() ( MCRA &= ((1<<MCRA_CKE)^255) )
/* -------------------------- Private constants ---------------------------- */
// MTCStatus bitfield
#define FREQ_CHANGE ((u8)0x01)
#define BRAKE_WAIT_DEMAG ((u8)0x02)
#define BRAKE_SET_UP ((u8)0x04)
#define BRAKE ((u8)0x08)
#define RATIO_INC ((u8)0x10)
#define RATIO_DEC ((u8)0x20)
#define INIT_ROLLING_AVRG ((u8)0x40)
#define DO_ROLLING_AVRG ((u8)0x80)
#define BRAKE_MASK ((u8) BRAKE_WAIT_DEMAG + BRAKE_SET_UP + BRAKE )
#define VOLT_INIT ((u8)0) /* Voltage initialisation at MCU start-up */
#define FREQ_INIT ((u16)50) /* Freq initialisation at MCU start-up */
#define STATOR_DEMAG_TIME ((u16)300) /* Time in ms before applying DC current for braking*/
#define CURRENT_SETUP_TIME ((u8)5) /* Time in ms between two consecutive values of duty cycle during braking current increase */
/*-----------------------------------------------------------------------------
ROUTINE Name : MTC_ResetPeripheral
Description: This function resets the circuitry of the Motor Controller Peripheral,
at the sole exception of the MDTG and MPOL registers.
-----------------------------------------------------------------------------*/
void MTC_ResetPeripheral(void)
{
SetBit(MCFR,MCFR_RST);
ClrBit(MCFR,MCFR_RST);
}
/*-----------------------------------------------------------------------------
ROUTINE Name : MTC_EnableMCOutputs
MTC_DisableMCOutputs
Description: These functions allows either to output PWM signals on the
MCO outputs or to put these outputs in their RESET
configuration (user selectable state: Hi-Z, Low or High)
MTC_DisableMCOutputs immediately disconnects the MCOx PWM outputs pins
from the Motor Controller peripheral. It resets the MOE bit in
the MCRA register, thus causing the MCOx pins to be in their
reset configuration, as defined in the options bytes (high
impedance or low impedance high/low state).
-----------------------------------------------------------------------------*/
void MTC_EnableMCOutputs(void)
{
MCRA |= (1<<MCRA_MOE);
}
void MTC_DisableMCOutputs(void)
{
MCRA &= ((1<<MCRA_MOE)^255);
}
/*-----------------------------------------------------------------------------
ROUTINE Name : MTC_InitPeripheral
Description: Initialization of the motor control peripheral hardware registers,
for the sinewave general parameters (such as PWM frequency, output
polarity, deadtime, interrupts,...) and speed feedback processing
(tacho input selection, edge sensitivity,...).
Comment: All required motor control related interrupts are enabled upon
completion of this routine.
-----------------------------------------------------------------------------*/
void MTC_InitPeripheral(void)
{
MTC_ResetPeripheral();
SET_MTC_PAGE(1);
MPOL = L6386_POLARITY; // Set polarity according to IGBT drivers
MDTG = DEADTIME; // Deadtime Duration
SetBit(MDTG,DTE); // Deadtime enabled
SetBit(MDTG,PCN); // Three independant PWM configuration
MPWME = DG_MSK; // Use Debug outputs
MPAR = TACHO_FALLING; // Speed Sensor mode enabled, falling edge sensitive
SET_MTC_PAGE(0);
MPCR = PWM_PRSC;
SetBit(MPCR,MPCR_CMS); // Centered pattern
MREP = 1; // Preload registers are loaded every PWM cycle
MCP0L = (u8)PWM_MCP0; // Set PWM frequency
MCP0H = ((u8)(PWM_MCP0>>8));
ToCMPxL(MCPUL,PWM_MCP0/2); // 50% duty cycle on U
ToCMPxH(MCPUH,PWM_MCP0/2);
ToCMPxL(MCPVL,PWM_MCP0/2); // 50% duty cycle on V
ToCMPxH(MCPVH,PWM_MCP0/2);
ToCMPxL(MCPWL,PWM_MCP0/2); // 50% duty cycle on W
ToCMPxH(MCPWH,PWM_MCP0/2);
SetBit(MCRA,MCRA_DAC); // Direct access mode to write into MPHST
MPHST = MCIC; // Comparator input for Tacho signal
ClrBit(MCRA,MCRA_DAC); // Disable direct access
MISR = 0; // Clear pending interrupts
MTCStatus = 0;
MCES_Status = FALSE;
// Init pointer on FIFO where speed infos are stored
SpeedFIFO_Index = (PSpeedMeas_s)SensorPeriod;
MTC_Set_CounterClockWise_Direction();
MTC_EnableClock(); // Enable peripheral clock and update active
// registers with preloaded values
// Unmask Update, Ratio change, Capture and emergency Stop interrupts
MIMR = MIMR_PUM_OR + MIMR_RIM_OR + MIMR_CIM_OR + MIMR_EIM_OR;
}
/*-----------------------------------------------------------------------------
ROUTINE Name : MTC_InitSineGen
Description: Initialization of software variables needed for sine wave
generation and used in the PWM update interrupt routine.
Ensures that once the PWM update interrupts will be enabled, the
sinewave generated will have a zero voltage and that stator
frequency change will be taken into account.
-----------------------------------------------------------------------------*/
void MTC_InitSineGen(void)
{
SineFreq = U8_MAX; // Arbitrary value
SineMag = 0;
MTC_Set_ClockWise_Direction();
Phase = 0;
StatorFreq = FREQ_INIT-1; // This ensure that Sinewave will be updated
MTC_UpdateSine( VOLT_INIT, FREQ_INIT);
}
/*-----------------------------------------------------------------------------
ROUTINE Name : MTC_UpdateSine
Description: Aim of this function is to update IMCFrequency and IMCVoltage
variables; limitation of output frequency as defined in MTCParam.h
is performed in this routine.
Caution: No tests are performed in this function on the input parameters,
except for the frequency range. The following conditions must
therefore be verified by the user before calling the function:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -