0
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心
发布
  • 发文章

  • 发资料

  • 发帖

  • 提问

  • 发视频

创作活动
AT42QTAN0040

AT42QTAN0040

  • 厂商:

    ATMEL(爱特梅尔)

  • 封装:

  • 描述:

    AT42QTAN0040 - Driving the AT42QT2160 QMatrix Sensor IC - ATMEL Corporation

  • 数据手册
  • 价格&库存
AT42QTAN0040 数据手册
Driving the AT42QT2160 QMatrix Sensor IC 1. Introduction This application note shows how the AT42QT2160-MMU (QT2160) 16-key QMatrix™ Sensor IC can be connected to a microprocessor to provide touch input functionality. Example code is provided to demonstrate how easily the QT2160 can be incorporated into a design. The code can be run “as is” or used as a starting point for QT2160based projects. The example compiles to under 1 Kbyte of code including all necessary I2C-compatible driver functions. The example code is written in the C programming language and can easily be adapted for many processor types. See Section 6 on page 8 for a listing of the complete program. Figure 1-1. Circuit Configuration for the Example Project Vdd GPIO3 GPO3 Driving the AT42QT2160 QMatrix Sensor IC Application Note AT42QTAN0040 Vdd GPO2 GPO1 GPO0 P0.3 P0.7 P0.6 CHANGE SCL SDA 8 Discrete Keys (keys 8 – 15) Host MCU QT2160 5-Key Slider (keys 0 – 4) QMatrix Key Array 2. Overview of the QT2160 2.1 Introduction The QT2160 is designed for use with up to 16 keys and a slider (constructed from 2 keys up to 8 keys). There are three dedicated general-purpose input/outputs (GPIO), which can be used as inputs (for example, for mechanical switches) or as driven outputs. There are eight shared general-purpose outputs (GPO). Pulse switch modulation (PWM) control can be applied to all GPIO and GPO pins. Keys are configured in a matrix format that minimizes the number of required scan lines and device pins. The key electrodes can be designed into a conventional printed circuit board (PCB) or flexible printed circuit board (FPCB) as a copper pattern, or as printed conductive ink on plastic film. The QT2160 can be fine tuned to optimize operation with the electrode design. Full details of the QT2160 can be found in the QT2160 datasheet, which should be read in conjunction with this application note. 10702A–AT42–07/08 2.2 QT2160 Host Interface The QT2160 connects to a host controller by means of an I2C-compatible serial interface. This uses a common two-wire connection between the host QT2160 and other I 2 C-compatible devices in the system. The bus protocol takes care of all addressing functions and bidirectional data transfer. Full details can be found in the Philips document “The I2C-Bus Specification”. In addition, the QT2160 features a CHANGE pin, which can be used either to wake the host in a battery-powered application or as an interrupt signal to inform the host when the QT2160 status has changed. The CHANGE pin can be left unconnected in systems where the QT2160 is polled via the I2C-compatible bus on a regular basis. 2.3 QT2160 Programming Model All communication with the QT2160 takes place via a set of addressable, 8-bit registers. All read and write operations to the registers are made via the I2C-compatible bus. The protocol for performing reads and writes is based on that used for standard serial EEPROM devices and is fully described in the QT2160 datasheet. The example code presented in this application note includes a suitable I2C-compatible driver. 3. The Example QT2160 Project 3.1 Introduction The example project shows how to use the host interface to read real-time touch information from the QT2160. It also shows how the discrete output pins of the QT2160 can be simultaneously driven to reflect the touch status. 3.2 Circuit Configuration Figure 1-1 on page 1 shows the circuit used for this example. Representative touch keys are connected to the X/Y drive signals as described in the datasheet. The example uses a five-key slider comprising keys 0 – 4, and eight discrete keys connected as keys 8 – 15. Connection to the host microcontroller uses the two I2C-compatible pins (SCL and SDA) and the CHANGE pin. The example uses LEDs on QT2160 pins GPO0 to GPO3 to indicate touches on keys 8 – 11. It also uses an LED on GPIO3 to indicate the slider position by means of the PWM function. The following points should be noted: • The QT2160 I2C-compatible slave address can be selected via pins I2CA1 and I2CA0. The demonstration program uses address 0x0d, which is selected by connecting I2CA1 = I2CA0 = Vss. • Pull-up resistors are required on the I2C-compatible signals (SCL and SDA). Either discrete pull-up resistors or the host microcontroller’s weak pull-ups can be used. Ensure that the pull-up resistors are connected to the same Vdd level as the QT2160. • The CHANGE pin has open-drain characteristics and requires a pull-up resistor. • LEDs are connected to the appropriate pins of the QT2160 using inverting buffers as described in the datasheet. The example code assumes the following port assignments on the host microcontroller: • SDA is connected to P0.6. • SCL is connected to P0.7. • CHANGE is connected to P0.3. 2 Driving the AT42QT2160 10702A–AT42–07/08 Driving the AT42QT2160 3.3 Interface Timing Figure 3-1 illustrates the interface timing used in the example. The QT2160 signals any change in its sense status by driving the CHANGE pin low. In this context, sense status includes bytes 2 through 6 of the address map. In response to the QT2160 driving CHANGE low, the host reads all of bytes 2 – 6. The QT2160 releases the CHANGE pin when all changed bytes have been read. Handshaking logic within the QT2160 guarantees that any unread changes in any status byte will cause CHANGE to be driven low, even if this occurs while the device is being read. Figure 3-1. QT2160 Interface Timing Change in status Cleared by read Change in status during read Cleared by following read CHANGE I2C-comaptible Status read Status read Status read 4. Demonstration Program 4.1 The main() Function A simplified flowchart for the QT2160 demonstration program is shown in Figure 4-1. The program flow is encapsulated in the main() function of the C code (see Section 6 on page 8). Figure 4-1. Simplified Flowchart for the Demonstration Program Main() Establish comms to QT2160 High CHANGE pin? Low Read QT2160 Status Yes RES set? No Display Status on LEDs Initialize QT2160 3 10702A–AT42–07/08 During initialization, the program establishes communication with the QT2160 and then sets it up for the required operating mode. In the main loop, the program monitors the CHANGE pin. If the CHANGE pin is low, the program reads the five status bytes. It then writes the state of the discrete keys back to the QT2160 pins GPO0 to GPO3 and writes the slider position back to the PWM driver for GPIO3. The effect of touching the keys and slider is thus displayed on LEDs connected to I/O pins of the QT2160. 4.2 QT2160 Initialization Initialization occurs at the start of the main() function, as follows: 1. The demonstration program establishes the initial communication with QT2160 by reading its Chip-ID. The QT2160 takes several milliseconds to initialize itself after power-up. During this time, it may not acknowledge (ACK) its slave address. The WriteQtI2c() function repeatedly attempts to send the slave address until the ACK is received. 2. Once the device has been read successfully, the returned chip ID is checked. If the wrong ID is returned, the demonstration program goes no further. Depending on the construction of the touch keys being used, it may be necessary to adjust the touch-related registers. This should be done in the initialization code. 4.3 Main Loop Following initialization, the remainder of the main() function consists of a main loop, as follows: 1. The state of the CHANGE pin is sampled. 2. If CHANGE is asserted low, the status registers (addresses 2 – 6) are read into the five-byte QtStatus[] array. Reading these registers restores the CHANGE pin to the inactive (high) state. 3. If the RES bit in the “General Status” register is set, indicating that the QT2160 has been reset, the device is initialized. This will occur after initial power up but may also occur if, for example, the RST pin is pulled low at any time. The demonstration program changes a few registers from their default values. Note that all the QT2160’s registers are set to default values every time the device is reset (refer to the QT2160 datasheet for the default values). The demonstration program makes the following changes: a. GPIO3 is set for output mode in “GPIO-Direction” (address 73). b. c. GPIO3 is set for PWM mode in “GPIO-PWM” (address 75). The “RESOLUTION” field is set to 8-bit in Slider-Options (address 21). d. Finally, a calibration cycle is initiated. 4. If RES is not set, the touch status is processed. The states of the discrete touch keys 8 – 5 from “Key Status 2” are returned in QtStatus[2]. Key states output by the QT2160 are fully debounced and can be used in an application without further processing. QtStatus[2] is simply written back to “GPO Drive 1” at address 70 and the LEDs indicate the touch state. 5. If the SDET bit in the “General Status” register is set, the slider touch position in QtStatus[3] is written to “PWM Level” at address 76. 4 Driving the AT42QT2160 10702A–AT42–07/08 Driving the AT42QT2160 5. I2C-compatible Driver 5.1 I2C-compatible Communication I2C-compatible communication is a major aspect of any program involving the QT2160. Some form of I2C-compatible driver must be written to handle read and write operations to the device. Most current microprocessors include a hardware I2C-compatible master function, which could be programmed to efficiently handle these transfers, but the driver code will always be specific to the hardware. The example code in this application note includes a software-based I2C-compatible driver using two port pins of the host microcontroller. It could easily be ported to many processor types. The I2C-compatible communication sequences used to read and write data to the QT2160 are fully described in the QT2160 datasheet. 5.2 Design Approach The driver presented here uses bit-banging techniques to manage the I2C-compatible interface. The driver is structured in three layers: • In the bottom layer, code-macros are used to drive the SCL and SDA pins and to create START and STOP conditions on the I2C-compatible bus. • In the middle layer, the SendByte() and GetByte() functions sequence the transmission and reception of bytes including handling the ACK bit. • In the top layer, the WriteQtI2c() and ReadQtI2c() functions can be called by an application to transfer one or more bytes to and from the QT2160. Note that for the sake of clarity, the driver is simplified (for example, it includes no timeouts). The following sections describe the driver and should be read in conjunction with the code listing. 5.3 Macros Table 5-1 describes the various code macros used to drive the SCL and SDA pins. Any macro that changes the state of a pin includes a fixed 5 µs delay following the edge. The 5 µs delay ensures the 100 kHz I2C-compatible specification is met and results in a bus clock rate of around 66 kHz. Table 5-1. Macro I2cDelay Code Macros for the I2C-compatible Bus Functions Description Generates a program-delay to guarantee I2C-compatible timing requirements. The constant BUS_DELAY should be adjusted to produce a 5 µs delay. Floats the SCL pin. SCL is then pulled high by the pull-up resistor. Note: the QT2160 may extend the low clock-phase. This means that after floating the pin, the driver waits until the high state is achieved before continuing. Drives the SCL pin low. There is no need to wait for the pin to achieve the low state. Floats the SDA pin. SDA is then pulled high by the pull-up resistor. After floating the pin, the driver waits until the high state is achieved before continuing. SetHiSCL SetLoSCL SetHiSDA 5 10702A–AT42–07/08 Table 5-1. Macro SetLoSDA FloatSDA SendSTART SendSTOP SendCLOCK Code Macros for the I2C-compatible Bus Functions (Continued) Description Drives the SDA pin low. There is no need to wait for the pin to achieve the low state. This simply floats the SDA pin so that the QT2160 can drive SDA during data-reads or ACK cycles. Outputs the START condition Outputs the STOP condition Pulses the SCL pin high-low 5.4 SendByte() The SendByte() function transmits a single byte onto the I 2 C-compatible bus. The byte is supplied as an input parameter. SendByte() returns I2C_OK if the byte is acknowledged or I2C_FAIL if a NACK is returned. 5.5 GetByte() The GetByte() function receives a single byte and terminates it with either ACK or NACK. NACK should be specified for the last byte of an I2C-compatible read transfer. GetByte() returns the received byte. 5.6 WriteQtI2c() The flowchart for the WriteQtI2c() function is shown in Figure 5-1. Figure 5-1. Flowchart for the WriteQtI2c() Function WriteQtI2c() Send I2C START Send (Slave-address + WRITE) Result? OK Send (Write-Address) Fail Send I2C STOP Result? OK Length = 0? No Fail Yes Decrement Length Send (Next Data byte) Send I2C STOP Return Result 6 Driving the AT42QT2160 10702A–AT42–07/08 Driving the AT42QT2160 The WriteQtI2c() function writes one or more bytes to the QT2160 as specified in the datasheet. It accepts four input parameters: • SlaveAddress: The QT2160 I2C-compatible address as selected at pins I2CA1 and I2CA0. • WriteAddress: The address of the first QT2160 register to be written. • WriteLength: The number of bytes to be written. • WritePtr: A pointer to the first byte to be written. The pointer should point to a byte array. The function returns I2C_OK if the entire write transfer is acknowledged (ACK), or I2C_FAIL if a NACK is returned. 5.7 ReadQtI2c() The flowchart for ReadQtI2c() is shown in Figure 5-2. Figure 5-2. Flowchart for the ReadQtI2c() Function ReadQtI2c() Write Address-pointer using WriteQtI2c() Fail Result? OK Send I2C START Send (Slave-address + READ) Result? OK Read (Next Data byte) Decrement Length No Fail Send I2C STOP Length = 0? Yes Send I2C STOP Return Result The ReadQtI2c() function reads one or more bytes to the QT2160 as specified in the datasheet. It accepts four input parameters: • SlaveAddress: The QT2160 I2C-compatible address as selected at pins I2CA1 and I2CA0. • ReadAddress: The address of the first QT2160 register to be read. • ReadLength: The number of bytes to be read. • ReadPtr: A pointer to a byte-array where the read data should be saved. The function returns I2C_OK if the entire read transfer is completed correctly. Note that the WriteQtI2c() function is called to set the address-pointer within the QT2160. 7 10702A–AT42–07/08 6. Source Code /*===================================================================================== Project: AT42QT2160-MMU Example Code =====================================================================================*/ typedef unsigned char uint8_t; enum { /* QT2160 registers */ QT_CODE_VERSION, QT_SLIDER_POSITION, QT_LP_MODE, QT_DI_LIMIT, QT_SLIDER_OPTIONS, QT_GPO_DRIVE = 70, QT_GPIO_PWM, QT_COMM_CHG_KEYS_2 QT_GENERAL_STATUS, QT_GPIO_READ, QT_NEG_RECAL_DELAY, QT_KEY_CONTROL, QT_GPIO_DRIVE, QT_PWM_LEVEL, QT_KEY_STATUS_1, QT_CALIBRATE = 10, QT_DRIFT_HOLD_TIME, QT_KEY_NTHR = 38, QT_GPIO_DIR = 73, QT_GPIO_WAKE, QT_CHIP_ID = 0, QT_KEY_STATUS_2, QT_RESET, QT_POS_DRIFT_COMP, QT_SLIDER_CONTROL, QT_KEY_BL = 54, QT_GPO_PWM, QT_COMM_CHG_KEYS_1, }; #define QT2160_ID #define QT_STATUS_SDET #define QT_STATUS_RES #define QT_GPIO_1 #define QT_GPIO_2 #define QT_GPIO_3 #define SLIDER_8BIT_RES QT_AWAKE_TIMEOUT = 14, QT_NEG_DRIFT_COMP, 0x11 0x01 0x80 0x04 0x08 0x10 0x00 0 /* Chip ID (read from device-address 0) */ /* bitmask for Slider-detect bit at device-address 2 */ /* bitmask for Reset bit at device-address 2 */ /* bitmask for GPIO1 */ /* bitmask for GPIO2 */ /* bitmask for GPIO3 */ /* 8-bit resolution (written to device-address 21) */ /* asserted state for /CHANGE pin */ /* I2C address used by demo. I2CA1 = I2CA0 = 0 */ #define CHANGE_ASSERTED_LOW #define QT2160_I2C_ADDRESS uint8_t QtStatus[5]; uint8_t QtData; 0x0d /* application storage for QT2160 device-status */ /* data buffer used for single-byte read and write transfers */ /* Port-assignment for QT2160-interface pins */ sbit SCL sbit SDA = P0 ^ 7; = P0 ^ 6; sbit CHANGE_PIN = P0 ^ 3; /* I2C driver – Function prototypes */ uint8_t WriteQtI2c ( uint8_t SlaveAddress, uint8_t WriteAddress, uint8_t WriteLength, uint8_t *WritePtr ); uint8_t ReadQtI2c ( uint8_t SlaveAddress, uint8_t ReadAddress, uint8_t ReadLength, uint8_t *ReadPtr ); 8 Driving the AT42QT2160 10702A–AT42–07/08 Driving the AT42QT2160 /*===================================================================================== Function: void main ( void ) { /*------------------------------- Initialisation -------------------------------------*/ /* (1) Establish communication with the QT2160 touch-sensor */ /* wait for successful transfer at the QT2160's address - Read Chip-ID */ while ( !ReadQtI2c (QT2160_I2C_ADDRESS, QT_CHIP_ID, 1, &QtData) ); /* (2) Check that the responding device is a QT2160! */ while ( QtData != QT2160_ID ); /*------------------------ End of Initialisation -------------------------------------*/ /*------------------------ Main Loop -------------------------------------------------*/ while (1) { if ( CHANGE_PIN == CHANGE_ASSERTED_LOW ) { /* If /CHANGE is asserted, read all status-bytes */ ReadQtI2c ( QT2160_I2C_ADDRESS, QT_GENERAL_STATUS, 5, QtStatus ); /* reading these registers will restore /CHANGE pin to the inactive (hi) state */ /* Has device just reset? */ if ( QtStatus[0] & QT_STATUS_RES ) { /* After any reset, configure device for demo requirements: The demo uses power-up default values for all touch-related registers. All keys are enabled by default and the slider is set to use 5-keys. QtData = QT_GPIO_3; /* Set GPIO3 for output mode */ WriteQtI2c ( QT2160_I2C_ADDRESS, QT_GPIO_DIR, 1, &QtData ); /* Configure GPIO3 for PWM mode */ WriteQtI2c ( QT2160_I2C_ADDRESS, QT_GPIO_PWM, 1, &QtData ); /* Configure Slider for 8-bit resolution */ QtData = SLIDER_8BIT_RES; WriteQtI2c ( QT2160_I2C_ADDRESS, QT_SLIDER_OPTIONS, 1, &QtData ); /* Send calibrate command */ QtData = 1; WriteQtI2c ( QT2160_I2C_ADDRESS, QT_CALIBRATE, 1, &QtData ); } */ /* test /CHANGE pin */ main() =====================================================================================*/ 9 10702A–AT42–07/08 else { /* write key-states back to the QT2160 LEDs */ WriteQtI2c ( QT2160_I2C_ADDRESS, QT_GPO_DRIVE, 1, &QtStatus[2] ); /* Update the PWM level if the slider is touched */ if (QtStatus[0] & QT_STATUS_SDET) WriteQtI2c ( QT2160_I2C_ADDRESS, QT_PWM_LEVEL, 1, &QtStatus[3] ); } /* End if ( QtStatus[0] & QT_STATUS_RES ) */ } /* End if ( CHANGE_PIN.. ) */ } /* End while (1) */ /*------------------------ End of Main Loop -----------------------------------------*/ } /*------------------------------ I2C Driver -----------------------------------------*/ /* The following code is the I2C driver */ /* ------------ I2C Driver Defines --------------- */ #define ACK #define NACK #define I2C_OK #define I2C_FAIL 0 1 1 0 #define READ_FLAG 0x01 #define BUS_DELAY 23 /*------------ I2C Code Macros ------------------- */ #define I2cDelay #define SetHiSCL #define SetLoSCL #define SetHiSDA #define SetLoSDA #define FloatSDA #define SendSTOP for (i = 0; i < BUS_DELAY; i++) {SCL = 1; while (!SCL); I2cDelay;} {SCL = 0; I2cDelay;} {SDA = 1; while (!SDA); I2cDelay;} {SDA = 0; I2cDelay;} {SDA = 1;} {SetLoSDA; SetHiSCL; SetHiSDA;} #define SendSTART {SetLoSDA; SetLoSCL;} #define SendCLOCK {SetHiSCL; SetLoSCL;} /*----------------------------------------------- */ 10 Driving the AT42QT2160 10702A–AT42–07/08 Driving the AT42QT2160 /*===================================================================================== Function: Input: Output: SendByte() Byte to send I2C_OK if device ACKs, I2C_FAIL if device NACKs ====================================================================================*/ uint8_t SendByte ( uint8_t TxByte ) { uint8_t i,b, Result = I2C_OK; for (b = 0; b < 8; b++) { if ( TxByte & 0x80 ) SetHiSDA else SetLoSDA SendCLOCK TxByte
AT42QTAN0040 价格&库存

很抱歉,暂时无法提供与“AT42QTAN0040”相匹配的价格&库存,您可以联系我们找货

免费人工找货