Use and source of sharp gp2y1010au0f dust sensor under STM32 platform

Source: Internet
Author: User

The gp2y1010au0f sensor is wired as follows:

Using PA0 to receive the analog signal, the PB1 sends a pulse signal.

The code is as follows:


Gp2y.c

#include   "GP2Y.h" #include   "system.h" u16 ad_pm;void gp2yinit (void) { //define variables      adc_inittypedef a_initstructure;    gpio_inittypedef g_initstructure ;//pa0    gpio_inittypedef gpio_initstructure;//pb1    rcc_ Apb2periphclockcmd (rcc_apb2periph_gpioa | rcc_apb2periph_adc1 , enable )    //enable ADC2 channel clock           rcc_adcclkconfig (RCC_PCLK2_DIV6)    //set ADC divide factor 6 72m/6=12, ADC maximum time cannot exceed 14m     //pa0  as analog channel input pin                               g_initstructure.gpio_pin = gpio_pin_0;    g_initstructure.gpio _mode = gpio_mode_ain; //analog Input pin     gpio_init (gpioa, &G_initstructure);        adc_deinit (ADC1);   //reset ADC1, Peripherals  ADC1 All registers of   are set to default values        a_initstructure.adc_mode = adc_mode_ INDEPENDENT; //ADC working mode: ADC1 and ADC2 work in standalone mode     a_initstructure.adc_scanconvmode =  disable; //analog-to-digital conversion works in single-channel mode     A_InitStructure.ADC_ContinuousConvMode =  disable; //analog-to-digital conversion works in a single conversion mode     A_INITSTRUCTURE.ADC_EXTERNALTRIGCONV = ADC_ externaltrigconv_none; //conversion triggered by software instead of externally     A_InitStructure.ADC_DataAlign =  ADC_DATAALIGN_RIGHT; //ADC Data right-justified     A_InitStructure.ADC_NbrOfChannel = 1;  //number of ADC channels     adc_init (adc1, &a_initstructure) for sequential rules conversion;  //according to Adc_ The parameters specified in the Initstruct initialize the registers of the peripheral adcx             adc_cmd ( adc1, enable);         //enables the Adc1    adc_resetcalibration (ADC1) to be specified;  Enable Reset Calibration      while (adc_getresetcalibrationstatus (ADC1))  //wait for reset calibration End      adc_startcalibration (ADC1);   //turn on ad calibration     while (ADC_ Getcalibrationstatus (ADC1))   //wait for calibration to end     gpio_initstructure.gpio_pin =  GPIO_Pin_1;                      gpio_initstructure.gpio_mode = gpio_mode_out_pp;//push-Pull output        Gpio_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;                gpio_init (gpiob, &gpio_initstructure);     gp2y_high;} void getgp2y (void) {float pm; Gp2y_low;delay_us (280); AD_PM = GET_ADC (ADC_CHANNEL_0);//pa0delay_us (40);Gp2y_high;delay_us (9680);p m = 0.17*ad_pm-0.1; //voltage-Dust conversion printf ("%f\n", PM);} 

GP2Y.h

#ifndef __gp2y_h#define __gp2y_h#include "stm32f10x_lib.h" #define Gp2y_high gpio_setbits (gpiob,gpio_pin_1); #define Gp2y_low gpio_resetbits (gpiob,gpio_pin_1); void gp2yinit (void); void getgp2y (void); #endif

PB1 Output Waveform

Gets the dust value.


Use and source of sharp gp2y1010au0f dust sensor under STM32 platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.