搞不懂
ad=0;
for(i=0;i<50;i++)
{
ad=ad+ADCData[i];
}
ad=ad/50;
这段话出来始终为0
哪位大哥给点建议,是哪里的问题,我采集的是3.3v电源电压
#include "stm32f10x.h"
#include "define.h"
#include "delay.h"
#include "stdio.h"
u16 ADCData[50];
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//12M ×î′ó14M
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
}
//GPIO3õê¼»ˉ
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;//ADμÄC41ü½Å
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//Ä£Äaá¿êäèë
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
//ÖD¶ÏóÅÏ輶éèÖÃ
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÖD¶Ï·Ö×é2
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; ;//′®¿ú1í¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//ÇàÕ¼0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//Ïìó|1
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;//¶àá¢Ä£ê½
ADC_InitStructure.ADC_ScanConvMode=DISABLE;//μ¥í¨μàÄ£ê½
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;//Ñ-»·2é¼ˉ
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//èí¼t′¥·¢
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;//óò¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel=1;//×a»»í¨μàêyÄ¿Îa1
ADC_Init(ADC1,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_239Cycles5);//1æÔò×é,ADC1,í¨μà10£¬ò»¸öí¨μ࣬239.5¸öÖüÆú
ADC_DMACmd(ADC1,ENABLE);//ÖØòa °ÑADoíDMA°ó¶¨ò»Æe
ADC_Cmd(ADC1,ENABLE);//ê1ÄüADC1
ADC_ResetCalibration(ADC1);//¸′λ
while(ADC_GetResetCalibrationStatus(ADC1)); //¡¢μè′yêÇ·ñ¸′λíê3é
ADC_StartCalibration(ADC1);//D£×¼ADC1
while(ADC_GetCalibrationStatus(ADC1));//¼ìÑéêÇ·ñD£×¼íê3é
ADC_SoftwareStartConvCmd(ADC1,ENABLE);//Æô¶ˉADC1
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);//ADC11òÔúDMA1μÄí¨μàéÏ
DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&ADC1->DR;//íaéèμØÖ·ADC1Ëí¸øDMA
DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&ADCData;//Äú′æμØÖ· Ëí¸øêy×é êy×éÃûμèóúêy×éμÄμØÖ·
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;//íaéèADC1×÷Îaêy¾YμÄà′Ô′
DMA_InitStructure.DMA_BufferSize=50;//»o3åÇøÎaêy×éμÄ′óD¡
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//íaéèADC1μÄ′æ′¢μØÖ·Îa2»±äμÄ
DMA_InitStructure.DMA_MemoryInc=DMA_PeripheralInc_Enable;//Äú′æμÄμØÖ·êÇμYÔöμÄ
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;//íaéèêy¾Y¿í¶è16
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;//Äú′æêy¾Y¿í¶è16
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;//á¬Dø1¤×÷ģ꽣¬Ñ-»·2é¼ˉ
DMA_InitStructure.DMA_Priority=DMA_Priority_High;//óÅÏ輶
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//2»êÇÄú′æμ½Äú′æ íaéèμ½Äú′æ
DMA_Init(DMA1_Channel1,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel1,ENABLE);//ê1ÄüDMA
}
int main(void)
{
u32 ad=0;
u8 i=0;
delay_init();
RCC_Configuration(); //Ïμí3ê±Öó3õê¼»ˉ
GPIO_Configuration();//¶Ë¿ú3õê¼»ˉ
USART_Configuration();
NVIC_Configuration();
DMA_Configuration();
ADC_Configuration();
while(1)
{
ad=0;
for(i=0;i<50;i++)
{
ad=ad+ADCData[i];
}
ad=ad/50;
printf("ad =%f
",3.3/4095*ad);//êμ¼êμçÑ1Öμ
delay_ms(1000);
delay_ms(1000);
}
}
一周热门 更多>