STM32L476 TOUCH 예제를 STM32L432KC에 적용해보았습니다.
사용해본 MCU 중에서 터치 제품은 CYPRESS 의 PSOC 시리즈가 제일 좋고 편하네요.
TSC_HandleTypeDef TscHandle;
TSC_IOConfigTypeDef IoConfig;
#define TSCx TSC
#define TSCx_CLK_ENABLE() __HAL_RCC_TSC_CLK_ENABLE()
#define TSCx_TS1_IO_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define TSCx_SHIELD_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define TSCx_TS1_CAPACITOR_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define TSCx_SHIELD_CAPACITOR_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define TSCx_FORCE_RESET() __HAL_RCC_TSC_FORCE_RESET()
#define TSCx_RELEASE_RESET() __HAL_RCC_TSC_RELEASE_RESET()
// IoConfig.ChannelIOs = TSC_GROUP2_IO3; // PB12
// IoConfig.SamplingIOs = (TSC_GROUP2_IO1 | TSC_GROUP2_IO2); //PB13, PC7
// IoConfig.ShieldIOs = TSC_GROUP2_IO4; // PC6
/* Definition for TSCx IO Pins */
#define TSCx_TS1_IO_PIN GPIO_PIN_6
#define TSCx_TS1_IO_GPIO_PORT GPIOB
#define TSCx_TS1_IO_AF GPIO_AF9_TSC
#define TSCx_SHIELD_IO_PIN GPIO_PIN_7
#define TSCx_SHIELD_IO_GPIO_PORT GPIOB
#define TSCx_SHIELD_IO_AF GPIO_AF9_TSC
/* Definition for TSCx Sampling Capacitor Pins */
#define TSCx_TS1_SAMPLING_PIN GPIO_PIN_4
#define TSCx_TS1_SAMPLING_GPIO_PORT GPIOB
#define TSCx_TS1_SAMPLING_AF GPIO_AF9_TSC
#define TSCx_SHIELD_SAMPLING_PIN GPIO_PIN_5
#define TSCx_SHIELD_SAMPLING_GPIO_PORT GPIOB
#define TSCx_SHIELD_SAMPLING_AF GPIO_AF9_TSC
/* Definition for TSCx's NVIC */
#define TSCx_EXTI_IRQn TSC_IRQn
#define TSCx_EXTI_IRQHandler TSC_IRQHandler
void HAL_TSC_MspInit(TSC_HandleTypeDef *htsc)
{
GPIO_InitTypeDef GPIO_InitStruct;
/*##-1- Enable TSC and GPIO clocks #########################################*/
TSCx_CLK_ENABLE();
TSCx_TS1_IO_GPIO_CLK_ENABLE();
TSCx_SHIELD_GPIO_CLK_ENABLE();
/*##-2- Configure Sampling Capacitor IOs (Alternate-Function Open-Drain) ###*/
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
/* Shield Sampling Capacitor IO = TSC_GROUP4_IO2 */
GPIO_InitStruct.Pin = TSCx_SHIELD_SAMPLING_PIN;
GPIO_InitStruct.Alternate = TSCx_SHIELD_SAMPLING_AF;
HAL_GPIO_Init(TSCx_SHIELD_SAMPLING_GPIO_PORT, &GPIO_InitStruct);
/* Channel TS1 Sampling Capacitor IO = TSC_GROUP1_IO2 */
GPIO_InitStruct.Pin = TSCx_TS1_SAMPLING_PIN;
GPIO_InitStruct.Alternate = TSCx_TS1_SAMPLING_AF;
HAL_GPIO_Init(TSCx_TS1_SAMPLING_GPIO_PORT, &GPIO_InitStruct);
/*##-3- Configure Channel & Shield IOs (Alternate-Function Output PP) ######*/
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
/* Shield IO = TSC_GROUP4_IO1 */
GPIO_InitStruct.Pin = TSCx_SHIELD_IO_PIN;
GPIO_InitStruct.Alternate = TSCx_SHIELD_IO_AF;
HAL_GPIO_Init(TSCx_SHIELD_IO_GPIO_PORT, &GPIO_InitStruct);
/* Channel IO (TS1) = TSC_GROUP1_IO1 */
GPIO_InitStruct.Pin = TSCx_TS1_IO_PIN;
GPIO_InitStruct.Alternate = TSCx_TS1_IO_AF;
HAL_GPIO_Init(TSCx_TS1_IO_GPIO_PORT, &GPIO_InitStruct);
/*##-4- Configure the NVIC for TSC #########################################*/
HAL_NVIC_SetPriority(TSCx_EXTI_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(TSCx_EXTI_IRQn);
}
/**
* @brief TSC MSP De-Initialization
* This function frees the hardware resources used in this example:
* - Disable the Peripheral's clock
* - Revert GPIO to their default state
* @param htsc: TSC handle pointer
* @retval None
*/
void HAL_TSC_MspDeInit(TSC_HandleTypeDef *htsc)
{
/*##-1- Reset peripherals ##################################################*/
TSCx_FORCE_RESET();
TSCx_RELEASE_RESET();
/*##-2- Disable peripherals and GPIO Clocks ################################*/
/* De-initialize the TSC GPIO pins */
HAL_GPIO_DeInit(TSCx_TS1_IO_GPIO_PORT, TSCx_TS1_IO_PIN);
HAL_GPIO_DeInit(TSCx_SHIELD_IO_GPIO_PORT, TSCx_SHIELD_IO_PIN);
HAL_GPIO_DeInit(TSCx_TS1_SAMPLING_GPIO_PORT, TSCx_TS1_SAMPLING_PIN);
HAL_GPIO_DeInit(TSCx_SHIELD_SAMPLING_GPIO_PORT, TSCx_SHIELD_SAMPLING_PIN);
/*##-3- Disable the NVIC for TSC ###########################################*/
/* Not used in this example. Keep it for reference.
HAL_NVIC_DisableIRQ(TSCx_EXTI_IRQn); */
}
int main(void)
{
/*##-1- Configure the TSC peripheral #######################################*/
TscHandle.Instance = TSCx;
TscHandle.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL;
TscHandle.Init.CTPulseHighLength = TSC_CTPH_2CYCLES;
TscHandle.Init.CTPulseLowLength = TSC_CTPL_2CYCLES;
TscHandle.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW;
TscHandle.Init.MaxCountInterrupt = ENABLE;
TscHandle.Init.MaxCountValue = TSC_MCV_16383;
TscHandle.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV64;
TscHandle.Init.SpreadSpectrum = DISABLE;
TscHandle.Init.SpreadSpectrumDeviation = 127;
TscHandle.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1;
TscHandle.Init.SynchroPinPolarity = TSC_SYNC_POLARITY_FALLING;
TscHandle.Init.ChannelIOs = 0; /* Not needed yet. Will be set with HAL_TSC_IOConfig() */
TscHandle.Init.SamplingIOs = 0; /* Not needed yet. Will be set with HAL_TSC_IOConfig() */
TscHandle.Init.ShieldIOs = 0; /* Not needed yet. Will be set with HAL_TSC_IOConfig() */
if (HAL_TSC_Init(&TscHandle) != HAL_OK)
{
/* Initialization Error */
}
/*##-2- Configure the touch-sensing IOs ####################################*/
IoConfig.ChannelIOs = TSC_GROUP2_IO3; // PB6
IoConfig.SamplingIOs = (TSC_GROUP2_IO1 | TSC_GROUP2_IO2); //PB4, PB5
IoConfig.ShieldIOs = TSC_GROUP2_IO4; //PB7
if (HAL_TSC_IOConfig(&TscHandle, &IoConfig) != HAL_OK)
{
/* Initialization Error */
}
/*##-3- Discharge the touch-sensing IOs ####################################*/
HAL_TSC_IODischarge(&TscHandle, ENABLE);
/*##-4- Start the acquisition process ######################################*/
if (HAL_TSC_Start_IT(&TscHandle) != HAL_OK)
{
/* Acquisition Error */
}
while(1){
;
}
}
#define TSCx_TS1_MAXTHRESHOLD 6000
__IO uint32_t uhTSCAcquisitionValue;
void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef* htsc)
{
uint32_t wait_loop_index = 0;
/*##-5- Discharge the touch-sensing IOs ####################################*/
HAL_TSC_IODischarge(&TscHandle, ENABLE);
/* Note: a delay can be added here */
/* wait 100us */
wait_loop_index = (SystemCoreClock / 10000);
while (wait_loop_index != 0)
{
wait_loop_index--;
}
/*##-6- Check if the acquisition is correct (no max count) #################*/
if (HAL_TSC_GroupGetStatus(&TscHandle, TSC_GROUP2_IDX) == TSC_GROUP_COMPLETED)
{
/*##-7- Read the acquisition value #######################################*/
uhTSCAcquisitionValue = HAL_TSC_GroupGetValue(&TscHandle, TSC_GROUP2_IDX);
/* Note: Show the touch activity on LEDs.
The threshold values are indicative and may need to be adjusted */
if (uhTSCAcquisitionValue < TSCx_TS1_MAXTHRESHOLD)
{
}
else
{
}
}
/*##-8- Re-start the acquisition process ###################################*/
if (HAL_TSC_Start_IT(&TscHandle) != HAL_OK)
{
/* Acquisition Error */
}
}
stm32l4xx_it,c
extern TSC_HandleTypeDef TscHandle;
void TSC_IRQHandler(void)
{
HAL_TSC_IRQHandler(&TscHandle);
}
'초보의 아웅다웅 설계하기 > STM32' 카테고리의 다른 글
STM32H7 USB CDC VCP (0) | 2021.01.11 |
---|---|
STM32 USB CDC를 사용하여 Ymodem DFU 만들기 (0) | 2020.12.17 |
STM32 USB CDC-ACM(Virtual Com Port) 리눅스 확인 (0) | 2020.11.20 |
SSD1353 TEST Program (0) | 2020.09.09 |
GPIO로 딜레이를 사용하여 Uart TX 출력 - Soft Uart (0) | 2020.07.02 |