ADC读取供电电压值与内部温度传感器值.两路连续转换模式.
vc_adc.c
- #include "vc_adc.h"
- //__IO u16 ADC_ConvertedValue[4][2];
- u16 ADC_ConvertedValue[4][2];
- /* 这里的ADC是暂时用来试验两路AD转换,一路是测量电压,另一路是读取内部温度值
- ** 暂时是用来测试的,以后测量电流一路.*/
- void Vc_AdcInit(void){
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- RCC_APB2PeriphClockCmd (RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA ,ENABLE );
- RCC_ADCCLKConfig (RCC_PCLK2_Div6 ); /* 72M/6=12M */
-
- GPIO_InitStructure .GPIO_Mode = GPIO_Mode_AIN ;
- GPIO_InitStructure .GPIO_Pin = VADC_PIN; // | CADC_PIN 这里暂时先检测电压这一路;
- /* 因为是输入,所以不需要设置速度 */
- GPIO_Init (ADC_PORT ,&GPIO_InitStructure );
- ADC_DeInit (ADC1 );
- ADC_InitStructure .ADC_Mode = ADC_Mode_Independent ; /* ADC独立模式 */
- //ADC_InitStructure .ADC_ScanConvMode = DISABLE ; /* 单通道模式*/
- ADC_InitStructure .ADC_ScanConvMode = ENABLE ; /* 多通道模式*/
- //ADC_InitStructure .ADC_ContinuousConvMode = DISABLE ; /* 单次模式*/
- ADC_InitStructure .ADC_ContinuousConvMode = ENABLE ; /* 连续模式*/
- ADC_InitStructure .ADC_ExternalTrigConv = ADC_ExternalTrigConv_None ;
- ADC_InitStructure .ADC_DataAlign = ADC_DataAlign_Right ;
- ADC_InitStructure .ADC_NbrOfChannel = 2; /* 设置ADC路数为2路*/
- ADC_Init (ADC1, &ADC_InitStructure );
- ADC_TempSensorVrefintCmd (ENABLE );
- ADC_RegularChannelConfig (ADC1 ,ADC_Channel_4 , 1, ADC_SampleTime_239Cycles5); /* 规则通道1*/
- ADC_RegularChannelConfig (ADC1 ,ADC_Channel_16 ,2, ADC_SampleTime_239Cycles5); /* 规则通道2*/
- ADC_DMACmd (ADC1 ,ENABLE );
- ADC_Cmd (ADC1 ,ENABLE );
- ADC_ResetCalibration (ADC1 );
- while(ADC_GetResetCalibrationStatus (ADC1));
- ADC_StartCalibration (ADC1 );
- while(ADC_GetCalibrationStatus (ADC1 ));
- ADC_SoftwareStartConvCmd (ADC1 ,ENABLE );
- }
- void DMA_Config(void){
-
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHBPeriphClockCmd (RCC_AHBPeriph_DMA1 ,ENABLE );
- DMA_DeInit (DMA1_Channel1);
- DMA_InitStructure .DMA_PeripheralBaseAddr = (u32)&(ADC1 ->DR);
- DMA_InitStructure .DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue ;
- DMA_InitStructure .DMA_DIR = DMA_DIR_PeripheralSRC ; /* 外设作为数据来源*/
- DMA_InitStructure .DMA_BufferSize =
- DMA_InitStructure .DMA_PeripheralInc = DMA_PeripheralInc_Disable ; /* 外设地址固定*/
- DMA_InitStructure .DMA_MemoryInc = DMA_MemoryInc_Enable ; /* 内存地址增加*/
- DMA_InitStructure .DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord ; /* 16bit*/
- DMA_InitStructure .DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord ;
- DMA_InitStructure .DMA_Mode = DMA_Mode_Circular ;
- DMA_InitStructure .DMA_Priority = DMA_Priority_High ;
- DMA_InitStructure .DMA_M2M = DMA_M2M_Disable ;
- DMA_Init (DMA1_Channel1 ,&DMA_InitStructure );
-
- DMA_Cmd (DMA1_Channel1 ,ENABLE );
- }
复制代码
vc_adc.h
- #ifndef _vc_adc_H
- #define _vc_adc_H
- #include "stm32f10x.h"
- /*电压ADC--PA4(ADC12-IN$)
- **电流ADC--PA3(ADC12-IN3)
- */
- #define VADC_PIN GPIO_Pin_4
- #define CADC_PIN GPIO_Pin_3
- #define ADC_PORT GPIOA
- extern void Vc_AdcInit(void);
- extern void DMA_Config(void);
- #endif
复制代码
|