找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

帖子
查看: 3373|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

stm32f10xADC的平均算法

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:379039 發(fā)表于 2018-7-25 17:08 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
主函數(shù)
#include "adc.h"
#include "delay.h"
#include "stm32f10x.h"
#include "usart.h"


int main(void)
{
   u16 adc;
         float temp;
        delay_init();
        uart_init(115200);
        Adc_Init();
        while(1)
        {
          adc = Get_Adc_Average(ADC_Channel_1,10);
                temp = (float) adc *(3.3/15520);         
                            
        }
       
ADC.c 文件
#include "adc.h"
#include "delay.h"


void Adc_Init()
{
        GPIO_InitTypeDef GPIO_InitStruct;
        ADC_InitTypeDef ADC_InitStruct;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
        RCC_ADCCLKConfig(RCC_PCLK2_Div6);            
       
                GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1;
                GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
                GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AIN;
                GPIO_Init(GPIOA,&GPIO_InitStruct);
       
       
          ADC_DeInit(ADC1);
       
          ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;   
          ADC_InitStruct.ADC_ScanConvMode=DISABLE;         
          ADC_InitStruct.ADC_ContinuousConvMode=DISABLE;   
          ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
          ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;
          ADC_InitStruct.ADC_NbrOfChannel=1;               
          ADC_Init(ADC1,&ADC_InitStruct);
               
               
               
    ADC_Cmd(ADC1,ENABLE);

                ADC_ResetCalibration(ADC1);               
                while(ADC_GetResetCalibrationStatus(ADC1))
                ADC_StartCalibration(ADC1);               
                while(ADC_GetCalibrationStatus(ADC1));   
       
}
       
        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);
        }
        u16 Get_Adc_Average(u8 ch,u8 times)
        {
            u32 temp_value=0;
                u8 t;
                        for(t=0;t<10;t++)
                {
                                temp_value +=Get_Adc(ch);
                                delay_ms(5);
                }
          return temp_value / times;
  }

.h文件

#ifndef _ADC_H
#define _ADC_H
#include "sys.h"

void Adc_Init(void);

        u16 Get_Adc(u8 ch);
       
  u16 Get_Adc_Average(u8 ch,u8 times);


#endif




評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表