stm8l15x_tsl_ct_acquisition.c

来自「STM8s」· C语言 代码 · 共 1,126 行 · 第 1/3 页

C
1,126
字号
#endif
              ;

  //wait for good discharging of all capacitors
  {u16 i;
    for (i = 0; i < 2000 ;i++) {}
  }

  //All IO in input with the analog switch OPEN
  RI->IOGCR = CGCR_P1_SW_OPEN_MASK | CGCR_SAMP_SW_OPEN_MASK
#if NUMBER_OF_ACQUISITION_PORTS > 1
              | CGCR_P2_SW_OPEN_MASK
#endif
              ;

  //close the analog switch of the channel used for the sampling capacitor
  //RI->IOGCR = CGCR_SAMP_SW_CLOSE_MASK;
}

/**
  ******************************************************************************
  * @brief Acquisition function for the 1st Channel IO of each group
  * @param[in] AcqNumber Number of times the acquisition is done.
  * @param[in] AdjustmentLevel Used to adjust the measured level.
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_IO_Acquisition_P1(u16 MaxAcqNumber)
{

  u16 MeasurementCounter;
#if SPREAD_SPECTRUM && !SW_SPREAD_SPECTRUM
  u8 HSI_fact_Calib;
#endif

  Channel_P1.Measure[0] = INIT_VALUE;
  Channel_P1.Measure[1] = INIT_VALUE;
  Channel_P1.Measure[2] = INIT_VALUE;
  Channel_P1.Measure[3] = INIT_VALUE;
  Channel_P1.Measure[4] = INIT_VALUE;
  Channel_P1.Measure[5] = INIT_VALUE;
  Channel_P1.Measure[6] = INIT_VALUE;
  Channel_P1.Measure[7] = INIT_VALUE;

#if (ACTIVE_SHIELD_GROUP)
	CCMR_P1   |= ACTIVE_SHIELD_GROUP;
#if NUMBER_OF_ACQUISITION_PORTS > 1
  CCMR_P2   &= (uint8_t)(~ACTIVE_SHIELD_GROUP);
#endif
#endif

	//P1 and Sampling capacitor IOs in input with the analog switch OPEN
  RI->IOGCR = (u8)((RI->IOGCR & (CGCR_P1_GLOBAL_MASK & CGCR_SAMP_GLOBAL_MASK)) | (CGCR_P1_SW_OPEN_MASK | CGCR_SAMP_SW_OPEN_MASK));

  //close the analog switch of the channel used for the sampling capacitor
  RI->IOGCR |= CGCR_SAMP_SW_CLOSE_MASK;

  Channel_P1.State.whole = 0x00;

  /*reset the counter*/
  MeasurementCounter = 0x0000;

  /*START spread spectrum*/
#if SPREAD_SPECTRUM && !SW_SPREAD_SPECTRUM
  /*save the user application calibration value*/
  if (CLK->HSITRIMR != 0x00) /*the application  use the Triming register*/
  {
    Previous_calibration_value = CLK->HSITRIMR;
  }
  else /*the application doesn't use the trimming register*/
  {
    Previous_calibration_value = CLK->HSICALR;
  }

  HSI_fact_Calib = CLK->HSICALR;
  CLK->HSIUNLCKR = 0xAC;
  CLK->HSIUNLCKR = 0x35;
  CLK->HSITRIMR = HSI_fact_Calib;

