📄 mtc.c
字号:
/**************** (c) 2004 STMicroelectronics **********************
PROJECT : ST7MC demokit
COMPILER : ST7 METROWERKS C (HIWARE) / COSMIC
MODULE : mtc.c
LIBRARY VERSION : 1.0.2
CREATION DATE : 07.2003
AUTHOR : Florent COSTE / Microcontroller Application Lab / ST Hong Kong
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DESCRIPTION : MTC peripheral control routines
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
******************************************************************************
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 "version.h"
#include "lib.h" /* General purpose typedefs and macros */
#include "ports.h"
#include "ST7MC_hr.h"
#include "mtc_hr.h"
#include "LinSCI.h"
#include "Timer.h"
#include "adc.h"
#include "regul.h"
#include "mtc.h" /* Public motor control function prototypes */
#include "misc.h"
#include "opamp.h"
#include "MTC_Settings_Sensorless.h"
/* -------------------------- Private constants ---------------------------- */
/* Pattern for the refresh of high side drivers bootstrap capacitor */
const u8 BOOT_REFRESH[3] = {
T2,
T4,
T6
};
const u8 PHASE_CONFIG[6] = {
mem_step1,mem_step2,mem_step3,
mem_step4,mem_step5,mem_step6
};
const u8 ZC_DEMAG_PWM[6] = {
mem_MCRB1,mem_MCRB2,mem_MCRB3,
mem_MCRB4,mem_MCRB5,mem_MCRB6
};
const u8 ZC_DEMAG_PWM_PWMON[6] = {
mem_MCRB1_PWMON,mem_MCRB2_PWMON,mem_MCRB3_PWMON,
mem_MCRB4_PWMON,mem_MCRB5_PWMON,mem_MCRB6_PWMON
};
/*
const u8 DEMAG_TIME[16] = {
Run_Soft_Dem0,Run_Soft_Dem1,Run_Soft_Dem2,Run_Soft_Dem3,
Run_Soft_Dem4,Run_Soft_Dem5,Run_Soft_Dem6,Run_Soft_Dem7,
Run_Soft_Dem8,Run_Soft_Dem9,Run_Soft_Dem10,Run_Soft_Dem11,
Run_Soft_Dem12,Run_Soft_Dem13,Run_Soft_Dem14,Run_Soft_Dem15
};
*/
const u8 DEMAG_TIME_C[16] = {
Run_Soft_Dem0_C,Run_Soft_Dem1_C,Run_Soft_Dem2_C,Run_Soft_Dem3_C,
Run_Soft_Dem4_C,Run_Soft_Dem5_C,Run_Soft_Dem6_C,Run_Soft_Dem7_C,
Run_Soft_Dem8_C,Run_Soft_Dem9_C,Run_Soft_Dem10_C,Run_Soft_Dem11_C,
Run_Soft_Dem12_C,Run_Soft_Dem13_C,Run_Soft_Dem14_C,Run_Soft_Dem15_C
};
const u8 DEMAG_TIME_V[16] = {
Run_Soft_Dem0_V,Run_Soft_Dem1_V,Run_Soft_Dem2_V,Run_Soft_Dem3_V,
Run_Soft_Dem4_V,Run_Soft_Dem5_V,Run_Soft_Dem6_V,Run_Soft_Dem7_V,
Run_Soft_Dem8_V,Run_Soft_Dem9_V,Run_Soft_Dem10_V,Run_Soft_Dem11_V,
Run_Soft_Dem12_V,Run_Soft_Dem13_V,Run_Soft_Dem14_V,Run_Soft_Dem15_V
};
const Step_s RAMP[64] = {
def_ramp_ratio0,def_ramp_value0,def_ramp_ratio1,def_ramp_value1,def_ramp_ratio2,def_ramp_value2,
def_ramp_ratio3,def_ramp_value3,def_ramp_ratio4,def_ramp_value4,def_ramp_ratio5,def_ramp_value5,
def_ramp_ratio6,def_ramp_value6,def_ramp_ratio7,def_ramp_value7,def_ramp_ratio8,def_ramp_value8,
def_ramp_ratio9,def_ramp_value9,def_ramp_ratio10,def_ramp_value10,def_ramp_ratio11,def_ramp_value11,
def_ramp_ratio12,def_ramp_value12,def_ramp_ratio13,def_ramp_value13,def_ramp_ratio14,def_ramp_value14,
def_ramp_ratio15,def_ramp_value15,def_ramp_ratio16,def_ramp_value16,def_ramp_ratio17,def_ramp_value17,
def_ramp_ratio18,def_ramp_value18,def_ramp_ratio19,def_ramp_value19,def_ramp_ratio20,def_ramp_value20,
def_ramp_ratio21,def_ramp_value21,def_ramp_ratio22,def_ramp_value22,def_ramp_ratio23,def_ramp_value23,
def_ramp_ratio24,def_ramp_value24,def_ramp_ratio25,def_ramp_value25,def_ramp_ratio26,def_ramp_value26,
def_ramp_ratio27,def_ramp_value27,def_ramp_ratio28,def_ramp_value28,def_ramp_ratio29,def_ramp_value29,
def_ramp_ratio30,def_ramp_value30,def_ramp_ratio31,def_ramp_value31,def_ramp_ratio32,def_ramp_value32,
def_ramp_ratio33,def_ramp_value33,def_ramp_ratio34,def_ramp_value34,def_ramp_ratio35,def_ramp_value35,
def_ramp_ratio36,def_ramp_value36,def_ramp_ratio37,def_ramp_value37,def_ramp_ratio38,def_ramp_value38,
def_ramp_ratio39,def_ramp_value39,def_ramp_ratio40,def_ramp_value40,def_ramp_ratio41,def_ramp_value41,
def_ramp_ratio42,def_ramp_value42,def_ramp_ratio43,def_ramp_value43,def_ramp_ratio44,def_ramp_value44,
def_ramp_ratio45,def_ramp_value45,def_ramp_ratio46,def_ramp_value46,def_ramp_ratio47,def_ramp_value47,
def_ramp_ratio48,def_ramp_value48,def_ramp_ratio49,def_ramp_value49,def_ramp_ratio50,def_ramp_value50,
def_ramp_ratio51,def_ramp_value51,def_ramp_ratio52,def_ramp_value52,def_ramp_ratio53,def_ramp_value53,
def_ramp_ratio54,def_ramp_value54,def_ramp_ratio55,def_ramp_value55,def_ramp_ratio56,def_ramp_value56,
def_ramp_ratio57,def_ramp_value57,def_ramp_ratio58,def_ramp_value58,def_ramp_ratio59,def_ramp_value59,
def_ramp_ratio60,def_ramp_value60,def_ramp_ratio61,def_ramp_value61,def_ramp_ratio62,def_ramp_value62,
def_ramp_ratio63,def_ramp_value63
};
/*
const u8 Rising_Table[101] = {
190,188,180,170,170,170,160,154,150,148,
145,142,140,140,138,138,134,132,130,126,123,121,118,116,112,110,108,104,100, 98,
94, 92, 90, 88, 78, 76, 76, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52,
118,115,113,110,107,104,100, 98, 96, 95, 94, 92, 90, 88, 86, 84, 82, 80, 78, 75,
73, 71, 69, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 52, 50, 48,
46, 44, 43, 42, 41, 40, 40, 40, 40, 40, 40
};
const u8 Falling_Table[101] = {
168,168,159,156,156,156,152,150,146,144,
142,140,138,138,136,136,134,132,130,126,124,120,119,118,116,114,110,108,104,102,
98, 96, 94, 90, 90, 88, 84, 82, 80, 78, 74, 72, 70, 66, 64, 60, 60, 60, 60, 58,
110,110,108,106,104,102,100, 98, 96, 95, 94, 92, 90, 88, 86, 84, 82, 80, 78, 75,
73, 71, 69, 67, 66, 65, 64, 63, 62, 61, 60, 58, 56, 54, 52, 50, 48, 47, 46, 45,
44, 43, 43, 42, 41, 40, 40, 40, 40, 40, 40
};
*/
/*const u8 Rising_Table[101] = {
150,148,146,144,142,140,138,138,136,136,
134,134,134,134,132,130,130,128,128,126,123,121,118,116,112,110,108,104,100, 98,
94, 92, 90, 88, 78, 76, 76, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52,
50, 50, 50, 50, 50, 48, 46, 46, 44, 44, 110, 108, 106, 104, 102, 100, 98, 96, 94, 92,
90, 88, 86, 84, 82, 82, 80, 78, 76, 76, 74, 70, 70, 68, 68, 66, 64, 62, 60, 60,
58, 54, 50, 50, 48, 48, 46, 46, 44, 44, 42 };
const u8 Falling_Table[101] = {
150,148,146,144,142,140,138,138,136,136,
134,134,134,134,132,130,130,128,128,126,124,120,119,118,116,114,110,108,104,102,
98, 96, 94, 90, 90, 88, 84, 82, 80, 78, 74, 72, 70, 66, 64, 60, 60, 60, 60, 58,
56, 56, 56, 56, 54, 52, 50, 50, 48, 47, 110, 108, 106, 104, 102, 100, 98, 96, 94, 92,
90, 88, 86, 84, 82, 82, 80, 78, 76, 76, 74, 70, 70, 68, 68, 66, 64, 62, 60, 60,
58, 54, 50, 50, 48, 48, 46, 46, 44, 44, 42 }; */ //2005.12.03 bak
const u8 Rising_Table[101] = {
150,148,146,144,142,140,138,138,136,136,
134,134,134,134,132,130,130,128,128,126,123,121,118,116,112,110,108,104,100, 98,
94, 92, 90, 88, 78, 76, 76, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52,
50, 50, 50, 50, 50, 48, 46, 46, 44, 44, 96, 96, 94, 94, 92, 92 ,90, 90, 88, 86,
86, 84, 84, 82, 82, 82, 80, 78, 76, 76, 74, 70, 70, 68, 68, 66, 64, 62, 60, 60,
58, 54, 50, 50, 48, 48, 46, 46, 44, 44, 42 };
const u8 Falling_Table[101] = {
150,148,146,144,142,140,138,138,136,136,
134,134,134,134,132,130,130,128,128,126,124,120,119,118,116,114,110,108,104,102,
98, 96, 94, 90, 90, 88, 84, 82, 80, 78, 74, 72, 70, 66, 64, 60, 60, 60, 60, 58,
56, 56, 56, 56, 54, 52, 50, 50, 48, 47, 90, 88, 88, 86, 86, 86, 86, 84, 84, 84,
84, 82, 82, 82, 82, 82, 80, 78, 76, 76, 74, 70, 70, 68, 68, 66, 64, 62, 60, 60,
58, 54, 50, 50, 48, 48, 46, 46, 44, 44, 42 };
/* -------------------------- Public variables ---------------------------- */
u8 Power_Motor_Status;
u16 Freq_Motor = 0;
u8 Ki = Ki_Fmin; // integral term
u8 Kp = Kp_Fmin; // proportionnal term
u8 Rising_bemf = Rising_Fmin ;
u8 Falling_bemf = Falling_Fmin;
u8 Flag_MTC;
u8 MotorCmd = STOP_CMD;
/* -------------------------- Private variables ---------------------------- */
#ifdef __HIWARE__ // only for Metrowerks Compiler
#pragma DATA_SEG SHORT _ZEROPAGE
static u8 RPICounter; // ASM SW: flagR_plus
static u16 SoftDemagTime; // ASM SW: mem_mdreg
static u8 CeventCounter; // ASM SW: memnbemf
static u8 BemfCounter; // ASM SW: nbemf
static u8 RampIndex; // ASM SW: com_t_nb
static u8 StepIndex; // ASM SW: PhaseStep
volatile u8 MotorStatus ; // ASM SW: MotorStatus
static u8 delay_counter;
static u8 Step_Z_Counter;
#pragma DATA_SEG DEFAULT
#else
#ifdef __CSMC__ // only for Cosmic Compiler
@tiny static u8 RPICounter; // ASM SW: flagR_plus
@tiny static u16 SoftDemagTime; // ASM SW: mem_mdreg
@tiny static u8 CeventCounter; // ASM SW: memnbemf
@tiny static u8 BemfCounter; // ASM SW: nbemf
@tiny static u8 RampIndex; // ASM SW: com_t_nb
@tiny static u8 StepIndex; // ASM SW: PhaseStep
@tiny volatile u8 MotorStatus; // ASM SW: MotorStatus
@tiny static u8 delay_counter;
@tiny static u8 Step_Z_Counter;
#else
#error"Unsupported Compiler!" // Compiler Defines not found!
#endif
#endif
static volatile u8 Step_counter;
static volatile u8 Flag_it;
#define RP_EVT 1
#define RM_EVT 2
#define Reset_RPM_MASK 0xF9
static volatile u8 RP_counter;
#define Reset_H_SDM_MASK 0x4f
#if (DRIVING_MODE == VOLTAGE_MODE)
static u8 Previous_Duty_L;
static u8 Previous_Duty_H;
#endif
static u8 Align_Index;
typedef enum
{
INIT_START, ALIGN_ON_GOING, INIT_SWITCHED_MODE, START_FINISHED
} StartStatus_t;
StartStatus_t Status_Start;
typedef enum
{
WAIT_FOR_C, ALIGN_NEXT
} AlignStatus_t;
AlignStatus_t AlignState;
typedef enum
{
BRAKE_START, BRAKE_ONGOING, BRAKE_FINISHED
} BrakeStatus_t;
BrakeStatus_t BrakeState;
/* -------------------------- Private functions ---------------------------- */
static void RefreshBootstrap(void);
static void AlignRotor(void);
static void MTC_ResetPeripheral(void);
/* -------------------------- Private macros ---------------------------- */
// number of Z event to wait before closed loop operation
#define Z_event_for_PI 12
// Below macros allow to validate PWM outputs or put them in their RESET state
#define MTC_EnableOutputs() ( MCRA |= (1<<MOE) )
#define MTC_DisableOutputs() ( MCRA &= ((1<<MOE)^255) )
// Below macros allow to enable/disable the motor control peripheral clock
#define MTC_EnableClock() ( MCRA |= (1<<CKE) )
#define MTC_DisableClock() ( MCRA &= ((1<<CKE)^255) )
// Below macros allow to enable/disable Direct Access mode to PWM outputs
#define MTC_EnableDirectAccess() ( MCRA |= (1<<DAC) )
#define MTC_DisableDirectAccess() ( MCRA &= ((1<<DAC)^255) )
// Macro updating MTIM prescaler during acceleration ramp in switched mode:
// If the ratio required for next step is equal to the 4 LSbits of MPRSR
// (ie presc ratio), then MISR is cleared, otherwise, the RMI bit of MISR is
// set -> the prescaler will be decreased when the next C event will occur
// Note: This function only works for the acceleration phase: no possibility
// to increase the MTIM prescaler
#define UpdateRatio() ((RAMP[RampIndex].Ratio == (MPRSR&0x0F)) ? (MISR = 0) : (MISR = RMI_MSK) )
#define SW_DEMAG ((u8)4) /* Demag Time is StepTime divided
by SW_DEMAG in autoswitched mode */
#define SWITCHED_SW_DEMAG ((u8)4) /* Demag Time is StepTime divided by
SWITCHED_SW_DEMAG in switched mode */
#define RAMP_SIZE ((u8)64) /* Acceleration Ramp size : MUST BE
below 254 to avoid CeventCounter overflow */
#define START_POSITION ((u8)T1+T4+T6) /* Defines the rotor position at start:
here an intermediary position between two steps */
#define BREAK_POSITION START_POSITION
#define PWM_CLK_PRSC 4 // Ftimer = Fmtc/4
#define TRANSITION_DELAY 255 // set to 255 to center Z on the step on first autoswitch
#define AUTO_DELAY 128 // delay use for the first electrical cycle in autoswitch
#define AUTO_DELAY_STEP 6 //number of step using AUTO_DELAY
#define MEDIUM_DELAY 110 // delay use for the second electrical cycle in autoswitched
#define MAX_DELAY_COUNTER 12 //number of step for managing of delay at startup
// b0-b2-b4:T2-T4-T6 are ODD (low)switch -> KANDA
// b1-b3-b5:T1-T3-T5 are EVEN (high) switch -> KANDA
#define OUTPUT_PARITY 0x2A // b7=b6=0 for BLDC mode (either sensored or sensorless)
// b1-b3-b5:T2-T4-T6 are ODD (low)switch
// b0s-b2-b4:T1-T3-T5 are EVEN (high) switch
#define STARTUP_DELAY 0xFF // Default delay coefficient at start ASM SW: delay_ki1
#define ALIGN_STEP_NUM 25 // Number of duty cycle increments during alignment phase
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* ------------------------ Functions description -------------------------- */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/*-----------------------------------------------------------------------------
ROUTINE Name : MTC_ResetPeripheral
Description: Reset all hardware registers related to the motor control
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -