stm8l10x_tsl_ct_acquisition.c
来自「STM8s」· C语言 代码 · 共 2,078 行 · 第 1/4 页
C
2,078 行
#if CHANNEL_PER_MCKEY > 5
, MCKEY1_F
, MCKEY1_G
, MCKEY1_H
#endif
#if NUMBER_OF_MULTI_CHANNEL_KEYS > 1
, MCKEY2_A
, MCKEY2_B
, MCKEY2_C
#if CHANNEL_PER_MCKEY > 3
, MCKEY2_D
, MCKEY2_E
#endif
#if CHANNEL_PER_MCKEY > 5
, MCKEY2_F
, MCKEY2_G
, MCKEY2_H
#endif
#endif
};
#endif
/* Basic delay function*/
void wait(u16 d)
{
while (d--) {}}
#if SPREAD_SPECTRUM
/**
******************************************************************************
* @brief Spread Spectrum waiting routine. A variable delay is
* inserted between each step.
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* SPREAD_SPECTRUM must be defined in the configuration file
******************************************************************************
*/
INLINE void TSL_SW_Spread_Spectrum(void)
{
u8 i;
SpreadCounter++;
if (SpreadCounter == SPREAD_COUNTER_MAX)
{
SpreadCounter = SPREAD_COUNTER_MIN;
}
i = SpreadCounter;
while (--i);
}
#endif /* SPREAD_SPECTRUM */
/**
******************************************************************************
* @brief Init for I/Os used in the application. Used for SW I/O toggling.
* If the hardware cell is used, the responsability of the I/O configuration is
* given to the user layer.
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
******************************************************************************
*/
void TSL_IO_Init(void)
{
/*enable the comparators 1 and 2 */
#if defined(COMP1) && defined(COMP2)
COMP->CR = 0x06;
#elif defined(COMP1)
COMP->CR = 0x02;
#else
COMP->CR = 0x04;
#endif
}
/**
******************************************************************************
* @brief Put All Sensing I/Os in ouput mode at 0.
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
******************************************************************************
*/
//void TSL_IO_Clamp(void)
//{
//All the analog switch OPEN
//COMP->CCS = 0x00;
//All IO used to pushpull LOW for discharging all capacitors (Ctouch and Csense)
/*Port B*/
//GPIOB->DDR = 0x0F;
//GPIOB->CR1 = 0x0F;
//GPIOB->ODR = 0x00;
///*Port D*/
//GPIOD->DDR = 0x0F;
//GPIOD->CR1 = 0x0F;
//GPIOD->ODR = 0x00;
/*wait a while*/
//wait(200);
//All IO in input with the analog switch OPEN
/*Port B*/
//GPIOB->DDR = 0x00;
//GPIOB->CR1 = 0x00;
/*Port D*/
//GPIOD->DDR = 0x00;
// GPIOD->CR1 = 0x00;
//}
/**
******************************************************************************
* @brief Acquisition function for the 1st Channel IO of each group
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
******************************************************************************
*/
void TSL_IO_Acquisition_P1(u16 MaxAcqNumber)
{
u16 MeasurementCounter;
/*reset the counter values*/
MeasurementCounter = 0x0000;
Channel_P1.Measure[0] = 0x0000;
Channel_P1.Measure[1] = 0x0000;
Channel_P1.State.whole = 0x00;
/*open the analog switches*/
COMP->CCS &= (u8)(~(SAMP_CAP_COMP_MASK | P1_COMP_MASK));
/*All IO to pushpull LOW for discharging all capacitors (Ctouch and Csense)*/
/*discharging Csample*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
SAMP_CAP_PORT->DDR |= SAMP_CAP_IO_MASK;
SAMP_CAP_PORT->CR1 |= SAMP_CAP_IO_MASK;
SAMP_CAP_PORT->ODR &= (u8)(~(SAMP_CAP_IO_MASK));
/*discharging Ctouch*/
P1_PORT->DDR |= P1_IO_MASK;
P1_PORT->CR1 |= P1_IO_MASK;
P1_PORT->ODR &= (u8)(~(P1_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
/*wait a while for good discharging of all capacitors*/
wait(200);
/*All IO in input floating*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P1_PORT->DDR &= (u8)(~(P1_IO_MASK));
P1_PORT->CR1 &= (u8)(~(P1_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
//close the Channel #1 analog switches (COMP1 CHA and COMP2 CH1 ie PB0 and PB2)
COMP->CCS |= SAMP_CAP_COMP_MASK;
/*loop while all the 1st channel of each group have not reach the VIH level*/
do
{
/*Close the sampling capacitor analog switch*/
COMP->CCS |= SAMP_CAP_COMP_MASK;
/*charging Ctouch with connecting the IO to Vdd (io in push-pull HIGH)*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P1_PORT->DDR |= P1_IO_MASK;//output push pull config
P1_PORT->CR1 |= P1_IO_MASK;
P1_PORT->ODR |= P1_IO_MASK; //HIGH level
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
/*wait a while for good charging*/
CLWHTA; //wait(5);
/*All IO in input */
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P1_PORT->DDR &= (u8)(~(P1_IO_MASK));
P1_PORT->CR1 &= (u8)(~(P1_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
MeasurementCounter++;
/*charging the Csense cap with connecting it to Ctouch by closing the analog switch*/
COMP->CCS |= P1_COMP_MASK;
/*wait a while for good charge transfering*/
CLWLTA; //wait(5);
#if SPREAD_SPECTRUM
TSL_SW_Spread_Spectrum();
#endif
/*open the analog switches to allow checking of the inputs*/
COMP->CCS &= (u8)(~(SAMP_CAP_COMP_MASK | P1_COMP_MASK));
/*Sampling capacitor IOs in input floating*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
SAMP_CAP_PORT->DDR &= (u8)(~(SAMP_CAP_IO_MASK));
SAMP_CAP_PORT->CR1 &= (u8)(~(SAMP_CAP_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
if ((Channel_P1.State.b.Grp1 == 0) && ((SAMP_CAP_PORT->IDR&SAMP_CAP_IO_MASK_1) == SAMP_CAP_IO_MASK_1))
{
Channel_P1.Measure[0] += MeasurementCounter;
Channel_P1.State.whole |= Group1;
}
if ((Channel_P1.State.b.Grp2 == 0) && ((SAMP_CAP_PORT->IDR&SAMP_CAP_IO_MASK_2) == SAMP_CAP_IO_MASK_2))
{
Channel_P1.Measure[1] += MeasurementCounter;
Channel_P1.State.whole |= Group2;
}
/*it's better to implement this like that because it's much more faster than to put this test in the "while test" below and only the MSByte is tested in order to speed up the code execution*/
if ((u8)(MeasurementCounter >> 8) > (u8)(MaxAcqNumber >> 8))
{
break;
}
}
while (((Channel_P1.State.whole & Channel_P1.EnabledChannels) != Channel_P1.EnabledChannels));
}
/**
******************************************************************************
* @brief Acquisition function for the 2nd Channel IO of each group
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
******************************************************************************
*/
#if (NUMBER_OF_ACQUISITION_PORTS > 1)
void TSL_IO_Acquisition_P2(u16 MaxAcqNumber)
{
u16 MeasurementCounter;
/*reset the counter values*/
MeasurementCounter = 0x0000;
Channel_P2.Measure[0] = 0x0000;
Channel_P2.Measure[1] = 0x0000;
Channel_P2.State.whole = 0x00;
/*open the analog switches*/
COMP->CCS &= (u8)(~(SAMP_CAP_COMP_MASK | P2_COMP_MASK));
/*All IO to pushpull LOW for discharging all capacitors (Ctouch and Csense)*/
/*discharging Csample*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
SAMP_CAP_PORT->DDR |= SAMP_CAP_IO_MASK;
SAMP_CAP_PORT->CR1 |= SAMP_CAP_IO_MASK;
SAMP_CAP_PORT->ODR &= (u8)(~(SAMP_CAP_IO_MASK));
/*discharging Ctouch*/
P2_PORT->DDR |= P2_IO_MASK;
P2_PORT->CR1 |= P2_IO_MASK;
P2_PORT->ODR &= (u8)(~(P2_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
/*wait a while for good discharge of all capacitors*/
wait(200);
/*All IO in input floating*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P2_PORT->DDR &= (u8)(~(P2_IO_MASK));
P2_PORT->CR1 &= (u8)(~(P2_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
//close the Chanel #1 analog switches (COMP1 CHA and COMP2 CH1 ie PB0 and PB2)
COMP->CCS |= SAMP_CAP_COMP_MASK;
/*loop while all the 1st channel of each group have not reach the VIH level*/
do
{
/*Close the sampling capacitor analog switch*/
COMP->CCS |= SAMP_CAP_COMP_MASK;
/*charging Ctouch with connecting the IO to Vdd (io in push-pull HIGH)*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P2_PORT->DDR |= P2_IO_MASK;//output push pull config
P2_PORT->CR1 |= P2_IO_MASK;
P2_PORT->ODR |= P2_IO_MASK; //HIGH level
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
/*wait a while for good charging*/
CLWHTA; //wait(5);
/*All IO in input */
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P2_PORT->DDR &= (u8)(~(P2_IO_MASK));
P2_PORT->CR1 &= (u8)(~(P2_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
MeasurementCounter++;
/*charging the Csense cap with connecting it to Ctouch by closing the analog switch*/
COMP->CCS |= P2_COMP_MASK;
/*wait a while for good charge transfering*/
CLWLTA; //wait(5);
#if SPREAD_SPECTRUM
TSL_SW_Spread_Spectrum();
#endif
/*open the analog switches to allow checking of the inputs*/
COMP->CCS &= (u8)(~(SAMP_CAP_COMP_MASK | P2_COMP_MASK));
/*Sampling capacitor IOs in input folating */
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
SAMP_CAP_PORT->DDR &= (u8)(~(SAMP_CAP_IO_MASK));
SAMP_CAP_PORT->CR1 &= (u8)(~(SAMP_CAP_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
if ((Channel_P2.State.b.Grp1 == 0) && ((SAMP_CAP_PORT->IDR&SAMP_CAP_IO_MASK_1) == SAMP_CAP_IO_MASK_1))
{
Channel_P2.Measure[0] += MeasurementCounter;
Channel_P2.State.whole |= Group1;
}
if ((Channel_P2.State.b.Grp2 == 0) && ((SAMP_CAP_PORT->IDR&SAMP_CAP_IO_MASK_2) == SAMP_CAP_IO_MASK_2))
{
Channel_P2.Measure[1] += MeasurementCounter;
Channel_P2.State.whole |= Group2;
}
/*it's better to implement this like that because it's much more faster than to put this test in the "while test" below and only the MSByte is tested in order to speed up the code execution*/
if ((u8)(MeasurementCounter >> 8) > (u8)(MaxAcqNumber >> 8))
{
break;
}
}
while (((Channel_P2.State.whole & Channel_P2.EnabledChannels) != Channel_P2.EnabledChannels));
}
#endif
/**
******************************************************************************
* @brief Acquisition function for the 3rd Channel IO of each group
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
******************************************************************************
*/
#if (NUMBER_OF_ACQUISITION_PORTS > 2)
void TSL_IO_Acquisition_P3(u16 MaxAcqNumber)
{
u16 MeasurementCounter;
/*reset the counter values*/
MeasurementCounter = 0x0000;
Channel_P3.Measure[0] = 0x0000;
Channel_P3.Measure[1] = 0x0000;
Channel_P3.State.whole = 0x00;
/*open the analog switches*/
COMP->CCS &= (u8)(~(SAMP_CAP_COMP_MASK | P3_COMP_MASK));
/*All IO to pushpull LOW for discharging all capacitors (Ctouch and Csense)*/
/*discharging Csample*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
SAMP_CAP_PORT->DDR |= SAMP_CAP_IO_MASK;
SAMP_CAP_PORT->CR1 |= SAMP_CAP_IO_MASK;
SAMP_CAP_PORT->ODR &= (u8)(~(SAMP_CAP_IO_MASK));
/*discharging Ctouch*/
P3_PORT->DDR |= P3_IO_MASK;
P3_PORT->CR1 |= P3_IO_MASK;
P3_PORT->ODR &= (u8)(~(P3_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
/*wait a while for good discharge of all capacitors*/
wait(200);
/*All IO in input floating*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P3_PORT->DDR &= (u8)(~(P3_IO_MASK));
P3_PORT->CR1 &= (u8)(~(P3_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
//close the Chanel #1 analog switches (COMP1 CHA and COMP2 CH1 ie PB0 and PB2)
COMP->CCS |= SAMP_CAP_COMP_MASK;
/*loop in this loop while all the 1st channel of each group have not reach the VIH level*/
do
{
/*Close the sampling capacitor analog switch*/
COMP->CCS |= SAMP_CAP_COMP_MASK;
/*charging Ctouch with connecting the IO to Vdd (io in push-pull HIGH)*/
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P3_PORT->DDR |= P3_IO_MASK;//output push pull config
P3_PORT->CR1 |= P3_IO_MASK;
P3_PORT->ODR |= P3_IO_MASK; //HIGH level
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
/*wait a while for good charging*/
CLWHTA; //wait(5);
/*All IO in input */
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
P3_PORT->DDR &= (u8)(~(P3_IO_MASK));
P3_PORT->CR1 &= (u8)(~(P3_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
/*increment the MeasurementCounter*/
MeasurementCounter++;
/*charging the Csense cap with connecting it to Ctouch by closing the analog switch*/
COMP->CCS |= P3_COMP_MASK;
/*wait a while for good charge transfering*/
CLWLTA; //wait(5);
#if SPREAD_SPECTRUM
TSL_SW_Spread_Spectrum();
#endif
/*open the analog switches to allow checking of the inputs*/
COMP->CCS &= (u8)(~(SAMP_CAP_COMP_MASK | P3_COMP_MASK));
/*Sampling capacitor IOs in input folating */
#ifdef PROTECT_IO_ACCESS
disableInterrupts();
#endif
SAMP_CAP_PORT->DDR &= (u8)(~(SAMP_CAP_IO_MASK));
SAMP_CAP_PORT->CR1 &= (u8)(~(SAMP_CAP_IO_MASK));
#ifdef PROTECT_IO_ACCESS
enableInterrupts();
#endif
if ((Channel_P3.State.b.Grp1 == 0) && ((SAMP_CAP_PORT->IDR&SAMP_CAP_IO_MASK_1) == SAMP_CAP_IO_MASK_1))
{
Channel_P3.Measure[0] += MeasurementCounter;
Channel_P3.State.whole |= Group1;
}
if ((Channel_P3.State.b.Grp2 == 0) && ((SAMP_CAP_PORT->IDR&SAMP_CAP_IO_MASK_2) == SAMP_CAP_IO_MASK_2))
{
Channel_P3.Measure[1] += MeasurementCounter;
Channel_P3.State.whole |= Group2;
}
/*it's better to implement this like that because it's much more faster than to put this test in the "while test" below and only the MSByte is tested in order to speed up the code execution*/
if ((u8)(MeasurementCounter >> 8) > (u8)(MaxAcqNumber >> 8))
{
break;
}
}
while (((Channel_P3.State.whole & Channel_P3.EnabledChannels) != Channel_P3.EnabledChannels));
}
#endif
#endif
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?