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

📄 stm8s_adc1.c

📁 STM8全部资料
💻 C
📖 第 1 页 / 共 2 页
字号:
    /* Disable the selected external trigger */
    ADC1->CR2 &= (u8)(~ADC1_CR2_EXTTRIG);
  }

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

}


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

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

  u16 temph = 0;
  u8 templ = 0;

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

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

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

  return ((u16)temph);

}

/**
  * @brief Enables or disables the analog watchdog for the given channel.
  * @param[in] Channel specifies the desired Channel.
  * It can be set of the values of @ref ADC1_Channel_TypeDef.
  * @param[in] NewState specifies the analog watchdog new state.
  * can have one of the values of @ref FunctionalState.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enables the analog watchdog on the channel 8.
  * @code
  * ADC1_AWDChannelConfig(ADC1_CHANNEL_8,ENABLE);
  * @endcode
  */
void ADC1_AWDChannelConfig(ADC1_Channel_TypeDef Channel, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  assert_param(IS_ADC1_CHANNEL_OK(Channel));

  if (Channel < (u8)8)
  {
    if (NewState != DISABLE)
    {
      ADC1->AWCRL |= (u8)((u8)1 << Channel);
    }
    else /* NewState == DISABLE */
    {
      ADC1->AWCRL &= (u8)(~((u8)1 << Channel));
    }
  }
  else
  {
    if (NewState != DISABLE)
    {
      ADC1->AWCRH |= (u8)((u8)1 << (Channel - (u8)8));
    }
    else /* NewState == DISABLE */
    {
      ADC1->AWCRH &= (u8)(~((u8)1 << (Channel - (u8)8)));
    }
  }
}

/**
  * @brief Sets the high threshold of the analog watchdog.
  * @param[in] Threshold specifies the high threshold value.
  * this value depends on the reference voltage range.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  *Set the high threshold to 0x2FF.
  * @code
  * ADC1_SetHighThreshold(0x2FF);
  * @endcode
  */
void ADC1_SetHighThreshold(u16 Threshold)
{
  ADC1->HTRH = (u8)(Threshold >> (u8)2);
  ADC1->HTRL = (u8)Threshold;
}

/**
  * @brief Sets the low threshold of the analog watchdog.
  * @param[in] Threshold specifies thelow threshold value.
  * this value depends on the reference voltage range.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  *Set the low threshold to 0x0FF.
  * @code
  * ADC1_SetLowThreshold(0x0FF);
  * @endcode
  */
void ADC1_SetLowThreshold(u16 Threshold)
{
  ADC1->LTRH = (u8)(Threshold >> (u8)2);
  ADC1->LTRL = (u8)Threshold;
}

/**
  * @brief Get one sample of measured signal.
  * @param[in] Buffer specifies the buffer to read.
  * @retval BufferValue:  value read from the given buffer.
  * @par Required preconditions:
  * ADC1 conversion finished.
  * @par Called functions:
  * None
  * @par Example:
  * Get the converted value of a given signal in the second buffer.
  * @code
  * u16 ADC1_BufferValue;
  * ADC1_BufferValue = ADC1_GetBufferValue(2);
  * @endcode
  */
u16 ADC1_GetBufferValue(u8 Buffer)
{

  u16 temph = 0;
  u8 templ = 0;

  /* Check the parameters */
  assert_param(IS_ADC1_BUFFER_OK(Buffer));

  if (ADC1->CR2 & ADC1_CR2_ALIGN) /* Right alignment */
  {
    /* Read LSB first */
    templ = *(u8*)(ADC1_BaseAddress + (Buffer << 1) + 1);
    /* Then read MSB */
    temph = *(u8*)(ADC1_BaseAddress + (Buffer << 1));

    temph = (u16)(templ | (u16)(temph << (u8)8));
  }
  else /* Left alignment */
  {
    /* Read MSB firts*/
    temph = *(u8*)(ADC1_BaseAddress + (Buffer << 1));
    /* Then read LSB */
    templ = *(u8*)(ADC1_BaseAddress + (Buffer << 1) + 1);

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

  return ((u16)temph);

}

/**
  * @brief Checks the specified analog watchdog channel status.
  * @param[in] Channel: specify the channel of which to check the analog watchdog
  * can be one of the values of @ref ADC1_Channel_TypeDef.
  * @retval FlagStatus Status of the analog watchdog.
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Get the EOC flag status.
  * @code
  * FlagStatus ADC1_analogwatchdog;
  * ADC1_analogwatchdog = ADC1_GetAWDChannelStatus(ADC1_CHANNEL_8);
  * @endcode
  */
FlagStatus ADC1_GetAWDChannelStatus(ADC1_Channel_TypeDef Channel)
{
  u8 status = 0;

  /* Check the parameters */
  assert_param(IS_ADC1_CHANNEL_OK(Channel));

  if (Channel < (u8)8)
  {
    status = (u8)(ADC1->AWSRL & ((u8)1 << Channel));
  }
  else /* Channel = 8 | 9 */
  {
    status = (u8)(ADC1->AWSRH & ((u8)1 << (Channel - (u8)8)));
  }

  return ((FlagStatus)status);
}

/**
  * @brief Checks the specified ADC1 flag status.
  * @param[in] Flag: ADC1 flag.
  * can be one of the values of @ref ADC1_Flag_TypeDef.
  * @retval FlagStatus Status of the ADC1 flag.
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Get the EOC flag status.
  * @code
  * FlagStatus ADC1_FlagStatus;
  * ADC1_FlagStatus = ADC1_GetFlagStatus(ADC1_FLAG_EOC);
  * @endcode
  */
FlagStatus ADC1_GetFlagStatus(ADC1_Flag_TypeDef Flag)
{
  u8 flagstatus = 0;
  u8 temp = 0;

  /* Check the parameters */
  assert_param(IS_ADC1_FLAG_OK(Flag));

  if ((Flag & 0x0F) == 0x01)
  {
    /* Get OVR flag status */
    flagstatus = (u8)(ADC1->CR3 & ADC1_CR3_OVR);
  }
  else if ((Flag & 0xF0) == 0x10)
  {
    /* Get analog watchdog channel status */
    temp = (u8)(Flag & 0x0F);
    if (temp < 8)
    {
      flagstatus = (u8)(ADC1->AWSRL & (1 << temp));
    }
    else
    {
      flagstatus = (u8)(ADC1->AWSRH & (1 << (temp - 8)));
    }
  }
  else  /* Get EOC | AWD flag status */
  {
    flagstatus = (u8)(ADC1->CSR & Flag);
  }
  return ((FlagStatus)flagstatus);

}

/**
  * @brief Clear the specified ADC1 Flag.
  * @param[in] Flag: ADC1 flag.
  * can be one of the values of @ref ADC1_Flag_TypeDef.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Clears the end of conversion Flag
  * @code
  * ADC1_ClearFlag();
  * @endcode
  */
void ADC1_ClearFlag(ADC1_Flag_TypeDef Flag)
{
  u8 temp = 0;

  /* Check the parameters */
  assert_param(IS_ADC1_FLAG_OK(Flag));

  if ((Flag & 0x0F) == 0x01)
  {
    /* Clear OVR flag status */
    ADC1->CR3 &= (u8)(~ADC1_CR3_OVR);
  }
  else if ((Flag & 0xF0) == 0x10)
  {
    /* Clear analog watchdog channel status */
    temp = (u8)(Flag & 0x0F);
    if (temp < 8)
    {
      ADC1->AWSRL &= (u8)(~((u8)1 << temp));
    }
    else
    {
      ADC1->AWSRH &= (u8)(~((u8)1 << (temp - 8)));
    }
  }
  else  /* Clear EOC | AWD flag status */
  {
    ADC1->CSR &= (u8) (~Flag);
  }
}

/**
  * @brief Returns the specified pending bit status
  * @param[in] ITPendingBit : the IT pending bit to check.
  * This parameter can be one of the following values:
  *    - ADC1_IT_AWD   : Analog WDG IT status
  *    - ADC1_IT_AWS0 : Analog channel 0 IT status
  *    - ADC1_IT_AWS1 : Analog channel 1 IT status
  *    - ADC1_IT_AWS2 : Analog channel 2 IT status
  *    - ADC1_IT_AWS3 : Analog channel 3 IT status
  *    - ADC1_IT_AWS4 : Analog channel 4 IT status
  *    - ADC1_IT_AWS5 : Analog channel 5 IT status
  *    - ADC1_IT_AWS6 : Analog channel 6 IT status
  *    - ADC1_IT_AWS7 : Analog channel 7 IT status
  *    - ADC1_IT_AWS8 : Analog channel 8 IT status
  *    - ADC1_IT_AWS9 : Analog channel 9 IT status
  *    - ADC1_IT_EOC    : EOC pending bit
  * @retval FlagStatus: status of the specified pending bit.
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Get the EOC bit status.
  * @code
  * ITStatus ADC1_ITStatus;
  * ADC1_ITStatus = ADC1_GetITStatus();
  * @endcode
  */
ITStatus ADC1_GetITStatus(ADC1_IT_TypeDef ITPendingBit)
{
  ITStatus itstatus = RESET;
  u8 temp = 0;

  /* Check the parameters */
  assert_param(IS_ADC1_ITPENDINGBIT_OK(ITPendingBit));

  if ((ITPendingBit & 0xF0) == 0x10)
  {
    /* Get analog watchdog channel status */
    temp = (u8)(ITPendingBit & 0x0F);
    if (temp < 8)
    {
      itstatus = (u8)(ADC1->AWSRL & (u8)((u8)1 << temp));
    }
    else
    {
      itstatus = (u8)(ADC1->AWSRH & (u8)((u8)1 << (temp - 8)));
    }
  }
  else  /* Get EOC | AWD flag status */
  {
    itstatus = (u8)(ADC1->CSR & ITPendingBit);
  }
  return ((ITStatus)itstatus);

}

/**
  * @brief Clear the ADC1 End of Conversion pending bit.
  * @param[in] ITPendingBit : the IT pending bit to clear.
  * This parameter can be one of the following values:
  *    - ADC1_IT_AWD   : Analog WDG IT status
  *    - ADC1_IT_AWS0 : Analog channel 0 IT status
  *    - ADC1_IT_AWS1 : Analog channel 1 IT status
  *    - ADC1_IT_AWS2 : Analog channel 2 IT status
  *    - ADC1_IT_AWS3 : Analog channel 3 IT status
  *    - ADC1_IT_AWS4 : Analog channel 4 IT status
  *    - ADC1_IT_AWS5 : Analog channel 5 IT status
  *    - ADC1_IT_AWS6 : Analog channel 6 IT status
  *    - ADC1_IT_AWS7 : Analog channel 7 IT status
  *    - ADC1_IT_AWS8 : Analog channel 8 IT status
  *    - ADC1_IT_AWS9 : Analog channel 9 IT status
  *    - ADC1_IT_EOC    : EOC pending bit
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Clears the end of conversion pending bit
  * @code
  * ADC1_ClearITPendingBit(ADC1_ITPENDINGBIT_EOC);
  * @endcode
  */
void ADC1_ClearITPendingBit(ADC1_IT_TypeDef ITPendingBit)
{
  u8 temp = 0;

  /* Check the parameters */
  assert_param(IS_ADC1_ITPENDINGBIT_OK(ITPendingBit));

  if ((ITPendingBit& 0xF0) == 0x10)
  {
    /* Clear analog watchdog channel status */
    temp = (u8)(ITPendingBit & 0x0F);
    if (temp < 8)
    {
      ADC1->AWSRL &= (u8)(~((u8)1 << temp));
    }
    else
    {
      ADC1->AWSRH &= (u8)(~((u8)1 << (temp - 8)));
    }
  }
  else  /* Clear EOC | AWD flag status */
  {
    ADC1->CSR &= (u8) (~ITPendingBit);
  }
}

/**
  * @}
  */

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

⌨️ 快捷键说明

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