搜索
bottom↓
回复: 28

求助:stm32f407vet6 adc采集值跳变很大,请问坛友这是怎么造成的

[复制链接]

出0入10汤圆

发表于 2020-7-7 09:21:10 | 显示全部楼层 |阅读模式
本帖最后由 lhshengshi 于 2020-7-7 10:13 编辑

求助:stm32f407vet6  adc采集值跳变很大,请问坛友这是怎么造成的

[2020/7/6 22:08:40 213]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:40 232]  adc now is 2511
adc now is 2515
adc now is 2527
adc now is 2528
adc now is 2545
adc now is 2540
adc now is 2539
adc now is 2568
adc now is 2598
adc now is 2627
humi  is  37 .............

[2020/7/6 22:08:41 211]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:41 223]  adc now is 2527
adc now is 2536
adc now is 2560
adc now is 2566
adc now is 2583
adc now is 2603
adc now is 2646
adc now is 2676
adc now is 2718
adc now is 2743
humi  is  35 .............

[2020/7/6 22:08:42 211]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:42 223]  adc now is 2579
adc now is 2616
adc now is 2655
adc now is 2682
adc now is 2724
adc now is 2758
adc now is 2784
adc now is 2811
adc now is 2852
adc now is 2879
humi  is  32 .............

[2020/7/6 22:08:43 214]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:43 238]  adc now is 2736
adc now is 2775
adc now is 2801
adc now is 2829
adc now is 2865
adc now is 2895
adc now is 2927
adc now is 2941
adc now is 2984
adc now is 3026
humi  is  29 .............

[2020/7/6 22:08:44 212]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:44 223]  adc now is 2891
adc now is 2920
adc now is 2946
adc now is 2977
adc now is 3006
adc now is 3033
adc now is 3070
adc now is 3100
adc now is 3128
adc now is 3147
humi  is  25 .............

[2020/7/6 22:08:45 275]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:45 313]  adc now is 3017
adc now is 3055
adc now is 3088
adc now is 3103
adc now is 3136
adc now is 3163
adc now is 3188
adc now is 3217
adc now is 3242
adc now is 3265
humi  is  22 .............

[2020/7/6 22:08:46 216]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:46 246]  adc now is 3160
adc now is 3184
adc now is 3199
adc now is 3232
adc now is 3243
adc now is 3266
adc now is 3274
adc now is 3298
adc now is 3290
adc now is 3303
humi  is  20 .............

[2020/7/6 22:08:47 216]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:47 244]  adc now is 3265
adc now is 3279
adc now is 3302
adc now is 3295
adc now is 3304
adc now is 3301
adc now is 3317
adc now is 3328
adc now is 3323
adc now is 3331
humi  is  18 .............

[2020/7/6 22:08:48 217]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:48 230]  adc now is 3304
adc now is 3319
adc now is 3319
adc now is 3323
adc now is 3326
adc now is 3324
adc now is 3337
adc now is 3335
adc now is 3327
adc now is 3345
humi  is  18 .............

[2020/7/6 22:08:49 220]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:49 227]  adc now is 3327
adc now is 3346
adc now is 3348
adc now is 3334
adc now is 3350
adc now is 3335
adc now is 3332
adc now is 3323
adc now is 3324
adc now is 3307
humi  is  18 .............

[2020/7/6 22:08:50 219]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:50 246]  adc now is 3331
adc now is 3332
adc now is 3332
adc now is 3311
adc now is 3311
adc now is 3295
adc now is 3303
adc now is 3301
adc now is 3288
adc now is 3295
humi  is  18 .............

[2020/7/6 22:08:51 222]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:51 228]  adc now is 3297
adc now is 3293
adc now is 3290
adc now is 3280
adc now is 3278
adc now is 3271
adc now is 3266
adc now is 3262
adc now is 3250
adc now is 3254
humi  is  19 .............

[2020/7/6 22:08:52 221]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:52 227]  adc now is 3262
adc now is 3266
adc now is 3254
adc now is 3239
adc now is 3231
adc now is 3235
adc now is 3230
adc now is 3234
adc now is 3208
adc now is 3209
humi  is  20 .............

[2020/7/6 22:08:53 225]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:53 237]  adc now is 3226
adc now is 3239
adc now is 3213
adc now is 3203
adc now is 3205
adc now is 3216
adc now is 3202
adc now is 3180
adc now is 3190
adc now is 3172
humi  is  21 .............

[2020/7/6 22:08:54 223]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:54 229]  adc now is 3183
adc now is 3191
adc now is 3176
adc now is 3163
adc now is 3163
adc now is 3168
adc now is 3151
adc now is 3158
adc now is 3144
adc now is 3152
humi  is  22 .............

[2020/7/6 22:08:55 222]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:55 228]  adc now is 3147
adc now is 3139
adc now is 3138
adc now is 3134
adc now is 3134
adc now is 3109
adc now is 3124
adc now is 3105
adc now is 3102
adc now is 3099
humi  is  23 .............

[2020/7/6 22:08:56 225]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:56 233]  adc now is 3115
adc now is 3105
adc now is 3110
adc now is 3098
adc now is 3097
adc now is 3095
adc now is 3081
adc now is 3096
adc now is 3071
adc now is 3038
humi  is  24 .............

[2020/7/6 22:08:57 227]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:57 230]  adc now is 3075
adc now is 3071
adc now is 3047
adc now is 3003
adc now is 2968
adc now is 2942
adc now is 2911
adc now is 2872
adc now is 2825
adc now is 2783
humi  is  27 .............

[2020/7/6 22:08:58 226]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:58 229]  adc now is 2851
adc now is 2813
adc now is 2780
adc now is 2749
adc now is 2720
adc now is 2695
adc now is 2651
adc now is 2641
adc now is 2600
adc now is 2566
humi  is  33 .............

[2020/7/6 22:08:59 227]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:08:59 230]  adc now is 2591
adc now is 2567
adc now is 2528
adc now is 2512
adc now is 2478
adc now is 2443
adc now is 2426
adc now is 2398
adc now is 2380
adc now is 2352
humi  is  39 .............

[2020/7/6 22:09:00 229]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:09:00 237]  adc now is 2345
adc now is 2329
adc now is 2318
adc now is 2301
adc now is 2287
adc now is 2282
adc now is 2290
adc now is 2275
adc now is 2272
adc now is 2263
humi  is  43 .............

[2020/7/6 22:09:01 230]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:09:01 238]  adc now is 2238
adc now is 2248
adc now is 2240
adc now is 2241
adc now is 2243
adc now is 2226
adc now is 2238
adc now is 2250
adc now is 2230
adc now is 2231
humi  is  45 .............

[2020/7/6 22:09:02 231]  COMB[\\.\COM24]接收208字节:
[2020/7/6 22:09:02 239]  adc now is 2224
adc now is 2226
adc now is 2240
adc now is 2236
adc now is 2248
adc now is 2243
adc now is 2251
adc now is 2270
adc now is 2259
adc now is 2278
humi  is  44 .............

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入20汤圆

发表于 2020-7-7 09:24:30 | 显示全部楼层
1、基准电压是否稳定,可以用示波器看,是否叠加了干扰信号。2、检测端,示波器看是否叠加干扰信号。3、1,2均没有的话,再检查程序。

出16170入6148汤圆

发表于 2020-7-7 09:25:45 来自手机 | 显示全部楼层
打赏!

庆祝论坛“打赏”功能实施, 现在开始发技术主题,可以获得打赏
https://www.amobbs.com/thread-5735948-1-1.html

出0入10汤圆

 楼主| 发表于 2020-7-7 10:00:35 | 显示全部楼层
本帖最后由 lhshengshi 于 2020-7-7 10:04 编辑
mon51 发表于 2020-7-7 09:24
1、基准电压是否稳定,可以用示波器看,是否叠加了干扰信号。2、检测端,示波器看是否叠加干扰信号。3、1, ...


电源纹波峰峰值35mv左右 ,是不是这个纹波对于adc来说有点大 电源用的24v+xl1509->5v+MP2161->3.3v  电源布线感觉问题不大 但是1509纹波是有一点点的  不如mp1584这种 ,请问这样的纹波会不会造成stm32f407的adc不稳定

