STM32 Introductory Learning ADC (STM32F030F4P6 based Coocox IDE)

Source: Internet
Author: User
Tags reset
#include "stm32_lib/inc/stm32f0xx_rcc.h" #include "stm32_lib/inc/stm32f0xx_adc.h" #include "stm32_lib/inc/stm32f0xx
	_gpio.h "int main (void) {//Clock configuration rcc_ahbperiphclockcmd (Rcc_ahbperiph_gpioa, ENABLE);

	Rcc_apb2periphclockcmd (rcc_apb2periph_adc1,enable);
	ADC IO configuration, where the PA0 port is defined as the ADC ports Gpio_inittypedef PORT_ADC; PORT_ADC.
	Gpio_pin=gpio_pin_0; PORT_ADC.
	Gpio_mode=gpio_mode_an; PORT_ADC.
	Gpio_pupd=gpio_pupd_nopull;

	Gpio_init (GPIOA,&PORT_ADC);
	ADC parameter configuration adc_inittypedef adc_initstuctrue; ADC_INITSTUCTRUE.ADC_RESOLUTION=ADC_RESOLUTION_12B;//12 bit accuracy adc_initstuctrue.adc_continuousconvmode=disable;//
	One-time ADC adc_initstuctrue.adc_externaltrigconvedge=adc_externaltrigconvedge_none; adc_initstuctrue.adc_dataalign=adc_dataalign_right;//Data right-justified adc_initstuctrue.adc_scandirection=adc_scandirection

	_backward;//Data Coverage adc_init (adc1,&adc_initstuctrue); Configure the channel and sample period for ADC sampling//PA0 for the ADC Channel 0//Note that the accuracy of the data collected is related to the sampling time Adc_channelconfig (adc1,adc_channel_0,adc_sampletime_239_
	5Cycles); If the internal temperature of the system is collected, the channel is16, at the same time to enable the temperature sensor//adc_channelconfig (adc1,adc_channel_16,adc_sampletime_239_5cycles);

	Adc_tempsensorcmd (ENABLE);
	Calibration Adc_getcalibrationfactor (ADC1);
	Enable Adc_cmd (adc1,enable);
	Wait for the ADC to prepare while (Adc_getflagstatus (Adc1,adc_flag_aden) ==reset);
	Software to start ADC conversion adc_startofconversion (ADC1);
	Wait for the ADC to complete while (Adc_getflagstatus (ADC1,ADC_FLAG_EOC) ==reset);

	The resulting data is the ADC data unsigned short adc_data=adc_getconversionvalue (ADC1); /*//If the acquisition is 16 channels, that is the chip temperature, the value of the temperature is as follows//below this price, refer to the STM32F030 Data Sheet (register version) a.7.16///factory calibration data stored address, 30°c and 110°c ADC value #define Temp110_ Cal_addr ((uint16_t*) ((uint32_t) 0x1ffff7c2)) #define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1ffff7b8) #define VDD
	_calib (uint16_t) #define VDD_APPLI ((uint16_t)) int32_t temperature;
	Temperature = (((int32_t) ADC1->DR * vdd_appli/vdd_calib)-(int32_t) *temp30_cal_addr);
	Temperature = temperature * (int32_t) (110-30);
	Temperature = temperature/(int32_t) (*temp110_cal_addr-*temp30_cal_addr); Temperature = TemperaturE + 30;
 */while (1) {}}

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.