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

📄 91x_adc.c

📁 a set or ARM9 examples by STM
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 91x_adc.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides all the ADC software functions.
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
* CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS
* A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
* OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/


/* Standard include ----------------------------------------------------------*/
#include "91x_adc.h"
#include "91x_scu.h"
/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* ADC mask */
#define   ADC_FLAG_MASK           0x001F     /* ADC Flag Mask           */
#define   ADC_RESULT_MASK         0x03FF     /* ADC Result Mask         */
#define   ADC_SCAN_MODE_MASK      0x0020     /* ADC Sacn Mode Mask      */
#define   ADC_STANDBY_MODE_MASK   0x0008     /* ADC Standby Mode Mask   */
#define   ADC_CMD_MASK            0x0002     /* ADC Command Mask        */
#define   ADC_CHANNEL_MASK        0xFE3F     /* ADC Channel Select Mask */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : ADC_DeInit
* Description    : Deinitialize the ADC module registers to their default reset
*                  values
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void ADC_DeInit(void)
{
  /* Reset the ADC registers values */
  SCU_APBPeriphReset(__ADC,ENABLE);
  SCU_APBPeriphReset(__ADC,DISABLE);
}

/*******************************************************************************
* Function Name  : ADC_Init
* Description    : Initializes ADC  peripheral according to the specified
*                  parameters in the ADC_InitTypeDef structure.
* Input          : ADC_InitStruct: pointer to a ADC_InitTypeDef structure that
*                  contains the configuration information for the specified
*                  ADC peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void ADC_Init(ADC_InitTypeDef* ADC_InitStruct)
{
  /* Set the low threshold of the watchdog */
  ADC->LTR = ADC_InitStruct->ADC_WDG_Low_Threshold;

  /* Set the high threshold of the watchdog */
  ADC->HTR = ADC_InitStruct->ADC_WDG_High_Threshold;

    
  /* Channel 0 conversion mode */
  ADC->CCR &= 0xFFFC; 
  ADC->CCR |= ADC_InitStruct->ADC_Channel_0_Mode;
  
  /* Channel 1 conversion mode */
  ADC->CCR &= 0xFFF3; 
  ADC->CCR |= ADC_InitStruct->ADC_Channel_1_Mode << 0x2;

  /* Channel 2 conversion mode */
  ADC->CCR &= 0xFFCF;
  ADC->CCR |= ADC_InitStruct->ADC_Channel_2_Mode << 0x4;
 
  /* Channel 3 conversion mode */
  ADC->CCR &= 0xFF3F; 
  ADC->CCR |= ADC_InitStruct->ADC_Channel_3_Mode << 0x6;

  /* Channel 4 conversion mode */
  ADC->CCR &= 0xFCFF; 
  ADC->CCR |= ADC_InitStruct->ADC_Channel_4_Mode << 0x8;

  /* Channel 5 conversion mode */
  ADC->CCR &= 0xF3FF; 
  ADC->CCR |= ADC_InitStruct->ADC_Channel_5_Mode << 0xA;

  /* Channel 6 conversion mode */
  ADC->CCR &= 0xCFFF; 
  ADC->CCR |= ADC_InitStruct->ADC_Channel_6_Mode << 0xC;

  /* Channel 7 conversion mode */
  ADC->CCR &= 0x3FFF; 
  ADC->CCR |= ADC_InitStruct->ADC_Channel_7_Mode << 0xE;

  /* Select the channel to be converted */
  ADC->CR &= ADC_CHANNEL_MASK;
  ADC->CR |= ADC_InitStruct->ADC_Select_Channel << 0x6;

  /* Enable/disable the scan mode */
  if (ADC_InitStruct->ADC_Scan_Mode == ENABLE)
  {
    /* Enable the scan mode */
    ADC->CR |= ADC_SCAN_MODE_MASK;
  }
  else
  {
    /* Disable the scan mode */
    ADC->CR &= ~ADC_SCAN_MODE_MASK;
  }

  /* Configure the conversion mode */
  if (ADC_InitStruct->ADC_Conversion_Mode == ADC_Continuous_Mode)
  {
    /* ADC continuous mode */
    ADC->CR |= ADC_Continuous_Mode;
  }
  else
  {
    /* ADC single mode */
    ADC->CR &= ADC_Single_Mode;
  }
}

/*******************************************************************************
* Function Name  : ADC_StructInit
* Description    : Fills each ADC_InitStruct member with its reset value.
* Input          : ADC_InitStruct : pointer to a ADC_InitTypeDef structure
*                   which will be initialized.
* Output         : None
* Return         : None.
*******************************************************************************/
void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
{
  ADC_InitStruct->ADC_WDG_High_Threshold = 0x0000;
  ADC_InitStruct->ADC_WDG_Low_Threshold  = 0x0000;
  ADC_InitStruct->ADC_Channel_0_Mode     = ADC_No_Conversion;
  ADC_InitStruct->ADC_Channel_1_Mode     = ADC_No_Conversion;
  ADC_InitStruct->ADC_Channel_2_Mode     = ADC_No_Conversion;
  ADC_InitStruct->ADC_Channel_3_Mode     = ADC_No_Conversion;
  ADC_InitStruct->ADC_Channel_4_Mode     = ADC_No_Conversion;
  ADC_InitStruct->ADC_Channel_5_Mode     = ADC_No_Conversion;
  ADC_InitStruct->ADC_Channel_6_Mode     = ADC_No_Conversion;
  ADC_InitStruct->ADC_Channel_7_Mode     = ADC_No_Conversion;
  ADC_InitStruct->ADC_Select_Channel     = ADC_Channel_0;
  ADC_InitStruct->ADC_Scan_Mode          = DISABLE;
  ADC_InitStruct->ADC_Conversion_Mode    = ADC_Single_Mode;
}

/*******************************************************************************
* Function Name  : ADC_PrescalerConfig
* Description    : This routine is used to configure the ADC prescaler value.
* Input          : ADC_Prescaler: specifies the prescaler value. This parameter
*                  can be a value from 0x0 to 0xFF.
* Output         : None
* Return         : None
*******************************************************************************/
void ADC_PrescalerConfig(u8 ADC_Prescaler)
{
  ADC->PRS &= 0xFF00;
  ADC->PRS |= ADC_Prescaler;

}
/*******************************************************************************
* Function Name  : ADC_GetPrescalerValue
* Description    : This routine is used to get the ADC prescaler value.
* Input          : None
* Output         : None
* Return         : The prescaler value.
*******************************************************************************/
u8 ADC_GetPrescalerValue(void)
{
  return ADC->PRS & 0x00FF;
}
/*******************************************************************************
* Function Name  : ADC_GetFlagStatus
* Description    : Checks whether the specified ADC flag is set or not.
* Input          : ADC_Flag: flag to check.
*                  This parameter can be one of the following values:
*                     - ADC_FLAG_OV_CH_0: Conversion overflow status for 
*                                         channel 0.
*                     - ADC_FLAG_OV_CH_1: Conversion overflow status for
*                                         channel 1.
*                     - ADC_FLAG_OV_CH_2: Conversion overflow status for
*                                         channel 2.
*                     - ADC_FLAG_OV_CH_3: Conversion overflow status for
*                                         channel 3.
*                     - ADC_FLAG_OV_CH_4: Conversion overflow status for
*                                         channel 4.
*                     - ADC_FLAG_OV_CH_5: Conversion overflow status for
*                                         channel 5.
*                     - ADC_FLAG_OV_CH_6: Conversion overflow status for
*                                         channel 6.
*                     - ADC_FLAG_OV_CH_7: Conversion overflow status for
*                                         channel 7.
*                     - ADC_FLAG_ECV:     End of conversion status.
*                     - ADC_FLAG_AWD:     Analog watchdog status.
* Output         : None
* Return         : The NewState of the ADC_Flag (SET or RESET).
*******************************************************************************/
FlagStatus ADC_GetFlagStatus(u16 ADC_Flag)
{
  u8 AdcReg = 0, FlagPos = 0;
  
  /* Get the ADC register index */
  AdcReg = ADC_Flag >> 5;

  /* Get the flag position */
  FlagPos = ADC_Flag & ADC_FLAG_MASK;

  if(AdcReg == 1) /* The flag to check is in CR register */
  {
    if((ADC->CR & (1<<FlagPos))!= RESET)
    {
      return SET;
    }
    else
    {
      return RESET;
    }
  }
  else if(AdcReg == 6) /* The flag to check is in DR0 register */
  {
    if((ADC->DR0 & (1<<FlagPos))!= RESET)
    {
      return SET;
    }
    else
    {
      return RESET;
    }
  }
  else if(AdcReg == 7) /* The flag to check is in DR1 register */
  {
    if((ADC->DR1 & (1<<FlagPos))!= RESET)
    {
      return SET;
    }
    else
    {
      return RESET;
    }
  }
  else if(AdcReg == 8) /* The flag to check is in DR2 register */
  {
    if((ADC->DR2 & (1<<FlagPos))!= RESET)
    {
      return SET;
    }
    else
    {
      return RESET;
    }
  }
  else if(AdcReg == 9) /* The flag to check is in DR3 register */
  {
    if((ADC->DR3 & (1<<FlagPos))!= RESET)
    {
      return SET;
    }
    else
    {
      return RESET;
    }
  }

  else if(AdcReg == 0xA) /* The flag to check is in DR4 register */
  {
    if((ADC->DR4 & (1<<FlagPos))!= RESET)
    {
      return SET;
    }
    else
    {
      return RESET;
    }
  }
  else if(AdcReg == 0xB) /* The flag to check is in DR5 register */
  {
    if((ADC->DR5 & (1<<FlagPos))!= RESET)
    {
      return SET;
    }
    else
    {

⌨️ 快捷键说明

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