湿度采集入下图所示 使用f103的板子 感觉是挺稳定的 f407的板子就不稳定,两个板子是两个人布局,程序也是两个人写的 但是读取的原始adc值就是f103较为未定,f407波动较大,我感觉布线的问题不太大,地平面还算完整

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入9汤圆

发表于 2020-7-7 10:03:49 | 显示全部楼层
看电路,基准的稳定度肯定有影响的:3.3V然后经过一个4148二极管给VREF供电,而且所有数字VDD和VREF连接在一起;
另外,VSSA我记得是模拟的地,你确定连接到3.3V还能正常工作?

出0入10汤圆

 楼主| 发表于 2020-7-7 10:05:00 | 显示全部楼层
wanggoals 发表于 2020-7-7 10:03
看电路,基准的稳定度肯定有影响的:3.3V然后经过一个4148二极管给VREF供电,而且所有数字VDD和VREF连接在 ...

这里经过的是一个磁珠  不是二极管

出0入33汤圆

发表于 2020-7-7 10:20:07 | 显示全部楼层
Vref+引脚都有,好歹也接个基准啊,哪怕是431也好。

出0入20汤圆

发表于 2020-7-7 11:03:14 | 显示全部楼层
不想用基准,给你一个图,VREF+-的处理。波纹很小。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入10汤圆

 楼主| 发表于 2020-7-7 11:16:55 | 显示全部楼层
mon51 发表于 2020-7-7 11:03
不想用基准,给你一个图,VREF+-的处理。波纹很小。

感谢!!/抱拳

出0入17汤圆

发表于 2020-7-7 11:29:17 来自手机 | 显示全部楼层
m4核心太复杂,本来就没有m3的稳定

出0入17汤圆

发表于 2020-7-7 11:48:44 来自手机 | 显示全部楼层
楼上说的是adc得数值稳定性。

出0入10汤圆

 楼主| 发表于 2020-7-7 13:12:21 | 显示全部楼层
mon51 发表于 2020-7-7 11:03
不想用基准,给你一个图,VREF+-的处理。波纹很小。

这里的电阻边上的10 和1  是指10r和1r吧?

出0入20汤圆

发表于 2020-7-7 15:44:05 | 显示全部楼层
lhshengshi 发表于 2020-7-7 13:12
这里的电阻边上的10 和1  是指10r和1r吧?

是电阻值,还可以加大些。

出0入10汤圆

 楼主| 发表于 2020-7-7 16:12:50 | 显示全部楼层
mon51 发表于 2020-7-7 15:44
是电阻值,还可以加大些。

加大效果会更好些么  请问你在工程中现在常用多大的?

出0入20汤圆

发表于 2020-7-7 16:22:39 | 显示全部楼层
最大:R14=100,R13=10  ,实际使用:R14=51,R13=10  ;精度1%;温漂要小;与PCB布线有一定关系。R14还用过贴片电感,小磁环,但它们只能阻挡一定高频下的扰动,用电阻全部挡住。实际上就是一个Π型滤波器。   

出0入20汤圆

发表于 2020-7-7 16:26:36 | 显示全部楼层
你可以先用一个精密VREF-PCB(电池供电,共地)挂上去,采集端用电阻接到VREF端,测试一下ADC的原值。如果还乱跳,就要换块PCB测试了。如果还是乱跳,最好把程序贴出来看看。

出0入20汤圆

发表于 2020-7-7 16:35:50 | 显示全部楼层
这是STM32F205的ADC,采集程序,希望有用。
#include        "use.h"

float Get_Temper(unsigned int Temper);
//**************************************************************
#define Max_U_5V                                (5.02)
#define Max_U_ao                                (0.47)
//**************************************************************
#define U_K                                                        ((float)Max_U_5V/(float)Max_U_ao)                        //分压比!
//*********************************************************************
#define ADC_Converted_cnt 202                //公用64个数据运行算+2个极值数据(去掉)
#define ADC_CH                                                9                        //1,2,...8, 9=dc,10=wendu 通道数
static unsigned short ADC_Value[ ADC_CH*ADC_Converted_cnt ]; //adc获取的原始数据
static float Uo[ADC_CH]; //数据:12v电压,电池电压,基准电压,温度
/*********************************************************************************************************

************************************************************
**函数名称:uint8 SetADnCH()
**函数功能:设置AD转换通道
**入口参数:CHn     AD通道(10-13)
**出口参数:
**说明: ad=10,11,12,13          对应:PC0,1,2,3口
********************************************************************************************************/

