#include "adc_driver.h"
#include "time_driver.h"
#define ADC1_DR_ADDRESS ((uint32_t)0x40012458)
__IO uint16_t ADC_ConvertedValue[2][10]; //采样数组,两路,每一路采样十次
float Battery_ADC,Photoactor_ADC,Photoactor_ADC1, Battery_ADC1; 实际 电压采样变量
/*
* */
void ADC_DMA_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure; //1
GPIO_InitTypeDef GPIO_InitStructure; //1
DMA_InitTypeDef DMA_InitStructure; //1
/*------------------------ DMA1 configuration ------------------------------*/
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //
/* DMA1 channel1 configuration */
DMA_DeInit(DMA1_Channel1); //DAMͨµÀ³õʼ»¯
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS; //
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_ConvertedValue;//
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //´
DMA_InitStructure.DMA_BufferSize = N*M; //
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //
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); //
/*----------------- ADC1 configuration with DMA enabled --------------------*/
/* Enable the HSI oscillator */
RCC_HSICmd(ENABLE); //
/* Enable GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //3
/* Configure PA.1 (ADC Channel1) in analog mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //5
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure); //
/*Enable GPIOB clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//4
/* ADC1 configuration */ //
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //
ADC_InitStructure.ADC_ContinuousConvMode =ENABLE; //
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //
ADC_InitStructure.ADC_NbrOfConversion = 2; //
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel18 configuration */ //
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_384Cycles ); //
ADC_RegularChannelConfig(ADC1, ADC_Channel_21 ,2,ADC_SampleTime_384Cycles );
/* Enable the request after last transfer for DMA Circular mode */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //ʹÄÜADC_DAM
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); //ʹÄÜADC
/* Wait until the ADC1 is ready */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
{
}
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConv(ADC1);
}
/*»采样二进制转换成十进制函数*/
float GetVolt(float ADC_ConvertedValue)
{
return(float)(ADC_ConvertedValue*3.3/4096);
}
float sum_guang,sum_volt; //平均电压变样
/ 前5次光敏,电池电压数据总和函数
void filter (void)
{ int count;
for(count=0;count<N;count=count+2)
{
sum_volt=(float)ADC_ConvertedValue[0][count]+sum_volt;
}
for(count=1;count<N;count=count+2)
{
sum_guang+=(float)ADC_ConvertedValue[0][count];
}
}
后五个光敏,电池电压数据总和函数
void filter1 (void)
{ int count;
for(count=0;count<N;count=count+2)
{
sum_volt+=(float)ADC_ConvertedValue[1][count];
}
for(count=1;count<N;count=count+2)
{
sum_guang+=(float)ADC_ConvertedValue[1][count];
}
}
光敏,电池电压采样函数
void sampling ()
{ float sum_guang1,sum_volt1;
ADC_DMACmd(ADC1, ENABLE);
ADC_DMA_Configuration();
//Clock_Wait_10ms(1);
while(DMA_GetFlagStatus(DMA1_FLAG_TC1)==RESET);
ADC_Cmd(ADC1, DISABLE);
ADC_DMACmd(ADC1, DISABLE);
filter();
filter1();
// ADC_Cmd(ADC1, DISABLE);
//ADC_DMACmd(ADC1, DISABLE);
sum_guang1=sum_guang/10;
sum_volt1=sum_volt/10;
Battery_ADC= GetVolt(sum_volt1);
Clock_Wait_10ms(1);
Photoactor_ADC= GetVolt(sum_guang1);
sum_guang=0;
sum_volt=0;
// while(Battery_ADC>3.5&Photoactor_ADC>4);
//while(Battery_ADC<0.1&Photoactor_ADC<0.08);
//Battery_ADC1=Battery_ADC;
//Photoactor_ADC1= Photoactor_ADC;
}
求指点,我用KEIL设断点 发现读出的实际电压,有时准确,有时不准
|