在MCXN947开发板上配有一个触摸键,其电路如图1所示。
图1 触摸电路
相应的可以在网上查找到它的示例程序,其主程序的内容如下:
int main(void)
{
volatile uint32_t i = 0;
tsi_selfCap_config_t tsiConfig_selfCap;
lptmr_config_t lptmrConfig;
memset((void *)&lptmrConfig, 0, sizeof(lptmrConfig));
CLOCK_EnableClock(kCLOCK_InputMux0);
CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
CLOCK_SetupClk16KClocking(kCLOCK_Clk16KToVsys);
CLOCK_SetClkDiv(kCLOCK_DivTsiClk, 1u);
CLOCK_AttachClk(kCLK_IN_to_TSI);
BOARD_InitPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
LED1_INIT();
LED2_INIT();
LPTMR_GetDefaultConfig(&lptmrConfig);
TSI_GetSelfCapModeDefaultConfig(&tsiConfig_selfCap);
LPTMR_Init(LPTMR0, &lptmrConfig);
TSI_InitSelfCapMode(APP_TSI, &tsiConfig_selfCap);
TSI_EnableNoiseCancellation(APP_TSI, true);
LPTMR_SetTimerPeriod(LPTMR0, USEC_TO_COUNT(LPTMR_USEC_COUNT, LPTMR_SOURCE_CLOCK));
NVIC_EnableIRQ(TSI0_IRQn);
TSI_EnableModule(APP_TSI, true);
PRINTF("\r\nTSI_V6 Self-Cap mode Example Start!\r\n");
memset((void *)&buffer, 0, sizeof(buffer));
TSI_SelfCapCalibrate(APP_TSI, &buffer);
for (i = 0U; i < FSL_FEATURE_TSI_CHANNEL_COUNT; i++)
{
PRINTF("Calibrated counters for channel %d is: %d \r\n", i, buffer.calibratedData[i]);
}
PRINTF("\r\nNOW, comes to the software trigger scan using polling method!\r\n");
TSI_EnableHardwareTriggerScan(APP_TSI, false); /* Enable software trigger scan */
TSI_DisableInterrupts(APP_TSI, kTSI_EndOfScanInterruptEnable);
TSI_ClearStatusFlags(APP_TSI, kTSI_EndOfScanFlag);
TSI_SetSelfCapMeasuredChannel(APP_TSI, BOARD_TSI_ELECTRODE_1);
TSI_StartSoftwareTrigger(APP_TSI);
while (!(TSI_GetStatusFlags(APP_TSI) & kTSI_EndOfScanFlag))
{
}
PRINTF("Channel %d Normal mode counter is: %d \r\n", BOARD_TSI_ELECTRODE_1, TSI_GetCounter(APP_TSI));
#if (defined(PAD_TSI_ELECTRODE_2_ENABLED) && PAD_TSI_ELECTRODE_2_ENABLED)
TSI_ClearStatusFlags(APP_TSI, kTSI_EndOfScanFlag);
TSI_SetSelfCapMeasuredChannel(APP_TSI, BOARD_TSI_ELECTRODE_2);
TSI_StartSoftwareTrigger(APP_TSI);
while (!(TSI_GetStatusFlags(APP_TSI) & kTSI_EndOfScanFlag))
{
}
PRINTF("Channel %d Normal mode counter is: %d \r\n", BOARD_TSI_ELECTRODE_2, TSI_GetCounter(APP_TSI));
#endif
TSI_ClearStatusFlags(APP_TSI, kTSI_EndOfScanFlag | kTSI_OutOfRangeFlag);
PRINTF("\r\nNOW, comes to the software trigger scan using interrupt method!\r\n");
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);
while (s_tsiInProgress)
{
TSI_StartSoftwareTrigger(APP_TSI);
}
s_tsiInProgress = true;
PRINTF("Channel %d Normal mode counter is: %d \r\n", BOARD_TSI_ELECTRODE_1, TSI_GetCounter(APP_TSI));
#if (defined(PAD_TSI_ELECTRODE_2_ENABLED) && PAD_TSI_ELECTRODE_2_ENABLED)
TSI_SetSelfCapMeasuredChannel(APP_TSI, BOARD_TSI_ELECTRODE_2);
TSI_StartSoftwareTrigger(APP_TSI);
while (s_tsiInProgress)
{
TSI_StartSoftwareTrigger(APP_TSI);
}
PRINTF("Channel %d Normal mode counter is: %d \r\n", BOARD_TSI_ELECTRODE_2, TSI_GetCounter(APP_TSI));
#endif
PRINTF("\r\nNOW, comes to the hardware trigger scan method!\r\n");
PRINTF("After running, touch pad %s each time, you will see LED toggles.\r\n", PAD_TSI_ELECTRODE_1_NAME);
TSI_EnableModule(APP_TSI, false);
TSI_EnableHardwareTriggerScan(APP_TSI, true);
TSI_EnableInterrupts(APP_TSI, kTSI_EndOfScanInterruptEnable);
TSI_ClearStatusFlags(APP_TSI, kTSI_EndOfScanFlag);
TSI_SetSelfCapMeasuredChannel(APP_TSI, BOARD_TSI_ELECTRODE_1);
TSI_EnableModule(APP_TSI, true);
INPUTMUX_AttachSignal(INPUTMUX0, 0U, kINPUTMUX_Lptmr0ToTsiTrigger);
LPTMR_StartTimer(LPTMR0);
while (1)
{
}
}
相应的中断服务程序是:
void TSI0_IRQHandler(void)
{
#if BOARD_TSI_ELECTRODE_1 > 15
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();
s_tsiInProgress = false;
}
}
TSI_ClearStatusFlags(APP_TSI, kTSI_EndOfScanFlag);
SDK_ISR_EXIT_BARRIER;
}
由此可知,当触摸PAD键时,会启动中断服务,即蓝色LED的状态被翻转一次,见图2和图3所示。
在连接串口的情况下,会见到图4的输出内容。
图2 熄灭状态
图3 点亮状态
图4 串口输出信息
|