[STM32] ADC+DMA多路采集数据,得到的结果都是一样的

[复制链接]
2652|6
 楼主| 泡沫馨馨 发表于 2014-8-9 11:03 | 显示全部楼层 |阅读模式
我的ADC配置如下:
  1. u16 ADC_ConvertedValue[M];
  2. u16 ADC_ConvertedValueLocal[M];
  3. u16 Upa,Upb,Upc;

  4. void ADC1_Init(void)
  5. {
  6.         GPIO_InitTypeDef GPIO_InitStructure;
  7.         ADC_InitTypeDef ADC_InitStructure;
  8.         DMA_InitTypeDef DMA_InitStructure;
  9.        
  10.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
  11.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  12.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
  13.         RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  14.        
  15.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;
  16.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
  17.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  18.        
  19.         DMA_DeInit(DMA1_Channel1);
  20.         DMA_InitStructure.DMA_PeripheralBaseAddr=(ADC1->DR);
  21.         DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&ADC_ConvertedValue;
  22.         DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
  23.         DMA_InitStructure.DMA_BufferSize=M;
  24.         DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
  25.         DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
  26.         DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
  27.         DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
  28.         DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;
  29.         DMA_InitStructure.DMA_Priority=DMA_Priority_High;
  30.         DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
  31.         DMA_Init(DMA1_Channel1,&DMA_InitStructure);
  32.        
  33.        
  34.         ADC_DeInit(ADC1);
  35.         ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  36.         ADC_InitStructure.ADC_ScanConvMode=ENABLE;
  37.         ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
  38. ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
  39.         ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
  40.         ADC_InitStructure.ADC_NbrOfChannel=M;
  41.         ADC_Init(ADC1, &ADC_InitStructure);

  42.         ADC_RegularChannelConfig(ADC1, ADC_Channel_4,1,ADC_SampleTime_239Cycles5);
  43.         ADC_RegularChannelConfig(ADC1, ADC_Channel_5,2,ADC_SampleTime_239Cycles5);
  44.         ADC_RegularChannelConfig(ADC1, ADC_Channel_6,3,ADC_SampleTime_239Cycles5);
  45.        
  46.         ADC_Cmd(ADC1, ENABLE);
  47.         ADC_DMACmd(ADC1, ENABLE);
  48.                
  49.         ADC_ResetCalibration(ADC1);
  50.         while(ADC_GetResetCalibrationStatus(ADC1));
  51.         ADC_StartCalibration(ADC1);
  52.         while(ADC_GetCalibrationStatus(ADC1));
  53.         DMA_Cmd(DMA1_Channel1,ENABLE);
  54.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  55. }
主函数如下:
  1. u8 i;
  2. int main(void)
  3. {
  4.         uart_init(9600);
  5.         CHIP_PWM_Init(8999);
  6.         ADC1_Init();
  7.         LED_Init();
  8.         while(1)
  9.         {
  10.                 LED2=!LED2;
  11.                 printf("\r\n let's a test \r\n\r\n");
  12.                
  13.                 change();

  14.                 for(i=0;i<3;i++)
  15.                         {
  16.                                 ADC_ConvertedValueLocal[i]=ADC_ConvertedValue[i]*33000/4096;
  17.                         }

  18.                 printf("\r\n Upa=%d \r\n\r\n",ADC_ConvertedValueLocal[0]);
  19.                 printf("\r\n Upb=%d \r\n\r\n",ADC_ConvertedValueLocal[1]);
  20.                 printf("\r\n Upc=%d \r\n\r\n",ADC_ConvertedValueLocal[2]);       

  21.         }
  22. }
采样后打印出来的结果,三路数据的值都是一样的。而且用示波器测量三路数据均为PWM波,波形如下,打印结果却显示三路数据永远都是高电平。请问这是为什么呢?
 楼主| 泡沫馨馨 发表于 2014-8-9 11:05 | 显示全部楼层
我也尝试了正点原子例程里面的方法,使用了Get_Adc(u8 ch)
  1. u16 Get_Adc(u8 ch)
  2. {
  3.         ADC_RegularChannelConfig(ADC1,ch,1,ADC_SampleTime_239Cycles5);

  4.         ADC_SoftwareStartConvCmd(ADC1,ENABLE);
  5.         while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
  6.         return ADC_GetConversionValue(ADC1);
  7. }
主函数中这样处理:
  1. ADC_ConvertedValueLocal[0]=Get_Adc(ADC_Channel_4);
  2.                 printf("\r\n Upa=%d \r\n\r\n",ADC_ConvertedValueLocal[0]);
  3.                 ADC_ConvertedValueLocal[1]=Get_Adc(ADC_Channel_5);
  4.                 printf("\r\n Upb=%d \r\n\r\n",ADC_ConvertedValueLocal[1]);
  5.                 ADC_ConvertedValueLocal[2]=Get_Adc(ADC_Channel_6);
  6.                 printf("\r\n Upc=%d \r\n\r\n",ADC_ConvertedValueLocal[2]);       
但是打印出来的结果中,Upa始终保持高电平,Upb和Upc保持相同的低电平不变。所以这个结果应该也是错误的。
求指点!!
airwill 发表于 2014-8-9 22:38 | 显示全部楼层
我看先借助调试器, 看看外设寄存器有没有设置正确, 验证一下 ADC 有没有转换, 再进一步分析
 楼主| 泡沫馨馨 发表于 2014-8-11 10:24 | 显示全部楼层
airwill 发表于 2014-8-9 22:38
我看先借助调试器, 看看外设寄存器有没有设置正确, 验证一下 ADC 有没有转换, 再进一步分析 ...

我在线调试出现了下面的提示:
***JLink Error: CPU is not halted
**JLink Warning: CPU could not be halted
***JLink Error: Can not read register 15 (R15) while CPU is running
***JLink Error: Can not read register 16 (XPSR) while CPU is running
***JLink Error: Can not read register 0 (R0) while CPU is running
***JLink Error: Can not read register 1 (R1) while CPU is running
***JLink Error: Can not read register 2 (R2) while CPU is running
***JLink Error: Can not read register 3 (R3) while CPU is running
***JLink Error: Can not read register 4 (R4) while CPU is running
***JLink Error: Can not read register 5 (R5) while CPU is running
***JLink Error: Can not read register 6 (R6) while CPU is running
***JLink Error: Can not read register 7 (R7) while CPU is running
***JLink Error: Can not read register 8 (R8) while CPU is running
***JLink Error: Can not read register 9 (R9) while CPU is running
***JLink Error: Can not read register 10 (R10) while CPU is running
***JLink Error: Can not read register 11 (R11) while CPU is running
***JLink Error: Can not read register 12 (R12) while CPU is running
***JLink Error: Can not read register 13 (R13) while CPU is running
***JLink Error: Can not read register 14 (R14) while CPU is running
***JLink Error: Can not read register 15 (R15) while CPU is running
***JLink Error: Can not read register 16 (XPSR) while CPU is running
***JLink Error: Can not read register 17 (MSP) while CPU is running
***JLink Error: Can not read register 18 (PSP) while CPU is running
***JLink Error: Can not read register 20 (CFBP) while CPU is running
***JLink Error: CPU is not halted
**JLink Warning: CPU could not be halted
***JLink Error: Can not read register 15 (R15) while CPU is running
***JLink Error: Can not read register 16 (XPSR) while CPU is running
***JLink Error: Can not read register 0 (R0) while CPU is running
***JLink Error: Can not read register 1 (R1) while CPU is running
***JLink Error: Can not read register 2 (R2) while CPU is running
***JLink Error: Can not read register 3 (R3) while CPU is running
***JLink Error: Can not read register 4 (R4) while CPU is running
***JLink Error: Can not read register 5 (R5) while CPU is running
***JLink Error: Can not read register 6 (R6) while CPU is running
***JLink Error: Can not read register 7 (R7) while CPU is running
***JLink Error: Can not read register 8 (R8) while CPU is running
***JLink Error: Can not read register 9 (R9) while CPU is running
***JLink Error: Can not read register 10 (R10) while CPU is running
***JLink Error: Can not read register 11 (R11) while CPU is running
***JLink Error: Can not read register 12 (R12) while CPU is running
***JLink Error: Can not read register 13 (R13) while CPU is running
***JLink Error: Can not read register 14 (R14) while CPU is running
***JLink Error: Can not read register 15 (R15) while CPU is running
***JLink Error: Can not read register 16 (XPSR) while CPU is running
***JLink Error: Can not read register 17 (MSP) while CPU is running
***JLink Error: Can not read register 18 (PSP) while CPU is running
***JLink Error: Can not read register 20 (CFBP) while CPU is running

看网上说是因为SWD频率太高的原因,可是我设置的频率仅为200K、还有说是因为所有管脚下拉了,我也没有设置管脚下拉。。
同样的板子,运行其他程序的时候就能成功在线调试,只有这个程序不可以,是不是还是程序有问题?

可是我查了网上N多的ADC+DMA,实在是不知道到底哪里不对了%>_<%
airwill 发表于 2014-8-11 14:35 | 显示全部楼层
这连接不了调试器, 最大的可能性是硬件连接问题, 还有就是 SWD 的端口功能屏蔽掉了
hlong0033 发表于 2014-9-29 14:48 | 显示全部楼层
PWM的频率是多少,ADC的转换时间200uS左右
wuliangbin111 发表于 2015-6-9 15:40 | 显示全部楼层
**JLink Warning: CPU could not be halted
***JLink Error: Can not read register 15 (R15) while CPU is running
***JLink Error: Can not read register 16 (XPSR) while CPU is running
***JLink Error: Can not read register 0 (R0) while CPU is running
***JLink Error: Can not read register 1 (R1) while CPU is running
***JLink Error: Can not read register 2 (R2) while CPU is running
***JLink Error: Can not read register 3 (R3) while CPU is running
***JLink Error: Can not read register 4 (R4) while CPU is running
***JLink Error: Can not read register 5 (R5) while CPU is running
***JLink Error: Can not read register 6 (R6) while CPU is running
***JLink Error: Can not read register 7 (R7) while CPU is running
***JLink Error: Can not read register 8 (R8) while CPU is running
***JLink Error: Can not read register 9 (R9) while CPU is running
***JLink Error: Can not read register 10 (R10) while CPU is running
***JLink Error: Can not read register 11 (R11) while CPU is running
***JLink Error: Can not read register 12 (R12) while CPU is running
***JLink Error: Can not read register 13 (R13) while CPU is running
***JLink Error: Can not read register 14 (R14) while CPU is running
***JLink Error: Can not read register 15 (R15) while CPU is running
***JLink Error: Can not read register 16 (XPSR) while CPU is running
***JLink Error: Can not read register 17 (MSP) while CPU is running
***JLink Error: Can not read register 18 (PSP) while CPU is running
***JLink Error: Can not read register 20 (CFBP) while CPU is running

看网上说是因为SWD频率太高的原因,可是我设置的频率仅为200K、还有说是因为所有管脚下拉了,我也没有设置管脚下拉。。
同样的板子,运行其他程序的时候就能成功在线调试,只有这个程序不可以,是不是还是程序有问题?

这个问题您解决了么?我现在也遇到这种问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则

14

主题

24

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部