To understand capacitive touch / TSC library based application, I’ve been studying the polling based sample code given in AN5105 for STM32F072B-Disco evaluation board. After tweaking the code to customize for the application, I’ve a few questions as below which I am not able to understand and can’t find any explanation even after reviewing various application notes from ST. Your input would be much appreciated and would be very helpful for me to clarify the concepts of TSC.
- Resolution and Calculation of touch Position on the slider in the range of 0...255:
The header file, tsl_conf.h, supplied by TSC, has the following parameters:
#define TSLPRM_LINROT_RESOLUTION (7)
//Position resolution in number of bits (range=1..8)
#define TSLPRM_USE_3CH_LIN_H (1)
//Half ended electrode design
MyLinRots[0].p_Data->Position structure is used to compare the position value in the range of 0...255, but I am not able to understand the correlation of the value and position of touch on the slider / channel. How does Resolution value 1..8 affect this calculation? Is there any formula based on resolution parameter to calculate position value based on which channel is touched on the slider?
In the example code given in AN5105, I am trying to get 4 LEDs to be evenly distributed in the entire slider over the range of 0...255 with the following code, but not able to understand the calculation of the values used to compare with MyLinRots[0].p_Data->Position structure :
if(MyLinRots[0].p_Data->StateId == TSL_STATEID_DETECT)
{
//TSLPRM_LINROT_RESOLUTION
if(MyLinRots[0].p_Data->Position >= 1 && MyLinRots[0].p_Data->Position < 60)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
}
if(MyLinRots[0].p_Data->Position >= 60 && MyLinRots[0].p_Data->Position < 120)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
}
if(MyLinRots[0].p_Data->Position >= 120 && MyLinRots[0].p_Data->Position < 180)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_SET);
}
if(MyLinRots[0].p_Data->Position >= 180 && MyLinRots[0].p_Data->Position < 255)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
}
}
else //if(MyLinRots[0].p_Data->StateId == TSL_STATEID_RELEASE)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
}
In another example, I re-configured the touch channels on the same STM32F072B-Disco board as 3 individual touch keys. If I keep TSLPRM_TKEY_Detect_IN_TH and TSLPRM_TKEY_Detect_OUT_TH parameters in tsl_conf.h header file as default of 110 & 120, the project compiles without any error.
// TouchKeys Detect state input threshold (range=0..255)
#define TSLPRM_TKEY_DETECT_IN_TH (110)
// TouchKeys Detect state output threshold (range=0..255)
#define TSLPRM_TKEY_DETECT_OUT_TH (120)
With these parameter values, I had to press very hard on the channels and often times, it was very difficult to detect touch, so I reconfigured detect threshold to lower value of 50.
#define TSLPRM_TKEY_DETECT_IN_TH (50)
With this change, I get compiling errors as below:
../Middlewares/ST/STM32_TouchSensing_Library/inc/tsl_check_config.h(162): error: #35: #error directive: "TSLPRM_TKEY_DETECT_OUT_TH is out of range (TSLPRM_TKEY_PROX_IN_TH+1 .. TSLPRM_TKEY_DETECT_IN_TH-1)."
User code is as follow:
//Private macro #define TEST_TKEY(NB) ((MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DETECT) || (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DEB_RELEASE_DETECT) || (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_TOUCH) || (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DEB_DETECT))
while (1)
{
if (tsl_user_Exec() == TSL_STATUS_OK)
{
ProcessSensors(); // Execute sensors related tasks
}
}
void ProcessSensors (void)
{
if (TEST_TKEY(0))
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
}
if (TEST_TKEY(1))
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
}
if (TEST_TKEY(2))
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD5_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
}
}
static void MX_TSC_Init(void)
{
/** Configure the TSC peripheral
htsc.Instance = TSC;
htsc.Init.CTPulseHighLength = TSC_CTPH_2CYCLES;
htsc.Init.CTPulseLowLength = TSC_CTPL_2CYCLES;
htsc.Init.SpreadSpectrum = DISABLE;
htsc.Init.SpreadSpectrumDeviation = 1;
htsc.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1;
htsc.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV4;
htsc.Init.MaxCountValue = TSC_MCV_8191;
htsc.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW;
htsc.Init.SynchroPinPolarity = TSC_SYNC_POLARITY_FALLING;
htsc.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL;
htsc.Init.MaxCountInterrupt = DISABLE;
htsc.Init.ChannelIOs = TSC_GROUP1_IO3|TSC_GROUP2_IO3|TSC_GROUP3_IO2;
htsc.Init.ShieldIOs = 0;
htsc.Init.SamplingIOs = TSC_GROUP1_IO4|TSC_GROUP2_IO4|TSC_GROUP3_IO3;
if (HAL_TSC_Init(&htsc) != HAL_OK)
{
Error_Handler();
}
}
I don't understand what is it that I am doing wrong and how can I configure 3 touch keys on this slider with adjustable detect threshold?
I've the project complied in Keil, which I can share to better understand the problem.
Your support with these questions is much appreciated and would be very helpful for me to understand and learn TSC and Touch application..
Thank you.