📄 stm32f10x_adc.c
字号:
/**
******************************************************************************
* @文件 stm32f10x_adc.c
* @作者 MCD 应用程序组
* @版本 V3.1.0
* @日期 06/19/2009
* @简述 这个文件 提供所有 ADC 固件函数.
* @翻译 ANSON/sweet1985 Email:airanson110@126.com
* @日期 17/07/2009
******************************************************************************
* @复件
*
* 这个固件仅仅是提供给客户作为设计产品而编写程序的参考目的使客户节约时间。由于
* 客户使用本固件在开发产品编程上产生的结果意法半导体公司不承担任何直接的和间接
* 的责任,也不承担任何损害而引起的赔偿。
*
* <h2><center>&复制; 版权所有 2009 意法半导体公司</center></h2>
* 翻译版本仅供学习,如与英文原版有出入应以英文原版为准。
*/
/* 包含 ------------------------------------------------------------------*/
#include "stm32f10x_adc.h"
#include "stm32f10x_rcc.h"
/* 自用类型 --------------------------------------------------------------*/
/* 自用定义 --------------------------------------------------------------*/
/* ADC DISCNUM mask */
#define CR1_DISCNUM_Reset ((uint32_t)0xFFFF1FFF)
/* ADC DISCEN mask */
#define CR1_DISCEN_Set ((uint32_t)0x00000800)
#define CR1_DISCEN_Reset ((uint32_t)0xFFFFF7FF)
/* ADC JAUTO mask */
#define CR1_JAUTO_Set ((uint32_t)0x00000400)
#define CR1_JAUTO_Reset ((uint32_t)0xFFFFFBFF)
/* ADC JDISCEN mask */
#define CR1_JDISCEN_Set ((uint32_t)0x00001000)
#define CR1_JDISCEN_Reset ((uint32_t)0xFFFFEFFF)
/* ADC AWDCH mask */
#define CR1_AWDCH_Reset ((uint32_t)0xFFFFFFE0)
/* ADC Analog watchdog enable mode mask */
#define CR1_AWDMode_Reset ((uint32_t)0xFF3FFDFF)
/* CR1 register Mask */
#define CR1_CLEAR_Mask ((uint32_t)0xFFF0FEFF)
/* ADC ADON mask */
#define CR2_ADON_Set ((uint32_t)0x00000001)
#define CR2_ADON_Reset ((uint32_t)0xFFFFFFFE)
/* ADC DMA mask */
#define CR2_DMA_Set ((uint32_t)0x00000100)
#define CR2_DMA_Reset ((uint32_t)0xFFFFFEFF)
/* ADC RSTCAL mask */
#define CR2_RSTCAL_Set ((uint32_t)0x00000008)
/* ADC CAL mask */
#define CR2_CAL_Set ((uint32_t)0x00000004)
/* ADC SWSTART mask */
#define CR2_SWSTART_Set ((uint32_t)0x00400000)
/* ADC EXTTRIG mask */
#define CR2_EXTTRIG_Set ((uint32_t)0x00100000)
#define CR2_EXTTRIG_Reset ((uint32_t)0xFFEFFFFF)
/* ADC Software start mask */
#define CR2_EXTTRIG_SWSTART_Set ((uint32_t)0x00500000)
#define CR2_EXTTRIG_SWSTART_Reset ((uint32_t)0xFFAFFFFF)
/* ADC JEXTSEL mask */
#define CR2_JEXTSEL_Reset ((uint32_t)0xFFFF8FFF)
/* ADC JEXTTRIG mask */
#define CR2_JEXTTRIG_Set ((uint32_t)0x00008000)
#define CR2_JEXTTRIG_Reset ((uint32_t)0xFFFF7FFF)
/* ADC JSWSTART mask */
#define CR2_JSWSTART_Set ((uint32_t)0x00200000)
/* ADC injected software start mask */
#define CR2_JEXTTRIG_JSWSTART_Set ((uint32_t)0x00208000)
#define CR2_JEXTTRIG_JSWSTART_Reset ((uint32_t)0xFFDF7FFF)
/* ADC TSPD mask */
#define CR2_TSVREFE_Set ((uint32_t)0x00800000)
#define CR2_TSVREFE_Reset ((uint32_t)0xFF7FFFFF)
/* CR2 register Mask */
#define CR2_CLEAR_Mask ((uint32_t)0xFFF1F7FD)
/* ADC SQx mask */
#define SQR3_SQ_Set ((uint32_t)0x0000001F)
#define SQR2_SQ_Set ((uint32_t)0x0000001F)
#define SQR1_SQ_Set ((uint32_t)0x0000001F)
/* SQR1 register Mask */
#define SQR1_CLEAR_Mask ((uint32_t)0xFF0FFFFF)
/* ADC JSQx mask */
#define JSQR_JSQ_Set ((uint32_t)0x0000001F)
/* ADC JL mask */
#define JSQR_JL_Set ((uint32_t)0x00300000)
#define JSQR_JL_Reset ((uint32_t)0xFFCFFFFF)
/* ADC SMPx mask */
#define SMPR1_SMP_Set ((uint32_t)0x00000007)
#define SMPR2_SMP_Set ((uint32_t)0x00000007)
/* ADC JDRx registers offset */
#define JDR_Offset ((uint8_t)0x28)
/* ADC1 DR register base address */
#define DR_ADDRESS ((uint32_t)0x4001244C)
/* 自用宏 -------------------------------------------------------------*/
/* 自用变量 -----------------------------------------------------------*/
/* 自用函数原型 -------------------------------------------------------*/
/* 自用函数 -----------------------------------------------------------*/
/**
* @简述 将外设 ADCx 的全部寄存器重设为缺省值.
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* @返回 没有
*/
void ADC_DeInit(ADC_TypeDef* ADCx)
{
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
if (ADCx == ADC1)
{
/* 使能 ADC1 复位状态 */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
/* 从复位状态释放 ADC1 */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
}
else if (ADCx == ADC2)
{
/* 使能 ADC2 复位状态 */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, ENABLE);
/* 从复位状态释放 ADC2 */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, DISABLE);
}
else
{
if (ADCx == ADC3)
{
/* 使能 ADC3 复位状态 */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, ENABLE);
/* 从复位状态释放 ADC3 */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, DISABLE);
}
}
}
/**
* @简述 根据 ADC_InitStruct 中指定的参数初始化外设 ADCx 的寄存器.
* @参数 ADCx: 其中x 可以是1 2或3,用来选择ADC 外围模块.
* @参数 ADC_InitStruct: 指向结构体 ADC_InitTypeDef 的指针,该结构包括了指定ADC外围模块的配置信息.
* @返回 没有
*/
void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
{
uint32_t tmpreg1 = 0;
uint8_t tmpreg2 = 0;
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_MODE(ADC_InitStruct->ADC_Mode));
assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));
assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));
assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel));
/*---------------------------- ADCx CR1 Configuration -----------------*/
/* 得到 ADCx CR1 的值 */
tmpreg1 = ADCx->CR1;
/* 清除 DUALMOD 和 SCAN 位 */
tmpreg1 &= CR1_CLEAR_Mask;
/* 配置 ADCx: 双重模式和扫描转换模式 */
/* 设置 DUALMOD 位, 按照 ADC_Mode 的值 */
/* 设置 SCAN 位,按照 ADC_ScanConvMode 的值 */
tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_Mode | ((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8));
/* 写 ADCx CR1 */
ADCx->CR1 = tmpreg1;
/*---------------------------- ADCx CR2 Configuration -----------------*/
/* 得到 ADCx CR2 的值 */
tmpreg1 = ADCx->CR2;
/* 清除 CONT, ALIGN 和 EXTSEL 位 */
tmpreg1 &= CR2_CLEAR_Mask;
/* 配置 ADCx: 外触发事件和连续转换模式 */
/* 设置 ALIGN 位,按照 ADC_DataAlign 的值 */
/* 设置 EXTSEL 位,按照 ADC_ExternalTrigConv 的值 */
/* 设置 CONT 位,按照 ADC_ContinuousConvMode 的值 */
tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv |
((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
/* 写 ADCx CR2 */
ADCx->CR2 = tmpreg1;
/*---------------------------- ADCx SQR1 Configuration -----------------*/
/* 得到 ADCx SQR1 的值 */
tmpreg1 = ADCx->SQR1;
/* 清除 L 位 */
tmpreg1 &= SQR1_CLEAR_Mask;
/* 配置 ADCx: 规则信道长度 */
/* 设置 L 位,按照 ADC_NbrOfChannel 的值 */
tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1);
tmpreg1 |= (uint32_t)tmpreg2 << 20;
/* 写 ADCx SQR1 */
ADCx->SQR1 = tmpreg1;
}
/**
* @简述 把 ADC_InitStruct 中的每一个参数按缺省值填入.
* @参数 ADC_InitStruct : 指向结构体 ADC_InitTypeDef 的指针,用来初始化.
* @返回 没有
*/
void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
{
/* 复位 ADC 初始结构变量 */
/* 初始化 ADC_Mode 成员 */
ADC_InitStruct->ADC_Mode = ADC_Mode_Independent;
/* 初始化 ADC_ScanConvMode 成员 */
ADC_InitStruct->ADC_ScanConvMode = DISABLE;
/* 初始化 ADC_ContinuousConvMode 成员 */
ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
/* 初始化 ADC_ExternalTrigConv 成员 */
ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
/* 初始化 ADC_DataAlign 成员 */
ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
/* 初始化 ADC_NbrOfChannel 成员 */
ADC_InitStruct->ADC_NbrOfChannel = 1;
}
/**
* @简述 使能或者失能指定的 ADC .
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* @参数 NewState: ADCx 设备的新状态.
* 这个参数可以是: ENABLE 或 DISABLE.
* @返回 没有
*/
void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* 设置 ADON 位,从省电模式下唤醒ADC */
ADCx->CR2 |= CR2_ADON_Set;
}
else
{
/* 失能指定的 ADC 设备 */
ADCx->CR2 &= CR2_ADON_Reset;
}
}
/**
* @简述 使能或失能指定的 ADC DMA 请求.
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* 注解: ADC2 没有 DMA 能力.
* @参数 NewState: 选定的 ADC DMA 转移器的新状态.
* 这个参数可以是: ENABLE 或 DISABLE.
* @返回 没有
*/
void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* 检查参数 */
assert_param(IS_ADC_DMA_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* 使能选定的 ADC DMA 请求 */
ADCx->CR2 |= CR2_DMA_Set;
}
else
{
/* 失能选定的 ADC DMA 请求 */
ADCx->CR2 &= CR2_DMA_Reset;
}
}
/**
* @简述 使能或者失能指定的ADC的中断.
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* @参数 ADC_IT: 指定的 ADC 中断源.
* 可以使用下述值的一个或者几个值的组合:
* ADC_IT_EOC: 换算结束中断屏蔽
* ADC_IT_AWD: 模拟看门狗中断屏蔽
* ADC_IT_JEOC: 注入通道完成中断屏蔽
* @参数 NewState: 指定的 ADC 中断的新状态.
* 这个参数可以是: ENABLE 或 DISABLE.
* @返回 没有
*/
void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
{
uint8_t itmask = 0;
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
assert_param(IS_ADC_IT(ADC_IT));
/* 得到 ADC IT 中断号 */
itmask = (uint8_t)ADC_IT;
if (NewState != DISABLE)
{
/* 使能选定的 ADC 中断 */
ADCx->CR1 |= itmask;
}
else
{
/* 失能指定的 ADC 中断 */
ADCx->CR1 &= (~(uint32_t)itmask);
}
}
/**
* @简述 重置指定的ADC的校准寄存器.
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* @返回 没有
*/
void ADC_ResetCalibration(ADC_TypeDef* ADCx)
{
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
/* 重置指定的ADC的校准寄存器 */
ADCx->CR2 |= CR2_RSTCAL_Set;
}
/**
* @简述 获取ADC重置校准寄存器的状态.
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* @返回 ADC重置校准寄存器的新状态 (SET 或 RESET).
*/
FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx)
{
FlagStatus bitstatus = RESET;
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
/* 检查 RSTCAL 位的状态 */
if ((ADCx->CR2 & CR2_RSTCAL_Set) != (uint32_t)RESET)
{
/* 设置 RSTCAL 位 */
bitstatus = SET;
}
else
{
/* 复位 RSTCAL 位 */
bitstatus = RESET;
}
/* 返回 RSTCAL 位的状态 */
return bitstatus;
}
/**
* @简述 开始指定ADC的校准状态.
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* @返回 没有
*/
void ADC_StartCalibration(ADC_TypeDef* ADCx)
{
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
/* 使能选定的 ADC 校准处理 */
ADCx->CR2 |= CR2_CAL_Set;
}
/**
* @简述 获取指定ADC的校准状态.
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* @返回 指定ADC的校准的新状态 (SET 或 RESET).
*/
FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx)
{
FlagStatus bitstatus = RESET;
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
/* 检查 CAL 位的状态 */
if ((ADCx->CR2 & CR2_CAL_Set) != (uint32_t)RESET)
{
/* 设置 CAL 位: 较准进行中 */
bitstatus = SET;
}
else
{
/* 复位 CAL 位: 校准结束 */
bitstatus = RESET;
}
/* 返回 CAL 位的状态 */
return bitstatus;
}
/**
* @简述 使能或者失能指定的ADC的软件转换启动功能.
* @参数 ADCx: 其中x可以是1、2或3,用来选择ADC外围模块.
* @参数 NewState: 选中的由软件发出开始信号的ADC的新状态.
* 这个参数可以是: ENABLE 或 DISABLE.
* @返回 没有
*/
void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* 检查参数 */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -