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

📄 tsc_sdk79524.c

📁 SHARP_ARM720T_LH79524/5软件开发包_支持TFT_LCD_NAND_FLASH_ETH_USB
💻 C
字号:
/***********************************************************************
 * $Workfile:   .c  $
 * $Revision:     $
 * $Author:     $
 * $Date:   Jul 21 2004 15:33:14  $
 *
 * Project: ADC Validation
 *
 * Description:
 *     A simple ADC driver example that demonstrates use of interrupts
 *     and automatic toucschreen detection.
 *
 * Revision History:
 * $Log::   //smaicnt2/pvcs/VM/CHIPS/archives/software/csps/lh79524/bsp$
 * 
 * 
 * 
 * 
 ***********************************************************************
 * 
 *  Copyright (c) 2004 Sharp Microelectronics of the Americas 
 * 
 *  All rights reserved 
 * 
 *  SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION 
 *  OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE, 
 *  AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES, 
 *  SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE. 
 * 
 *  SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY
 *  FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A 
 *  SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE 
 *  FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS. 
 * 
 **********************************************************************/
#include <stdio.h>
#include "lh79524_adc_driver.h"

#define EVB79524_XTAL_IN    (11289600)

typedef struct 
{
  BOOL_32 mode_5wire;
  BOOL_32 seperate_pen_int;
  ADC_VREFP_T vrefp_x;    /* Positive analog sample reference */
  ADC_VREFN_T vrefn_x;    /* Negative analog sample reference */
  ADC_VREFP_T vrefp_y;    /* Positive analog sample reference */
  ADC_VREFN_T vrefn_y;    /* Negative analog sample reference */
} ADC_4_5WIRE_TSC_PARAM_T;


#define MAX_CONVERSION _BITMASK(10)
#define PEN_THRESHHOLD (MAX_CONVERSION/3)
#define WIDTH 320
#define HEIGHT 240


/* define for sample indexes */
#define XCOORD_START      2
#define XCOORD_END        5
#define YCOORD_START      6
#define YCOORD_END        9
#define PEN_DOWN_START    1
#define PEN_DOWN_END      11

static INT_32 dev_adc;

/* Captured coordinates */
static UNS_32 x_coord = 0;
static UNS_32 y_coord = 0;

/* Total samples requested */
#define N_SAMPLES     12 /* in 4 wire mode */
/* Sample array */
static UNS_16 samples[N_SAMPLES] = { 0,};

/* Flag used to indicate that sample is complete */
static volatile BOOL_32 sampleflag = FALSE;




/***********************************************************************
*
* Function: eos_callback
*
* Purpose:
*  Handle the end of sequence interrupt. If the pen is down and the
*  pen position has changed since the last measurement, or if the
*  pen has gone from being down to being up post a new set of 
*  coordinates to the touch screen queue. If the pen was down and
*  is now up, stop the periodic polling and start pen down detect.
* 
* Processing:
*  Clear the end of sequence interrupt
*  Stop the conversion poll timer if it was already running.
*  If there was an ADC FIFO overrun error detected, light an LED,
*  flush the measurment FIFO, restart the measurement timer, and exit.
*  Compute the next position in the touch screen coordinate queue;
*  if there is no room for a new result, flush the FIFO, restart the
*  polling timer, and exit.
*  Read and discard the deglitch conversion
*  Read and record the pen state.
*  If the pen is not down, flush the result FIFO.
*  Otherwise,
*      Read and average the 4 x coordinate measurements
*      Read and average the 4 y coordinate measurements
*      Read and discard the 2nd deglitch conversion
*      Read and record the pen state.
*  If the recorded pen state is pen-up then record the pen up state
*  in the next available touch screen coordinate queue location.
*  If the recorded pen state is pen-down, map the measured coordinates
*  to screen coordinates and record pen-down along with these coords.
*  in the next available touch screen coordinate queue location.
*  If the touch screen coordinates or pen state are different from
*  those of the previous measurement, record this new state as the
*  previous state and update the touch screen queue input index so that
*  the new measurement is enqueued in the touch screen queue.
*  If the pen is now up, enable pen-down detect. Otherwise, restart
*  the measurement timer.
* 
* Parameters: None
* 
* Outputs: None. 
* 
* Returns: Nothing
* 
* Notes:
*  This code requires a 4-wire touch screen to be connected
*  to the EVB.
*
**********************************************************************/
static void eos_callback(void)
{
  	INT_32 idx;
  	ADC_CH_CONV_T idle_ch;
  	UNS_32 pen_level;
  
  	/* Read samples from ADC */
  	//adc_read_ring(dev_adc, samples, N_SAMPLES*sizeof(UNS_16));
    adc_read_polled(dev_adc, samples, N_SAMPLES*sizeof(UNS_16));
    
  	/* sample 0 is dummy and sample 1 & sample 11 are pen level 
  		mesurements */
  	pen_level = ADC_READ_OUTPUT(samples[PEN_DOWN_START]);
  
  	if(pen_level < PEN_THRESHHOLD) 
  	{
    	pen_level = ADC_READ_OUTPUT(samples[PEN_DOWN_END]);
    
    	if(pen_level < PEN_THRESHHOLD)
    	{
      		x_coord = y_coord = 0;
      		/* Strip off sample number and justify data */
      		for (idx = XCOORD_START; idx <= XCOORD_END; idx++)
      		{
        		x_coord += ADC_READ_OUTPUT(samples[idx]);
      		}
      		x_coord /= (XCOORD_END - XCOORD_START + 1);
      
      		/* Strip off sample number and justify data */
      		for (idx = YCOORD_START; idx <= YCOORD_END; idx++)
      		{
        		y_coord += ADC_READ_OUTPUT(samples[idx]);
      		}
      		y_coord /= (YCOORD_END - YCOORD_START + 1);
      
      		//y_coord = ((MAX_CONVERSION - y_coord) * HEIGHT) /
        	//	MAX_CONVERSION;
      		//x_coord = (x_coord * WIDTH) / MAX_CONVERSION;
      
      		/* Set sample flag so background knows it has data */
      		sampleflag = TRUE;
    	}
    	else
    	{
      		sampleflag = FALSE;
    	}
  	}
  	/* Clear the FIFO of any garbage */
  	adc_ioctl(dev_adc, ADC_CLEAR_FIFO, 0); 
}


/***********************************************************************
*
* Function: tsc_init
*
* Purpose: Initialize the ADC
*
* Processing:
*			Initialize the ADC for touch screen usage
*
* Parameters: None
*
* Outputs: None
*
* Returns: None
*
* Notes:
*
**********************************************************************/
#define ACQTIME 2000
int tsc_init(ADC_4_5WIRE_TSC_PARAM_T* tst_param)
{
  
  	ADC_CH_CONV_T chdata = { 0,};
  	ADC_TSC_CONV_T tsc_cfg;

  	
  	/* Open ADC */
  	dev_adc = adc_open(ADC_BASE, (INT_32)EVB79524_XTAL_IN);
  
    if(dev_adc == 0)
        return -1;
  
  	/* Reset the ADC and driver to a known 'clean' state. This isn't
  	needed directly after open, but is good practice */
  	adc_ioctl(dev_adc, ADC_SET_STATE, ADC_MODE_RESET);
  
  	/* Set ADC clock speed to 400KHz */
  	adc_ioctl(dev_adc, ADC_SET_CLOCK, 400000);
  
  	tsc_cfg.settle_time = ACQTIME;
  	tsc_cfg.vrefp_x = tst_param->vrefp_x;
  	tsc_cfg.vrefn_x = tst_param->vrefn_x;
  	tsc_cfg.vrefp_y = tst_param->vrefp_y;
  	tsc_cfg.vrefn_y = tst_param->vrefn_y;
  
  	if(tst_param->mode_5wire)
  	{
  		/* Set ADC in 5 wire TSC mode to do x & y coordinates mesurement 
    		with 2000 usec of acquisition/settling time */
    	adc_ioctl(dev_adc, ADC_SET_5WIRE_TSC_MODE, (INT_32)&tsc_cfg);
  	}
  	else
  	{
  		/* Set ADC in 4 wire TSC mode to do x & y coordinates mesurement 
    		with 2000 usec of acquisition/settling time */
    	adc_ioctl(dev_adc, ADC_SET_4WIRE_TSC_MODE, (INT_32)&tsc_cfg);
  	}
  
  	/* Set FIFO trip level to 2 samples (X and Y) */
  	adc_ioctl(dev_adc, ADC_SET_WMLEVEL, 12);
  
  	/* Enable the ADC interrupt to interrupt on end of sequence and
  		a pendown event */
  	//adc_ioctl(dev_adc, ADC_INT_ENABLE, (ADC_EOS_INT | ADC_PEN_INT));
  
  	/* Clear the FIFO of any garbage */
  	adc_ioctl(dev_adc, ADC_CLEAR_FIFO, 0);
  
  	/* Set sample mode to sample on pendown event */
  	//adc_ioctl(dev_adc, ADC_SET_STATE, ADC_PENDOWN_TRIGGERED);
  
	/*  
	*  Clear the pen interrupt.
	*  Disable the pen interrupt
	*  Clear the idle state (so the pen won't draw current between 
	*  measurements)
	*  Switch the ADC to soft-triggered mode.
	*/  
  
	adc_ioctl(dev_adc, ADC_CLEAR_INTS, ADC_PEN_CLR);
  	adc_ioctl(dev_adc, ADC_INT_DISABLE, ADC_PEN_INT);
  	/* clear idle channel mesurement*/
  	adc_ioctl(dev_adc, ADC_SET_IDLE_CH, (INT_32)&chdata);
  
  	/* Set sample mode to sample on SW trigger event */
  	adc_ioctl(dev_adc, ADC_SET_STATE, ADC_SW_TRIGGERED);
  	/*switch on the internal refrence buffer for proper measurements*/
  	adc_ioctl(dev_adc, ADC_ENABLE_IREF, 1);
  	
  	return 0;
  	
}


/***********************************************************************
*
* Function: c_entry
*
* Purpose: Validate ADC interface
*
* Processing:
*     This function will initialize UART for generic outoput and then
*     calls various test functions based on the settings defines in
*     adc_test_param.h file.
*
* Parameters: None
*
* Outputs: None
*
* Returns: Returns value returned by the test function.
*
* Notes:
*
**********************************************************************/
int c_entry(void)
{
    ADC_4_5WIRE_TSC_PARAM_T tst_param;
    int status;
    
    tst_param.mode_5wire = FALSE;
    tst_param.seperate_pen_int = FALSE;
    tst_param.vrefp_x = ADC_REFP_AN0;
    tst_param.vrefn_x = ADC_REFN_AN1;
    tst_param.vrefp_y = ADC_REFP_AN2;
    tst_param.vrefn_y = ADC_REFN_AN3;
  
    if(tsc_init(&tst_param))
        printf("ERROR!!!\n");
      
    /* Loop around. Check the result */   
    while (1)
    {

        /* Clear status */
        adc_ioctl(dev_adc, ADC_CLEAR_INTS, ADC_PEN_CLR | ADC_EOS_CLR);   
        
        /* Clear the FIFO of any garbage */
        adc_ioctl(dev_adc, ADC_CLEAR_FIFO, 0);
       
       /* Trigger to start next conversion */
        status = adc_ioctl(dev_adc, ADC_START_SAMP, 0); 
        if (status == _ERROR) return -1;               
    
        /* Wait for conversion to complete. */
        while((adc_ioctl(dev_adc, ADC_GET_STATUS, (INT_32)ADC_RAWINT_ST) 
               & ADC_EOS_IRQ) == 0);
    
        /* Get the ADC result */
        eos_callback();
        
        /* Print out the value if pen is down */
        if(sampleflag)
        {
            sampleflag = FALSE;
        	printf("X is %d, Y is %d.\n",x_coord,y_coord);
    	}
    }    
}

⌨️ 快捷键说明

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