6.1 Button Controls

Two buttons are used as input to trigger the start/stop operation and CCW/CW direction. Both input buttons are continuously monitored after initialization as shown in Example 6-1. When switch S1 is pressed, it checks the motor state if it is in running or stall state. If it is in a Stall state, the RunMotor() subroutine will be called enabling CWG, Interrupt routines and CCP. If the motor is in running state and switch S1 is pressed, StopMotor() subroutine will be called disabling CWG, Interrupt routines and CCP, toggling the motor condition flag.

Program Main Loop

while (1)  {

            // wait until an action is detected
            CheckTriggerButton();
            CheckDirectionButton();
}

Switch S2 toggles the direction flag. When S2 is pressed during a motor Stop condition, it will simply change the direction flag from CCW to CW, or vice versa. By toggling the direction flag, the commutation order based on the sinusoidal data array during Hall transition is changed. If S2 button is pressed in running condition, the motor is forced to stop before changing the commutation sequence. Once the direction flag is toggled and the position is identified, the motor will start running again.