?? switches.c
字號:
//*****************************************************************************
//
// switches.c - Routines for handling the switches on the CNC machine.
//
// Copyright (c) 2006-2007 Luminary Micro, Inc. All rights reserved.
//
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
//
// The software is owned by LMI and/or its suppliers, 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 "AS IS". 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.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 220 of sw01246.
//
//*****************************************************************************
#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "gpio.h"
#include "interrupt.h"
#include "draw.h"
#include "switches.h"
#include "table.h"
#include "tool.h"
//*****************************************************************************
//
//! \page main_switches_intro Introduction
//!
//! There are seven switches on the CNC machine. Six are limit switches, two
//! per axis, with one being the home limit switch (to indicate that the axis
//! is in the home position) and the other being a limit switch (to indicate
//! that the axis is at the extent of its travel). The seventh switch is a
//! panic switch that causes all power to the machine (but not controller) to
//! be interrupted.
//!
//! The limit switches are wired up with two switches per GPIO pin; each switch
//! has a different resistor value and for part of an RC circuit. The paired
//! limit switches can be read as a normal GPIO, providing no means of
//! distingishing between the two. For normal operation of the CNC machine,
//! this is adequate.
//!
//! Alternatively, the charge time of the capacitor can be measured by sampling
//! the switch GPIO and used to determine which switch is engaged. This is
//! used during the homing operation since the CNC machine needs to move the
//! gantry such that the home limit switch is engaged, not the limit switch.
//!
//! The panic switch is wired up like a normal switch. When the panic switch
//! is engaged, an emergency stop of all mechanical systems on the CNC machine
//! must be performed as a safety measure. After the panic switch is
//! disengaged, the table must undergo another homing operation to restore the
//! positional knowledge of the table.
//!
//! The code for handling the switches is contained in
//! <tt>main_micro/switches.c</tt>, with <tt>main_micro/switches.h</tt>
//! containing the API definitions for use by the remainder of the application.
//
//*****************************************************************************
//*****************************************************************************
//
//! \defgroup main_switches_api Definitions
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
//! The GPIO pin on port C to which the X limit switches are connected.
//
//*****************************************************************************
#define LIMIT_X GPIO_PIN_2
//*****************************************************************************
//
//! The GPIO pin on port C to which the Y limit switches are connected.
//
//*****************************************************************************
#define LIMIT_Y GPIO_PIN_3
//*****************************************************************************
//
//! The GPIO pin on port C to which the Z limit switches are connected.
//
//*****************************************************************************
#define LIMIT_Z GPIO_PIN_4
//*****************************************************************************
//
//! The GPIO pin on port E to which the panic switch is connected.
//
//*****************************************************************************
#define SHUTDOWN GPIO_PIN_2
//*****************************************************************************
//
//! Provide a small delay.
//!
//! \param ulCount is the number of delay loop iterations to perform.
//!
//! Delay for a fixed period of time while the capacitor in the limit switch
//! RC circuit is discharged. It is written in assembly to keep the delay
//! consistent across tool chains, avoiding the need to tune the delay based on
//! the tool chain in use.
//!
//! \return None.
//
//*****************************************************************************
#if defined(ewarm)
static void
SwitchesDelay(unsigned long ulCount)
{
__asm(" subs r0, #1\n"
" bne SwitchesDelay\n"
" bx lr");
}
#endif
#if defined(gcc)
static void __attribute__((naked))
SwitchesDelay(unsigned long ulCount)
{
__asm(" subs r0, #1\n"
" bne SwitchesDelay\n"
" bx lr");
}
#endif
#if defined(rvmdk) || defined(__ARMCC_VERSION)
__asm void
SwitchesDelay(unsigned long ulCount)
{
subs r0, #1;
bne SwitchesDelay;
bx lr;
}
#endif
//*****************************************************************************
//
//! Interrupt handler for the shutdown switch interrupt.
//!
//! This is the interrupt handler that is called when the shutdown switch is
//! pressed.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOEIntHandler(void)
{
//
// Clear the shutdown switch interrupt.
//
GPIOPinIntClear(GPIO_PORTE_BASE, SHUTDOWN);
//
// Turn off the tool.
//
ToolOff();
//
// Immediately stop the motion on all axes.
//
TableEmergencyStop();
//
// Stop the drawing operation (if any).
//
DrawStop();
}
//*****************************************************************************
//
//! Read the state of the switches.
//!
//! This function reads the state of the switches on the CNC machine. The
//! limit switches are read as a simple GPIO; therefore the min and max limit
//! on each switch pin can not be distinguished.
//!
//! \return A bit field of \b SWITCH_??? values to indicate the switches that
//! are currently pressed.
//
//*****************************************************************************
unsigned long
SwitchesRead(void)
{
unsigned long ulFlags, ulReg;
//
// Set the flags to indicate no buttons are pressed.
//
ulFlags = 0;
//
// Read the X, Y, and Z limit switches.
//
ulReg = GPIOPinRead(GPIO_PORTC_BASE, LIMIT_X | LIMIT_Y | LIMIT_Z);
//
// Set the X min and max limit flags if one of the X limit switches are
// pressed. Both flags are set since this does not distinguish which of
// the two X limit switches are being pressed.
//
if(ulReg & LIMIT_X)
{
ulFlags |= SWITCH_LIMIT_X;
}
//
// Set the Y min and max limit flags if one of the Y limit switches are
// pressed. Both flags are set since this does not distinguish which of
// the two Y limit switches are being pressed.
//
if(ulReg & LIMIT_Y)
{
ulFlags |= SWITCH_LIMIT_Y;
}
//
// Set the Z min and max limit flags if one of the Z limit switches are
// pressed. Both flags are set since this does not distinguish which of
// the two Z limit switches are being pressed.
//
if(ulReg & LIMIT_Z)
{
ulFlags |= SWITCH_LIMIT_Z;
}
//
// Read the shutdown switch.
//
ulReg = GPIOPinRead(GPIO_PORTE_BASE, SHUTDOWN);
//
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -