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

📄 ts.c

📁 CNC.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
//*****************************************************************************
//
// ts.c - Driver for the touch screen.
//
// 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 "ssi.h"
#include "ts.h"

//*****************************************************************************
//
//! \page ui_ts_intro Introduction
//!
//! This driver communicates with the Analog Devices AD7843 (or clone) touch
//! screen controller on the Microtips LCD panel used for the user interface.
//! The touch screen itself consists of two resistive sheets separates by a
//! thin layer of microbeads.  Pressing on the screen depresses the sheets and
//! makes them contact each other.  By applying power and ground across one of
//! the sheets, the contact of the two sheets acts as a wiper on a
//! potentiometer.  Measuring the resistance results in the position across
//! the energized sheet where the press has occurred.  Swapping the function
//! of the sheets provides the other position.
//!
//! The touch screen controller has integrated switches to apply power and
//! ground to the two resistive sheets as required to take a reading, along
//! with an ADC to measure the position.  Special considerations must be taken
//! into account in order to get valid data from the touch screen controller.
//! When power is applied across one of the resistive sheets, it takes a finite
//! amount of time for the voltage across the sheet to stabilize.  If a
//! position measurement is taking during this time, the reading will be
//! invalid.
//!
//! Once the voltage has stabilized, a good position reading can be made.
//! Changing from reading the X position to reading the Y position requires
//! that the energization of the sheets be swapped; this voltage stabilization
//! delay must be handled here as well.
//!
//! Taking a reading involves returning the previous reading and starting a new
//! reading.  This allows a reading to be returned as soon as required without
//! waiting for a new reading to be taken.  In order for this to work
//! effectively, the touch screen must be sampled on a regular and frequent
//! basis.
//!
//! The resistive touch screen sheets are not always perfectly aligned to the
//! array of LCD pixels that they overlay.  Additionally, a conversion is
//! required from the range of the ADC that is sampling the touch screen to the
//! coordinate system of the LCD.  Both of these problems are dealt with by
//! applying an algorithm described by Carlos E. Videles in the June 2002 issue
//! of Embedded Systems Design.  It can be found online at
//! <a href="http://www.embedded.com/story/OEG20020529S0046">
//! http://www.embedded.com/story/OEG20020529S0046</a>.
//!
//! The code for driving the touch screen controller is contained in
//! <tt>ui_micro/ts.c</tt>, with <tt>ui_micro/ts.h</tt> containing the API
//! definitions for use by the remainder of the application.
//
//*****************************************************************************

//*****************************************************************************
//
//! \defgroup ui_ts_api Definitions
//! @{
//
//*****************************************************************************

//*****************************************************************************
//
//! The GPIO pin on port A to which the touch screen controller chip select
//! signal is connected.
//
//*****************************************************************************
#define SSI_CS                  GPIO_PIN_3

//*****************************************************************************
//
//! The GPIO pin on port A to which the touch screen controller SPI clock
//! signal is connected.
//
//*****************************************************************************
#define SSI_CLK                 GPIO_PIN_2

//*****************************************************************************
//
//! The GPIO pin on port A to which the touch screen controller SPI Tx signal
//! is connected.
//
//*****************************************************************************
#define SSI_TX                  GPIO_PIN_5

//*****************************************************************************
//
//! The GPIO pin on port A to which the touch screen controller SPI Rx signal
//! is connected.
//
//*****************************************************************************
#define SSI_RX                  GPIO_PIN_4

//*****************************************************************************
//
//! The GPIO pin on port A to which the pen IRQ signal from the touch screen
//! controller is connected.
//
//*****************************************************************************
#define PEN_IRQ                 GPIO_PIN_0

//*****************************************************************************
//
//! The GPIO pin on port A to which the touch screen controller pen IRQ enable
//! signal is connected.
//
//*****************************************************************************
#define PEN_IRQ_ENABLE          GPIO_PIN_1

//*****************************************************************************
//
//! The most recently read X position from the touch screen controller.
//
//*****************************************************************************
static unsigned long g_ulTSX;

//*****************************************************************************
//
//! The most recently read Y position from the touch screen controller.
//
//*****************************************************************************
static unsigned long g_ulTSY;

//*****************************************************************************
//
//! The count of X coordinate requests that have been made to the touch screen
//! controller.  When transitioning from the X axis to the Y axis, this count
//! must reach zero before valid Y coordinates are returned from the touch
//! screen controller.
//
//*****************************************************************************
static unsigned long g_ulTSCount;

//*****************************************************************************
//
//! Initializes the interface to the touch screen.
//!
//! This function will initialize the interface to the touch screen controller.
//!
//! \return None.
//
//*****************************************************************************
void
TSInit(void)
{
    unsigned long ulTemp;

    //
    // Configure the GPIO pins used for the SSI and IRQ interface to the touch
    // screeen controller.
    //
    GPIODirModeSet(GPIO_PORTA_BASE, SSI_CS | PEN_IRQ_ENABLE,
                   GPIO_DIR_MODE_OUT);
    GPIODirModeSet(GPIO_PORTA_BASE, SSI_CLK | SSI_TX | SSI_RX,
                   GPIO_DIR_MODE_HW);
    GPIOPinWrite(GPIO_PORTA_BASE, SSI_CS | PEN_IRQ_ENABLE,
                 SSI_CS | PEN_IRQ_ENABLE);

    //
    // Configure the SSI controller.
    //
    SSIConfig(SSI_BASE, SSI_FRF_MOTO_MODE_3, SSI_MODE_MASTER, 2000000, 16);

    //
    // Enable the SSI controller.
    //
    SSIEnable(SSI_BASE);

    //
    // Clear any residual data from the receive FIFO.
    //
    while(SSIDataNonBlockingGet(SSI_BASE, &ulTemp) != 0)
    {
    }

    //
    // Assert the SSI chip select.
    //
    GPIOPinWrite(GPIO_PORTA_BASE, SSI_CS, 0);

    //
    // Do a bogus Y read to get the touch screen controller to go into low
    // power mode and enable the pen IRQ.
    //
    SSIDataPut(SSI_BASE, 0x0090);
    SSIDataPut(SSI_BASE, 0x0000);

    //
    // Wait until the command has been transferred and read back the data.
    //
    SSIDataGet(SSI_BASE, &ulTemp);
    SSIDataGet(SSI_BASE, &ulTemp);

    //
    // Deassert the SSI chip select.
    //
    GPIOPinWrite(GPIO_PORTA_BASE, SSI_CS, SSI_CS);

    //
    // Enable the SSI interrupt.
    //
    SSIIntEnable(SSI_BASE, SSI_RXFF | SSI_RXTO);
    IntEnable(INT_SSI);
}

//*****************************************************************************
//
//! Handles interrupts from the SSI interface.
//!
//! This is the interrupt handler for the SSI interrupt.  The SSI interface is
//! utilized in SPI mode to communicate with the AD7843 touch screen controller
//! to read the position on the LCD that has been pressed.
//!
//! When initially turned on, the voltage in the touch screen will spike and
//! then oscillate towards the true value.  Software debouncing is performed to
//! ensure a good reading, and to avoid sampling of the ringing portion of the
//! touch screen turn on time.
//!

⌨️ 快捷键说明

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