//********************************************************************************************************/
void SetAD_CH(void){
   GPIO_InitTypeDef GPIO_InitStructure;
   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_1 | GPIO_Pin_5;// | GPIO_Pin_7;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOC, &GPIO_InitStructure);        //用PC口
}
//**************************************************************
//个模式先读取Ad的值处理
/**
  * @brief  ADC1 Channel Vbat configuration (DMA, ADC, CLK)
  * @param  None
  * @retval None
  */
void Init_AD(void){
  ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;
   
  /* Enable peripheral clocks *************************************************/
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  /* DMA2_Stream0 channel0 configuration **************************************/
  DMA_DeInit(DMA2_Stream0);
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;  
  DMA_InitStructure.DMA_PeripheralBaseAddr =(unsigned int)&(ADC1->DR);// (uint32_t)ADC1_DR_ADDRESS;
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADC_Value;
       
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
       
  DMA_InitStructure.DMA_BufferSize = ADC_CH*ADC_Converted_cnt;
       
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//DMA_MemoryInc_Disable;
  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_Low;
  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);
  /* DMA2_Stream0 enable */
  DMA_Cmd(DMA2_Stream0, ENABLE);
   
  /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode =ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode =  ENABLE;//DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //关闭外部触发源!!!
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;  //这个没有用啊。
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = ADC_CH;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* Enable ADC1 DMA */
// ADC_DMACmd(ADC1, ENABLE);
  
  /* ADC1 regular channel18 (VBAT) configuration ******************************/
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_480Cycles);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_480Cycles);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_480Cycles);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 4, ADC_SampleTime_480Cycles);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 5, ADC_SampleTime_480Cycles);   //NTC电压!!
        ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 6, ADC_SampleTime_480Cycles);  //工作电压
  ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 7, ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_Vrefint, 8, ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_Vbat, 9, ADC_SampleTime_480Cycles);

  /* Enable VBAT channel */
  ADC_VBATCmd(ENABLE);
        ADC_TempSensorVrefintCmd(ENABLE);
  /* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

/* Enable ADC3 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  /* Enable ADC1 **************************************************************/
  ADC_Cmd(ADC1, ENABLE);
  ADC_SoftwareStartConv(ADC1);
}
//***************************************************************************************************
//模拟读AD值!        参考电压=3.3v                  U=Val*1000/1024*3.3
//***************************************************************************************************
void AD_Start(void){
        SetAD_CH();
        Init_AD();
}
//******************************************************************************
//AD数据转换处理!
void Get_ADC_Covert_Value(unsigned int *p){
    unsigned char i,j;
    unsigned int MAX ;
    unsigned int MIN ;
    unsigned int adc_temp;
    unsigned int   adc_sum;
       
   for(j=0;j<ADC_CH;j++){                         //读取4个通道数据
             adc_sum = 0;                                                 //总数值初始化
            MAX = 0;                                                          //最大值初始化
            MIN = 0xffffffff;                         //最小值初始化
      for(i=0;i<ADC_Converted_cnt;i++){
            adc_temp = ADC_Value[j+i*ADC_CH];
            adc_sum += adc_temp; //累加
            if( adc_temp > MAX ) MAX = adc_temp;
            if( adc_temp < MIN ) MIN = adc_temp;
        }       
        adc_sum -= MAX ; //去掉最大值
        adc_sum -= MIN ; //去掉最小值
        p[j] = adc_sum / (ADC_Converted_cnt - 2); //求均值
    }
}
//**********************************************************************************
void Get_Value_AD(void){
        unsigned int u[ADC_CH];
        unsigned char i;
       
        Get_ADC_Covert_Value(u);
        //**************************************************
        //2013-5-13 添加处理
        for(i=0;i<ADC_CH;i++){
                Uo[i]=(float)u[i]*MAX_U_VREF/(1024*4);//得到电压值 MV       
        }
}
//********************************************************************
//传出数据  2014-1-18
void HW_CpuHeatInit( void ){         // CPU内部温度检测初始化   
                AD_Start();
}
//**********************************************************************
//温度处理
//        Uo[1] = (Uo[1]-760)/2.5 + 25;   //760
signed char HW_GetCpuHeat( void ){    // 获取CPU内部温度值   
        return (signed char )((Uo[6]-760)/2.5 + 25);
}
//************************************************************************
//稳压电源输入的电源
float HW_GetWorkVoltage( void ){    // 获取工作值 v
        return Uo[5]*U_K/1000;
}
//************************************************************************
float HW_GetBatteryVoltage( void ){    // 获取电池值   v
        return Uo[8]*2/1000;                                                                //VBAT要乘以2  500=>U*2/1000;
}
//*****************************************************************************
//计算NTC的电压值,单位MV
float Get_NTC_Value(void){
                return Uo[4];
}
//*****************************************************************************
//4路ADC采集数据处理,注意分清楚是电压分压还是电流分流
//初始电压在变量Uo[0-3] 内  分压比是1:1
//*****************************************************************************
void Get_ADC_Value_U (float *p){
        *p++        =Uo[0]/1000/I1_k; //得到V!值之后,转换成电流!!
        *p++        =Uo[1]/1000/I2_k; //得到V!值之后,转换成电流!!
        *p++        =Uo[2]/1000/I3_k; //得到V!值之后,转换成电流!!
        *p                =Uo[3]/1000/I4_k; //得到V!值之后,转换成电流!!
}







出0入20汤圆

发表于 2020-7-7 16:36:23 | 显示全部楼层
#ifndef __ADC_1_USE
#define  __ADC_1_USE

//********************************************************************
//传出数据  2014-1-18
void HW_CpuHeatInit( void );        // CPU内部温度检测初始化   
//**********************************************************************
signed char HW_GetCpuHeat( void );   // 获取CPU内部温度值   

//************************************************************************
float HW_GetWorkVoltage( void );    // 获取工作值 v
//************************************************************************
float HW_GetBatteryVoltage( void );    // 获取电池值   v

#endif

出0入20汤圆

发表于 2020-7-7 16:41:57 | 显示全部楼层
这是STM32L152的采集程序:
#include        "use.h"
#include        "Ver.H"

//****************************************************
#define ADC_Converted_cnt 34                //公用64个数据运行算+2个极值数据(去掉)
#define ADC_CH                                                2                        //通道数
unsigned short ADC_Value[ ADC_CH*ADC_Converted_cnt ]; //adc获取的原始数据
/*********************************************************************************************************
************************************************************
**函数名称:uint8 SetADnCH()
**函数功能:设置AD转换通道
**入口参数:CHn     AD通道(10-13)
**出口参数:
**说明: ad=10,11,12,13          对应:PC0,1,2,3口
********************************************************************************************************/
void SetAD_CH(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1 ;//| GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOC, &GPIO_InitStructure);        //用PC口
}
//*****************************************************************************
//DMA初始化
void Init_DMA(void){
                DMA_InitTypeDef DMA_InitStructure;
   
    /* Enable DMA1 clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    /* DMA channel1 configuration ----------------------------------------------*/
    DMA_DeInit(DMA1_Channel1);
    DMA_InitStructure.DMA_PeripheralBaseAddr =(unsigned int)&(ADC1->DR);//ADC1_DR_Address;      // 外设地址
    DMA_InitStructure.DMA_MemoryBaseAddr = (unsigned int)&ADC_Value; // 内存地址
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;               // DMA 传输方向单向
    DMA_InitStructure.DMA_BufferSize = ADC_CH*ADC_Converted_cnt;         // 设置DMA在传输时缓冲区的长度 word
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 设置DMA的外设递增模式,一个外设
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;         // 设置DMA的内存递增模式
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;  // 外设数据字长
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;          // 内存数据字长
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;     // 设置DMA的传输模式:连续不断的循环模式
    DMA_InitStructure.DMA_Priority = DMA_Priority_High; // 设置DMA的优先级别
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;        // 设置DMA的2个memory中的变量互相访问
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    /* Enable DMA channel1 */
    DMA_Cmd(DMA1_Channel1, DISABLE);
    DMA_Cmd(DMA1_Channel1, ENABLE);
}

