OpenEdv-开源电子网

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

独立ADC多通道采集,电压值不准。求助大神,想不出啊!

[复制链接]

3

主题

17

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
278
金钱
278
注册时间
2016-9-28
在线时间
82 小时
发表于 2016-11-11 22:20:08 | 显示全部楼层 |阅读模式
1金钱
ADC1采集4通道的12位电压值,通过DMA传输,电压偏差太大,不知道什么原因,求大神解答


以下是部分程序


[mw_shl_code=c,true]//初始化GPIO  PA0,PA1,PA6,PA7作为采集通道                                                                                                                  
void MYADC_GPIO_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;       
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_6|GPIO_Pin_7;//PA0 PA1 PA6 PA7
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AN;//模拟输入
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;        //浮空
        GPIO_Init(GPIOA,&GPIO_InitStructure);

}
void MYADC_Init_Config(void)
{
        ADC_CommonInitTypeDef ADC_CommonInitStructrue;
        ADC_InitTypeDef ADC_InitStructrue;

       
        // 开启ADC时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);

        ADC_CommonInitStructrue.ADC_Mode=ADC_Mode_Independent;//独立模式
        ADC_CommonInitStructrue.ADC_Prescaler=ADC_Prescaler_Div4;//4分频
        ADC_CommonInitStructrue.ADC_DMAAccessMode=ADC_DMAAccessMode_1;//DMA模式1
        ADC_CommonInitStructrue.ADC_TwoSamplingDelay=ADC_TwoSamplingDelay_10Cycles;
        ADC_CommonInit(&ADC_CommonInitStructrue);
       
        ADC_InitStructrue.ADC_Resolution=ADC_Resolution_12b;
        ADC_InitStructrue.ADC_ScanConvMode=ENABLE;//扫描模式
        ADC_InitStructrue.ADC_ContinuousConvMode=ENABLE;//连续模式
        ADC_InitStructrue.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;//不需要外部触发
        ADC_InitStructrue.ADC_DataAlign=ADC_DataAlign_Right;//数据方向最右
        ADC_InitStructrue.ADC_NbrOfConversion=4;//通道数量
        ADC_Init(ADC1,&ADC_InitStructrue);
       
        //转换的通道、顺序
        ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_56Cycles);//第一次转换通道0
        ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_56Cycles);//第二次转换通道1
        ADC_RegularChannelConfig(ADC1,ADC_Channel_6,3,ADC_SampleTime_56Cycles);//第三次转换通道3
        ADC_RegularChannelConfig(ADC1,ADC_Channel_7,4,ADC_SampleTime_56Cycles);//第四次转换通道4
       

       
        ADC_Cmd(ADC1,ENABLE);//使能ADC
        ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);// 使能DMA请求
       
        ADC_DMACmd(ADC1,ENABLE);
          
        ADC_SoftwareStartConv(ADC1);//开始adc转换,软件触发
       
}

[/mw_shl_code]

以下是main()函数部分内容
[mw_shl_code=c,true]                for(i=0;i<2;i++)//通道0 1
                {
                        ADC_Conversion_Value=(float)ADC_DMA_VALUE*(3.3/4096);
                        printf("ADC1的通道%d的电压值:  %f\r\n",i,ADC_Conversion_Value);                       
                }
                for(i=2;i<4;i++)//通道6 7
                {
                        ADC_Conversion_Value=(float)ADC_DMA_VALUE*(3.3/4096);
                        printf("ADC1的通道%d的电压值:  %f\r\n",(i+4),ADC_Conversion_Value);                       
                }
                printf("\r\n");[/mw_shl_code]


12.jpg

最佳答案

查看完整内容[请看2#楼]

以解决,GPIO的速度应低于50Mhz
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

3

主题

17

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
278
金钱
278
注册时间
2016-9-28
在线时间
82 小时
 楼主| 发表于 2016-11-11 22:20:09 | 显示全部楼层
以解决,GPIO的速度应低于50Mhz
回复

使用道具 举报

3

主题

17

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
278
金钱
278
注册时间
2016-9-28
在线时间
82 小时
 楼主| 发表于 2016-11-11 22:26:02 | 显示全部楼层
DMA的代码
[mw_shl_code=c,true]//par:外设地址,mar:SRAM地址,data_amount:传输的数据长度
void MYDMA_Init(u32 par,u32 mar,u16 data_amount)
{
        DMA_InitTypeDef DMA_InitStructrue;
       
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2时钟使能
        DMA_DeInit(DMA2_Stream0);
        while(DMA_GetCmdStatus(DMA2_Stream0)!=DISABLE);//等待DMA可以配置
       

        DMA_InitStructrue.DMA_Channel=DMA_Channel_0;
        DMA_InitStructrue.DMA_PeripheralBaseAddr= par;//外设地址
        DMA_InitStructrue.DMA_Memory0BaseAddr=mar;//外设地址
        DMA_InitStructrue.DMA_DIR=DMA_DIR_PeripheralToMemory;
        DMA_InitStructrue.DMA_BufferSize=data_amount;//数据传输量
        DMA_InitStructrue.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
        DMA_InitStructrue.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式
        DMA_InitStructrue.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设数据长度:16位
        DMA_InitStructrue.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//存储器数据长度:16位
        DMA_InitStructrue.DMA_Mode = DMA_Mode_Circular;// 模式选择
        DMA_InitStructrue.DMA_Priority = DMA_Priority_Medium;//中等优先级
        DMA_InitStructrue.DMA_FIFOMode = DMA_FIFOMode_Disable;         
        DMA_InitStructrue.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
        DMA_InitStructrue.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输
        DMA_InitStructrue.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
        DMA_Init(DMA2_Stream0,&DMA_InitStructrue);
       
        DMA_Cmd(DMA2_Stream0, ENABLE);                      //开启DMA传输
}[/mw_shl_code]
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-3-1 09:39

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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