📄 bldc_hall.c
字号:
}
// Otherwise, slow down
else Speed--;
// Has the speed ramped down to 0?
// If so, set the acceleration flag and reverse the direction.
if(Speed == 0)
{
Flags.Accelerate = 1;
Flags.Reverse = ~Flags.Reverse;
}
}
if(Flags.Restart)
{
if(Speed == OldSpeed) Flags.Restart = 0;
else Speed++;
}
Flags.MediumEvent = 0;
} //end if(MediumEvent)
//-----------------------------------------------------------------
// Slow event handler executes every 100msec
//-----------------------------------------------------------------
if(Flags.SlowEvent)
{
// These statements check to see if any of the buttons are pressed.
// If so, a software flag is set so the button press can be debounced.
if(BUTTON1) Flags.Button1 = 1;
if(BUTTON2) Flags.Button2 = 1;
if(BUTTON3) Flags.Button3 = 1;
if(BUTTON4) Flags.Button4 = 1;
// If button #1 is pressed, a reset signal will be sent to the
// power module to clear any fault LEDs that are lit.
// The fault A interrupt flag is also cleared to reactivate
// the dsPIC MCPWM.
if(Flags.Button1)
{
// Wait until the button is released before doing anything.
if(!BUTTON1)
{
if(Flags.PWMFault)
{
// Reset the speed variable. Save the old speed for use
// in the restart profile.
OldSpeed = Speed;
Speed = 0;
// Set the duty cycles back to 0% and set a restart flag.
PDC1 = 0;
PDC2 = 0;
PDC3 = 0;
Flags.Restart = 1;
// Turn off the status LED
LED1 = 0;
// Reset the power module.
FAULT_RESET = 1;
Nop();
Nop();
Nop();
FAULT_RESET = 0;
// Clear the PWM fault software flag.
Flags.PWMFault = 0;
// Clear the button status flag.
Flags.Button1 = 0;
}
else
{
// If button #1 is pressed during normal operation, don't do anything.
Flags.Button1 = 0;
}
}
}
// Button2 doesn't do anything except light the LED
if(Flags.Button2)
{
LED2 = 1;
if(!BUTTON2)
{
LED2 = 0;
Flags.Button2 = 0;
}
}
// Button2 doesn't do anything except light the LED
if(Flags.Button3)
{
LED3 = 1;
if(!BUTTON3)
{
LED3 = 0;
Flags.Button3 = 0;
}
}
// Button #4 is used to set the direction of the motor.
if(Flags.Button4)
{
// Wait until the button is released before doing anything.
// Start a direction change if one is not already in progress.
if(!BUTTON4 && !Flags.DirChange)
{
LED4 = 1;
// Set the direction change flag.
Flags.DirChange = 1;
// Tell the motion profile to decelerate the motor
Flags.Accelerate = 0;
// Save the current Speed setting
OldSpeed = Speed;
// Clear the button status flag
Flags.Button4 = 0;
}
}
Flags.SlowEvent = 0;
} // end if(SlowEvent)
} // end while(1)
} // end main
//---------------------------------------------------------------------
void Setup(void)
{
// Initialize PORTs
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;
PORTG = 0;
TRISA = 0x39FF; // RA15, RA14, RA10, RA9 are outputs
ADPCFG = 0x0038; // RB3, RB4, and RB5 digital inputs for QEI
TRISB = 0xFFFF;
TRISC = 0xFFFF;
TRISD = 0xF74F; // RD11 is output for PWM_OUTPUT_ENABLE line
// RD5 is PFC_FIRE line
// RD4 is BRAKE_FIRE line
// RD7 is for UP/DWN status LED
TRISE = 0xFDFF; // RE9 is output for FAULT_RESET line
TRISG = 0xFFFF;
// Initialize PWM
PTPER = 460; // Value gives 16KHZ edge aligned PWM at 7.38MIPS
PDC1 = 0;
PDC2 = 0;
PDC3 = 0;
PDC4 = 0;
PWMCON1 = 0x0777; // Enable PWM 1,2,3 pairs for independent mode
DTCON1 = 0; // Dead time disabled
DTCON2 = 0;
FLTACON = 0x0007; // Fault A enabled for latched mode on PWM1, 2, and 3
FLTBCON = 0; // Fault B not used.
OVDCON = 0x3F00; // Enable PWM1H,1L, 2H, 2L, 3L, 3H for PWM
PTCON = 0x8000; // Enable PWM for edge aligned operation
IFS2bits.PWMIF = 0;
IEC2bits.PWMIE = 1; // Enable PWM interrupts.
IFS2bits.FLTAIF = 0;// Clear the fault A interrupt flag.
IEC2bits.FLTAIE = 1;// Enable interrupts for Fault A
// Initialize ADC
ADCON1 = 0;
ADCON2 = 0;
ADCON3 = 0;
ADCHS = 0x0007;
ADCON1bits.ADON = 1;
// Clear any active faults on the power module.
FAULT_RESET = 1;
Nop();
Nop();
Nop();
FAULT_RESET = 0;
// Enable the driver IC on the motor control PCB
PWM_OUTPUT_ENABLE = 0;
// Ensure PFC switch is off.
PFC_FIRE = 0;
// Turn brake off.
BRAKE_FIRE = 0;
// Ensure FLTA flag is cleared
FLTA_FLAG = 0;
}
//---------------------------------------------------------------------
int ReadADC(unsigned int channel)
{
int Delay;
if(channel > 0x000F) return(0);
ADCHS = channel;
ADCON1bits.SAMP = 1;
for(Delay = 0; Delay < 20; Delay++);
IFS0bits.ADIF = 0;
ADCON1bits.SAMP = 0;
while(!IFS0bits.ADIF);
return(ADCBUF0);
}
//---------------------------------------------------------------------
// The PWM ISR just sets a software flag to trigger SVM calculations
// in the main software loop.
void __attribute__((__interrupt__)) _PWMInterrupt(void)
{
SlowEventCount--;
if(SlowEventCount == 0)
{
Flags.SlowEvent = 1;
SlowEventCount = SLOW_EVENT_PERIOD;
}
MediumEventCount--;
if(MediumEventCount == 0)
{
Flags.MediumEvent = 1;
MediumEventCount = MEDIUM_EVENT_PERIOD;
}
Flags.PWMEvent = 1;
IFS2bits.PWMIF = 0;
}
//---------------------------------------------------------------------
// The FLTA ISR responds to events on the PWM fault pin.
// This ISR code just turns off all the PWM outputs via the OVDCON
// register and signals the main loop that a problem has occurred.
void __attribute__((__interrupt__)) _FLTAInterrupt(void)
{
// Keep all outputs disabled until we figure out what is going on!
OVDCON = 0;
// Signal a fault to the main loop.
Flags.PWMFault = 1;
// Clear the FLTA interrupt flag.
IFS2bits.FLTAIF = 0;
}
// End of file.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -