📄 main.c
字号:
/*
* Project: 3-PHASE AC DRIVE
*
* Microcontroller: Motorola MC68HC908MR24
*
* Module: MAIN.C
* Revision/Date: 2.0 / February 2000
* Description: File contains initialisation of the system and
* software timer.
*
* Compiler: Hiware08 Compiler
*
* Author: Radim VISINKA
* Company: MOTOROLA SPS
* Motorola Czech Systems Laboratories
* Roznov pod Radhostem, Czech Republic
*
* ===================================================================
*
* Copyright (c): MOTOROLA Inc.,1999, All rights reserved.
*
* ====================================================================
* THIS SOFTWARE IS PROVIDED BY MOTOROLA RSAL "AS IS" AND ANY
* EXPRESSED 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 MOTOROLA RSAL OR
* ITS 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.
* ====================================================================
*/
/* DEFINITION_START */
/* Include Header Files */
#include "MR24IO.H"
// #include "VECTORS.H" /* file contains interrupt vectors */
#include "RAM.H" /* file contains RAM global variables */
#include "CONST.H" /* file contains global constants and
definitions */
/* Assembler Subroutine Definitions */
/* Constant Definitions */
/* Global Variables (External) - 8 bit */
extern unsigned char Amplitude; /* 0 to 255 gives 0 to 100% modulation*/
extern unsigned char Pwm_count; /* counter for PWM interrupt */
extern unsigned char Fault_flags; /* 8 bit fault flag register */
extern unsigned char Speed_control; /* Control register for OPEN/CLOSED loop */
/* Global Variables (External) - 16 bit */
extern signed int V_command; /* command speed */
extern signed int V_com_actual; /* actual ramp speed */
extern signed int Accel; /* acceleration constant */
extern signed int Decel; /* deceleration constant */
extern unsigned int Boost_slope; /* Boost slope pre-calculation */
/* Local Variables - 8 bit */
unsigned char count1; /* count_down during initialisation */
/* Local Variables - 16 bit */
unsigned int count; /* count_down during initialisation */
unsigned int Time_ptr_1; /* time constant 1 for timeout loop SPEED */
unsigned int Time_ptr_2; /* time constant 2 for timeout loop PI */
unsigned int Temp_tacnt; /* temporary timer A counter register */
/* Local Variables - 32 bit */
unsigned long temp32; /* temporary register */
/* External Modules */
extern void PWMcalc (void); /* PWM calculation routine */
extern void speed (void); /* Speed routine */
// extern void tacho (void); /* Tacho IC interrupt handler */
extern void pi (void); /* PI controller routine */
extern void fault (void); /* Over Current Fault routine */
/* DEFINITION_END */
void main(void)
{
/* clear RAM */
// _asm("ldhx #$0060\n ");
// _asm("loop:\n");
// _asm("clr ,X\n");
// _asm("aix #1\n");
// _asm("cphx #$035f\n");
// _asm("blo loop\n");
/* setup PLL clock */
PBWC = 0x80; /* set Auto Bandwidth Control */
PCTL &= ~(0x20); /* turn PLL off to be able to program multiplier */
PPG=0x87; /* multiply X-tal frequency by 8 => X-tal=4MHz *8 = 32MHz
bus frequency 32MHz/4 = 8MHz, VCO range = 7 */
PCTL |= 0x20; /* turn on the PLL */
while (~PBWC & 0x40); /* wait for PLL lock */
PCTL |= 0x10; /* use PLL clock */
/* setup PWM module */
MOR = 0x00; /* 0x00: pos. center PWM mode; cop and LVI enabled */
/* 0x60: neg. center PWM mode; cop and LVI enabled */
PMOD = PWM_MODULUS; /* set up PWM modulus => PWM frequency */
/* for 8MHz Bus Frequency
PWM_MODULUS = 0x00fa gives 16kHz PWM */
DEADTM=15; /* 2microsecond deadtime= 16 */
DISMAP=0xff; /* when PWM disabled, disable PWM1-6 */
PCTL2 = 0x80; /* interrupt every 4th. pwm loads */
PCTL1 |= 0xc0; /* disable MCPWM */
PWMOUT = 0x00; /* output port control is PWM */
PCTL1 |= 0x02; /* set LDOK bit */
FCR = 0x08; /* Flt2 enabled in manual mode */
PVAL1 = PWM_MODULUS/2; /* set phase A pwm to 50% */
PVAL3 = PWM_MODULUS/2; /* set phase B pwm to 50% */
PVAL5 = PWM_MODULUS/2; /* set phase C pwm to 50% */
/* setup PORTs input/output */
DDRA=0x80; /* PORT A: 0123456 inputs, 7 output */
/* 1000 0000 */
PORTA=0x00;
DDRB=0x00; /* PORT B: A/D */
DDRC=0x70; /* PORT C: bits 0-3:inputs; 4,5,6:outputs */
/* 111 0000 */
PORTC &= ~(FAULT_LED); /* FAULT_LED OFF */
PORTC |= YELLOW_LED;
PORTC &= ~(MCS_LED); /* STATUS_LED OFF*/
DDRE=0x00; /* bit 0-7:input */
/* 0000 0000*/
DDRF=0x00; /* not used */
/* setup Timer A for IC (on TCH3A) & for software timer reference */
TASC |= 0x20; /* stop TIMA */
TASC &= 0xe0; /* reset TIMA */
TASC=TBSC | 0x06; /* sets prescaller for Clock/64 */
TASC3 = 0x44;
/* 0100 0100
TCHA3 interrupt enabled
selected TCHB0: IC on rising edge
x100 01xx */
/* setup ADC */
ADCLK = 0x70; /* ADC clock = internal bus clock / 8
ADC mode: 8-bit truncation */
/* MANUAL/DEMO Operating Mode set-up */
Om_mode = PORTA & 0x04; /* OM set-up coppied to Om_mode variable */
/* Initialise Variables */
Tacho_pulses = 0x0b40;
Accel = (ACC * PI_CONST)>>8; /* acceleration */
Decel = (DEC * PI_CONST)>>8; /* deceleration */
/* Initialise boost of V/Hz ramp */
temp32 = ((long)FREQ_BOOST*(long)0xffff);
Boost_slope = temp32/FREQ_BASE;
/* scale the VOLTS_MIN const. and substract */
Boost_slope = Boost_slope - (VOLTS_BOOST * 0x028f);
Mcs_status |= MCS_STAND_BY; /* Stand_by state */
Speed_control = 0;
/*****************************************************************/
/* OPEN/CLOSED speed control loop */
Speed_control = OPEN_LOOP; /* Disactivate the line for Closed Loop */
/*****************************************************************/
/* wait */
count1 = 0x04;
while (count1 != 0)
{
count1--;
count = 0xffff;
while (count != 0)
{
COPCTL = 0x00;
count--;
}
}
/* START */
PCTL1 |= 0x20; /* enables pwm interrupts */
PCTL1 |= 0x01; /* enables PWM */
FTAC |= 0x04; /* clears FFLAG2 - fault2 flag bit */
TASC=TASC & 0xdf; /* starts timer A - clear stop bit */
TASC3 &= 0x7f; /* clears CHANNEL0 Flag Bit */
ADSCR = 0x00; /* starts AD conversion - Pot_voltage */
/* ADC channel 0 */
Time_ptr_1 = TACNT; /* initialise software timer pointer */
Time_ptr_2 = TACNT;
cli(); /* enables interrupt */
endless_loop:
Temp_tacnt = TACNT; /* Software Time No. 1 */
if ((Temp_tacnt >= Time_ptr_1) && (!((Temp_tacnt > (0xFFFF - READ_CONST)) && (Time_ptr_1 < READ_CONST))))
{
Time_ptr_1 += READ_CONST;
speed();
}
Temp_tacnt = TACNT; /* Software Time No. 2 */
if ((Temp_tacnt >= Time_ptr_2) && (!((Temp_tacnt > (0xFFFF - PI_CONST)) && (Time_ptr_2 <= PI_CONST))))
{
Time_ptr_2 += PI_CONST;
pi();
}
goto endless_loop;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -