⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 switches.c

📁 CNC.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
    // Set the shutdown flag if the shutdown switch is pressed.
    //
    if(!(ulReg & SHUTDOWN))
    {
        ulFlags |= SWITCH_SHUTDOWN;
    }

    //
    // Return the set of switches that are presently pressed.
    //
    return(ulFlags);
}

//*****************************************************************************
//
//! Read the full state of the switches.
//!
//! This function reads the state of the switches on the CNC machine.  The
//! limit switches are read by timing the capacitor charge time in the RC
//! circuit; therefore the min and max limit on each switch pin is individually
//! recognized.
//!
//! \return A bit field of \b SWITCH_??? values to indicate the switches that
//! are currently pressed.
//
//*****************************************************************************
unsigned long
SwitchesLimitRead(void)
{
    unsigned long ulIdx, ulX, ulY, ulZ;
    unsigned char pucBuffer[32];

    //
    // Set the limit switch GPIOs for maximum drive strength and turn off the
    // weak pull-down.
    //
    GPIOPadConfigSet(GPIO_PORTC_BASE, LIMIT_X | LIMIT_Y | LIMIT_Z,
                     GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);

    //
    // Set the limit switch GPIOs as low outputs to drain the capacitors in the
    // limit switch RC circuit.
    //
    GPIODirModeSet(GPIO_PORTC_BASE, LIMIT_X | LIMIT_Y | LIMIT_Z,
                   GPIO_DIR_MODE_OUT);
    GPIOPinWrite(GPIO_PORTC_BASE, LIMIT_X | LIMIT_Y | LIMIT_Z, 0);

    //
    // Delay while the capacitors drain.
    //
    SwitchesDelay(100);

    //
    // Disable processor interrupts.
    //
    IntMasterDisable();

    //
    // Set the limit switch GPIOs to inputs, allowing the capacitors to charge
    // if one of the limit switches is pressed.
    //
    GPIODirModeSet(GPIO_PORTC_BASE, LIMIT_X | LIMIT_Y | LIMIT_Z,
                   GPIO_DIR_MODE_IN);

    //
    // Capture a set of limit switch samples to time the rise time on the RC
    // circuit.
    //
    for(ulIdx = 0; ulIdx < (sizeof(pucBuffer) / sizeof(pucBuffer[0])); ulIdx++)
    {
        pucBuffer[ulIdx] = GPIOPinRead(GPIO_PORTC_BASE,
                                       LIMIT_X | LIMIT_Y | LIMIT_Z);
    }

    //
    // Enable processor interrupts.
    //
    IntMasterEnable();

    //
    // Set the limit switch GPIOs back to low drive strength with the weak
    // pull-down enabled.
    //
    GPIOPadConfigSet(GPIO_PORTC_BASE, LIMIT_X | LIMIT_Y | LIMIT_Z,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);

    //
    // Set the low counts for the three limit switches to zero.
    //
    ulX = 0;
    ulY = 0;
    ulZ = 0;

    //
    // Loop over the limit switch samples.
    //
    for(ulIdx = 0; ulIdx < (sizeof(pucBuffer) / sizeof(pucBuffer[0])); ulIdx++)
    {
        //
        // Increment the X low count if the X limit switch is low in this
        // sample.
        //
        if(!(pucBuffer[ulIdx] & LIMIT_X))
        {
            ulX++;
        }

        //
        // Increment the Y low count if the Y limit switch is low in this
        // sample.
        //
        if(!(pucBuffer[ulIdx] & LIMIT_Y))
        {
            ulY++;
        }

        //
        // Increment the Z low count if the Z limit switch is low in this
        // sample.
        //
        if(!(pucBuffer[ulIdx] & LIMIT_Z))
        {
            ulZ++;
        }
    }

    //
    // Set the flag to indicate no buttons are pressed.
    //
    ulIdx = 0;

    //
    // If the X low count is zero, then both X limit switches are pressed
    // (which shouldn't happen unless a switch fails or someone sticks their
    // finger into the machine).  If the X low count is 4 +/- 2, then the X
    // minimum limit switch is pressed.  If the X low count is 18 +/- 2, then
    // the X maximum limit switch is pressed.
    //
    if(ulX == 0)
    {
        ulIdx |= SWITCH_LIMIT_X;
    }
    else if((ulX >= 11) && (ulX <= 15))
    {
        ulIdx |= SWITCH_LIMIT_X_MINUS;
    }
    else if((ulX >= 26) && (ulX <= 30))
    {
        ulIdx |= SWITCH_LIMIT_X_PLUS;
    }

    //
    // If the Y low count is zero, then both Y limit switches are pressed
    // (which shouldn't happen unless a switch fails or someone sticks their
    // finger into the machine).  If the Y low count is 4 +/- 2, then the Y
    // minimum limit switch is pressed.  If the Y low count is 18 +/- 2, then
    // the Y maximum limit switch is pressed.
    //
    if(ulY == 0)
    {
        ulIdx |= SWITCH_LIMIT_Y;
    }
    else if((ulY >= 11) && (ulY <= 15))
    {
        ulIdx |= SWITCH_LIMIT_Y_MINUS;
    }
    else if((ulY >= 26) && (ulY <= 30))
    {
        ulIdx |= SWITCH_LIMIT_Y_PLUS;
    }

    //
    // If the Z low count is zero, then both Z limit switches are pressed
    // (which shouldn't happen unless a switch fails or someone sticks their
    // finger into the machine).  If the Z low count is 4 +/- 2, then the Z
    // minimum limit switch is pressed.  If the Z low count is 18 +/- 2, then
    // the Z maximum limit switch is pressed.
    //
    if(ulZ == 0)
    {
        ulIdx |= SWITCH_LIMIT_Z;
    }
    else if((ulZ >= 11) && (ulZ <= 15))
    {
        ulIdx |= SWITCH_LIMIT_Z_MINUS;
    }
    else if((ulZ >= 26) && (ulZ <= 30))
    {
        ulIdx |= SWITCH_LIMIT_Z_PLUS;
    }

    //
    // Set the shutdown flag if the shutdown switch is pressed.
    //
    if(GPIOPinRead(GPIO_PORTE_BASE, SHUTDOWN) == 0)
    {
        ulIdx |= SWITCH_SHUTDOWN;
    }

    //
    // Return the set of switches that are presently pressed.
    //
    return(ulIdx);
}

//*****************************************************************************
//
//! Initializes the switches on the CNC machine.
//!
//! This function initializes the interface to the switches on the CNC machine.
//!
//! \return None.
//
//*****************************************************************************
void
SwitchesInit(void)
{
    //
    // Make the limit switch GPIOs be inputs with weak pull-downs.
    //
    GPIODirModeSet(GPIO_PORTC_BASE, LIMIT_X | LIMIT_Y | LIMIT_Z,
                   GPIO_DIR_MODE_IN);
    GPIOPadConfigSet(GPIO_PORTC_BASE, LIMIT_X | LIMIT_Y | LIMIT_Z,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);

    //
    // Make the shutdown switch GPIO be an input.
    //
    GPIODirModeSet(GPIO_PORTE_BASE, SHUTDOWN, GPIO_DIR_MODE_IN);

    //
    // Configure the panic switch GPIO for falling edge interrupts.
    //
    GPIOIntTypeSet(GPIO_PORTE_BASE, SHUTDOWN, GPIO_FALLING_EDGE);
    GPIOPinIntEnable(GPIO_PORTE_BASE, SHUTDOWN);
    IntEnable(INT_GPIOE);
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -