打印
[STM32]

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

[复制链接]
2352|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
泡沫馨馨|  楼主 | 2014-8-9 11:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我的ADC配置如下:
u16 ADC_ConvertedValue[M];
u16 ADC_ConvertedValueLocal[M];
u16 Upa,Upb,Upc;

void ADC1_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        ADC_InitTypeDef ADC_InitStructure;
        DMA_InitTypeDef DMA_InitStructure;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
        RCC_ADCCLKConfig(RCC_PCLK2_Div6);
       
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        DMA_DeInit(DMA1_Channel1);
        DMA_InitStructure.DMA_PeripheralBaseAddr=(ADC1->DR);
        DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&ADC_ConvertedValue;
        DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
        DMA_InitStructure.DMA_BufferSize=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);
       
       
        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_RegularChannelConfig(ADC1, ADC_Channel_4,1,ADC_SampleTime_239Cycles5);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_5,2,ADC_SampleTime_239Cycles5);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_6,3,ADC_SampleTime_239Cycles5);
       
        ADC_Cmd(ADC1, ENABLE);
        ADC_DMACmd(ADC1, ENABLE);
               
        ADC_ResetCalibration(ADC1);
        while(ADC_GetResetCalibrationStatus(ADC1));
        ADC_StartCalibration(ADC1);
        while(ADC_GetCalibrationStatus(ADC1));
        DMA_Cmd(DMA1_Channel1,ENABLE);
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
主函数如下:
u8 i;
int main(void)
{
        uart_init(9600);
        CHIP_PWM_Init(8999);
        ADC1_Init();
        LED_Init();
        while(1)
        {
                LED2=!LED2;
                printf("\r\n let's a test \r\n\r\n");
               
                change();

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

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

        }
}
采样后打印出来的结果,三路数据的值都是一样的。而且用示波器测量三路数据均为PWM波,波形如下,打印结果却显示三路数据永远都是高电平。请问这是为什么呢?

相关帖子

沙发
泡沫馨馨|  楼主 | 2014-8-9 11:05 | 只看该作者
我也尝试了正点原子例程里面的方法,使用了Get_Adc(u8 ch)
u16 Get_Adc(u8 ch)
{
        ADC_RegularChannelConfig(ADC1,ch,1,ADC_SampleTime_239Cycles5);

        ADC_SoftwareStartConvCmd(ADC1,ENABLE);
        while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
        return ADC_GetConversionValue(ADC1);
}
主函数中这样处理:
ADC_ConvertedValueLocal[0]=Get_Adc(ADC_Channel_4);
                printf("\r\n Upa=%d \r\n\r\n",ADC_ConvertedValueLocal[0]);
                ADC_ConvertedValueLocal[1]=Get_Adc(ADC_Channel_5);
                printf("\r\n Upb=%d \r\n\r\n",ADC_ConvertedValueLocal[1]);
                ADC_ConvertedValueLocal[2]=Get_Adc(ADC_Channel_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,实在是不知道到底哪里不对了%>_<%

使用特权

评论回复
5
airwill| | 2014-8-11 14:35 | 只看该作者
这连接不了调试器, 最大的可能性是硬件连接问题, 还有就是 SWD 的端口功能屏蔽掉了

使用特权

评论回复
6
hlong0033| | 2014-9-29 14:48 | 只看该作者
PWM的频率是多少,ADC的转换时间200uS左右

使用特权

评论回复
7
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

粉丝