//*****************************************************************************
//采用自动连续转换 DMA存放数据
void Init_AD(void){
        ADC_InitTypeDef ADC_InitStructure;
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        /* ADCCLK = PCLK2/8 */
        //RCC_ADCCLKConfig(RCC_PCLK2_Div8);
        /* Enable ADC1 clock */
        //RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
        //******************************************************************       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);                 
        /* Enable the temperature sensor and vref internal channel */
        ADC_TempSensorVrefintCmd(ENABLE); //ENABLE
        /* Setup ADC common init struct */
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
        ADC_CommonInit(&ADC_CommonInitStructure);

        ADC_StructInit(&ADC_InitStructure);
        /* ADC1 configuration ------------------------------------------------------*/
        //ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;   // 独立工作模式
        // ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;                          // Disable Continuous conversion
        ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;                  // Set conversion resolution to 12bit
        ADC_InitStructure.ADC_ScanConvMode = ENABLE;         // 扫描方式
        ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;   // 连续转换
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None ;//ADC_ExternalTrigConv_None; // 外部触发禁止
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;              // 数据右对齐
        ADC_InitStructure.ADC_NbrOfConversion = ADC_CH;              // 用于转换的通道数
        ADC_Init(ADC1, &ADC_InitStructure);

        /* ADC1 regular channel16 configuration */   // 55Cycles5  采样频率: 15.086us
        ADC_RegularChannelConfig(ADC1, ADC_Channel_1 , 1, ADC_SampleTime_384Cycles  );
        ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor , 2, ADC_SampleTime_384Cycles  );
        //   ADC_RegularChannelConfig(ADC1, ADC_Channel_12 , 3, ADC_SampleTime_55Cycles5  );
        //   ADC_RegularChannelConfig(ADC1, ADC_Channel_13 , 4, ADC_SampleTime_55Cycles5  );

        /* Enable the request after last transfer for DMA Circular mode */
        ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
        /* Enable ADC1 DMA */
        //ADC_DMACmd(ADC1, ENABLE);
        /* Enables the ADC1 Power Down during Delay */
        //ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE);

        /* Delay until the converted data has been read */
        //ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze);       
        //********************************************
        /* Enable ADC1 */
        //////////////////////////////////////////////////////////////////////////////////////
        Init_DMA();
        /* Disable DMA mode for ADC1 */
        ADC_DMACmd(ADC1, DISABLE);
        /* Enable ADC1 DMA */
        ADC_DMACmd(ADC1, ENABLE);
        ADC_Cmd(ADC1, ENABLE);   
                                                                                       
        /* Wait until the ADC1 is ready */
        while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET);
        /* Start ADC1 Software Conversion */
        ADC_SoftwareStartConv(ADC1);
}
//***************************************************************************************************
//模拟读AD值!        参考电压=3.3v                  U=Val*1000/1024*3.3
//***************************************************************************************************
void AD_Start(void){
//        unsigned char i;
       
        /* Enable the HSI oscillator */
        RCC_HSICmd(ENABLE);    //必须打开,ADC使用的!
//        for(i=0;i<(ADC_CH*ADC_Converted_cnt);i++) ADC_Value[i]=0;
        /* Check that HSI oscillator is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
        //**********************************************************
        SetAD_CH();
        Init_AD();
}
//******************************************************************************
//关闭ADC,DMA的电源,时钟!
void PowerDown_ADC_DMA(void){
  /* Disable DMA channel1 */
  DMA_Cmd(DMA1_Channel1, DISABLE);  
  /* Disable ADC1 */
  ADC_Cmd(ADC1, DISABLE);

  /* Disable ADC1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, DISABLE);  
  /* Disable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, DISABLE);
        //*****************************************************
        //关闭Hsi
        RCC_HSICmd(DISABLE);    //,ADC使用的!
}

//******************************************************************************
//AD数据转换处理!
void Get_ADC_Covert_Value(unsigned short *p){
    unsigned char i,j;
    unsigned short MAX ;
    unsigned short MIN ;
    unsigned short adc_temp;
    unsigned int   adc_sum;
       

   for(j=0;j<ADC_CH;j++){                 //读取4个通道数据
             adc_sum = 0;                 //总数值初始化
            MAX = 0;                          //最大值初始化
            MIN = 0xffff;                 //最小值初始化
      for(i=0;i<ADC_Converted_cnt;i++){
            adc_temp = ADC_Value[j+i*ADC_CH];
            adc_sum += adc_temp; //累加
            if( adc_temp > MAX ) MAX = adc_temp;
            if( adc_temp < MIN ) MIN = adc_temp;
        }       
        adc_sum -= MAX ; //去掉最大值
        adc_sum -= MIN ; //去掉最小值
        p[j] = adc_sum / (ADC_Converted_cnt - 2); //求均值
    }
}
//**********************************************************************************
void Get_U_AD(void){
        unsigned short u[ADC_CH];
        float f1;
        static float uo[ADC_CH]={0,0};

        Get_ADC_Covert_Value(u);
        f1=(float)u[0]*MAX_U_REF/(1024*4);//得到电压值 MV
        //*************************************************
        //加入数字滤波
        if(fabs(f1-uo[0])>0.1)        uo[0]=f1;        //TEST
        LedMenu.Uo=uo[0]*10/1000;                                //电池电压V 电阻比值:2000k:200K=10
        f1=Get_Temper(u[1]);
        if(fabs(f1-uo[1])>0.1)        uo[1]=f1;        //TEST
        LedMenu.Co=uo[1];
        //**************************************************
        PowerDown_ADC_DMA();
}






出0入10汤圆

 楼主| 发表于 2020-7-7 21:02:03 | 显示全部楼层
mon51 发表于 2020-7-7 16:41
这是STM32L152的采集程序:
#include        "use.h"
#include        "Ver.H"

感谢兄弟的建议和代码   /抱拳  

出300入477汤圆

发表于 2020-7-7 21:53:47 来自手机 | 显示全部楼层
lhshengshi 发表于 2020-7-7 21:02
感谢兄弟的建议和代码   /抱拳

st自己的手册都看完没?
STM32F4自己内部的AD原本跳变就很大,你做的再好也会跳很大。官方都说的很清楚了

出300入477汤圆

发表于 2020-7-7 21:56:36 来自手机 | 显示全部楼层
https://www.amobbs.com/thread-5684139-1-1.html

出300入477汤圆

发表于 2020-7-7 22:03:26 来自手机 | 显示全部楼层
如果你能做到跳动20多个字,就算过关了。想要更好的精度就得靠多采一点数据用算法处理了。厂家告诉你的跳动就是20个字。你不可能比厂家做的更好

出0入0汤圆

发表于 2020-7-8 09:09:53 | 显示全部楼层
突然想到,你换个引脚试试,我自己测试PA0波动也是比较大的,但是类似PB0、PB1这些效果就好很多。

出0入0汤圆

发表于 2020-7-8 10:11:53 | 显示全部楼层
好像ST的IC就没有AD值跳动不大的

出300入477汤圆

发表于 2020-7-8 10:14:47 来自手机 | 显示全部楼层
f117_2r 发表于 2020-7-8 10:11
好像ST的IC就没有AD值跳动不大的

F103就很好,算是我用过的单片机内置AD里面很好的12位了。F407则是我用过的非常差的一个。

出0入10汤圆

 楼主| 发表于 2020-10-9 22:03:01 | 显示全部楼层
已解决 stm32f407adc简单的应用没有问题 是lm258比较挑剔电源 重新选择电源后就好多了

出300入477汤圆

发表于 2020-10-10 10:12:22 来自手机 | 显示全部楼层
lhshengshi 发表于 2020-10-9 22:03
已解决 stm32f407adc简单的应用没有问题 是lm258比较挑剔电源 重新选择电源后就好多了 ...

按你最好的效果,原始数据的跳动有多少个字?

出0入0汤圆

发表于 2020-11-20 16:56:46 | 显示全部楼层
mark........
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-27 02:31

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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