[STM32L5] 【STM32L562 DK试用】驱动触摸屏

[复制链接]
 楼主| lulugl 发表于 2025-5-17 08:47 | 显示全部楼层 |阅读模式
开发板上的LCD集成了触摸屏,型号为FT6x06的电容触摸屏。连接到开发板上的I2C1
205166827d55f8d4e0.png
中断引脚连接到了PF1上:
717136827d5b7daed6.png
【移植步骤】
1、在stm32cubemx中使能i2c1,但是不生成代码:
550896827db05661a2.png
774236827daf94a9e4.png
然后重新生成代码
2、把bsp下的ft6x06添加到工程中,加入头文件的路径。
3、bsp下面的stm32l562e_discovery_ts.c加入工程中。
834396827db71d69c7.png
4、同时创建ftfx06_conf.h配置文件:
  1. /* Define to prevent recursive inclusion -------------------------------------*/
  2. #ifndef FT6X06_CONF_H
  3. #define FT6X06_CONF_H

  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif

  7. /* Includes ------------------------------------------------------------------*/
  8. /* Macros --------------------------------------------------------------------*/
  9. /* Exported types ------------------------------------------------------------*/
  10. /* Exported constants --------------------------------------------------------*/
  11. #define FT6X06_AUTO_CALIBRATION_ENABLED      0U
  12. #define FT6X06_MAX_X_LENGTH                  240U
  13. #define FT6X06_MAX_Y_LENGTH                  240U
  14.   
  15. #ifdef __cplusplus
  16. }
  17. #endif
  18. #endif /* FT6X06_CONF_H */
5、在main.c中添加触摸初始化如下:

  1.             TS_Init_t TsInit;

  2.     /* Initialize the TouchScreen */
  3.     TsInit.Width       = 240;
  4.     TsInit.Height      = 240;
  5.     TsInit.Orientation = TS_ORIENTATION_PORTRAIT;
  6.     TsInit.Accuracy    = 10;
  7.     if (BSP_TS_Init(0, &TsInit) != BSP_ERROR_NONE)
  8.     {
  9.       Error_Handler();
  10.     }

  11.     /* Configure TS interrupt */
  12.     if (BSP_TS_EnableIT(0) != BSP_ERROR_NONE)
  13.     {
  14.       Error_Handler();
  15.     }
  16.                        
6、添加测试代码如下:
  1.     /* USER CODE BEGIN 3 */
  2.                 if (BSP_TS_Get_MultiTouchState(0, &TsMultipleState) != BSP_ERROR_NONE)
  3.       {
  4.         Error_Handler();
  5.       }
  6.                        
  7.                 if(TsMultipleState.TouchDetected >= 1)
  8.       {
  9.         /* Get X and Y position of the touch post calibrated */
  10.         x = TsMultipleState.TouchX[0];
  11.         y = TsMultipleState.TouchY[0];
  12.                                 printf("touch:x:%d, y%d\r\n",x,y);

  13.                         }

  14.   }
  15.   /* USER CODE END 3 */
在这段代码中,首先判断是否有触摸中断产生,如果有通过串口输出触摸的坐标。
【测试效果】
通过串口查看,触摸屏后,串口打印数据如下:
610536827dc852066b.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

180

主题

830

帖子

12

粉丝
快速回复 在线客服 返回列表 返回顶部