?? switches.c
字號:
//*****************************************************************************
//
// switches.c - Code for the push button.
//
// Copyright (c) 2005-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 199 of an01245.
//
//*****************************************************************************
#include "../../hw_gpio.h"
#include "../../hw_memmap.h"
#include "../../hw_nvic.h"
#include "../../hw_types.h"
#include "car.h"
#include "compiler.h"
#include "switches.h"
#include "system.h"
//*****************************************************************************
//
//! \page switches_intro Introduction
//!
//! There is a single momentary push button switch on the car that is used to
//! provide basic control of the car's operation. The switch does not have any
//! hardware debouncing (such as an RC low pass filter), so it is the
//! responsibility of software to provide debouncing of the switch input.
//!
//! Debouncing is accomplished by sampling the switch at a relatively fast rate
//! (relative to a human's ability to press and release the button). The
//! sampled state of the switch must match for four consecutive samples before
//! it is assumed to be in the given state. So, as the switch bounces due to a
//! press or release, it it not sampled in the same state and therefore the
//! bouncing is ignored.
//!
//! The code for handling the switches is contained in <tt>switches.c</tt>,
//! with <tt>switches.h</tt> containing the API definitions for use by the
//! remainder of the application.
//
//*****************************************************************************
//*****************************************************************************
//
//! \defgroup switches_api Definitions
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
//! The GPIO port that contains the pin to which the push button is connected.
//
//*****************************************************************************
#define PUSH_BUTTON_PORT GPIO_PORTC_BASE
//*****************************************************************************
//
//! The GPIO pin to which the push button is connected.
//
//*****************************************************************************
#define PUSH_BUTTON (1 << 4)
//*****************************************************************************
//
//! The debounced state of the switch(es). There is only one switch on the
//! car, so only bit zero it utilized.
//
//*****************************************************************************
ZERO_INIT unsigned long g_ulSwitches;
//*****************************************************************************
//
//! This structure describes the state of the switch debouncer. The debouncre
//! uses four consecutive samples with the switch in the non-debounced state
//! to detect a change in the state of the switch. This structure and
//! algorithm allows up to eight switches to be debounced in parallel, though
//! only one is utilized by the car.
//
//*****************************************************************************
typedef struct
{
//
//! This is the low order bit of the clock used to count the number of
//! samples with the switch in the non-debounced state.
//
unsigned char ucClockA;
//
//! This is the high order bit of the clock used to count the number of
//! samples with the switch in the non-debounced state.
//
unsigned char ucClockB;
//
//! This is the count of the number of samples during which the switch has
//! been pressed; it is used to distinguish a button press from a button
//! hold.
//
unsigned short usCount;
}
tSwitchState;
//*****************************************************************************
//
//! The current state of the switch debouncer.
//
//*****************************************************************************
static ZERO_INIT tSwitchState g_sSwitchState;
//*****************************************************************************
//
//! Handles the SysTick interrupt.
//!
//! This fucntion is called when SysTick generates an interrupt. The state of
//! the push button will be sampled, debounced, and the appropriate action
//! taken when required.
//!
//! Pressing the button will call the CarStop() function on the button press
//! and the CarRun() function in the button release. Holding the button for an
//! extended period of time (controlled by #DIAG_HOLD_TIME) will call the
//! CarDiagnostic() function when the timeout period expires (and therefore
//! CarRun() is not called).
//!
//! On every SysTick interrupt, the CarTick() function is called to allow the
//! car to make decisions about how to proceed.
//!
//! \return None.
//
//*****************************************************************************
void
SysTickHandler(void)
{
unsigned char ucValue, ucDelta;
//
// Read the current switch input value.
//
ucValue = HWREG(PUSH_BUTTON_PORT + GPIO_O_DATA + (PUSH_BUTTON << 2));
//
// Determine the switches that are at a different state than the debounced
// state.
//
ucDelta = ucValue ^ (g_ulSwitches & 0xff);
//
// Increment the clocks by one.
//
g_sSwitchState.ucClockA ^= g_sSwitchState.ucClockB;
g_sSwitchState.ucClockB = ~g_sSwitchState.ucClockB;
//
// Reset the clocks corresponding to switches that have not changed state.
//
g_sSwitchState.ucClockA &= ucDelta;
g_sSwitchState.ucClockB &= ucDelta;
//
// Get the new debounced switch state.
//
g_ulSwitches &= (g_sSwitchState.ucClockA | g_sSwitchState.ucClockB);
g_ulSwitches |= ((~(g_sSwitchState.ucClockA | g_sSwitchState.ucClockB)) &
ucValue);
//
// Determine the switches that just changed debounced state.
//
ucDelta ^= (g_sSwitchState.ucClockA | g_sSwitchState.ucClockB);
//
// See if the push button just changed state.
//
if(ucDelta & PUSH_BUTTON)
{
//
// See if the push button was just pressed or released.
//
if(!(g_ulSwitches & PUSH_BUTTON))
{
//
// The button was just pressed, so reset the hold time counter.
//
g_sSwitchState.usCount = 0;
//
// If currently in run mode, switch to stop mode immediately.
//
CarStop();
}
else
{
//
// The button was just released, so see if it was held less than
// the diag mode hold time.
//
if(g_sSwitchState.usCount < (DIAG_HOLD_TIME * SYSTICK_CLOCK))
{
//
// If currently in stop mode, switch to run mode.
//
CarRun();
}
}
}
//
// See if the push button is currently pressed.
//
if(!(g_ulSwitches & PUSH_BUTTON))
{
//
// Increment the hold counter if it is not maxed out.
//
if(g_sSwitchState.usCount < 65535)
{
g_sSwitchState.usCount++;
}
//
// See if the button has pressed for 5 seconds.
//
if(g_sSwitchState.usCount == (DIAG_HOLD_TIME * SYSTICK_CLOCK))
{
//
// Switch to diag mode.
//
CarDiagnostic();
}
}
//
// Call the car's tick handler.
//
CarTick();
}
//*****************************************************************************
//
//! Configures the switch input.
//!
//! This function prepares the push button switch for normal operation. The
//! corresponding pin is configured as an input and the SysTick counter is
//! configured to generate periodic interrupts.
//!
//! The default debounced state of the switch is taken to be the state of the
//! switch during initialization. If the switch is being pressed or released
//! during initialization, the default debounced state may end up incorrect.
//! In this case, the debouncing of the switch will quickly rectify the
//! situation, though a spurious press or release event may result.
//!
//! \return None.
//
//*****************************************************************************
void
SwitchesInit(void)
{
//
// Make the push button pin be an input.
//
HWREG(PUSH_BUTTON_PORT + GPIO_O_DIR) &= ~(PUSH_BUTTON);
//
// Read the current switch input values. This becomes the default
// debounced switch state.
//
g_ulSwitches = HWREG(PUSH_BUTTON_PORT + GPIO_O_DATA + (PUSH_BUTTON << 2));
//
// Setup SysTick to interrupt at the appropriate rate.
//
HWREG(NVIC_ST_RELOAD) = (SYSTEM_CLOCK / SYSTICK_CLOCK) - 1;
HWREG(NVIC_ST_CTRL) = (NVIC_ST_CTRL_CLK_SRC | NVIC_ST_CTRL_INTEN |
NVIC_ST_CTRL_ENABLE);
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -