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

📄 qt60xx0.c

📁 qprox公司的芯片qt60xx0的驱动程序
💻 C
字号:
/*============================================================================
    Project     QT60xx0 Example Code
    File        qt60xx0.c
    Function    Driver code for QT60xx0 - 24 Key QMatrix Touch IC
    Requires    Silicon Labs C8051F310 target
    Originated	D Spokes
    
    (c) Quantum Research Group
============================================================================*/
#include "C8051F310.H"
#include "common.h"
#include "qt60xx0.h"
#include "i2c.h"

/* DEFINES */
#define LEN_QT_SETUP		123			/* setup table lenghth */
#define NUM_QT_KEYS			24
#define NUM_QT_KEY_BYTES	3

#define QT_REV_OFFSET		0
#define QT_KEYS_OFFSET		1
#define QT_CALIBRATE_OFFSET	125
#define QT_UNLOCK_OFFSET	130
#define QT_SETUP_OFFSET		131

#define QT_COMMAND_CODE		0x55

/* TYPES */
struct param_def {
	UINT8 Offset;
	UINT8 Position;
	UINT8 Mask;
	};

/* LOCAL DATA */
xdata UINT8 QtSetupBlock[LEN_QT_SETUP]; /* setup image */
xdata UINT8 QtAddress = 117;	/* assumes default device address */
xdata UINT8 QtKeys[NUM_QT_KEY_BYTES];


/* CONSTANTS */
code struct param_def ParamDef[] = {
	/* NTHR	*/		{ 0,	0,	0x0f	},
	/* NDRIFT */	{ 0,	4,	0x0f	},
	/* PDRIFT */	{ 24,	0,	0x0f	},
	/* NDIL	*/		{ 48,	0,	0x0f	},
	/* FDIL	*/		{ 48,	4,	0x0f	},
	/* NRD	*/		{ 72,	0,	0xff	},
	/* WAKE	*/		{ 96,	3,	0x01	},
	/* BL	*/		{ 96,	4,	0x03	},
	/* AKS	*/		{ 96,	6,	0x01	},
	/* SSYNC */		{ 96,	7,	0x01	},
	/* SLEEP */		{ 120,	0,	0x07	},
	/* MSYNC */		{ 120,	6,	0x01	},
	/* AWAKE */		{ 121,	0,	0xff	},
	/* DHT */		{ 122,	0,	0xff	}
	}; /* used to insert / extract fields in setup image */

code UINT8 QtCommand = QT_COMMAND_CODE; /* used to pass command byte to I2c driver */


/*---------------- Function Header -------------------------------------------

	FUNCTION	SetQtAddress
	PURPOSE		Specifies the device address on the I2c bus
	
	INPUTS:		Address: specifies device address
	OUTPUTS:	None
	----------------------------------------------------------------------------*/
void SetQtAddress ( UINT8 Address )
{
	QtAddress = Address;
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	ReadQtKeys
	PURPOSE		Reads QT60xx0 key values
	
	INPUTS:		None
	OUTPUTS:	Returns true if function executed normally
	
----------------------------------------------------------------------------*/
UINT8 ReadQtKeys ( void )
{
	return (I2cRead( QtAddress, QT_KEYS_OFFSET, QtKeys, NUM_QT_KEY_BYTES) == 0) ? 1 : 0;
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	GetQtKeys
	PURPOSE		Returns the requested key states
	
	INPUTS:		Index: specifies key byte in range 0..2
				
	OUTPUTS:	Returns QT key-byte
	----------------------------------------------------------------------------*/
UINT8 GetQtKeys ( UINT8 Index )
{
	return ( QtKeys[Index] );
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	CalibrateQt
	PURPOSE		Sends Calibrate instruction to QT60xx0
	
	INPUTS:		None
	OUTPUTS:	Returns true if function executed normally
	
----------------------------------------------------------------------------*/
UINT8 CalibrateQt ( void )
{
	/* Send calibrate command to QT via I2c */
	return (I2cWrite( QtAddress, QT_CALIBRATE_OFFSET, &QtCommand, 1) == 0) ? 1 : 0;
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	ReadQtSetup
	PURPOSE		Reads QT701 setup info into driver database
	
	INPUTS:		None
	OUTPUTS:	Returns true if function executed normally
	
----------------------------------------------------------------------------*/
UINT8 ReadQtSetup ( void )
{
	return (I2cRead( QtAddress, QT_SETUP_OFFSET, QtSetupBlock, LEN_QT_SETUP) == 0) ? 1 : 0;
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	GetQtSetup
	PURPOSE		Returns the requested setup value from the driver's Setup image
	
	INPUTS:		Key: defines QT key number in range 0..23
				Param: defines parameter to return
	OUTPUTS:	Returns setup value
	----------------------------------------------------------------------------*/
UINT8 GetQtSetup ( UINT8 Key, UINT8 Param )
{
	UINT8 Index;
	
	/* calculate index into Setup image aarray */
	Index = ParamDef[Param].Offset;
	if ( Param <= QT_PARAM_SSYNC )
		Index += Key;

	/* access required value */
	return (QtSetupBlock[Index] >> ParamDef[Param].Position) & ParamDef[Param].Mask;
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	SetQtSetup
	PURPOSE		Writes the specified setup value to the driver's Setup image
				Optionally writes the entire setup to QT60xx0
	INPUTS:		Key: defines QT key number in range 0..23
				Param: defines parameter to write
				Value: new value for specified parameter
				WriteFlag: control flag for Write QT60xx0 setups block
	OUTPUTS:	Returns true if function executed normally

-----------------------------------------------------------------------------*/
UINT8 SetQtSetup ( UINT8 Key, UINT8 Param, UINT8 Value, UINT8 WriteFlag )
{
	UINT8 RetVal = true; /* function return value */
	UINT8 Temp; 
	UINT8 Index; 

	/* calculate index into Setup image aarray */
	Index = ParamDef[Param].Offset;
	if ( Param <= QT_PARAM_SSYNC )
		Index += Key;

	/* only process data if a valid index has been specified */
	if ( (Index < LEN_QT_SETUP) && (Key < NUM_QT_KEYS) )
	{
		/* overwrite relevant bit-field within Setup image using ParamDef array */
		Temp = (Value & ParamDef[Param].Mask) << ParamDef[Param].Position;
		QtSetupBlock[Index] &= ~(ParamDef[Param].Mask << ParamDef[Param].Position);
		QtSetupBlock[Index] += Temp;

		if ( WriteFlag )	/* if required, write complete Setup image to QT device */
		{
			/* first, unlock the setup data area for writing */
			if ( I2cWrite (QtAddress, QT_UNLOCK_OFFSET, &QtCommand, 1) )
				RetVal = false; /* fail if I2C returned non-zero status */
			else
			{	/* if the unlock was successful, write the setup image */
				if ( I2cWrite (QtAddress, QT_SETUP_OFFSET, QtSetupBlock, LEN_QT_SETUP) )
					RetVal = false; /* fail if I2C returned non-zero status */
			}
		}
	}
	else
		RetVal = false; /* fail if illegal parameter was specified */
		
	return RetVal; /* completed */
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	RestoreDefaultQtSetup
	PURPOSE		Restores driver's Setup image to factory defaults
				and writes the default setup to QT60xx0
	INPUTS:		None
	OUTPUTS:	Returns true if function executed normally

----------------------------------------------------------------------------*/
UINT8 RestoreDefaultQtSetup ( void )
{
	UINT8 i; 

	/* write default values to key parameters for all keys */
	for (i = 0; i < NUM_QT_KEYS; i++)
	{
		QtSetupBlock[i]		 = QT_DEF_NTHR + (QT_DEF_NDRIFT << 4);
		QtSetupBlock[i + 24] = QT_DEF_PDRIFT;
		QtSetupBlock[i + 48] = QT_DEF_NDIL + (QT_DEF_FDIL << 4);
		QtSetupBlock[i + 72] = QT_DEF_NRD;
		QtSetupBlock[i + 96] = QT_DEF_BL << 4;
	}

	/* write global parameters */
	QtSetupBlock[120]		 = QT_DEF_SLEEP;
	QtSetupBlock[121]		 = QT_DEF_AWAKE;

	/* set DHT default by calling SetQtSetup() also writes device */
	return ( SetQtSetup(0, QT_PARAM_DHT, QT_DEF_DHT, true) );
}

⌨️ 快捷键说明

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