AN3301 Software
Doc ID 18161 Rev 1 19/35
2.6 Programming guide
In this section, the reference software programming code is shared based on the general
application of the STMPE812A. This includes the initialization and interrupt handling
routines for touchscreen controller (in all 3 acquisition modes and both normal and pen
down interrupt modes), GPIO, ADC and PWM.
The reference code is supplemented by the STMPE812A software library.
2.6.1 Touchscreen initialization and interrupt handling (pen down mode)
The following is an example for touchscreen initialization, interrupt servicing routine in pen
down mode, whereby all other interrupt sources (eg. GPIO, PWM and ADC) are not used in
the application.
A: Initialization
void STMPE812A_Initialization (void) {
/*Enabling the Soft Reset in the System Control Register (0x03)*/
STMPE812A_SoftReset ();
/*Enabling the ADC and TSC Clocks in the System Control Register (0x03)*/
STMPE812A_SetClock (STMPE812A_ENABLE, STMPE812A_ADC_CLK|STMPE812A_TSC_CLK);
/*Setting the Port - 2 as a special function in the Port Function Control Register
(0x04)*/
STMPE812A_SetPortFunction (STMPE812A_PORT2, STMPE812A_PORT_FUNC_SPECIAL_FUNC);
/*Setting the MAV_MODE [2:0] as ‘20 remove 4’, Precharge [1:0] as ‘no pre-charge’ and
Current _Limit [1:0] as ‘20mA’ in the TSC Control Register (0x40) respectively*/
STMPE812A_SetTSCController (STMPE812A_TSC_MAV_20_4, STMPE812A_TSC_NO_PRE,
STMPE812A_TSC_Current20M);
/*Setting the PenStrength [1:0] as ‘Most Sensitive (20K Pull-Up)’, TDetDly [2:0] as
‘40us’ and Settling [2:0] as ‘40us’ in the TSC Detection Configuration 1 Register
(0x41) respectively*/
STMPE812A_ConfigureTSC (STMPE812A_TSC_MOST_SEN, STMPE812A_TSC_TDD40,
STMPE812A_TSC_PDST40);
/*Setting the TSC operating mode OpMode [4] as ’12-bit X, 12-bit Y, 8-bit Z
acquisition’ in the TSC Detection Configuration 2 Register (0x42)*/
STMPE812A_SetTSCOpMode (STMPE812A_DISABLE);
/*Enabling the StatusRead [5] in the TSC Detection Configuration 2 Register (0x42)*/
STMPE812A_SetTSCStatusRead (STMPE812A_ENABLE);
/*Setting the Acq_Mode [7:6] as ‘Data acquisition timed by internal timer’ in the TSC
Detection Configuration 2 Register (0x42)*/
STMPE812A_SetTSCAcquisitionMode (STMPE812A_TSC_INTERNAL_TIMER);
/*Setting the Acq_Mode [7:6] as ‘Data acquisition triggered by a write to “ACQ” bit’
in the TSC Detection Configuration 2 Register (0x42)*/
//STMPE812A_SetTSCAcquisitionMode (STMPE812A_TSC_TRI_ACQ);
/*Setting the Acq_Mode [7:6] as ‘Data acquisition using Host-Controlled Sampling Rate
Control’ in the TSC Detection Configuration 2 Register (0x42)*/
//STMPE812A_SetTSCAcquisitionMode (STMPE812A_TSC_HC_SRC);
/*Setting the TSC sampling rate value of ‘0x0A’ in the TSC Sampling Rate Register
(0x43)*/
STMPE812A_SetTSCSamplingRate (0x0A);