📄 bldc_hall.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: BLDC_HALL.c *
* Date: 2/25/03 *
* File Version: 2.00 *
* *
* Tools used: MPLAB GL -> 6.12.02 *
* Compiler -> 1.10 beta *
* *
* Linker File: p30f6010.gld *
* *
* *
***********************************************************************
* Code Description
*
* This file demonstrates BLDC motor control using the
* dsPIC30F010.
* Port pins RD8, RD9, and RD10 are used to sample
* the hall effect sensors from the BLDC motor and determine the
* correct commutation state.
* The speed demand is read from a potentiometer.
**********************************************************************/
#include "c:\pic30_tools\support\h\p30F6010.h"
// These are the pushbutton locations
#define BUTTON1 !PORTGbits.RG6
#define BUTTON2 !PORTGbits.RG7
#define BUTTON3 !PORTGbits.RG8
#define BUTTON4 !PORTGbits.RG9
// These are the LED locations
#define LED1 PORTAbits.RA9
#define LED2 PORTAbits.RA10
#define LED3 PORTAbits.RA14
#define LED4 PORTAbits.RA15
// These are the hall effect sensor pins.
#define HALL1 PORTDbits.RD8
#define HALL2 PORTDbits.RD9
#define HALL3 PORTDbits.RD10
// This is the control line for the driver IC on the
// motor control PCB. It must be low to activate
// the PWM drive signals to the power module.
#define PWM_OUTPUT_ENABLE PORTDbits.RD11
// PFC switch firing line
#define PFC_FIRE PORTDbits.RD5
// BRAKE circuit firing line
#define BRAKE_FIRE PORTDbits.RD4
// This is the fault reset line for the motor power module.
// The dsPIC must generate an active high pulse to clear any
// of the red fault LEDs on the power module.
#define FAULT_RESET PORTEbits.RE9
// This is the fault input line from the power module. This
// line is connected to the PWM Fault A input pin (RE8).
#define FAULT_INPUT PORTEbits.RE8
// This is the interrupt flag for the PWM Fault A input. It is
// renamed for convenience.
#define FLTA_FLAG IFS2bits.FLTAIF
// 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.
// Two sets of override values are provided, one for hard switching
// and the other for soft switching. For hard switching, both upper
// and lower inverter switches are modulated with PWM. For soft
// switching, the top device is modulated, while the bottom device
// is always on.
// The type of switching can be controlled by commenting or un-
// commenting the 'HARD_SWITCH' definition below.
//#define HARD_SWITCH
#ifdef HARD_SWITCH
#define STATE0 0x0000
#define STATE1 0x1200
#define STATE2 0x2400
#define STATE3 0x0600
#define STATE4 0x0900
#define STATE5 0x1800
#define STATE6 0x2100
#define STATE7 0x0000
#endif
#ifndef HARD_SWITCH
#define STATE0 0x0000
#define STATE1 0x0210
#define STATE2 0x2004
#define STATE3 0x0204
#define STATE4 0x0801
#define STATE5 0x0810
#define STATE6 0x2001
#define STATE7 0x0000
#endif
// These are the counting periods for the medium and slow speed events.
// Count variables are decremented at each PWM interrupt.
#define SLOW_EVENT_PERIOD 1600 // 100msec period for 16KHz PWM
#define MEDIUM_EVENT_PERIOD 72 // 5msec period for 16KHz PWM
//---------------------------------------------------------------------
// This bit structure provides status flags for the software
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 :1;
unsigned :1;
unsigned PWMEvent:1;
unsigned MediumEvent:1;
unsigned SlowEvent:1;
} Flags;
// This is a software counter used to time slower system events, such as
// button polling, etc.
unsigned int SlowEventCount, MediumEventCount;
// This variable holds the speed setting
unsigned int Speed, OldSpeed;
// 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;
//----------------------------------------------------------------------
main ( void )
{
// Initialize variables
Speed = 0;
OldSpeed = 0;
SlowEventCount = SLOW_EVENT_PERIOD;
MediumEventCount = MEDIUM_EVENT_PERIOD;
Setup();
while(1)
{
// This code is executed after each PWM interrupt occurs.
// The PWM ISR sets PWMflag to signal the interrupt.
if(Flags.PWMEvent)
{
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];
}
ClrWdt(); // Clear the watchdog timer
Flags.PWMEvent = 0;
} // end if(PWMEvent)
//-----------------------------------------------------------------
// Medium speed event handler executes every 5msec
//-----------------------------------------------------------------
if(Flags.MediumEvent)
{
// If no fault management or direction change is in progress,
// read the new speed setting from the potentiometer on AN7
if(!Flags.PWMFault && !Flags.Restart && !Flags.DirChange)
{
Speed = ReadADC(7);
// Limit the maximum duty cycle
if(Speed > 1000) Speed = 1000;
}
// Write the new PWM duty cycles.
PDC1 = Speed;
PDC2 = Speed;
PDC3 = Speed;
// This is the main loop code that responds to a fault event
// signalled by the fault ISR. Here, we just turn on a status
// LED to let the user know that the fault occurred.
// The fault condition is reset when Button 1 is pressed.
if(Flags.PWMFault)
{
LED1 = 1;
}
// 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 back to our old speed, we're done!
// Clear the DirChange flag
if(Speed == OldSpeed)
{
Flags.DirChange = 0;
LED4 = 0;
}
// Otherwise, speed up
else Speed++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -