第一次接触STM32的ADC,百度过例程但是也没找出来问题,哪位大神帮忙解决一下,在下感激不尽,谢谢啦
1.采集的ADC电压值不对
2.只有第一路对采集有影响
3.结果如下图
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd.h"
#include "adc.h"
#include "stm32f10x.h"
#define ADC1_DR_Address ((u32)0x4001244C)
uint16_t ADC_ConvertedValue[3];
void Adc_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct0;
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStruct0.GPIO_Mode=GPIO_Mode_AIN;//Ä£Äaêäèë
GPIO_InitStruct0.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1 |GPIO_Pin_2;
GPIO_InitStruct0.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct0);
DMA_DeInit(DMA1_Channel1);
DMA_InitStruct.DMA_BufferSize=3;
DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_M2M=DMA_M2M_Disable;
DMA_InitStruct.DMA_MemoryBaseAddr=(u32)&ADC_ConvertedValue;
DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_Mode=DMA_Mode_Circular;
DMA_InitStruct.DMA_PeripheralBaseAddr=ADC1_DR_Address;
DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_Priority=DMA_Priority_High;
DMA_Init(DMA1_Channel1,&DMA_InitStruct);
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA1_Channel1,ENABLE);
//RCC_ADCCLKConfig( RCC_PCLK2_Div6);//éèÖÃADC·ÖÆμòò×ó6 72M/6=12,ADC×î′óê±¼ä2»Äü3¬1y14M
ADC_DeInit(ADC1);
ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;
ADC_InitStruct.ADC_ScanConvMode=ENABLE;//êÇ·ñé¨Ãè
ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//′¥·¢·½ê½
ADC_InitStruct.ADC_ContinuousConvMode=ENABLE;
ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStruct.ADC_NbrOfChannel=3;
ADC_Init( ADC1,&ADC_InitStruct);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3, ADC_SampleTime_239Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1); //ê1Äü¸′λD£×¼
while(ADC_GetResetCalibrationStatus(ADC1)); //μè′y¸′λD£×¼½áêø
ADC_StartCalibration(ADC1); //¿aÆôADD£×¼
while(ADC_GetCalibrationStatus(ADC1)); //μè′yD£×¼½áêø
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ê1ÄüÖ¸¶¨μÄADC1μÄèí¼t×a»»Æô¶ˉ1|Äü
}
void NCIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=DMA1_Channel1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
}
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC1))
{
DMA_ClearITPendingBit(DMA1_IT_TC1);
LED0=!LED0;
}
}
int main(void)
{
uint16_t ADC_ConvertedValue[3];
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200); //′®¿ú3õê¼»ˉÎa9600
delay_init();
LED_Init();
LCD_Init();
Adc_init();
NCIC_Config();
while(1)
{
printf("PA0=%d PA1=%d PA2=%d
",ADC_ConvertedValue[0],ADC_ConvertedValue[1],ADC_ConvertedValue[2]);
LED1=!LED1;
delay_ms(10);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>