#endif

  /*loop while all the 1st channel of each group have not reach the VIH level*/
  do
  {
    /*charging the Ctouch cap with connecting the IO to Vdd (IO in push-pull HIGH)*/
    RI->IOGCR = (u8)((RI->IOGCR & CGCR_P1_GLOBAL_MASK) | CGCR_P1_PP_HIGH_MASK);

    if (CSR_SAMP & (uint8_t)~Channel_P1.State.whole)
    {
      /*To guaranty a good charging the high level on Ctouch has to be maintained about 2us.
      Instead of performing a waiting loop, the first part of the test to detect if a pin 
      has reach the VDDIO/2 threshold is performed here.*/
      if ((Channel_P1.State.b.Grp1 == 0) && ((CSR_SAMP&Group1) == Group1))
      {
        Channel_P1.Measure[0] = MeasurementCounter;
        Channel_P1.State.whole |= Group1;
      }
      if ((Channel_P1.State.b.Grp2 == 0) && ((CSR_SAMP&Group2) == Group2))
      {
        Channel_P1.Measure[1] = MeasurementCounter;
        Channel_P1.State.whole |= Group2;
      }
      if ((Channel_P1.State.b.Grp3 == 0) && ((CSR_SAMP&Group3) == Group3))
      {
        Channel_P1.Measure[2] = MeasurementCounter;
        Channel_P1.State.whole |= Group3;
      }
      if ((Channel_P1.State.b.Grp4 == 0) && ((CSR_SAMP&Group4) == Group4))
      {
        Channel_P1.Measure[3] = MeasurementCounter;
        Channel_P1.State.whole |= Group4;
      }
      if ((Channel_P1.State.b.Grp5 == 0) && ((CSR_SAMP&Group5) == Group5))
      {
        Channel_P1.Measure[4] = MeasurementCounter;
        Channel_P1.State.whole |= Group5;
      }

      if ((Channel_P1.State.b.Grp6 == 0) && ((CSR_SAMP&Group6) == Group6))
      {
        Channel_P1.Measure[5] = MeasurementCounter;
        Channel_P1.State.whole |= Group6;
      }
      if ((Channel_P1.State.b.Grp7 == 0) && ((CSR_SAMP&Group7) == Group7))
      {
        Channel_P1.Measure[6] = MeasurementCounter;
        Channel_P1.State.whole |= Group7;
      }
      if ((Channel_P1.State.b.Grp8 == 0) && ((CSR_SAMP&Group8) == Group8))
      {
        Channel_P1.Measure[7] = MeasurementCounter;
        Channel_P1.State.whole |= Group8;
      }
    }
    MeasurementCounter++;

    CLWHTA;  /**< programmable delay */

    /*charging the Csense cap with connecting it to Ctouch by closing the analog switch.
    Here the charge is transfered from the Ctouch to the Csens capacitors*/
    RI->IOGCR = (u8)((RI->IOGCR & CGCR_P1_GLOBAL_MASK) | CGCR_P1_SW_CLOSE_MASK);

#if SPREAD_SPECTRUM && !SW_SPREAD_SPECTRUM
    if (CLK->HSITRIMR > HSI_fact_Calib - SPREAD_COUNTER_MAX)
    {
      u8 Prev_HSI_TRIM;
      Prev_HSI_TRIM = (u8)(CLK->HSITRIMR - 1);
      CLK->HSIUNLCKR = 0xAC;
      CLK->HSIUNLCKR = 0x35;
      CLK->HSITRIMR = Prev_HSI_TRIM;
    }
    else
    {
      CLK->HSIUNLCKR = 0xAC;
      CLK->HSIUNLCKR = 0x35;
      CLK->HSITRIMR = HSI_fact_Calib ;
    }
#endif

    /*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 the test up */
    if ((u8)(MeasurementCounter >> 8) > (u8)(MaxAcqNumber >> 8))
    {
      break;
    }
    /*wait for good discharging of the the Ctouch cap in the Csense */
    CLWLTA;  /**< programmable delay */

#if SPREAD_SPECTRUM && SW_SPREAD_SPECTRUM
    TSL_SW_Spread_Spectrum();
#endif

  }
  while (((Channel_P1.State.whole & Channel_P1.EnabledChannels) != Channel_P1.EnabledChannels));
  /*problem with the line above when the MASK is not done.
  In fact when the SCKEY_P1_MASK is not performed some unused channels are is detected state */

#if SPREAD_SPECTRUM && !SW_SPREAD_SPECTRUM
  /*Restore the user application calibration value*/
  CLK->HSIUNLCKR = 0xAC;
  CLK->HSIUNLCKR = 0x35;
  CLK->HSITRIMR = Previous_calibration_value;
#endif

  //P1 and Sampling capacitor IOs in input with the analog switch OPEN
  RI->IOGCR = (u8)((RI->IOGCR & (CGCR_P1_GLOBAL_MASK & CGCR_SAMP_GLOBAL_MASK)) | (CGCR_P1_SW_OPEN_MASK | CGCR_SAMP_SW_OPEN_MASK));

  //P1 and Sampling capacitors IOs to LOW for discharging
  RI->IOGCR = (u8)((RI->IOGCR & (CGCR_P1_GLOBAL_MASK & CGCR_SAMP_GLOBAL_MASK)) | (CGCR_P1_PP_LOW_MASK | CGCR_SAMP_PP_LOW_MASK));
}


/**
  ******************************************************************************
  * @brief Acquisition function for the 1st Channel IO of each group
  * @param[in] AcqNumber Number of times the acquisition is done.
  * @param[in] AdjustmentLevel Used to adjust the measured level.
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
#if NUMBER_OF_ACQUISITION_PORTS > 1
void TSL_IO_Acquisition_P2(u16 MaxAcqNumber)
{

  u16 MeasurementCounter;

#if SPREAD_SPECTRUM && !SW_SPREAD_SPECTRUM
  u8 HSI_fact_Calib;
#endif

  Channel_P2.Measure[0] = INIT_VALUE;
  Channel_P2.Measure[1] = INIT_VALUE;
  Channel_P2.Measure[2] = INIT_VALUE;
  Channel_P2.Measure[3] = INIT_VALUE;
  Channel_P2.Measure[4] = INIT_VALUE;
  Channel_P2.Measure[5] = INIT_VALUE;
  Channel_P2.Measure[6] = INIT_VALUE;
  Channel_P2.Measure[7] = INIT_VALUE;

#if (ACTIVE_SHIELD_GROUP)
	CCMR_P2   |= ACTIVE_SHIELD_GROUP;
	CCMR_P1   &= (uint8_t)(~ACTIVE_SHIELD_GROUP);  
#endif

  //P2 and Sampling capacitor IOs in input with the analog switch OPEN
  RI->IOGCR = (u8)((RI->IOGCR & (CGCR_P2_GLOBAL_MASK & CGCR_SAMP_GLOBAL_MASK)) | (CGCR_P2_SW_OPEN_MASK | CGCR_SAMP_SW_OPEN_MASK));

  //close the analog switch of the channel used for the sampling capacitor
  RI->IOGCR |= CGCR_SAMP_SW_CLOSE_MASK;

  Channel_P2.State.whole = 0x00;

  /*reset the counter*/
  MeasurementCounter = 0x0000;

  /*START spread spectrum*/
#if SPREAD_SPECTRUM && !SW_SPREAD_SPECTRUM
  /*save the user application calibration value*/
  if (CLK->HSITRIMR != 0x00) /*the application  use the Triming register*/
  {
    Previous_calibration_value = CLK->HSITRIMR;
  }
  else /*the application doesn't use the trimming register*/
  {
    Previous_calibration_value = CLK->HSICALR;
  }

  HSI_fact_Calib = CLK->HSICALR;
  CLK->HSIUNLCKR = 0xAC;
  CLK->HSIUNLCKR = 0x35;
  CLK->HSITRIMR = HSI_fact_Calib;

