谁能分享下STM32F0的ADC多通道采样,新手一枚。我搞的出不来结果。
#include "public.h"
#include "printf.h"
#include "adc.h"
#include "systick.h"
#include "usart.h"
extern uint32_t After_filter[M];
/****************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
****************************************************************************/
uint32_t GetVolt(uint16_t advalue)
{
return (uint16_t)((uint32_t)(advalue*330)/4096);
}
int main(void)
{
uint32_t value[M];
int i;
adc_init();
// printf_init();
while(1)
{
//ADC_SoftwareStartConvCmd(ADC1, ENABLE);
DMA_Cmd(DMA1_Channel1,ENABLE);
//while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
// while((DMA_GetFlagStatus(DMA1_FLAG_TC1)) == RESET );
//DMA_ClearFlag(DMA1_FLAG_TC1);
filter();
for(i=0;i<12;i++)
{
value=GetVolt(After_filter);
printf("value[%d]: %d.%dv
",i,value/100,value%100);
//delay_ms(1000);
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#include "adc.h"
#define ADC1_DR_Address ((uint32_t)0x40012400+0x4c)
__IO uint16_t AD_Value[N][M];
__IO uint16_t ADC_ConvertedValue;
int i;
uint32_t After_filter[M];
void adc_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_DMA1,ENABLE);
RCC_ADCCLKConfig(RCC_ADCCLK_HSI14);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//ADC
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;//ADC
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AN;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//ADC
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AN;
GPIO_Init(GPIOC,&GPIO_InitStructure);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr =ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr =(uint32_t)&AD_Value;
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);
//DMA_ITConfig(DMA_Channel1,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA1_Channel1,ENABLE);
ADC_DeInit(ADC1);
/* 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 = M;
ADC_Init(ADC1, &ADC_InitStructure); */
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ChannelConfig(ADC1,ADC_Channel_0,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_1,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_2,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_3,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_8,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_9,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_10,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_11,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_12,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_13,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_14,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_15,ADC_SampleTime_239_5Cycles);
ADC_GetCalibrationFactor(ADC1);
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1,ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
ADC_StartOfConversion(ADC1);
/*ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void filter(void)
{
uint32_t sum = 0;
int count;
for(i=0;i<12;i++)
{
for(count = 0;count<N;count++)
{
sum+=AD_Value[count];
}
After_filter=sum/N;
sum=0;
}
}
一周热门 更多>