在线时间23 小时
UID3370139
注册时间2024-2-29
NXP金币828
TA的每日心情 | 奋斗 3 天前 |
---|
签到天数: 8 天 [LV.3]偶尔看看II
中级会员
- 积分
- 226
- 最后登录
- 2024-4-29
|
本帖最后由 dirty123 于 2024-4-10 23:27 编辑
MCX-N947-BRK 板载触摸按键,其基于电容检测原理。主控MCU集成有25通道触摸功能,配合自校准计算法,可实现触摸按键功能,下面予以实现。
一.硬件原理
开发板触摸引脚为P0_21(A8)引脚,触摸通道为CH16,硬件原理如下:
图1:触摸硬件原理
二.代码准备
这里以MCUXpresso IDE 获取SDK下mcxn9xxevk_tsi_v6_self_cap工程为基础,做详细的代码更改与理解实现。
由于SDK开发板与MCX-N947-BRK 原理有所不同,这里做相应的适配修改
1.修改配置,主要涉及到两处。一个是BOARD_InitPins引脚初始化修改,使用P0_21作为ch16通道触摸配置,代码如下:- void BOARD_InitPins(void)
- {
- /* Enables the clock for GPIO3: Enables clock */
- CLOCK_EnableClock(kCLOCK_Gpio3);
- /* Enables the clock for PORT0: Enables clock */
- CLOCK_EnableClock(kCLOCK_Port0);
- /* Enables the clock for PORT1: Enables clock */
- CLOCK_EnableClock(kCLOCK_Port1);
- /* Enables the clock for PORT3: Enables clock */
- CLOCK_EnableClock(kCLOCK_Port3);
- gpio_pin_config_t gpio3_pinD16_config = {
- .pinDirection = kGPIO_DigitalOutput,
- .outputLogic = 0U
- };
- /* Initialize GPIO functionality on pin PIO3_3 (pin D16) */
- GPIO_PinInit(GPIO3, 3U, &gpio3_pinD16_config);
- gpio_pin_config_t gpio3_pinF14_config = {
- .pinDirection = kGPIO_DigitalOutput,
- .outputLogic = 0U
- };
- /* Initialize GPIO functionality on pin PIO3_4 (pin F14) */
- GPIO_PinInit(GPIO3, 4U, &gpio3_pinF14_config);
- #if 0
- const port_pin_config_t port1_10_pinC3_config = {/* Internal pull-up/down resistor is disabled */
- kPORT_PullDisable,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as TSI0_CH19 */
- kPORT_MuxAlt0,
- /* Digital input disabled; it is required for analog functions */
- kPORT_InputBufferDisable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT1_10 (pin C3) is configured as TSI0_CH19 */
- PORT_SetPinConfig(PORT1, 10U, &port1_10_pinC3_config);
- #else
- const port_pin_config_t port0_21_pinA8_config = {/* Internal pull-up/down resistor is disabled */
- kPORT_PullDisable,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as TSI0_CH19 */
- kPORT_MuxAlt0,
- /* Digital input disabled; it is required for analog functions */
- kPORT_InputBufferDisable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT0_21 (pin A8) is configured as TSI0_CH16 */
- PORT_SetPinConfig(PORT0, 21U, &port0_21_pinA8_config);
- #endif
- /*
- 略
- */
- }
复制代码
2.将在board.h里将BOARD_TSI_ELECTRODE_1触摸通道改成16
- /*! @brief Indexes of the TSI channels for on-board electrodes */
- #ifndef BOARD_TSI_ELECTRODE_1
- #define BOARD_TSI_ELECTRODE_1 16//19
- #endif
- #ifndef BOARD_TSI_ELECTRODE_2
- #define BOARD_TSI_ELECTRODE_2 21U
- #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中断函数如下,我们可在此做触摸按键应用,这里加了下触摸按键触发日志。
- void TSI0_IRQHandler(void)
- {
- #if BOARD_TSI_ELECTRODE_1 > 15
- /* errata ERR051410: When reading TSI_COMFIG[TSICH] bitfield, the upper most bit will always be 0. */
- if ((TSI_GetSelfCapMeasuredChannel(APP_TSI) | 0x10U) == BOARD_TSI_ELECTRODE_1)
- #else
- if (TSI_GetSelfCapMeasuredChannel(APP_TSI) == BOARD_TSI_ELECTRODE_1)
- #endif
- {
- if (TSI_GetCounter(APP_TSI) > (uint16_t)(buffer.calibratedData[BOARD_TSI_ELECTRODE_1] + TOUCH_DELTA_VALUE))
- {
- LED1_TOGGLE(); /* Toggle the touch event indicating LED */
- s_tsiInProgress = false;
- PRINTF("\r\nTouched\r\n");
- }
- }
- /* Clear endOfScan flag */
- TSI_ClearStatusFlags(APP_TSI, kTSI_EndOfScanFlag);
- SDK_ISR_EXIT_BARRIER;
- }
复制代码 自此代码准备完毕。
三.编译烧录与测试
1.编译烧录
图2:触摸按键工程及编译
2.测试
复位,可看到初始化及校准,按键触摸日志如下,触摸灯色也随之改变。
图3:触摸按键日志
至此,实现了开发板按键触摸功能。
|
|