misc.c
来自「该程序是ST7MC驱动BLDC120」· C语言 代码 · 共 258 行
C
258 行
/**************** (c) 2004 STMicroelectronics **********************
PROJECT : ST7MC demokit
COMPILER : ST7 METROWERKS C (HIWARE) / COSMIC
MODULE : Misc.c
LIBRARY VERSION : 1.0.2
CREATION DATE : 04.2005
AUTHOR : Jiang Jian-guo / Microcontroller Competence center / ST Shang Hai
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DESCRIPTION : Miscellanous 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"
#include "ST7MC_hr.h"
#include "mtc_hr.h"
#include "mtc.h"
#include "Misc.h"
#include "Timer.h"
#include "adc.h"
#include "ports.h"
#include "MTC_Settings_Sensorless.h"
#include "it_ST7MC.h"
u16 Current_Target_Freq =0, Target_Speed =0; //electronical frequency & revolution: 0.1Hz.
u8 SpdVarPeriod = 50; // Acceleration/Deceleration rate -- resolution: 10ms/1Hz(electronical frequency)
/* -------------------------- Private constants ---------------------------- */
// ISPR0 Register interrupts priority definitions
#define MCES_LVL_0 ((u8)0x02)
#define MCES_LVL_1 ((u8)0x01)
#define MCES_LVL_2 ((u8)0x00)
#define MCES_LVL_3 ((u8)0x03)
#define MCC_SI_LVL_0 ((u8)(0x02<<2))
#define MCC_SI_LVL_1 ((u8)(0x01<<2))
#define MCC_SI_LVL_2 ((u8)(0x00<<2))
#define MCC_SI_LVL_3 ((u8)(0x03<<2))
#define EXT_IT0_LVL_0 ((u8)(0x02<<4))
#define EXT_IT0_LVL_1 ((u8)(0x01<<4))
#define EXT_IT0_LVL_2 ((u8)(0x00<<4))
#define EXT_IT0_LVL_3 ((u8)(0x03<<4))
#define EXT_IT1_LVL_0 ((u8)(0x02<<6))
#define EXT_IT1_LVL_1 ((u8)(0x01<<6))
#define EXT_IT1_LVL_2 ((u8)(0x00<<6))
#define EXT_IT1_LVL_3 ((u8)(0x03<<6))
// ----------------------- ISPR1 Register interrupts priority
#define EXT_IT2_LVL_0 ((u8)0x02)
#define EXT_IT2_LVL_1 ((u8)0x01)
#define EXT_IT2_LVL_2 ((u8)0x00)
#define EXT_IT2_LVL_3 ((u8)0x03)
#define MTC_U_CL_SO_LVL_0 ((u8)(0x02<<2))
#define MTC_U_CL_SO_LVL_1 ((u8)(0x01<<2))
#define MTC_U_CL_SO_LVL_2 ((u8)(0x00<<2))
#define MTC_U_CL_SO_LVL_3 ((u8)(0x03<<2))
#define MTC_R_Z_LVL_0 ((u8)(0x02<<4))
#define MTC_R_Z_LVL_1 ((u8)(0x01<<4))
#define MTC_R_Z_LVL_2 ((u8)(0x00<<4))
#define MTC_R_Z_LVL_3 ((u8)(0x03<<4))
#define MTC_C_D_LVL_0 ((u8)(0x02<<6))
#define MTC_C_D_LVL_1 ((u8)(0x01<<6))
#define MTC_C_D_LVL_2 ((u8)(0x00<<6))
#define MTC_C_D_LVL_3 ((u8)(0x03<<6))
// ----------------------- ISPR2 Register interrupts priority
#define SPI_LVL_0 ((u8)0x02)
#define SPI_LVL_1 ((u8)0x01)
#define SPI_LVL_2 ((u8)0x00)
#define SPI_LVL_3 ((u8)0x03)
#define TIMER_A_LVL_0 ((u8)(0x02<<2))
#define TIMER_A_LVL_1 ((u8)(0x01<<2))
#define TIMER_A_LVL_2 ((u8)(0x00<<2))
#define TIMER_A_LVL_3 ((u8)(0x03<<2))
#define TIMER_B_LVL_0 ((u8)(0x02<<4))
#define TIMER_B_LVL_1 ((u8)(0x01<<4))
#define TIMER_B_LVL_2 ((u8)(0x00<<4))
#define TIMER_B_LVL_3 ((u8)(0x03<<4))
#define SCI_LVL_0 ((u8)(0x02<<6))
#define SCI_LVL_1 ((u8)(0x01<<6))
#define SCI_LVL_2 ((u8)(0x00<<6))
#define SCI_LVL_3 ((u8)(0x03<<6))
// ----------------------- ISPR3 Register interrupts priority
#define AVD_LVL_0 ((u8)0x02)
#define AVD_LVL_1 ((u8)0x01)
#define AVD_LVL_2 ((u8)0x00)
#define AVD_LVL_3 ((u8)0x03)
#define PWMART_LVL_0 ((u8)(0x02<<2))
#define PWMART_LVL_1 ((u8)(0x01<<2))
#define PWMART_LVL_2 ((u8)(0x00<<2))
#define PWMART_LVL_3 ((u8)(0x03<<2))
/*-----------------------------------------------------------------------------
ROUTINE Name : ST7_IntPrioritySetUp
Description: This function performs the initialisation of the Interrupt
Software Priority Registers for the nested interrupts management.
-----------------------------------------------------------------------------*/
void ST7_IntPrioritySetUp(void)
{
// Interrupt Software priority Register 0
// MCES: top level, others AVD medium/low priority (MCC, CSS, Ext int0, Ext int1)
ITSPR0 = MCES_LVL_3 + MCC_SI_LVL_1 + EXT_IT0_LVL_1 + EXT_IT1_LVL_1;
// Interrupt Software priority Register 1
// PMW update: top level, Ratio update and tacho capture medium/high level,
// Ext int2 AVD medium/low priority
ITSPR1 = EXT_IT2_LVL_1 + MTC_U_CL_SO_LVL_2 + MTC_R_Z_LVL_3 + MTC_C_D_LVL_3;
// Interrupt Software priority Register 2
// SPI, TimerA, TimerB and SCI medium/low priority
ITSPR2 = SPI_LVL_1 + TIMER_A_LVL_1 + TIMER_B_LVL_1 + SCI_LVL_2;
// Interrupt Software priority Register 3
// PWMART medium/high priority, AVD medium/low priority
ITSPR3 = AVD_LVL_1 + PWMART_LVL_1;
}
/*-----------------------------------------------------------------------------
ROUTINE Name : Waitxxx
Description: Waiting routines.
Input/Output: none/none
Comments: None
-----------------------------------------------------------------------------*/
void Wait(u8 time)
{
timer_10ms = time;
while (timer_10ms != 0);
}
void Wait100us(void)
{
u8 i;
for (i=0; i<18; i++) Wait1us();
}
void Wait1ms(void)
{
u8 i;
for (i=0; i<180; i++) Wait1us();;
}
/*-----------------------------------------------------------------------------
ROUTINE Name : Chk_Power_Motor_Status
Description: Check temperature level, high voltage bus and update status flag
accordingly.
Input/Output: none/BOOL
Comments: None
-----------------------------------------------------------------------------*/
void Chk_Power_Motor_Status(void)
{
if (timer_CL_10ms ==0) ClrBit(Power_Motor_Status,OverCurrent);
else if (timer_CL_10ms <= 10) MIMR |= CLIM_MSK; // re-validate CL Int. after 200 ms
}
/*-----------------------------------------------------------------------------
ROUTINE Name : change_speed
Description: change spead by rate: Acceleration_Rate Hz/s( mechanical frequency )
Input: Acceleration_Rate: resolution: 1Hz(mechanical frequency)/s
output: None
Comments: None
-----------------------------------------------------------------------------*/
void Change_Speed(void)
{
if (Target_Speed != Current_Target_Freq)
{
if (Timer_SpdVarPeriodElapsed() == TRUE)
{
if (Target_Speed > Current_Target_Freq) Current_Target_Freq += 10;
else Current_Target_Freq -= 10;
Timer_SetSpdVarPeriod(SpdVarPeriod); // 0.5 Sec delay
}
}
}
/*-----------------------------------------------------------------------------
ROUTINE Name : Inc_TargetSpeed/Dec_TargetSpeed
Description: change the target spead by rate: Inc_Step/Dec_Step Hz( mechanical frequency )
Input: Inc_Step/Dec_Step -- resolution: 1Hz(mechanical frequency)
output: None
Comments: None
-----------------------------------------------------------------------------*/
void Inc_TargetSpeed(void)
{
Target_Speed += Pole_Pair_Num * 10;
}
void Dec_TargetSpeed(void)
{
Target_Speed -= Pole_Pair_Num * 10;
}
/*-----------------------------------------------------------------------------
ROUTINE Name : Get_TargetSpeed
Description: Get the target spead by rate.
Input: None
output: Target speed -- resolution: 0.1Hz(electronical frequency)
Comments: None
-----------------------------------------------------------------------------*/
u16 Get_TargetSpeed(void)
{
return(Target_Speed);
}
void Set_TargetSpeed(u8 Speed)
{
Target_Speed = (Speed/2) * 10 * Pole_Pair_Num;
}
u16 Get_CurrentTargetSpeed(void)
{
return(Current_Target_Freq);
}
void Set_CurrentTargetSpeed(u16 Speed)
{
Current_Target_Freq = Speed;
}
void Set_SpdVarPeriod(u8 Period)
{
SpdVarPeriod = (u8)(Period * 10);
}
/*** (c) 2004 STMicroelectronics ****************** END OF FILE ***/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?