#endif

  /*loop while all the 1st channel of each group have not reach the VIH level*/
  do
  {
    /*charging the Ctouch cap with connecting the IO to Vdd (IO in push-pull HIGH)*/
    RI->IOGCR = (u8)((RI->IOGCR & CGCR_P2_GLOBAL_MASK) | CGCR_P2_PP_HIGH_MASK);

    if (CSR_SAMP & (uint8_t)~Channel_P2.State.whole)
    {
      /*To guaranty a good charging the high level on Ctouch has to be maintained about 2us.
      Instead of performing a waiting loop, the first part of the test to detect if a pin 
      has reach the VDDIO/2 threshold is performed here.*/
      if ((Channel_P2.State.b.Grp1 == 0) && ((CSR_SAMP&Group1) == Group1))
      {
        Channel_P2.Measure[0] = MeasurementCounter;
        Channel_P2.State.whole |= Group1;
      }
      if ((Channel_P2.State.b.Grp2 == 0) && ((CSR_SAMP&Group2) == Group2))
      {
        Channel_P2.Measure[1] = MeasurementCounter;
        Channel_P2.State.whole |= Group2;
      }
      if ((Channel_P2.State.b.Grp3 == 0) && ((CSR_SAMP&Group3) == Group3))
      {
        Channel_P2.Measure[2] = MeasurementCounter;
        Channel_P2.State.whole |= Group3;
      }
      if ((Channel_P2.State.b.Grp4 == 0) && ((CSR_SAMP&Group4) == Group4))
      {
        Channel_P2.Measure[3] = MeasurementCounter;
        Channel_P2.State.whole |= Group4;
      }
      if ((Channel_P2.State.b.Grp5 == 0) && ((CSR_SAMP&Group5) == Group5))
      {
        Channel_P2.Measure[4] = MeasurementCounter;
        Channel_P2.State.whole |= Group5;
      }

      if ((Channel_P2.State.b.Grp6 == 0) && ((CSR_SAMP&Group6) == Group6))
      {
        Channel_P2.Measure[5] = MeasurementCounter;
        Channel_P2.State.whole |= Group6;
      }
      if ((Channel_P2.State.b.Grp7 == 0) && ((CSR_SAMP&Group7) == Group7))
      {
        Channel_P2.Measure[6] = MeasurementCounter;
        Channel_P2.State.whole |= Group7;
      }
      if ((Channel_P2.State.b.Grp8 == 0) && ((CSR_SAMP&Group8) == Group8))
      {
        Channel_P2.Measure[7] = MeasurementCounter;
        Channel_P2.State.whole |= Group8;
      }
    }

    MeasurementCounter++;

    CLWHTA;  /**< programmable delay */

    /*charging the Csense cap with connecting it to Ctouch by closing the analog switch.
    Here the charge is transfered from the Ctouch to the Csens capacitors*/
    RI->IOGCR = (u8)((RI->IOGCR & CGCR_P2_GLOBAL_MASK) | CGCR_P2_SW_CLOSE_MASK);

#if SPREAD_SPECTRUM && !SW_SPREAD_SPECTRUM
    if (CLK->HSITRIMR > HSI_fact_Calib - SPREAD_COUNTER_MAX)
    {
      u8 Prev_HSI_TRIM;
      Prev_HSI_TRIM = (u8)(CLK->HSITRIMR - 1);
      CLK->HSIUNLCKR = 0xAC;
      CLK->HSIUNLCKR = 0x35;
      CLK->HSITRIMR = Prev_HSI_TRIM;
    }
    else
    {
      CLK->HSIUNLCKR = 0xAC;
      CLK->HSIUNLCKR = 0x35;
      CLK->HSITRIMR = HSI_fact_Calib ;
    }
#endif

    /*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 the test up */
    if ((u8)(MeasurementCounter >> 8) > (u8)(MaxAcqNumber >> 8))
    {
      break;
    }
    /*wait for good discharging of the the Ctouch cap in the Csense */
    CLWLTA;  /**< programmable delay */
#if SPREAD_SPECTRUM && SW_SPREAD_SPECTRUM
    TSL_SW_Spread_Spectrum();
#endif

  }
  while (((Channel_P2.State.whole & Channel_P2.EnabledChannels) != Channel_P2.EnabledChannels));

#if SPREAD_SPECTRUM && !SW_SPREAD_SPECTRUM
  /*Restore the user application calibration value*/
  CLK->HSIUNLCKR = 0xAC;
  CLK->HSIUNLCKR = 0x35;
  CLK->HSITRIMR = Previous_calibration_value;
#endif

  //P2 and Sampling capacitor IOs in input with the analog switch OPEN
  RI->IOGCR = (u8)((RI->IOGCR & (CGCR_P2_GLOBAL_MASK & CGCR_SAMP_GLOBAL_MASK)) | (CGCR_P2_SW_OPEN_MASK | CGCR_SAMP_SW_OPEN_MASK));

  //P2 and Sampling capacitors IOs to LOW for discharing
  RI->IOGCR = (u8)((RI->IOGCR & (CGCR_P2_GLOBAL_MASK & CGCR_SAMP_GLOBAL_MASK)) | (CGCR_P2_PP_LOW_MASK | CGCR_SAMP_PP_LOW_MASK));

}
#endif
#endif


/* Public functions ----------------------------------------------------------*/

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?