标签:stm32
//==文件adc.h============================================================
#ifndef __ADC_H
#define __ADC_H
#include "stm32f0xx.h"
#define Adc_Channel_Num 4 //ADC通道数目
//========各通道在结果列表中的位置===================
#define Adc_Data_IS 0
#define Adc_Data_Bat 1
#define Adc_Data_Vr 2
#define Adc_Data_NTC 3
//=======AD通道定义==========================================
#define ADC_CHANNEL_IS ADC_Channel_5 //电流检测
#define ADC_CHANNEL_BAT ADC_Channel_1 //电池电压检测
#define ADC_CHANNEL_VR ADC_Channel_0 //电位器通道
#define ADC_CHANNEL_NTC ADC_Channel_2 //NTC通道
//=========================================
extern __IO uint16_t RegularConvData_Tab[Adc_Channel_Num];
//extern uint16_t R_AdcResult_Tab[Adc_Channel_Num] ;
extern uint32_t Adc_Channel_Tab[Adc_Channel_Num] ;
extern uint32 Adc_Index ;
extern void ADC_GPIO_Init(void) ;
extern void ADC1_Init(void);
extern uint16 Adc_Switch(uint32 ADC_Channel) ;
extern void Adc_StartSwitch(uint32 ADC_Channel) ;
#endif /* __ADC_H */
//==文件adc.c============================================================
#include "global.h"
#include "adc.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address 0x40012440
__IO uint16_t RegularConvData_Tab[Adc_Channel_Num]; //adc转换结果表
//uint16_t R_AdcResult_Tab[Adc_Channel_Num]; //adc转换结果表-最终取值表
uint32_t Adc_Channel_Tab[Adc_Channel_Num]={ADC_CHANNEL_IS,ADC_CHANNEL_BAT,ADC_CHANNEL_VR,ADC_CHANNEL_NTC} ;//adc转换通道表
uint32 Adc_Index = 0 ; //adc转换索引
/*******************************************************************************
* 函数名称: ADC_GPIO_Init();
* 功能描述: ADC--GPIO输入引脚配置---在此可以设置16路外部输入通道
* 输入参数: void
* 返回参数: 无
********************************************************************************/
void ADC_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
//==================VR========================PA4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_5 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //AD无需要上下拉
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 做输入时不用设置速率
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*******************************************************************************
* 函数名称: ADC1_Init
* 功能描述:
* 输入参数: void
* 返回参数: 无
********************************************************************************/
void ADC1_Init(void)
{
ADC_InitTypeDef ADC_InitStruct;
// DMA_InitTypeDef DMA_InitStruct;
/* ADC1 DeInit */
ADC_DeInit(ADC1);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4) ; //时钟分频48M/4=12M 最大时钟不超过14M
// /* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
/* Enable ADC_DMA */
// ADC_DMACmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, DISABLE);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStruct);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruct.ADC_ContinuousConvMode = DISABLE ;//ENABLE;
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward;//ADC_ScanDirection_Backward;
ADC_Init(ADC1, &ADC_InitStruct);
// /* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */
// ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles);
// ADC_TempSensorCmd(ENABLE);
/* Convert the ADC1 Vref with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_0 , ADC_SampleTime_55_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_1 , ADC_SampleTime_55_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_2 , ADC_SampleTime_55_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_5 , ADC_SampleTime_55_5Cycles);
// ADC_VrefintCmd(ENABLE);
/* ADC Calibration 校验 */
ADC_GetCalibrationFactor(ADC1);
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
}
/*******************************************************************************
* 函数名称: Adc_Switch
* 功能描述: ADC转换
* 输入参数: ADC转换通道标号
* 返回参数: 该通道的转换结果
********************************************************************************/
uint16 Adc_Switch(uint32 ADC_Channel)
{
ADC1->CHSELR = 0 ;
ADC1->CHSELR = (uint32_t)ADC_Channel; //选择通道
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1); //启动转换
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) ; //等级转换
return ADC_GetConversionValue(ADC1) ;
}
/*******************************************************************************
* 函数名称: Adc_StartSwitch
* 功能描述: ADC转换
* 输入参数: ADC转换通道标号
* 返回参数: 该通道的转换结果
********************************************************************************/
void Adc_StartSwitch(uint32 ADC_Channel)
{
ADC1->CHSELR = 0 ;
ADC1->CHSELR |= (uint32_t)ADC_Channel; //选择通道
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1); //启动转换
// while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) ; //等级转换
// return ADC_GetConversionValue(ADC1) ;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:stm32
原文地址:http://blog.csdn.net/libiaojs/article/details/46988943