📄 mtc.lst
字号:
ANSI-C/cC++ Compiler for ST7 V-5.0.7, Aug 21 2001
1: /**************** (c) 2004 STMicroelectronics ********************************
2:
3: PROJECT : 3-phase AC induction motor drive Software Library
4: COMPILER : COSMIC / METROWERKS
5:
6: MODULE : mtc.c
7: VERSION : 1.0.0
8:
9: CREATION DATE : April 2004
10:
11: AUTHOR : V. Onde / Microcontroller Division Applications
12: Consumer & Micro Group
13:
14: -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
15:
16: DESCRIPTION : Low level motor control routines
17:
18: -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
19:
20: MODIFICATIONS :
21:
22: *******************************************************************************
23: THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. ST MICROELECTRONICS
24: SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES
25: WITH RESPECT TO ANY CLAIMS ARISING FROM USE OF THIS SOFTWARE.
26: ******************************************************************************/
27:
28: #include "lib.h" /* General purpose typedefs and macros */
29: #include "mtc.h" /* Public motor control peripheral function prototypes */
30: #include "MTCparam.h" /* Motor Control parameters */
31: #include "mtc_bits.h" /* MTC peripheral Hardware Registers declaration */
32: #include "pwmart.h" /* Software Timebase needed during braking */
33: #include "config.h" /* Software library configuration file */
34: // ST7FMC peripherals Hardware Registers declaration
35: #include "ST7FMC2N6.h"
36:
37: /* --------------------------- Private typedefs ---------------------------- */
38: #ifdef __HIWARE__ /* Metrowerks Compiler */
39: typedef struct {
40: TwoBytes Capture;
41: u8 Prsc_Reg;
42: } SpeedMeas_s, * near PSpeedMeas_s;
43: #elif defined(__CSMC__) /* Cosmic Compiler */
44: typedef struct {
45: TwoBytes Capture;
46: u8 Prsc_Reg;
47: } SpeedMeas_s, * @tiny PSpeedMeas_s;
48: #endif
49:
50: /* -------------------------- Private variables ---------------------------- */
51: // Some variables accessed in assembly located in page zero to decrease CPU load
52: // WARNING: if SensorPeriod structure is put outside Zero page, the declaration
53: // of the pointer PSpeedMeas_s has to be modified (remove near/@tiny)
54: #ifdef __HIWARE__ /* Metrowerks Compiler */
55:
56: #pragma DATA_SEG SHORT _ZEROPAGE
57: volatile u8 SineMag;
58: volatile u16 SineFreq;
59: volatile u16 SineFreqBuf;
60: volatile s8 PhaseShift;
61: volatile u8 MTCStatus;
62: volatile u16 Phase;
63: volatile SpeedMeas_s SensorPeriod[SPEED_FIFO_SIZE];
64: volatile PSpeedMeas_s SpeedFIFO_Index;
65: #pragma DATA_SEG DEFAULT
66:
67: #elif defined(__CSMC__) /* Cosmic Compiler */
68:
69: @tiny volatile u8 SineMag;
70: @tiny volatile u16 SineFreq;
71: @tiny volatile u16 SineFreqBuf;
72: @tiny volatile s8 PhaseShift;
73: @tiny volatile u8 MTCStatus;
74: @tiny volatile u16 Phase;
75: @tiny volatile SpeedMeas_s SensorPeriod[SPEED_FIFO_SIZE];
76: @tiny volatile PSpeedMeas_s SpeedFIFO_Index; // Pointer initialized in MTC_InitPeripheral function
77:
78: #endif
79:
80: static u16 StatorFreq;
81: static u16 PrevStatorFreq;
82: static u16 BrakeDutyCycle;
83: static BOOL MCES_Status;
84:
85: /* -------------------------- Private functions ---------------------------- */
86: u32 GetLastTachoPeriod(void);
87: u32 GetAvrgTachoPeriod(void);
88:
89: /* --------------------------- Private macros ------------------------------ */
90: #define MTC_EnableClock() ( MCRA |= (1<<MCRA_CKE) )
91: #define MTC_DisableClock() ( MCRA &= ((1<<MCRA_CKE)^255) )
92:
93: /* -------------------------- Private constants ---------------------------- */
94: // MTCStatus bitfield
95: #define FREQ_CHANGE ((u8)0x01)
96: #define BRAKE_WAIT_DEMAG ((u8)0x02)
97: #define BRAKE_SET_UP ((u8)0x04)
98: #define BRAKE ((u8)0x08)
99: #define RATIO_INC ((u8)0x10)
100: #define RATIO_DEC ((u8)0x20)
101: #define INIT_ROLLING_AVRG ((u8)0x40)
102: #define DO_ROLLING_AVRG ((u8)0x80)
103:
104: #define BRAKE_MASK ((u8) BRAKE_WAIT_DEMAG + BRAKE_SET_UP + BRAKE )
105:
106: #define VOLT_INIT ((u8)0) /* Voltage initialisation at MCU start-up */
107: #define FREQ_INIT ((u16)50) /* Freq initialisation at MCU start-up */
108:
109: #define STATOR_DEMAG_TIME ((u16)300) /* Time in ms before applying DC current for braking*/
110: #define CURRENT_SETUP_TIME ((u8)5) /* Time in ms between two consecutive values of duty cycle during braking current increase */
111:
112: /*-----------------------------------------------------------------------------
113: ROUTINE Name : MTC_ResetPeripheral
114:
115: Description: This function resets the circuitry of the Motor Controller Peripheral,
116: at the sole exception of the MDTG and MPOL registers.
117: -----------------------------------------------------------------------------*/
118: void MTC_ResetPeripheral(void)
119: {
Function: MTC_ResetPeripheral
Source : ..\..\source\mtc.c
Options : -Cc -F7 -Lasm=%n.lst -Ml -N -Os -Ou -Of -Ol0 -OnPMNC -Or
120: SetBit(MCFR,MCFR_RST);
0000 1c00 BSET MCFR,#6
121: ClrBit(MCFR,MCFR_RST);
0002 1d00 BRES MCFR,#6
122: }
0004 81 RET
123:
124:
125: /*-----------------------------------------------------------------------------
126: ROUTINE Name : MTC_EnableMCOutputs
127: MTC_DisableMCOutputs
128:
129: Description: These functions allows either to output PWM signals on the
130: MCO outputs or to put these outputs in their RESET
131: configuration (user selectable state: Hi-Z, Low or High)
132: MTC_DisableMCOutputs immediately disconnects the MCOx PWM outputs pins
133: from the Motor Controller peripheral. It resets the MOE bit in
134: the MCRA register, thus causing the MCOx pins to be in their
135: reset configuration, as defined in the options bytes (high
136: impedance or low impedance high/low state).
137: -----------------------------------------------------------------------------*/
138: void MTC_EnableMCOutputs(void)
139: {
Function: MTC_EnableMCOutputs
Source : ..\..\source\mtc.c
Options : -Cc -F7 -Lasm=%n.lst -Ml -N -Os -Ou -Of -Ol0 -OnPMNC -Or
140: MCRA |= (1<<MCRA_MOE);
0000 1e00 BSET MCRA,#7
141: }
0002 81 RET
142:
143: void MTC_DisableMCOutputs(void)
144: {
Function: MTC_DisableMCOutputs
Source : ..\..\source\mtc.c
Options : -Cc -F7 -Lasm=%n.lst -Ml -N -Os -Ou -Of -Ol0 -OnPMNC -Or
145: MCRA &= ((1<<MCRA_MOE)^255);
0000 1f00 BRES MCRA,#7
146: }
0002 81 RET
147:
148:
149: /*-----------------------------------------------------------------------------
150: ROUTINE Name : MTC_InitPeripheral
151:
152: Description: Initialization of the motor control peripheral hardware registers,
153: for the sinewave general parameters (such as PWM frequency, output
154: polarity, deadtime, interrupts,...) and speed feedback processing
155: (tacho input selection, edge sensitivity,...).
156:
157: Comment: All required motor control related interrupts are enabled upon
158: completion of this routine.
159: -----------------------------------------------------------------------------*/
160: void MTC_InitPeripheral(void)
161: {
Function: MTC_InitPeripheral
Source : ..\..\source\mtc.c
Options : -Cc -F7 -Lasm=%n.lst -Ml -N -Os -Ou -Of -Ol0 -OnPMNC -Or
162:
163: MTC_ResetPeripheral();
0000 ad00 CALLR MTC_ResetPeripheral
164:
165: SET_MTC_PAGE(1);
0002 1e00 BSET MCFR,#7
166: MPOL = L6386_POLARITY; // Set polarity according to IGBT drivers
0004 a63f LD A,#63
0006 b700 LD MPOL,A
167: MDTG = DEADTIME; // Deadtime Duration
0008 a604 LD A,#4
000a b700 LD MDTG,A
168: SetBit(MDTG,DTE); // Deadtime enabled
000c 1c00 BSET MDTG,#6
169: SetBit(MDTG,PCN); // Three independant PWM configuration
000e 1e00 BSET MDTG,#7
170: MPWME = DG_MSK; // Use Debug outputs
0010 a680 LD A,#128
0012 b700 LD MPWME,A
171: MPAR = TACHO_FALLING; // Speed Sensor mode enabled, falling edge sensitive
0014 b700 LD MPAR,A
172: SET_MTC_PAGE(0);
0016 1f00 BRES MCFR,#7
173:
174: MPCR = PWM_PRSC;
0018 3f00 CLR MPCR
175: SetBit(MPCR,MPCR_CMS); // Centered pattern
001a 1600 BSET MPCR,#3
176: MREP = 1; // Preload registers are loaded every PWM cycle
001c a601 LD A,#1
001e b700 LD MREP,A
177: MCP0L = (u8)PWM_MCP0; // Set PWM frequency
0020 a67f LD A,#127
0022 b700 LD MCP0L,A
178: MCP0H = ((u8)(PWM_MCP0>>8));
0024 a602 LD A,#2
0026 b700 LD MCP0H,A
179:
180: ToCMPxL(MCPUL,PWM_MCP0/2); // 50% duty cycle on U
0028 a6f8 LD A,#248
002a b700 LD MCPUL,A
181: ToCMPxH(MCPUH,PWM_MCP0/2);
002c a609 LD A,#9
002e b700 LD MCPUH,A
182: ToCMPxL(MCPVL,PWM_MCP0/2); // 50% duty cycle on V
0030 a6f8 LD A,#248
0032 b700 LD MCPVL,A
183: ToCMPxH(MCPVH,PWM_MCP0/2);
0034 a609 LD A,#9
0036 b700 LD MCPVH,A
184: ToCMPxL(MCPWL,PWM_MCP0/2); // 50% duty cycle on W
0038 a6f8 LD A,#248
003a b700 LD MCPWL,A
185: ToCMPxH(MCPWH,PWM_MCP0/2);
003c a609 LD A,#9
003e b700 LD MCPWH,A
186:
187: SetBit(MCRA,MCRA_DAC); // Direct access mode to write into MPHST
0040 1800 BSET MCRA,#4
188: MPHST = MCIC; // Comparator input for Tacho signal
0042 a680 LD A,#128
0044 b700 LD MPHST,A
189: ClrBit(MCRA,MCRA_DAC); // Disable direct access
0046 1900 BRES MCRA,#4
190:
191: MISR = 0; // Clear pending interrupts
0048 3f00 CLR MISR
192: MTCStatus = 0;
004a 3f00 CLR MTCStatus
193: MCES_Status = FALSE;
004c 4f CLR A
004d c70000 LD MCES_Status,A
194: // Init pointer on FIFO where speed infos are stored
195: SpeedFIFO_Index = (PSpeedMeas_s)SensorPeriod;
0050 a600 LD A,#SensorPeriod
0052 b700 LD SpeedFIFO_Index,A
196:
197: MTC_Set_CounterClockWise_Direction();
0054 cd0000 CALL MTC_Set_CounterClockWise_Direction
198:
199: MTC_EnableClock(); // Enable peripheral clock and update active
0057 1c00 BSET MCRA,#6
200: // registers with preloaded values
201: // Unmask Update, Ratio change, Capture and emergency Stop interrupts
202: MIMR = MIMR_PUM_OR + MIMR_RIM_OR + MIMR_CIM_OR + MIMR_EIM_OR;
0059 a6a9 LD A,#169
005b b700 LD MIMR,A
203: }
005d 81 RET
204:
205:
206: /*-----------------------------------------------------------------------------
207: ROUTINE Name : MTC_InitSineGen
208:
209: Description: Initialization of software variables needed for sine wave
210: generation and used in the PWM update interrupt routine.
211: Ensures that once the PWM update interrupts will be enabled, the
212: sinewave generated will have a zero voltage and that stator
213: frequency change will be taken into account.
214: -----------------------------------------------------------------------------*/
215: void MTC_InitSineGen(void)
216: {
Function: MTC_InitSineGen
Source : ..\..\source\mtc.c
Options : -Cc -F7 -Lasm=%n.lst -Ml -N -Os -Ou -Of -Ol0 -OnPMNC -Or
217: SineFreq = U8_MAX; // Arbitrary value
0000 a6ff LD A,#255
0002 b701 LD SineFreq:1,A
0004 3f00 CLR SineFreq
218: SineMag = 0;
0006 3f00 CLR SineMag
219: MTC_Set_ClockWise_Direction();
0008 cd0000 CALL MTC_Set_ClockWise_Direction
220: Phase = 0;
000b 3f01 CLR Phase:1
000d 3f00 CLR Phase
221: StatorFreq = FREQ_INIT-1; // This ensure that Sinewave will be updated
000f a631 LD A,#49
0011 c70001 LD StatorFreq:1,A
0014 5f CLR X
0015 cf0000 LD StatorFreq,X
222: MTC_UpdateSine( VOLT_INIT, FREQ_INIT);
0018 3f00 CLR _MTC_UpdateSinep1
001a 4c INC A
001b cc0000 JP MTC_UpdateSine
223: }
224:
225:
226: /*-----------------------------------------------------------------------------
227: ROUTINE Name : MTC_UpdateSine
228:
229: Description: Aim of this function is to update IMCFrequency and IMCVoltage
230: variables; limitation of output frequency as defined in MTCParam.h
231: is performed in this routine.
232: Caution: No tests are performed in this function on the input parameters,
233: except for the frequency range. The following conditions must
234: therefore be verified by the user before calling the function:
235:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -