📄 75x_adc.c
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name : 75x_adc.c
* Author : MCD Application Team
* Date First Issued : 03/10/2006 : V0.1
* Description : This file provides all the ADC software functions.
********************************************************************************
* History:
* 03/10/2006 : V0.1
********************************************************************************
* 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.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "75x_adc.h"
#include "75x_mrcc.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Mask for Power Down Mode */
#define ADC_PowerDown_Enable 0x8000
#define ADC_PowerDown_Disable 0x7FFF
/* Mask for Watchdog Thresholds Enable */
#define ADC_AnalogWatchdog_Enable 0x8000
#define ADC_AnalogWatchdog_Disable 0x7FFF
/* Mask for Injected conversion start */
#define ADC_Injec_ConversionStart 0x8000
/* DMA enable */
#define ADC_DMA_ExtEnable_Mask 0x4000
/* Injected start trigger enable */
#define ADC_Injec_ExtTrigger_Enable 0x4000
/* ADC Masks */
#define ADC_DMAFirstEnabledChannel_Mask 0x000F
#define ADC_DataRegisterOffset 0x0050
#define ADC_FirstChannel_Mask 0xFFF0
#define ADC_ChannelNumber_Mask 0xFC3F
#define ADC_Threshold_Mask 0xFC00
#define ADC_AnalogWatchdogChannel_Mask 0xC3FF
#define ADC_Prescalers_Mask 0x7F18
#define ADC_SPEN_Mask 0x8000
#define ADC_FallingEdge_Mask 0xEFFF
#define ADC_LowLevel_Mask 0x4000
#define ADC_HighLevel_Mask 0xDFFF
#define ADC_Calibration_Mask 0x0002
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : ADC_DeInit
* Description : Deinitializes the ADC peripheral registers to their default
* reset values.
* Input : None.
* Output : None
* Return : None.
*******************************************************************************/
void ADC_DeInit(void)
{
/* Reset the ADC registers values*/
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_ADC,ENABLE);
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_ADC,DISABLE);
}
/*******************************************************************************
* Function Name : ADC_Init
* Description : Initializes the ADC peripheral according to the specified
* parameters in the ADC_InitStruct.
* Input : - ADC_InitStruct: pointer to an ADC_InitTypeDef structure that
contains the configuration information for the ADC peripheral.
* Output : None
* Return : None
*******************************************************************************/
void ADC_Init(ADC_InitTypeDef* ADC_InitStruct)
{
/* Configure the conversion mode */
if(ADC_InitStruct->ADC_ConversionMode == ADC_ConversionMode_Scan)
{
/* Set the scan conversion mode */
ADC->CLR2 |= ADC_ConversionMode_Scan;
}
else
{
/* Set the one-shot conversion mode */
ADC->CLR2 &= ADC_ConversionMode_OneShot;
}
/* Configure the external start conversion trigger */
switch(ADC_InitStruct->ADC_ExtTrigger)
{
case ADC_ExtTrigger_HighLevel:
/* Start conversion on High level of the external trigger (TIM0) */
ADC->CLR0 &= ADC_HighLevel_Mask;
ADC->CLR0 |= ADC_ExtTrigger_HighLevel;
break;
case ADC_ExtTrigger_LowLevel:
/* Start conversion on low level of the external trigger (TIM0) */
ADC->CLR0 &= ADC_ExtTrigger_LowLevel;
ADC->CLR0 |= ADC_LowLevel_Mask;
break;
case ADC_ExtTrigger_RisingEdge:
/* Start conversion on rising edge of the external trigger (TIM0) */
ADC->CLR0 |= ADC_ExtTrigger_RisingEdge;
break;
case ADC_ExtTrigger_FallingEdge:
/* Start conversion on falling edge of the external trigger (TIM0) */
ADC->CLR0 &= ADC_FallingEdge_Mask;
ADC->CLR0 |= ADC_ExtTrigger_FallingEdge;
break;
case ADC_ExtTrigger_Disable:
/* Disable the external trigger and start the conversion by software */
ADC->CLR0 &= ADC_ExtTrigger_Disable;
break;
default:
break;
}
/* Configure the auto clock off feature */
if (ADC_InitStruct->ADC_AutoClockOff == ADC_AutoClockOff_Enable)
{
/* Enable the auto clock off feature */
ADC->CLR4 |= ADC_AutoClockOff_Enable;
}
else
{
/* Disable the auto clock off feature */
ADC->CLR4 &= ADC_AutoClockOff_Disable;
}
/* Clear conversion prescaler CNVP[2:0], sampling prescaler SMPP[2:0] bits
and Sample prescaler enable SPEN bit */
ADC->CLR1 &= ADC_Prescalers_Mask;
/* Set conversion prescaler value (sampling and conversion prescalers are equal
while SPEN bit is reset */
ADC->CLR1 |= (ADC_InitStruct->ADC_ConversionPrescaler<<5);
/* In case ADC_SamplingPrescaler member is different from the conversion one */
if(ADC_InitStruct->ADC_SamplingPrescaler != ADC_InitStruct->ADC_ConversionPrescaler)
{
/* Set the sampling prescaler value */
ADC->CLR1 |= ADC_InitStruct->ADC_SamplingPrescaler;
/* Set SPEN bit (sampling and conversion prescalers are different */
ADC->CLR1 = (ADC->CLR1 | ADC_SPEN_Mask);
}
/* Clear first channel to be converted FCH[3:0] bits */
ADC->CLR2 &= ADC_FirstChannel_Mask;
/* Set the first channel to be converted */
ADC->CLR2 |= ADC_InitStruct->ADC_FirstChannel;
/* Clear number of channels to be converted NCH[3:0] bits */
ADC->CLR2 &= ADC_ChannelNumber_Mask;
/* Set the number of channels to be converted */
ADC->CLR2 |= ((ADC_InitStruct->ADC_ChannelNumber)-1<<6);
}
/*******************************************************************************
* Function Name : ADC_StructInit
* Description : Fills each ADC_InitStruct member with its default value.
* Input : - ADC_InitStruct: pointer to an ADC_InitTypeDef structure
which will be initialized.
* Output : None
* Return : None.
*******************************************************************************/
void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
{
/* Initialize the ADC_ConversionMode member */
ADC_InitStruct->ADC_ConversionMode = ADC_ConversionMode_OneShot;
/* Initialize the ADC_ExtTrigger member */
ADC_InitStruct->ADC_ExtTrigger = ADC_ExtTrigger_Disable;
/* Initialize the ADC_AutoClockOff member */
ADC_InitStruct->ADC_AutoClockOff = ADC_AutoClockOff_Disable;
/* Initialize the ADC_SamplingPrescaler member */
ADC_InitStruct->ADC_SamplingPrescaler = 0;
/* Initialize the ADC_ConversionPrescaler member */
ADC_InitStruct->ADC_ConversionPrescaler = 0;
/* Initialize the ADC_FirstChannel member */
ADC_InitStruct->ADC_FirstChannel = ADC_CHANNEL0;
/* Initialize the ADC_ChannelNumber member */
ADC_InitStruct->ADC_ChannelNumber = 1;
}
/*******************************************************************************
* Function Name : ADC_StartCalibration
* Description : Starts the ADC Calibration. Calibration average enabled/disabled.
* Input : - ADC_CalibAverage: Enables or disables ADC calibration average.
* This parameter can be one of the following values:
* - ADC_CalibAverage_Enable: enable calibration average
* - ADC_CalibAverage_Disable: disable calibration average
* Output : None
* Return : None
*******************************************************************************/
void ADC_StartCalibration(u16 ADC_CalibAverage)
{
if (ADC_CalibAverage == ADC_CalibAverage_Enable)
{
/* Enable ADC Calibration Average */
ADC->CLR4 &= ADC_CalibAverage_Enable;
}
else
{
/* Disable ADC Calibration Average */
ADC->CLR4 |= ADC_CalibAverage_Disable;
}
/* Start Calibration */
ADC->CLR0 |= ADC_Calibration_ON;
}
/*******************************************************************************
* Function Name : ADC_GetCalibrationStatus
* Description : Get the ADC Calibration Status.
* Input : None
* Output : None
* Return : The NewState of the ADC calibration (SET or RESET).
*******************************************************************************/
FlagStatus ADC_GetCalibrationStatus(void)
{
/* Check the status of the ADC calibration */
if((ADC->CLR0 & ADC_Calibration_Mask) != RESET)
{
/* Return SET if ADC Calibration is on going */
return SET;
}
else
{
/* Return RESET if ADC Calibration is finished */
return RESET;
}
}
/*******************************************************************************
* Function Name : ADC_ConversionCmd
* Description : Starts or stops the ADC conversion.
* Input : - ADC_Conversion: specifies the ADC command to apply.
* This parameter can be one of the following values:
* - ADC_Conversion_Start: start conversion
* - ADC_Conversion_Stop: stop conversion
* Output : None
* Return : None
*******************************************************************************/
void ADC_ConversionCmd (u16 ADC_Conversion)
{
if (ADC_Conversion == ADC_Conversion_Start)
{
/* Start the ADC Conversion */
ADC->CLR0 |= ADC_Conversion_Start;
}
else
{
/* Stop the ADC Conversion */
ADC->CLR0 &= ADC_Conversion_Stop;
}
}
/*******************************************************************************
* Function Name : ADC_GetSTARTBitStatus
* Description : Gets the ADC START/STOP bit Status.
* Input : None
* Output : None
* Return : The NewState of the ADC START/STOP bit (SET or RESET).
*******************************************************************************/
FlagStatus ADC_GetSTARTBitStatus(void)
{
/* Check the status of the ADC START/STOP bit */
if((ADC->CLR0 & ADC_Conversion_Start) != RESET)
{
/* Return SET if ADC Conversion is started */
return SET;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -