⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm8s_adc2.c

📁 STM8-触摸例程
💻 C
📖 第 1 页 / 共 2 页
字号:
    if (ADC2_SchmittTriggerState != DISABLE)
    {
      ADC2->TDRL &= (u8)(~(u8)((u8)0x01 << (u8)ADC2_SchmittTriggerChannel));
    }
    else /* ADC2_SchmittState == DISABLE */
    {
      ADC2->TDRL |= (u8)((u8)0x01 << (u8)ADC2_SchmittTriggerChannel);
    }
  }
  else /* ADC2_SchmittTriggerChannel >= ADC2_SCHMITTTRIG_CHANNEL8 */
  {
    if (ADC2_SchmittTriggerState != DISABLE)
    {
      ADC2->TDRH &= (u8)(~(u8)((u8)0x01 << ((u8)ADC2_SchmittTriggerChannel - (u8)8)));
    }
    else /* ADC2_SchmittState == DISABLE */
    {
      ADC2->TDRH |= (u8)((u8)0x01 << ((u8)ADC2_SchmittTriggerChannel - (u8)8));
    }
  }

}


/**
  * @brief Configure the ADC2 conversion on selected channel.
  * @param[in] ADC2_ConversionMode Specifies the conversion type.
  * It can be set of the values of @ref ADC2_ConvMode_TypeDef
  * @param[in] ADC2_Channel specifies the ADC2 Channel.
  * It can be set of the values of @ref ADC2_Channel_TypeDef
  * @param[in] ADC2_Align specifies the conerted data alignement.
  * It can be set of the values of @ref ADC2_Align_TypeDef
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Configure the ADC2 conversion in continuous mode on channel 2
  * @code
  * ADC2_ConversionConfig(ADC2_CHANNEL_2, ADC2_CONVERSIONMODE_CONTINUOUS, ADC2_ALIGN_RIGHT);
  * @endcode
  */
void ADC2_ConversionConfig(ADC2_ConvMode_TypeDef ADC2_ConversionMode, ADC2_Channel_TypeDef ADC2_Channel, ADC2_Align_TypeDef ADC2_Align)
{

  /* Check the parameters */
  assert_param(IS_ADC2_CONVERSIONMODE_OK(ADC2_ConversionMode));
  assert_param(IS_ADC2_CHANNEL_OK(ADC2_Channel));
  assert_param(IS_ADC2_ALIGN_OK(ADC2_Align));

  /* Clear the align bit */
  ADC2->CR2 &= (u8)(~ADC2_CR2_ALIGN);
  /* Configure the data alignment */
  ADC2->CR2 |= (u8)(ADC2_Align);

  if (ADC2_ConversionMode == ADC2_CONVERSIONMODE_CONTINUOUS)
  {
    /* Set the continuous coversion mode */
    ADC2->CR1 |= ADC2_CR1_CONT;
  }
  else /* ADC2_ConversionMode == ADC2_CONVERSIONMODE_SINGLE */
  {
    /* Set the single conversion mode */
    ADC2->CR1 &= (u8)(~ADC2_CR1_CONT);
  }

  /* Clear the ADC2 channels */
  ADC2->CSR &= (u8)(~ADC2_CSR_CH);
  /* Select the ADC2 channel */
  ADC2->CSR |= (u8)(ADC2_Channel);

}


/**
  * @brief Configure the ADC2 conversion on external trigger event.
  * @par Full description:
  * The selected external trigger evant can be enabled or disabled.
  * @param[in] ADC2_ExtTrigger to select the External trigger event.
  * can have one of the values of @ref ADC2_ExtTrig_TypeDef.
  * @param[in] ADC2_ExtTrigState to enable/disable the selected external trigger
  * can have one of the values of @ref FunctionalState.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable the TIM1 TRGO to trigger the conversion.
  * @code
  * ADC2_ExternalTriggerConfig(ADC2_EXTTRIG_TIM, ENABLE);
  * @endcode
  */
void ADC2_ExternalTriggerConfig(ADC2_ExtTrig_TypeDef ADC2_ExtTrigger, FunctionalState ADC2_ExtTrigState)
{

  /* Check the parameters */
  assert_param(IS_ADC2_EXTTRIG_OK(ADC2_ExtTrigger));
  assert_param(IS_FUNCTIONALSTATE_OK(ADC2_ExtTrigState));

  /* Clear the external trigger selection bits */
  ADC2->CR2 &= (u8)(~ADC2_CR2_EXTSEL);

  if (ADC2_ExtTrigState != DISABLE)
  {
    /* Enable the selected external Trigger */
    ADC2->CR2 |= (u8)(ADC2_CR2_EXTTRIG);
  }
  else /* ADC2_ExtTrigStatus == DISABLE */
  {
    /* Disable the selected external trigger */
    ADC2->CR2 &= (u8)(~ADC2_CR2_EXTTRIG);
  }

  /* Set the slected external trigger */
  ADC2->CR2 |= (u8)(ADC2_ExtTrigger);

}


/**
  * @brief Start ADC2 conversion
  * @par Full description:
  * This function  triggers the start of conversion, after ADC2 configuration.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * Enable the ADC2 peripheral before calling this fuction
  * @par Called functions:
  * None
  * @par Example:
  * Start conversion
  * @code
  * ADC2_StartConversion();
  * @endcode
  */
void ADC2_StartConversion(void)
{
  ADC2->CR1 |= ADC2_CR1_ADON;
}

/**
  * @brief Get one sample of measured signal.
  * @par Parameters:
  * None
  * @retval ConversionValue:  value of the measured signal.
  * @par Required preconditions:
  * ADC2 conversion finished.
  * @par Called functions:
  * None
  * @par Example:
  * Get the converted value of a given signal on a given channel.
  * @code
  * u16 ADC2_ConversionValue;
  * ADC2_ConversionValue = ADC2_GetConversionValue();
  * @endcode
  */
u16 ADC2_GetConversionValue(void)
{

  u16 temph = 0;
  u8 templ = 0;

  if (ADC2->CR2 & ADC2_CR2_ALIGN) /* Right alignment */
  {
    /* Read LSB first */
    templ = ADC2->DRL;
    /* Then read MSB */
    temph = ADC2->DRH;

    temph = (u16)(templ | (u16)(temph << (u8)8));
  }
  else /* Left alignment */
  {
    /* Read MSB firts*/
    temph = ADC2->DRH;
    /* Then read LSB */
    templ = ADC2->DRL;

    temph = (u16)((u16)(templ << (u8)6) | (u16)(temph << (u8)8));
  }

  return ((u16)temph);

}

/**
  * @brief Checks the ADC2 EOC flag status.
  * @par Parameters:
  * None
  * @retval FlagStatus Status of the ADC2 EOC flag.
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Get the EOC flag status.
  * @code
  * FlagStatus ADC2_FlagStatus;
  * ADC2_FlagStatus = ADC2_GetFlagStatus();
  * @endcode
  */
FlagStatus ADC2_GetFlagStatus(void)
{
  /* Get EOC  flag status */
  return ((u8)(ADC2->CSR & ADC2_CSR_EOC));

}

/**
  * @brief Clear the ADC2 EOC Flag.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Clears the end of conversion Flag
  * @code
  * ADC2_ClearFlag();
  * @endcode
  */
void ADC2_ClearFlag(void)
{
    ADC2->CSR &= (u8)(~ADC2_CSR_EOC);
}

/**
  * @brief Returns the EOC  pending bit status
 * @par Parameters:
  * None
  * @retval FlagStatus: status of the EOC pending bit.
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Get the EOC bit status.
  * @code
  * ITStatus ADC2_ITStatus;
  * ADC2_ITStatus = ADC2_GetITStatus();
  * @endcode
  */
ITStatus ADC2_GetITStatus(void)
{
  return ((u8)(ADC2->CSR & ADC2_CSR_EOC));
}

/**
  * @brief Clear the ADC2 End of Conversion pending bit.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Clears the end of conversion pending bit
  * @code
  * ADC2_ClearITPendingBit();
  * @endcode
  */
void ADC2_ClearITPendingBit(void)
{
    ADC2->CSR &= (u8)(~ADC2_CSR_EOC);
}

/**
  * @}
  */

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

⌨️ 快捷键说明

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