OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 6847|回复: 6

STM32F4 ADC DMA .打印结果一直全为0,请问哪里没配置好

[复制链接]

16

主题

66

帖子

0

精华

初级会员

Rank: 2

积分
149
金钱
149
注册时间
2013-7-25
在线时间
1 小时
发表于 2015-8-14 16:18:47 | 显示全部楼层 |阅读模式
5金钱

#include "stm32f4xx.h"
#include "usart.h"
#include  "hmi_driver.h"
#include  "hmi_user_uart.h"
#include  "stm32f4xx_tim.h"
#include  "stm32f4xx_dac.h"

//void GPIO_Configuration(void);
void Delay(__IO uint32_t nCount);
//void PWM_Config(void);
void TIMADC_Config(void);
void ADC1_CH6_DMA_Config(void);
void ADC3_DMA_Config(void);
void ADC12_DMA_Config(void);

 

#define ADC1_DR_Address    ((u32)0x4001204C)
#define ADC3_DR_ADDRESS     ((uint32_t)0x4001224C)
#define ADC_CDR_ADDRESS    ((uint32_t)0x40012308)
#define num 4000

__IO uint16_t ADC_ConvertedValue[num];
__IO uint32_t ADC12_ConvertedValue[num];
__IO uint32_t ADC3_ConvertedValue[num];
//__IO uint16_t abc = 0;
//u16 i=0;
unsigned char *xianshi;


int main(void)
{ int SS;
 
    USART_Configuration();//USART_NVIC_Config();
  ADC12_DMA_Config();
  ADC_SoftwareStartConv(ADC2);  
 while (1)
 {
   for (SS=0;SS<400;SS++)
   {
    printf("%d ",(ADC12_ConvertedValue[SS]>>16)*230/4095); 
    }printf("\r\nthis over\r\n");
 }
}


void ADC12_DMA_Config(void)
{
 ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;
  GPIO_InitTypeDef      GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_ADC2, ENABLE);

  DMA_DeInit(DMA2_Stream0);
  /* DMA2 Stream0 channe0 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_Channel_0; 
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_CDR_ADDRESS;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC12_ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = num;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
 // DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;        
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  DMA_Cmd(DMA2_Stream0, ENABLE);

  /* Configure ADC1 Channel6 pin as analog input ******************************/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None ;
// ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
// ADC_InitStructure.ADC_ExternalTrigConv= ADC_ExternalTrigConv_T2_CC2;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC2, &ADC_InitStructure);

 /* ADC1 regular channe6 configuration *************************************/
  ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 1, ADC_SampleTime_3Cycles);
  ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);
  ADC_DMACmd(ADC2, ENABLE);
 ADC_Cmd(ADC2, ENABLE);

}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

58

主题

6293

帖子

1

精华

资深版主

Rank: 8Rank: 8

积分
11473
金钱
11473
注册时间
2014-4-1
在线时间
1297 小时
发表于 2015-8-14 21:08:25 | 显示全部楼层
打印输出的时候,怎么知道数据有效还是无效?
回复

使用道具 举报

16

主题

66

帖子

0

精华

初级会员

Rank: 2

积分
149
金钱
149
注册时间
2013-7-25
在线时间
1 小时
 楼主| 发表于 2015-8-14 22:29:49 | 显示全部楼层
回复【2楼】xuande:
---------------------------------
根据引脚接地,接3.3V,悬空状态读出来的值,接地是0 ,接3.3V是230,悬空是180左右波动
回复

使用道具 举报

16

主题

66

帖子

0

精华

初级会员

Rank: 2

积分
149
金钱
149
注册时间
2013-7-25
在线时间
1 小时
 楼主| 发表于 2015-8-14 22:31:21 | 显示全部楼层
DMA2 的通道0,数据流0,对应的是ADC1
我把ADC2改为ADC1,还是错误。
回复

使用道具 举报

16

主题

66

帖子

0

精华

初级会员

Rank: 2

积分
149
金钱
149
注册时间
2013-7-25
在线时间
1 小时
 楼主| 发表于 2015-8-14 22:36:09 | 显示全部楼层
#include "stm32f4xx.h"
#include "usart.h"
//#include  "hmi_driver.h"
//#include  "hmi_user_uart.h"
//#include  "stm32f4xx_tim.h"
#include  "stm32f4xx_dac.h"

void ADC12_DMA_Config(void);
#define ADC1_DR_Address    ((u32)0x4001204C)
#define ADC3_DR_ADDRESS     ((uint32_t)0x4001224C)
#define ADC_CDR_ADDRESS    ((uint32_t)0x40012308)
#define num 4000

__IO uint16_t ADC_ConvertedValue[num];
__IO uint32_t ADC12_ConvertedValue[num];

int main(void)
{
int SS;
  USART_Configuration();//USART_NVIC_Config();
while (1)
{
    ADC_Cmd(ADC1, DISABLE);
ADC_Cmd(ADC2, DISABLE);
ADC_Cmd(ADC3, DISABLE);
DMA_Cmd(DMA2_Stream0, DISABLE);
  ADC_DeInit();
  DMA_DeInit(DMA2_Stream0);
ADC12_DMA_Config();
ADC_SoftwareStartConv(ADC1);
for (SS=0;SS<400;SS++)
 { 
 printf("%d ",(ADC12_ConvertedValue[SS]>>16)*230/4095);
  }printf("\r\nthis over\r\n");
}
}


void ADC12_DMA_Config(void)
{
ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;
  GPIO_InitTypeDef      GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_ADC2, ENABLE);

  DMA_DeInit(DMA2_Stream0);
  /* DMA2 Stream0 channe0 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC12_ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = num;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
 // DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  DMA_Cmd(DMA2_Stream0, ENABLE);


  /* Configure ADC1 Channel6 pin as analog input ******************************/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None ;
// ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
// ADC_InitStructure.ADC_ExternalTrigConv= ADC_ExternalTrigConv_T1_CC2;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

/* ADC1 regular channe6 configuration *************************************/
  ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_3Cycles);
  ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
  ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);

}


//#ifdef  USE_FULL_ASSERT

///**
//  * @brief  Reports the name of the source file and the source line number
//  *         where the assert_param error has occurred.
//  * @param  file: pointer to the source file name
//  * @param  line: assert_param error line source number
//  * @retval None
//  */
//void assert_failed(uint8_t* file, uint32_t line)
//{ 
//  /* User can add his own implementation to report the file name and line number,
//     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

//  /* Infinite loop */
//  while (1)
//  {

//  }
//}
//#endif

/**
  * @}
  */ 

/**
  * @}
  */ 

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
回复

使用道具 举报

16

主题

66

帖子

0

精华

初级会员

Rank: 2

积分
149
金钱
149
注册时间
2013-7-25
在线时间
1 小时
 楼主| 发表于 2015-8-14 22:37:05 | 显示全部楼层
这是最新改过的,改过后还是不行,求解
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165371
金钱
165371
注册时间
2010-12-1
在线时间
2110 小时
发表于 2015-8-16 22:22:36 | 显示全部楼层
帮顶....
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-25 03:26

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表