Library 10- STMPE811 Touch screen driver for STM32F429 Discovery board
STM32F429 Discovery board has LCD with ILI9341 controller and resistive touch screen with STMPE811 controller from STMicroelectonics.
The STMPE811 is a 4-wire resistive touch screen controller with a GPIO (general purpose input/output) port expander able to interface a microcontroller or a main digital ASIC via I2C or SPI serial interface. The STMPE811 offers great flexibility, as each I/O can be configured as input, output or specific functions. The touch screen controller is enhanced with a movement tracking algorithm to avoid excessive data, 128 x 32 bit buffer and a programmable active window feature.
We will communicate with STMPE811 via I2C at Fast Mode of 100kbit/s. On Discovery board, driver is connected to pins:
function | pin | I2Cx | PInspack |
---|---|---|---|
SCL | PA8 | I2C3 | 1 |
SDA | PC9 | I2C3 | 1 |
STMPE811 support High speed I2C mode, so this give us clock frequency up to 400kb/s.
Library dependencies
To run STMPE811 Library, you need some other libraries:
- TM DELAY
- TM I2C
Initialization
First you have to initialize STMPE811. You can do this with
1 2 |
//Initialize Touch TM_STMPE811_Init(); |
Then, make an instance of touch data structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Struct, which is passed into TM_STMPE811_ReadTouch function * * Parameters: * - uint16_t x: x coordinate for lcd * - uint16_t y: y coordinate for lcd * - TM_STMPE811_State_t pressed: * - TM_STMPE811_State_Pressed when pressed * - TM_STMPE811_State_Released when released * - TM_STMPE811_Orientation_t orientation: Set touch screen orientation to read */ typedef struct { uint16_t x; uint16_t y; TM_STMPE811_State_t pressed; TM_STMPE811_Orientation_t orientation; } TM_STMPE811_TouchData; |
1 2 |
//Instance in main TM_STMPE811_TouchData touchData; |
and pass your touch screen orientation:
1 2 |
//Select touch screen orientation touchData.orientation = TM_STMPE811_Orientation_Portrait_2; |
That is used to synchronize X and Y coordinates with ILI9341 LCD coordinates. Other possible configurations are:
1 2 3 4 5 6 7 8 9 |
/** * Enums for set how to read x and y from controller */ typedef enum { TM_STMPE811_Orientation_Portrait_1, TM_STMPE811_Orientation_Portrait_2, TM_STMPE811_Orientation_Landscape_1, TM_STMPE811_Orientation_Landscape_2, } TM_STMPE811_Orientation_t; |
Read XY coordinates
You can read coordinates with
1 |
TM_STMPE811_ReadTouch(&touchData); |
First you have to read, if touch has been pressed. Do this with
1 2 3 |
if (touchData.pressed == TM_STMPE811_State_Pressed) { //Touch valid } |
or
1 2 3 |
if (TM_STMPE811_ReadTouch(&touchData) == TM_STMPE811_State_Pressed) { //Touch valid } |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
/** * Keil project for STMPE811 touch driver controller * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://majerle.eu * @ide Keil uVision 5 */ #include "stm32f4xx.h" #include "defines.h" #include "tm_stm32f4_ili9341.h" #include "tm_stm32f4_stmpe811.h" #include <stdio.h> int main(void) { char str[30]; //Create TouchData struct TM_STMPE811_TouchData touchData; //Initialize system SystemInit(); //Initialize LCD TM_ILI9341_Init(); //Fill with orange color TM_ILI9341_Fill(ILI9341_COLOR_ORANGE); //Rotate LCD TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_2); //Initialize Touch if (TM_STMPE811_Init() != TM_STMPE811_State_Ok) { TM_ILI9341_Puts(20, 20, "STMPE811 Error", &TM_Font_11x18, ILI9341_COLOR_ORANGE, ILI9341_COLOR_BLACK); while (1); } //Select touch screen orientation touchData.orientation = TM_STMPE811_Orientation_Portrait_2; //Print some text TM_ILI9341_Puts(20, 20, "Press on LCD", &TM_Font_11x18, ILI9341_COLOR_ORANGE, ILI9341_COLOR_BLACK); TM_ILI9341_Puts(93, 310, "stm32f4-discovery.net", &TM_Font_7x10, ILI9341_COLOR_GREEN, ILI9341_COLOR_BLACK); while (1) { if (TM_STMPE811_ReadTouch(&touchData) == TM_STMPE811_State_Pressed) { //Touch valid sprintf(str, "Pressed \n\nX: %03d\nY: %03d", touchData.x, touchData.y); TM_ILI9341_Puts(20, 80, str, &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); TM_ILI9341_DrawPixel(touchData.x, touchData.y, 0x0000); } else { sprintf(str, "Not Pressed\n\n \n "); TM_ILI9341_Puts(20, 80, str, &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); } } } |
Project available on Github, download library below.
STMPE811 Touch screen library for STM32F429 Discovery
Recent comments