#include "adc.h"
#include "delay.h"
#include "stm32f10x_dma.h"
///4.548786990135964
///1.137196747533991
AdcStrTypedef ADC;
volatile float Scal[8];
const float Bit10ScalDefault[]={BIT10SCAL0,BIT10SCAL1,BIT10SCAL2,BIT10SCAL3,BIT10SCAL4,BIT10SCAL5,BIT10SCAL6,BIT10SCAL7};
const float Bit12ScalDefault[]={BIT12SCAL0,BIT12SCAL1,BIT12SCAL2,BIT12SCAL3,BIT12SCAL4,BIT12SCAL5,BIT12SCAL6,BIT12SCAL7};
volatile float AdcScal[2][8];
volatile unsigned long adc_ms;
void RCC_ADC_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC时钟不能超过14M
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA传输
}
void ADC_16Channel_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;//定义ADC采集的结构体
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式
ADC_InitStructure.ADC_ScanConvMode =ENABLE; //模数转换工作在扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模数转换工作在连续模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部触发关闭
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 8; //顺序进行转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStructADC指定的参数进行初始化外设ADCx寄存器
/* ADC1 regular channel11 configuration */
//设置指定ADC的规则组转换通道,设置它们的转换顺序和通道
//ADC1,ADC通道x,规则采样顺序值y,采样时间为239.5周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 ); // PA5 土湿1
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 ); // PA6
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_239Cycles5 );
//开启ADC的DMA支持(要实现DMA功能,还需独立配置DMA通道参数)
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1); //复位指定的ADC1的校准寄存器
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1)); //获取ADC1复位校准寄存器的状态,设置状态则等待
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1); //开始指定ADC1的校准程序
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1)); //获取指定ADC的校准程序,设置状态则等待
}
void DMA_Configuration(void)
{
/* ADC1 DMA1 Channel Config */
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1); //将DMA通道1的寄存器重设为缺省值
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //DMA外设ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC.buf; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //内存作为数据传输的目的地
DMA_InitStructure.DMA_BufferSize = AD_CHANNEL*AD_BUF_LEN; //DMA通道DMA缓存的大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //数据宽度为16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //数据宽度为16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //工作在循环缓存模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道X拥有高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道 x没有设置为内存到内存传输
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //根据DMA_InitStruct指定的参数初始化DMA的通道
}
void ADC_Work_Init(void)
{
RCC_ADC_Init();
ADC_16Channel_Init();
DMA_Configuration();
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能指定的ADC的软件转换启动功能
DMA_Cmd(DMA1_Channel1, ENABLE);//使能指定的通道
}
unsigned short BubbleSortAverage( volatile unsigned short *p,unsigned short len )
{
volatile unsigned short i,j,tem,aver;
for ( i = 0; i < (len - 1 ); i++ )
{
for ( j = 0; j < ( len - i - 1 ); j++ )
{
if ( p[j] > p[j+1] )
{
tem = p[j+1];
p[j+1] = p[j];
p[j] = tem;
}else{}
}
}
aver &=0;
for ( i = (len/2-2); i < (len/2+3); i++ )
{
aver += p[i];
}
aver /= 5;
return aver;
}
void AdcDataChange( void )
{
volatile unsigned long diff;
volatile unsigned short buf[AD_BUF_LEN],i,j,aver;
diff = DvalueTime(adc_ms);
if ( diff > 100 )
{
adc_ms = sys_ms;
for ( i = 0; i < AD_CHANNEL; i++ )
{
for ( j = 0; j < AD_BUF_LEN; j++ )
buf[j] = ADC.buf[j][i];
aver = BubbleSortAverage(buf,AD_BUF_LEN);
ADC.sampbit12[i] = aver;
//ADC.buf10bit[i] = aver * Scal[i]*1000;
ADC.buf10bit[i] = (aver / AdcScal[0][i])+0.5;
if ( ADC.buf10bit[i] < 5 ) ADC.buf10bit[i] = 0;
ADC.buf12bit[i] = aver / AdcScal[1][i];
}
}else{}
}
评论1