查看: 121|回复: 0

[原创] 【MCX-N947分享】--6. TSI触摸按键

[复制链接]
  • TA的每日心情
    奋斗
    3 天前
  • 签到天数: 8 天

    [LV.3]偶尔看看II

    6

    主题

    19

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    226
    最后登录
    2024-4-29
    发表于 2024-4-10 23:13:48 | 显示全部楼层 |阅读模式
    本帖最后由 dirty123 于 2024-4-10 23:27 编辑

          MCX-N947-BRK 板载触摸按键,其基于电容检测原理。主控MCU集成有25通道触摸功能,配合自校准计算法,可实现触摸按键功能,下面予以实现。

    一.硬件原理
          开发板触摸引脚为P0_21(A8)引脚,触摸通道为CH16,硬件原理如下:
    1_触摸原理图_a.jpg 1_触摸原理图_b.jpg
    图1:触摸硬件原理

    二.代码准备
          这里以MCUXpresso IDE 获取SDK下mcxn9xxevk_tsi_v6_self_cap工程为基础,做详细的代码更改与理解实现。
    由于SDK开发板与MCX-N947-BRK 原理有所不同,这里做相应的适配修改
    1.修改配置,主要涉及到两处。一个是BOARD_InitPins引脚初始化修改,使用P0_21作为ch16通道触摸配置,代码如下:
    1. void BOARD_InitPins(void)
    2. {
    3.     /* Enables the clock for GPIO3: Enables clock */
    4.     CLOCK_EnableClock(kCLOCK_Gpio3);
    5.     /* Enables the clock for PORT0: Enables clock */
    6.     CLOCK_EnableClock(kCLOCK_Port0);
    7.     /* Enables the clock for PORT1: Enables clock */
    8.     CLOCK_EnableClock(kCLOCK_Port1);
    9.     /* Enables the clock for PORT3: Enables clock */
    10.     CLOCK_EnableClock(kCLOCK_Port3);

    11.     gpio_pin_config_t gpio3_pinD16_config = {
    12.         .pinDirection = kGPIO_DigitalOutput,
    13.         .outputLogic = 0U
    14.     };
    15.     /* Initialize GPIO functionality on pin PIO3_3 (pin D16)  */
    16.     GPIO_PinInit(GPIO3, 3U, &gpio3_pinD16_config);

    17.     gpio_pin_config_t gpio3_pinF14_config = {
    18.         .pinDirection = kGPIO_DigitalOutput,
    19.         .outputLogic = 0U
    20.     };
    21.     /* Initialize GPIO functionality on pin PIO3_4 (pin F14)  */
    22.     GPIO_PinInit(GPIO3, 4U, &gpio3_pinF14_config);
    23. #if 0
    24.     const port_pin_config_t port1_10_pinC3_config = {/* Internal pull-up/down resistor is disabled */
    25.                                                      kPORT_PullDisable,
    26.                                                      /* Low internal pull resistor value is selected. */
    27.                                                      kPORT_LowPullResistor,
    28.                                                      /* Fast slew rate is configured */
    29.                                                      kPORT_FastSlewRate,
    30.                                                      /* Passive input filter is disabled */
    31.                                                      kPORT_PassiveFilterDisable,
    32.                                                      /* Open drain output is disabled */
    33.                                                      kPORT_OpenDrainDisable,
    34.                                                      /* Low drive strength is configured */
    35.                                                      kPORT_LowDriveStrength,
    36.                                                      /* Pin is configured as TSI0_CH19 */
    37.                                                      kPORT_MuxAlt0,
    38.                                                      /* Digital input disabled; it is required for analog functions */
    39.                                                      kPORT_InputBufferDisable,
    40.                                                      /* Digital input is not inverted */
    41.                                                      kPORT_InputNormal,
    42.                                                      /* Pin Control Register fields [15:0] are not locked */
    43.                                                      kPORT_UnlockRegister};
    44.     /* PORT1_10 (pin C3) is configured as TSI0_CH19 */
    45.     PORT_SetPinConfig(PORT1, 10U, &port1_10_pinC3_config);
    46. #else
    47.     const port_pin_config_t port0_21_pinA8_config = {/* Internal pull-up/down resistor is disabled */
    48.                                                      kPORT_PullDisable,
    49.                                                      /* Low internal pull resistor value is selected. */
    50.                                                      kPORT_LowPullResistor,
    51.                                                      /* Fast slew rate is configured */
    52.                                                      kPORT_FastSlewRate,
    53.                                                      /* Passive input filter is disabled */
    54.                                                      kPORT_PassiveFilterDisable,
    55.                                                      /* Open drain output is disabled */
    56.                                                      kPORT_OpenDrainDisable,
    57.                                                      /* Low drive strength is configured */
    58.                                                      kPORT_LowDriveStrength,
    59.                                                      /* Pin is configured as TSI0_CH19 */
    60.                                                      kPORT_MuxAlt0,
    61.                                                      /* Digital input disabled; it is required for analog functions */
    62.                                                      kPORT_InputBufferDisable,
    63.                                                      /* Digital input is not inverted */
    64.                                                      kPORT_InputNormal,
    65.                                                      /* Pin Control Register fields [15:0] are not locked */
    66.                                                      kPORT_UnlockRegister};
    67.     /* PORT0_21 (pin A8) is configured as TSI0_CH16 */
    68.     PORT_SetPinConfig(PORT0, 21U, &port0_21_pinA8_config);
    69. #endif

    70. /*

    71. */
    72. }
    复制代码

    2.将在board.h里将BOARD_TSI_ELECTRODE_1触摸通道改成16
    1. /*! @brief Indexes of the TSI channels for on-board electrodes */
    2. #ifndef BOARD_TSI_ELECTRODE_1
    3. #define BOARD_TSI_ELECTRODE_1 16//19
    4. #endif
    5. #ifndef BOARD_TSI_ELECTRODE_2
    6. #define BOARD_TSI_ELECTRODE_2 21U
    7. #endif
    复制代码
    3.了解触摸功能。触摸功能主要在tsi_v6_self_cap.c和fsl_tsi_v6.c源文件实现。
    TSI_GetSelfCapModeDefaultConfig(&tsiConfig_selfCap);//TSI 默认硬件配置
    TSI_InitSelfCapMode(APP_TSI, &tsiConfig_selfCap);   //TSI初始化
    TSI_SelfCapCalibrate(APP_TSI, &buffer);             //TSI自校准。上电校准手指不要放在触摸按键上
    ----------------------------------------------------------------------------------------------------------------------------
    /*下面是触发、中断使能、通道配置、清状态等*/
    TSI_EnableInterrupts(APP_TSI, kTSI_GlobalInterruptEnable);
    TSI_EnableInterrupts(APP_TSI, kTSI_EndOfScanInterruptEnable);
    TSI_ClearStatusFlags(APP_TSI, kTSI_EndOfScanFlag);
    TSI_SetSelfCapMeasuredChannel(APP_TSI, BOARD_TSI_ELECTRODE_1);
    工程用了两个方式演示,先轮询方式,后中断方式覆盖
    ----------------------------------------------------------------------------------------------------------------------------
    TSI中断函数如下,我们可在此做触摸按键应用,这里加了下触摸按键触发日志。

    1. void TSI0_IRQHandler(void)
    2. {
    3. #if BOARD_TSI_ELECTRODE_1 > 15
    4.     /* errata ERR051410: When reading TSI_COMFIG[TSICH] bitfield, the upper most bit will always be 0. */
    5.     if ((TSI_GetSelfCapMeasuredChannel(APP_TSI) | 0x10U) == BOARD_TSI_ELECTRODE_1)
    6. #else
    7.     if (TSI_GetSelfCapMeasuredChannel(APP_TSI) == BOARD_TSI_ELECTRODE_1)
    8. #endif
    9.     {
    10.         if (TSI_GetCounter(APP_TSI) > (uint16_t)(buffer.calibratedData[BOARD_TSI_ELECTRODE_1] + TOUCH_DELTA_VALUE))
    11.         {
    12.             LED1_TOGGLE(); /* Toggle the touch event indicating LED */
    13.             s_tsiInProgress = false;
    14.             PRINTF("\r\nTouched\r\n");
    15.         }
    16.     }

    17.     /* Clear endOfScan flag */
    18.     TSI_ClearStatusFlags(APP_TSI, kTSI_EndOfScanFlag);
    19.     SDK_ISR_EXIT_BARRIER;
    20. }
    复制代码
         自此代码准备完毕。

    三.编译烧录与测试
    1.编译烧录
    2_工程编译烧录.jpg
    图2:触摸按键工程及编译


    2.测试
          复位,可看到初始化及校准,按键触摸日志如下,触摸灯色也随之改变。
    3_触摸按键日志.jpg

    图3:触摸按键日志


          至此,实现了开发板按键触摸功能。
    该会员没有填写今日想说内容.
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-30 01:33 , Processed in 0.108667 second(s), 19 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表