SPRUJ26A September 2021 – April 2024
It is often useful to read push buttons to allow a motor to run, stop, or simply to change the state of a global variable when pushing a button. As an example, the user can connect GPIO23 to a button to start/stop the motor. To do this, enable the pre-define symbol CMD_SWITCH_EN in project build properties as shown in Figure 3-19. The GPIO state will be assigned to motorVars_M1.flagEnableRunAndIdentify. The detailed steps are as follows.
#define MTR1_CMD_SWITCH_GPIO 23
// GPIO23->Command Switch Button
GPIO_setPinConfig(GPIO_23_GPIO23);
GPIO_setDirectionMode(23, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(23, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(23, GPIO_QUAL_3SAMPLE);
GPIO_setQualificationPeriod(23, 4);
if(GPIO_readPin(MTR1_CMD_SWITCH_GPIO) == 0)
{
objMtr->cmdSwtich.lowTimeCnt++;
if(objMtr->cmdSwtich.lowTimeCnt > objMtr->cmdSwtich.delayTimeSet)
{
objMtr->cmdSwtich.flagCmdRun = true;
}
if(objMtr->cmdSwtich.highTimeCnt > 0)
{
objMtr->cmdSwtich.highTimeCnt--;
}
}
else
{
objMtr->cmdSwtich.highTimeCnt++;
if(objMtr->cmdSwtich.highTimeCnt > objMtr->cmdSwtich.delayTimeSet)
{
objMtr->cmdSwtich.flagCmdRun = false;
}
if(objMtr->cmdSwtich.lowTimeCnt > 0)
{
objMtr->cmdSwtich.lowTimeCnt--;
}
}
if((objMtr->cmdSwtich.flagEnablCmd == true) && (objMtr->faultMtrUse.all == 0))
{
objMtr->flagEnableRunAndIdentify = objMtr->cmdSwtich.flagCmdRun;
}