DMA初始化
void MYDMA_Config(void)
{
__HAL_RCC_DMA2_CLK_ENABLE();//DMA2时钟使能
//Tx DMA配置
ADC1_DMA_Handler.Instance=DMA2_Stream0; //数据流选择
ADC1_DMA_Handler.Init.Channel=DMA_CHANNEL_0; //通道选择
ADC1_DMA_Handler.Init.Direction=DMA_PERIPH_TO_MEMORY; //存储器到外设
ADC1_DMA_Handler.Init.PeriphInc=DMA_PINC_DISABLE; //外设增量模式
ADC1_DMA_Handler.Init.MemInc=DMA_MINC_ENABLE; //存储器增量模式
ADC1_DMA_Handler.Init.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD; //外设数据长度:16位
ADC1_DMA_Handler.Init.MemDataAlignment=DMA_PDATAALIGN_HALFWORD; //存储器数据长度:16位
ADC1_DMA_Handler.Init.Mode=DMA_CIRCULAR; //外设普通模式
ADC1_DMA_Handler.Init.Priority=DMA_PRIORITY_MEDIUM; //中等优先级
ADC1_DMA_Handler.Init.FIFOMode=DMA_FIFOMODE_DISABLE;
ADC1_DMA_Handler.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;
ADC1_DMA_Handler.Init.MemBurst=DMA_MBURST_SINGLE; //存储器突发单次传输
ADC1_DMA_Handler.Init.PeriphBurst=DMA_PBURST_SINGLE; //外设突发单次传输
__HAL_LINKDMA(&ADC1_Handler,DMA_Handle,ADC1_DMA_Handler); //将DMA与USART1联系起来(发送DMA)
ADC1_DMA_Handler.Instance->PAR = (u32)&ADC1->DR;//外设地址
ADC1_DMA_Handler.Instance->M0AR = (vu32)ADC_ConvertedValue;
ADC1_DMA_Handler.Instance->NDTR = 8;
HAL_DMA_DeInit(&ADC1_DMA_Handler);
HAL_DMA_Init(&ADC1_DMA_Handler);
HAL_DMA_Start(&ADC1_DMA_Handler, (u32)&ADC1->DR, (vu32)ADC_ConvertedValue, N*ADC_CHANNELS);
}
/***************************************************************************************************
*名称: MY_ADC_Init()
*描述: 配置AD转换,使用ADC1,DMA2,转换8个通道
*入口参数: 无
*出口参数: 无
***************************************************************************************************/
void MY_ADC_Init(void)
{
ADC_ChannelConfTypeDef ADC1_ChanConf;
ADC1_Handler.Instance=ADC1;
ADC1_Handler.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4; //4分频,ADCCLK=PCLK2/4=90/4=22.5MHZ
ADC1_Handler.Init.Resolution=ADC_RESOLUTION_12B; //12位模式
ADC1_Handler.Init.DataAlign=ADC_DATAALIGN_RIGHT; //右对齐
ADC1_Handler.Init.ScanConvMode=ENABLE; //扫描模式?
ADC1_Handler.Init.EOCSelection=DISABLE; //关闭EOC中断
ADC1_Handler.Init.ContinuousConvMode=ENABLE; //开启连续转换
ADC1_Handler.Init.NbrOfConversion=8; //8个转换在规则序列中 也就是只转换规则序列1
ADC1_Handler.Init.DiscontinuousConvMode=DISABLE; //禁止不连续采样模式
ADC1_Handler.Init.NbrOfDiscConversion=0; //不连续采样通道数为0
ADC1_Handler.Init.ExternalTrigConv=ADC_SOFTWARE_START; //软件触发
ADC1_Handler.Init.ExternalTrigConvEdge=ADC_EXTERNALTRIGCONVEDGE_NONE;//使用软件触发
ADC1_Handler.Init.DMAContinuousRequests=ENABLE; //开启DMA请求
HAL_ADC_Init(&ADC1_Handler); //初始化
//转换序列
ADC1_ChanConf.Channel = ADC_CHANNEL_8;
ADC1_ChanConf.Rank = 1; //第1个序列,序列1
ADC1_ChanConf.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC1_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
ADC1_ChanConf.Channel = ADC_CHANNEL_15;
ADC1_ChanConf.Rank = 2; //第1个序列,序列2
ADC1_ChanConf.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC1_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
ADC1_ChanConf.Channel = ADC_CHANNEL_14;
ADC1_ChanConf.Rank = 3; //第1个序列,序列3
ADC1_ChanConf.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC1_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
ADC1_ChanConf.Channel = ADC_CHANNEL_7;
ADC1_ChanConf.Rank = 4; //第1个序列,序列4
ADC1_ChanConf.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC1_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
ADC1_ChanConf.Channel = ADC_CHANNEL_6;
ADC1_ChanConf.Rank = 5; //第1个序列,序列5
ADC1_ChanConf.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC1_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
ADC1_ChanConf.Channel = ADC_CHANNEL_3;
ADC1_ChanConf.Rank = 6; //第1个序列,序列6
ADC1_ChanConf.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC1_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
ADC1_ChanConf.Channel = ADC_CHANNEL_9;
ADC1_ChanConf.Rank = 7; //第1个序列,序列6
ADC1_ChanConf.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC1_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
ADC1_ChanConf.Channel = ADC_CHANNEL_5;
ADC1_ChanConf.Rank = 8; //第1个序列,序列6
ADC1_ChanConf.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC1_ChanConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
// HAL_ADC_Start(&ADC1_Handler);
MYDMA_Config();//初始化并开启DMA
HAL_ADC_Start_DMA(&ADC1_Handler, (vu32*)&ADC_ConvertedValue, N*ADC_CHANNELS);
}
//ADC,引脚配置,时钟使能
//此函数会被HAL_ADC_Init()调用
//hadc:ADC句柄
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_ADC1_CLK_ENABLE(); //使能ADC1时钟
__HAL_RCC_GPIOA_CLK_ENABLE(); //开启GPIOA时钟
__HAL_RCC_GPIOB_CLK_ENABLE(); //开启GPIOA时钟
__HAL_RCC_GPIOC_CLK_ENABLE(); //开启GPIOA时钟
GPIO_Initure.Pin=GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_5; //PA3,6,7, 5用于测试
GPIO_Initure.Mode=GPIO_MODE_ANALOG; //模拟
GPIO_Initure.Pull=GPIO_NOPULL; //不带上下拉
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
GPIO_Initure.Pin=GPIO_PIN_0|GPIO_PIN_1; //PB0,1
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
GPIO_Initure.Pin=GPIO_PIN_4|GPIO_PIN_5; //PC4,5
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
}
/***************************************************************************************************
*名称:getAdc()
*描述:读取指定通道AD值
*入口参数:通道号channel,取值分为0到(ADC_CHANNELS-1)
*出口参数:AD值
***************************************************************************************************/
uint16 getADC_CH(uint8 channel){
u16 tbuf[N];
__IO u32 tdat=0;
u8 ti;
//判断传入参数是否合法
if(channel>=ADC_CHANNELS){
return 0;
}
for(ti=0;ti<N;ti++)
{
tbuf[ti]=ADC_ConvertedValue[ti][channel];
}
bubbleSort(tbuf, N);
for(ti=25;ti<(N-25);ti++)
{
tdat+=tbuf[ti];
}
tdat/=50;
if(tdat<5)
{
tdat=0;
}
return tdat;
}
|