📄 mc_dev_board.c
字号:
/**********************************************************************
* *
* Software License Agreement *
* *
* The software supplied herewith by Microchip Technology *
* Incorporated (the "Company") for its dsPIC controller *
* is intended and supplied to you, the Company's customer, *
* for use solely and exclusively on Microchip dsPIC *
* products. The software is owned by the Company and/or its *
* supplier, and is protected under applicable copyright laws. All *
* rights are reserved. Any use in violation of the foregoing *
* restrictions may subject the user to criminal sanctions under *
* applicable laws, as well as to civil liability for the breach of *
* the terms and conditions of this license. *
* *
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO *
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, *
* BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE *
* COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, *
* INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. *
* *
**********************************************************************/
/**********************************************************************
* *
* Author: Steve Bowling *
* *
* Filename: mc_dev_board.c *
* Date: 03/02/04 *
* File Version: 2.00 *
* *
* Tools used: MPLAB IDE -> 6.43 *
* Compiler -> 1.20 *
* *
* Linker File: p30f6010.gld *
* *
* *
***********************************************************************
* Code Description
*
* This code demonstrates the features of the dsPIC motor control
* development board and 3-phase power modules.
* The code can be programmed to drive different motor types depending
* on the buttons pressed on the PCB. The following motors are
* supported:
*
* 1. open loop control of AC induction motor using V/Hz algorithm
* 2. open loop control of BLDC motor using hall-effect sensors
* 3. open loop control of brush DC motor.
*
**********************************************************************
Revison History:
Version 2.00:
-------------
-Code modified to include C implementation of SVM() function. SVM
routine now allows full modulation range over previous version.
-Mode selection interface modified to wait for key press at reset.
**********************************************************************/
#include <p30F2010.h>
#include "hardware.h"
//#include "xlcd.h"
#include "svm.h"
/************************Configuration Bits***************************/
_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR(PBOR_OFF & MCLR_EN);
_FGS(CODE_PROT_OFF);
/*********************** START OF DEFINITIONS *******************/
// These are the counting periods for the medium and slow speed events.
// Count variables are decremented at each PWM interrupt.
#define SLOW_EVENT_PERIOD 3200 // 100msec period for 16KHz PWM
#define MEDIUM_EVENT_PERIOD 72 // 5msec period for 16KHz PWM
//----------------------------------------------------------------------
// Constants required by ACIM software
//----------------------------------------------------------------------
// These are the definitions used to derive the maximum modulation
// frequency.
// First, specify the max modulation frequency that you want.
#define MAX_MOD_FREQ 60
// Next, specify the frequency of the PWM carrier
#define PWM_MOD_FREQ 16000
// Finally, specify the resolution of the modulation phase angle.
// This is how many counts it takes to complete one modulation
// electrical cycle.
#define MOD_ANGLE_RES 65536
// Now, compute the modulation frequency limit value. This
// constant will be used to limit the speed demand variable.
#define MOD_FREQ_LIMIT (MAX_MOD_FREQ*MOD_ANGLE_RES)/PWM_MOD_FREQ
// These constants define the slope of the Volts-Hertz profile
// and the lower and upper modulation index shelves.
#define V_HZ_SLOPE 200
#define LOW_AMPLITUDE_LIMIT 2000
#define HIGH_AMPLITUDE_LIMIT 32000
#define AMPLITUDE_POT_LIMIT HIGH_AMPLITUDE_LIMIT/V_HZ_SLOPE
//----------------------------------------------------------------------
// Constants required by BLDC motor software
//----------------------------------------------------------------------
// The following definitions are the values that will be written to
// the OVDCON register to acheive the various BLDC commutation states.
// The commutation state is determined by reading the value of the
// hall effect sensors on the motor.
// States 0 and 7 are invalid states and therefore all PWM outputs
// are disabled.
// For soft
// switching, the top device is modulated, while the bottom device
// is always on.
#define STATE0 0x0000
#define STATE1 0x0210
#define STATE2 0x2004
#define STATE3 0x0204
#define STATE4 0x0801
#define STATE5 0x0810
#define STATE6 0x2001
#define STATE7 0x0000
#define BLDC_SPEED_LIMIT 900
//----------------------------------------------------------------------
// Constants required by DC motor software
//----------------------------------------------------------------------
#define BDC_SPEED_LIMIT 400
//----------------------------------------------------------------------
// Software flags
//----------------------------------------------------------------------
// This bit structure provides status flags for the software
volatile struct {
unsigned :1;
unsigned Reverse:1;
unsigned Button1:1;
unsigned Button2:1;
unsigned Button3:1;
unsigned Button4:1;
unsigned DirChange:1;
unsigned Accelerate:1;
unsigned PWMFault:1;
unsigned Restart:1;
unsigned :1;
unsigned StarStop:1;
unsigned :1;
unsigned PWMEvent:1;
unsigned MediumEvent:1;
unsigned SlowEvent:1;
} Flags;
// This bit structure provides mode flags for the software
volatile struct {
unsigned ACIMTest:1;
unsigned ACIM:1;
unsigned BLDC:1;
unsigned BDC:1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
unsigned :1;
} Mode;
//----------------------------------------------------------------------
// System variables
//----------------------------------------------------------------------
// This is a software counter used to time slower system events, such as
// button polling, etc.
unsigned int SlowEventCount, MediumEventCount;
// This variable is used as a software counter to turn the voltage boost
// circuit switch on and off.
unsigned int BoostCount;
// This variable holds the A/D potentiometer reading that controls speed.
unsigned int Speed, OldSpeed;
unsigned int temp;
//----------------------------------------------------------------------
// Variables required by ACIM software
//----------------------------------------------------------------------
// This variable holds the V/Hz slope for the ACIM profile.
unsigned int V_Hz_Slope;
// Phase and Amplitude are the two input variables to the PWM modulation
// function.
unsigned int Amplitude, Phase;
//----------------------------------------------------------------------
// Variables required for BLDC code
//----------------------------------------------------------------------
// This array holds the commutation states for the OVDCON register.
unsigned int BLDCstates[] = {STATE0, STATE1, STATE2, STATE3, STATE4, STATE5, STATE6, STATE7};
// The hall effect sensor states are read from the I/O port and stored
// in HallState. HallState is used as an index to the BLDCstates
// array.
unsigned char HallState;
//----------------------------------------------------------------------
// System functions
//----------------------------------------------------------------------
void Setup(void); // Initializes dsPIC and peripherals
int ReadADC(unsigned int channel);
void __attribute__((__interrupt__)) _PWMInterrupt(void);
void Delay(unsigned int count);
//----------------------------------------------------------------------
// Functions required by ACIM software
//----------------------------------------------------------------------
void Modulate(int volts, unsigned int angle);
//----------------------------------------------------------------------
// Code functions
//----------------------------------------------------------------------
main ( void )
{
Setup();
while(1)
{
ClrWdt(); // Clear the watchdog timer
// This code is executed after each PWM interrupt occurs.
// The PWM ISR sets PWMflag to signal the interrupt.
if(Flags.PWMEvent)
{
if(Flags.StarStop)
{
// Run this code if driving an AC motor
if(Mode.ACIM || Mode.ACIMTest)
{
// The Speed variable is added or subtracted from the Phase
// variable to control the modulation frequency.
if(Flags.Reverse) Phase -= Speed;
else Phase += Speed;
// Write the duty cycles to the PWM module by calling the SVM
// routine.
SVM(Amplitude,Phase);
}
// Run this code if driving a BLDC motor
/* if(Mode.BLDC)
{
if(!Flags.PWMFault)
{
// Read the hall effect sensors on PORTD
HallState = (unsigned char)(PORTD >> 8) & 0x07;
// Update the PWM override register with the appropriate commutation
// state.
if(Flags.Reverse)
OVDCON = BLDCstates[7 - HallState];
else
OVDCON = BLDCstates[HallState];
}
}
// Run this code if driving a brush DC motor
if(Mode.BDC)
{
}
*/
// Clear the WDT in the PWM event loop
ClrWdt();
Flags.PWMEvent = 0;
}
} // end if(PWMEvent)
//-----------------------------------------------------------------
// Medium speed event handler executes every 5msec
//-----------------------------------------------------------------
if(Flags.MediumEvent)
{
// This is the 'motion profile'. If a direction change is
// requested, this code will ramp down the speed setting,
// reverse the motor, then slowly ramp it back up.
if(Flags.DirChange)
{
if(Flags.Accelerate)
{
// If we're not back to original speed, then accelerate
if(Speed != OldSpeed) Speed++;
// If back at original speed, clear the DirChange flag and turn off LED
else
{
Flags.DirChange = 0;
// LED4 = 0;
}
}
// If decelerating and speed is non-zero, slow down
else if(Speed) Speed--;
// Has the speed ramped down to 0?
// If so, set the acceleration flag and reverse the direction.
else if(!Speed)
{
Flags.Accelerate = 1;
Flags.Reverse = ~Flags.Reverse;
}
}
if(Flags.Restart)
{
if(Speed == OldSpeed) Flags.Restart = 0;
else Speed++;
}
// Get the motor speed setting from the potentiometer on AN7
if(!Flags.DirChange && !Flags.Restart)
{
Speed = ReadADC(5);
if(Mode.ACIM || Mode.ACIMTest) Speed >>= 2;
}
if(Mode.ACIMTest)
{
// In test mode, the V_Hz slope is determined by the voltage
// on AN12.
V_Hz_Slope = ReadADC(4) >> 1;
}
else
{
// In the normal ACIM mode, the V/Hz slope is set by a constant.
V_Hz_Slope = V_HZ_SLOPE;
}
// The following code handles the V/Hz profile for the ACIM modes
if(Mode.ACIM || Mode.ACIMTest)
{
// Limit the frequency range.
if(Speed > MOD_FREQ_LIMIT) Speed = MOD_FREQ_LIMIT;
// The following code limits the Amplitude variable that is
// sent to the modulaton routine.
if(V_Hz_Slope == 0) Amplitude = 0;
else
{
// Calculate the maximum speed input setting that will not
// exceed the amplitude limit when multiplied by the
// V_Hz_Slope variable.
temp = HIGH_AMPLITUDE_LIMIT/V_Hz_Slope;
// If the speed input is greater than this, set the Amplitude
// variable at its maximum limit.
// Otherwise, multiply the speed input by the VHz slope to
// get the modulation amplitude.
if(Speed > temp)
Amplitude = HIGH_AMPLITUDE_LIMIT;
else
Amplitude = Speed*V_Hz_Slope;
if(Amplitude < LOW_AMPLITUDE_LIMIT)
Amplitude = LOW_AMPLITUDE_LIMIT;
}
}
/* if(Mode.BLDC)
{
// Limit the maximum duty cycle
if(Speed > BLDC_SPEED_LIMIT) Speed = BLDC_SPEED_LIMIT;
// Write the new PWM duty cycles.
PDC1 = Speed;
PDC2 = Speed;
PDC3 = Speed;
}
if(Mode.BDC)
{
// Limit the range of duty cycles.
if(Speed > BDC_SPEED_LIMIT) Speed = BDC_SPEED_LIMIT;
// Write the duty cycles to the PWM module.
if(Flags.Reverse)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -