#include "adc.h"
#include "delay.h"
#include <stdio.h>
#define SAMPLE_TIME 10
#define CHANEL_INDEX 8
#define INVALID_NUM 2
__IO uint16_t adc1_value[average_time];
__IO uint16_t adc2_value[average_time];
__IO uint16_t ADC1_ConvertedValueTab[SAMPLE_TIME][CHANEL_INDEX]; //
__IO uint16_t ADC2_ConvertedValueTab[SAMPLE_TIME][CHANEL_INDEX]; //??AD????,????
__IO uint32_t uwADCxConvertedVoltage = 0;
//初始化ADC
void Adc_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
// u8 i=0;
//时钟配置
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE);//使能GPIOA时钟,PA0-7模拟输入
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //使能ADC1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
// //PA 1、3、5、7 0、2、4、6
// //初始化ADC1通道0-7
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_AN;//模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 |GPIO_Pin_4 |GPIO_Pin_5| GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE); //ADC1复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE); //复位结束
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2,ENABLE); //ADC2复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2,DISABLE); //复位结束
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;//两个采样阶段之间的延迟5个时钟
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz
ADC_CommonInit(&ADC_CommonInitStructure);//初始化
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE;//扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//关闭连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测,使用软件触发
//ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
ADC_InitStructure.ADC_NbrOfConversion = 8;//1个转换在规则序列中 也就是只转换规则序列1
ADC_Init(ADC1, &ADC_InitStructure);//ADC初始化
ADC_Init(ADC2, &ADC_InitStructure);//ADC初始化
/* ADC1 regular channel7 configuration **************************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_8, 1, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_9, 2, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_10,3, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_11,4, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_12,5, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 6, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_14, 7, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 8, ADC_SampleTime_3Cycles);
//ADC_Cmd(ADC2, ENABLE);
//DMA_DeInit(DMA1_Stream4);//DMA2_Stream4
DMA_DeInit(DMA2_Stream2);
// while (DMA_GetCmdStatus(DMA2_Stream4) != DISABLE){}//等待DMA可配置
while (DMA_GetCmdStatus(DMA2_Stream2) != DISABLE){}//等待DMA可配置
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr=((u32)ADC1+0x4c);//ADC1的数据ADC1_BASE+0x4C; //ADC->DR??((u32)ADC1+0x4c)
DMA_InitStructure.DMA_Memory0BaseAddr=(u32)&ADC1_ConvertedValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//从外设到内存
DMA_InitStructure.DMA_BufferSize = SAMPLE_TIME*CHANEL_INDEX;;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //12位AD,存放16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Normal; //need continue visit
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);//开启AD转换器
DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr=((u32)ADC2+0x4c);//ADC1的数据ADC1_BASE+0x4C; //ADC->DR??((u32)ADC1+0x4c)
DMA_InitStructure.DMA_Memory0BaseAddr=(u32)&ADC2_ConvertedValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//从外设到内存
DMA_InitStructure.DMA_BufferSize = SAMPLE_TIME*CHANEL_INDEX;;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //12位AD,存放16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Normal; //need continue visit
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream2, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream2, ENABLE);
ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);
ADC_DMACmd(ADC2, ENABLE);
ADC_Cmd(ADC2, ENABLE);//开启AD转换器
ADC_SoftwareStartConv(ADC1); //启动ADC转换
ADC_SoftwareStartConv(ADC2); //启动ADC转换
//测试程序开始
// while (1)
// {printf("
--------------
");
//
// for(i = 0;i< 8;i++)
// {
// uwADCxConvertedVoltage = adc1_value[i][0] *3300/0xFFF;
// printf("adc_chanel %d = %d
",i,uwADCxConvertedVoltage);
// delay_ms(20);
// }
// delay_ms(1000);
// }
}
/*****************************************************
//??????,?????array??columnindex??count????????????
*****************************************************/
void QuickSort(void)
{
u8 i=0,j=0,k=0;
u16 temp;
u32 sum = 0;
for(k=0;k<CHANEL_INDEX;k++) //??
{
for(i=0; i< SAMPLE_TIME; i++)
{
for(j=i+1; j< SAMPLE_TIME; j++)
{
if(ADC1_ConvertedValueTab[i][k]> ADC1_ConvertedValueTab[j][k])
{
temp = ADC1_ConvertedValueTab[i][k];
ADC1_ConvertedValueTab[i][k]= ADC1_ConvertedValueTab[j][k];
ADC1_ConvertedValueTab[j][k]= temp; //???????????,???????
}
}
for(j=i+1; j< SAMPLE_TIME; j++)
{
if(ADC2_ConvertedValueTab[i][k]> ADC2_ConvertedValueTab[j][k])
{
temp = ADC2_ConvertedValueTab[i][k];
ADC2_ConvertedValueTab[i][k]= ADC2_ConvertedValueTab[j][k];
ADC2_ConvertedValueTab[j][k]= temp; //???????????,???????
}
}
}
}
//??????,????
for(i=0; i<CHANEL_INDEX ;i++) //???
{
for(j=INVALID_NUM;j<SAMPLE_TIME-INVALID_NUM;j++)//?????2?,??
{
sum += ADC1_ConvertedValueTab[j][i];
}
adc1_value[i] = sum/(SAMPLE_TIME-INVALID_NUM-INVALID_NUM);
sum = 0; //?????
}
for(i=0; i<CHANEL_INDEX ;i++) //???
{
for(j=INVALID_NUM;j<SAMPLE_TIME-INVALID_NUM;j++)//?????2?,??
{
sum += ADC2_ConvertedValueTab[j][i];
}
adc2_value[i] = sum/(SAMPLE_TIME-INVALID_NUM-INVALID_NUM);
sum = 0; //?????
// //?????AD????????,??01??,???1,???0
// if(GraySensorValue_AVG[i]<=BackGround_ADValue[i] -400) //AD?????2500,???,??2000,???
// unSensor_Status.bysensor_status = unSensor_Status.bysensor_status | (0x80>>i);//& (0x7F>>i);
// else //if(GraySensorValue_AVG[i]<2000)
// unSensor_Status.bysensor_status = unSensor_Status.bysensor_status & (0xFE<<(7-i));//| (0x80>>i);
}
}
void Get_AD(void)
{
if(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0) == SET)
{
//if(DMA_GetFlagStatus(DMA2_Stream2,DMA_FLAG_TCIF2) == SET)
{
QuickSort();
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
//DMA_ClearFlag(DMA2_Stream2,DMA_FLAG_TCIF2);
}
}
}
一周热门 更多>