采用ADC+DMA三通道AD转换为什么AD值不对,改变引脚位置对应...
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(!(DMA_GetFlagStatus(DMA1_FLAG_TC1)));
adc_current[i]=adc_value[0];
adc_voltagea[i]=adc_value[1];
adc_voltageb[i]=adc_value[2];
DMA_ClearFlag(DMA1_FLAG_TC1);
ADC_SoftwareStartConvCmd(ADC1,DISABLE);
i++;
LED_on;
if(i==7)
{
i=0;
Max_cu=adc_current[0];
Min_cu=adc_current[0];
sum_cu=adc_current[0];
Max_voa=adc_voltagea[0];
Min_voa=adc_voltagea[0];
sum_voa=adc_voltagea[0];
Max_vob=adc_voltageb[0];
Min_vob=adc_voltageb[0];
sum_vob=adc_voltageb[0];
for(i=1;i<7;i++)
{
sum_cu=sum_cu+adc_current[i];
if(adc_current[i]>Max_cu) Max_cu=adc_current[i];
if(adc_current[i]<Min_cu) Min_cu=adc_current[i];
sum_voa=sum_voa+adc_voltagea[i];
if(adc_voltagea[i]>Max_voa) Max_voa=adc_voltagea[i];
if(adc_voltagea[i]<Min_voa) Min_voa=adc_voltagea[i];
sum_vob=sum_vob+adc_voltageb[i];
if(adc_voltageb[i]>Max_vob) Max_vob=adc_voltageb[i];
if(adc_voltageb[i]<Min_vob) Min_vob=adc_voltageb[i];
}
adc_cu=(sum_cu-Max_cu-Min_cu)/5;
adc_voa=(sum_voa-Max_voa-Min_voa)/5;
adc_vob=(sum_vob-Max_vob-Min_vob)/5;
i=0;
}
}
void RCC_Configuration(void)
{
/* Enable Prefetch Buffer 设置预取缓冲器可以提高CPU的执行速度*/
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
//也可以用:while (!(RCC_WaitForHSEStartUp()== SUCCESS));
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08);
/* Enable GPIOB clocks */
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2; //选择PX.10/11
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模式为推挽输出
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //选择PX.10/11
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //模式为推挽输出
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
//GPIO的配置设置,包括管脚、模式和频率
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Timer2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period=9;
TIM_TimeBaseStructure.TIM_Prescaler=35999;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_div1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void ADC_RegularChannelConfiguration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
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_InitStructure.ADC_NbrOfChannel=3;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_11,2,ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_12,3,ADC_SampleTime_1Cycles5);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
ADC_SoftwareStartConvCmd(ADC1,DISABLE);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//adc1的数据寄存器设备地址,数据手册上有
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&adc_value;//一个有3个元素数组的首地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//BufferSize=2,因为ADC转换序列有2个通道
//如此设置,使序列1结果放在adc_value[0],序列2结果放在adc_value[1]
DMA_InitStructure.DMA_BufferSize = 3;
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;
//循环模式开启,Buffer写满后,自动回到初始地址开始传输
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_ClearFlag(DMA1_FLAG_TC1);
//配置完成后,启动DMA通道
DMA_ClearITPendingBit(DMA1_IT_TC1);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
ADC+DMA采集三通道AD值不对,改变引脚对应电平的AD值又变了。
第一次代码不全
#include "stm32f10x.h"
#define ADC1_DR_Address ((u32)0x4001244C)
void TIM1_UP_IRQHandler(void)
{
round1++;
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}
void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
// while(!(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)));
while(!(DMA_GetFlagStatus(DMA1_FLAG_TC1)));
adc_current[i]=adc_value[0];
adc_voltagea[i]=adc_value[1];
adc_voltageb[i]=adc_value[2];
// while(!times);
// while(!(times==3));
// times=0;
DMA_ClearFlag(DMA1_FLAG_TC1);
// ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
i++;
LED_on;
if(i==7)
{
i=0;
Max_cu=adc_current[0];
Min_cu=adc_current[0];
sum_cu=adc_current[0];
Max_voa=adc_voltagea[0];
Min_voa=adc_voltagea[0];
sum_voa=adc_voltagea[0];
Max_vob=adc_voltageb[0];
Min_vob=adc_voltageb[0];
sum_vob=adc_voltageb[0];
for(i=1;i<7;i++)
{
sum_cu=sum_cu+adc_current[i];
if(adc_current[i]>Max_cu) Max_cu=adc_current[i];
if(adc_current[i]<Min_cu) Min_cu=adc_current[i];
sum_voa=sum_voa+adc_voltagea[i];
if(adc_voltagea[i]>Max_voa) Max_voa=adc_voltagea[i];
if(adc_voltagea[i]<Min_voa) Min_voa=adc_voltagea[i];
sum_vob=sum_vob+adc_voltageb[i];
if(adc_voltageb[i]>Max_vob) Max_vob=adc_voltageb[i];
if(adc_voltageb[i]<Min_vob) Min_vob=adc_voltageb[i];
}
adc_cu=(sum_cu-Max_cu-Min_cu)/5;
adc_voa=(sum_voa-Max_voa-Min_voa)/5;
adc_vob=(sum_vob-Max_vob-Min_vob)/5;
i=0;
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOC , ENABLE);// 使能APB2外设GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOF , ENABLE);// 使能APB2外设GPIOE时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 |RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//使能DMA时钟
void ADC_RegularChannelConfiguration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
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_InitStructure.ADC_NbrOfChannel=3;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_11,2,ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_12,3,ADC_SampleTime_1Cycles5);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
ADC_SoftwareStartConvCmd(ADC1,DISABLE);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//adc1的数据寄存器设备地址,数据手册上有
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&adc_value;//一个有3个元素数组的首地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//BufferSize=2,因为ADC转换序列有2个通道
//如此设置,使序列1结果放在adc_value[0],序列2结果放在adc_value[1]
DMA_InitStructure.DMA_BufferSize = 3;
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;
//循环模式开启,Buffer写满后,自动回到初始地址开始传输
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_ClearFlag(DMA1_FLAG_TC1);
//配置完成后,启动DMA通道
DMA_ClearITPendingBit(DMA1_IT_TC1);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}