Stm32--adc_stm32

Source: Internet
Author: User

Turn from: http://www.cnblogs.com/hnrainll/archive/2011/01/18/1937888.html

The 12-bit ADC is a successive approximation of the analog digital digital converter. It has up to 18 channels, measuring 16 external and 2 internal signal sources.

ADC input clock must not exceed 14MHZ, it is produced by PCLK2 by frequency division.

If the analog voltage converted by the ADC is lower than the low threshold or higher than the high threshold, the AWD simulated watchdog state bit is set.

ADC is typically used with DMA this is just a simple use of library configuration ADC continuous scan to achieve ADC applications.

First configure the GPIO with the ADC clock:

Adc_inittypedef adc_initstructure;
Gpio_inittypedef gpio_initstructure;

Rcc_apb2periphclockcmd (rcc_apb2periph_adc1,enable);
Rcc_apb2periphclockcmd (rcc_apb2periph_gpiob,enable);

Gpio_initstructure.gpio_pin =gpio_pin_1;
Gpio_initstructure.gpio_mode =gpio_mode_ain;
Gpio_init (gpiob,&gpio_initstructure); The default speed is two megabytes

To configure the operation of the ADC:

Adc_initstructure.adc_mode = adc_mode_independent; Standalone mode
Adc_initstructure.adc_scanconvmode =disable; Continuous multi-channel mode
Adc_initstructure.adc_continuousconvmode =enable; Continuous conversion
Adc_initstructure.adc_externaltrigconv = Adc_externaltrigconv_none; Conversion is not determined by the outside world
Adc_initstructure.adc_dataalign =adc_dataalign_right; Align Right
Adc_initstructure.adc_nbrofchannel = 1; Number of scan channels
Adc_init (adc1,&adc_initstructure);
Adc_regularchannelconfig (Adc1,adc_channel_9, 1,ADC_SAMPLETIME_1CYCLES5); Channel X, sampling time is 1.5 cycles, 1 is the rule channel 1th What do you mean by this 1? I'm not sure if my ADC is normal until 1.
Adc_cmd (adc1,enable); To enable or fail to specify the ADC
Adc_softwarestartconvcmd (adc1,enable)///enable ADC to be able or fail to specify the software conversion boot function

STM32 ADC Channel and GPIO table: (STM32 incomplete manual)


Here I use the ADC1 9-channel PB1 PIN.

Also with some default configuration functions like Gpio, for example: Adc_structinit

Adc_inittypedef structureadc_inittypedef is defined in the file "stm32f10x_adc.h":

typedef struct

{

U32 Adc_mode; Functionalstate Adc_scanconvmode; Functionalstateadc_continuousconvmode; U32 Adc_externaltrigconv; U32adc_dataalign; U8 Adc_nbrofchannel;

} adc_inittypedef

Note: In order to properly configure each ADC channel, the user must call Adc_channelconfig () after calling Adc_init () to configure the conversion order and sampling time for each channel used.

And then just keep reading;

U16 TESTADC (void)
{
U16 ADC;
while (Adc_getflagstatus (ADC1, ADC_FLAG_EOC) ==reset); Check to make ADC flag position 1 or not ADC_FLAG_EOC conversion end sign Bit
Adc=adc_getconversionvalue (ADC1);

returnadc;//returns the transformation results of the most recent ADCX rule group
}

The program's 8-bit microcontroller style is very heavy, the real ADC must be placed in the DMA or interrupt.


Turn from: http://blog.csdn.net/wangxiaobupt/article/details/18562111

ADC do not say much, follow the usual habit of walking.

Fill: Channel for ADC

There are three ADC in the STM32, each ADC has up to 16 channels, and 16 channels share an ADC converter in time division multiplexing, just like there are many electronic switches inside, each electronic switch controls the access of the signal, so the efficiency of ADC is improved a lot

First, initialization

1. Define the structure body

Composition of the//ADC structure of the typedef struct
{
uint32_t Adc_mode;
Functionalstate Adc_scanconvmode;
Functionalstate Adc_continuousconvmode;
uint32_t Adc_externaltrigconv;
uint32_t adc_dataalign;
uint8_t Adc_nbrofchannel;
}adc_inittypedef;


Adc_inittypedef adc_initstructure;
Gpio_inittypedef gpio_initstructure;

2. Open ADCX Clock
Rcc_apb2periphclockcmd (Rcc_apb2periph_gpioa | RCC_APB2PERIPH_ADC1, ENABLE); Enable ADC1 Channel Clock

3. Set the rate of ADC acquisition
Rcc_adcclkconfig (RCC_PCLK2_DIV6); Set ADC frequency factor 6 72M/6=12,ADC maximum time cannot exceed 14M


4. Initialize the corresponding Gpio//set to analog input
Gpio_initstructure.gpio_pin = gpio_pin_1;
Gpio_initstructure.gpio_mode = Gpio_mode_ain; Analog Input Pins
Gpio_init (Gpioa, &gpio_initstructure);

5. Reset ADC
Adc_deinit (ADC1); Resets the ADC1 to set the default value for all ADC1 of the peripherals

6. Initialize ADC
Adc_initstructure.adc_mode = adc_mode_independent; ADC work mode: ADC1 and ADC2 work in standalone mode
Adc_initstructure.adc_scanconvmode = DISABLE; Analog-to-digital conversion works in single channel mode
Adc_initstructure.adc_continuousconvmode = disable;//analog-to-digital conversion work in single conversion mode
Adc_initstructure.adc_externaltrigconv = adc_externaltrigconv_none;//conversion initiated by software rather than by external triggers
Adc_initstructure.adc_dataalign = ADC_DATAALIGN_RIGHT;//ADC Data right-aligned
Adc_initstructure.adc_nbrofchannel = 1; Number of ADC channels in order for rule conversions
Adc_init (ADC1, &adc_initstructure); Initializes the registers of the peripheral adcx based on the parameters specified in Adc_initstruct

7. To enable

Adc_regularchannelconfig (ADC1, CH, 1, adc_sampletime_239cycles5);//ADC1,ADC channel, sampling time is 239.5 cycles
Adc_cmd (ADC1, ENABLE); To enable the ADC1 to be specified.


8. Reset and Calibration
Adc_resetcalibration (ADC1); To enable reset and calibration
while (Adc_getresetcalibrationstatus (ADC1)); Waiting for reset calibration to end
Adc_startcalibration (ADC1); Turn on AD calibration
while (Adc_getcalibrationstatus (ADC1)); Wait for calibration to end


Second, collection

1. Software start//software startup is the software to open the conversion can also be set to external open, external level changes automatically open conversion

Adc_softwarestartconvcmd (ADC1, enable);//enable the ADC1 to be specified to start the software conversion function

2. Return results after waiting for conversion

while (! Adc_getflagstatus (ADC1, ADC_FLAG_EOC));/wait for transition to end
Return Adc_getconversionvalue (ADC1); Returns the transformation result of the most recent ADC1 rule group

Turn from: http://blog.csdn.net/liuxizhen2009/article/details/17094757

#include "led.h" #include "delay.h" #include "key.h" #include "sys.h" #include "lcd.h" #include "usart.h" #include "ADC. H "//alientek Battleship STM32 Development Board Experiment//ADC Experiment//Technical support: www.openedv.com//Guangzhou star Wing Electronic Technology Co., LTD./* Private define------------------------- 
-----------------------------------*/#define ADC1_DR_ADDRESS ((u32) 0x4001244c) extern void dma_configuration (void);
extern void Uart1putc (char c); 
U16 Ad_value=0xff; 
	void Adc_init (void) {adc_inittypedef adc_initstructure;

	Gpio_inittypedef gpio_initstructure; Rcc_apb2periphclockcmd (Rcc_apb2periph_gpioa |	  RCC_APB2PERIPH_ADC1, ENABLE);   Enable ADC1 Channel clock rcc_adcclkconfig (RCC_PCLK2_DIV6); Set ADC frequency factor 6 72M/6=12,ADC maximum time cannot exceed 14M//PA1 as analog channel input pin Gpio_initstructure.gpio_pin = gpio_pin_1
	;		Gpio_initstructure.gpio_mode = Gpio_mode_ain;	

	Analog input Pin Gpio_init (GPIOA, &gpio_initstructure);  Adc_deinit (ADC1);	Resetting the ADC1, setting the ADC1 of the peripherals to the default value Adc_initstructure.adc_mode = Adc_mode_independent; ADC Working mode: ADC1 and ADC2Work in Independent mode Adc_initstructure.adc_scanconvmode = DISABLE;	Analog-to-digital conversion works in single channel mode adc_initstructure.adc_continuousconvmode = ENABLE;	The Analog-to-digital conversion work is Adc_initstructure.adc_externaltrigconv = Adc_externaltrigconv_none in the single conversion mode;	The conversion is triggered by the software rather than by the external trigger adc_initstructure.adc_dataalign = Adc_dataalign_right;	ADC Data Right-aligned adc_initstructure.adc_nbrofchannel = 1;	Number of ADC channels Adc_init (ADC1, &adc_initstructure) in order to convert the rules; Initializes the Register//settings of the peripheral adcx according to the parameters specified in adc_initstruct Specifies the rule group channel for the ADC, a sequence, sampling time Adc_regularchannelconfig (ADC1, Adc_channel_1, 1, AD	C_SAMPLETIME_13CYCLES5);	ADC1,ADC Channel, sampling time is 239.5 cycle Adc_cmd (ADC1, ENABLE);
	
	Enable DMA support for the specified ADC1//ADC (to implement DMA function, also require independent configuration of DMA channel parameters) Adc_dmacmd (ADC1, enable);	Adc_resetcalibration (ADC1);	Enables the reset of the calibration while (Adc_getresetcalibrationstatus (ADC1));	 Wait for reset calibration to end adc_startcalibration (ADC1);	 Open AD Calibration while (Adc_getcalibrationstatus (ADC1));		Wait for calibration to end//Adc_softwarestartconvcmd (ADC1, ENABLE); Enable ADC1 software conversion boot function to be specified//Get ADC value//ch: Channel value 0~3 U16 GET_ADC (U8 Ch{//Set the rule group channel for the specified ADC, a sequence, sampling time adc_regularchannelconfig (ADC1, CH, 1, adc_sampletime_239cycles5);//ADC1,ADC channel, sampling time is		239.5 cycle Adc_softwarestartconvcmd (ADC1, ENABLE); Enable the ADC1 software transformation to start the function while (!	Adc_getflagstatus (ADC1, ADC_FLAG_EOC));/wait for transition to end return Adc_getconversionvalue (ADC1);
	Returns the transformation result of the most recent ADC1 rule Group U16 get_adc_average (U8 ch,u8 times) {u32 temp_val=0;
	U8 T;
		for (t=0;t<times;t++) {TEMP_VAL+=GET_ADC (CH);
	Delay_ms (5);
return temp_val/times; }/******************************************************************************* * Function Name:dma_  Configuration * DESCRIPTION:DMA settings: Automatically read conversion results from ADC modules to memory * input:none * Output:none * return: None *******************************************************************************/void DMA_Configuration (void
	    ) {Dma_inittypedef dma_initstructure;
   
    Start DMA Clock Rcc_ahbperiphclockcmd (RCC_AHBPERIPH_DMA1, ENABLE);
    Dma_deinit (DMA1_CHANNEL1); Dma_initstructurE.DMA_PERIPHERALBASEADDR = adc1_dr_address;
    Dma_initstructure.dma_memorybaseaddr = (u32) &AD_Value;
    Dma_initstructure.dma_dir = DMA_DIR_PERIPHERALSRC;
    buffersize=2, because the ADC conversion sequence has 2 channels//So set, so that the sequence 1 results are placed in ad_value[0], the sequence 2 results are placed in ad_value[1] dma_initstructure.dma_buffersize = 1;
    Dma_initstructure.dma_peripheralinc = dma_peripheralinc_disable;
    Dma_initstructure.dma_memoryinc = dma_memoryinc_disable;
    Dma_initstructure.dma_peripheraldatasize = Dma_peripheraldatasize_halfword;
    Dma_initstructure.dma_memorydatasize = Dma_memorydatasize_halfword;
    The circulation mode opens, the buffer fills up, automatically returns to the initial address to begin the transmission dma_initstructure.dma_mode = Dma_mode_circular;
    dma_initstructure.dma_priority = Dma_priority_high;
    dma_initstructure.dma_m2m = dma_m2m_disable;
    Dma_init (Dma1_channel1, &dma_initstructure);
After the configuration is complete, start the DMA channel Dma_cmd (Dma1_channel1, ENABLE);
	int main (void) {U16 adcx;
	float temp;	    	 Delay_init (); 	 Delay function Initialization of nvic_configuration (); Set Nvic Interrupt grouping 2:2-bit preemptionPriority level, 2-bit response priority uart_init (9600);			     Serial port initialized to 9600 led_init ();	
	LED port initialization lcd_init ();		 	
 	Dma_configuration ();		  		Adc_init ();	
	ADC initialization point_color=red;//set font to Red lcd_showstring (60,50,200,16,16, "warship STM32");	
	Lcd_showstring (60,70,200,16,16, "ADC TEST");
	Lcd_showstring (60,90,200,16,16, "Atom@alientek");	
	Lcd_showstring (60,110,200,16,16, "2012/9/7");	      
	Display prompt point_color=blue;//set font to Blue lcd_showstring (60,130,200,16,16, "Adc_ch0_val:");
	    Lcd_showstring (60,150,200,16,16, "adc_ch0_vol:0.000v");
    Start the first ad conversion adc_softwarestartconvcmd (ADC1, ENABLE);
		Since DMA has been configured, the AD is automatically continuously converted, and the result is automatically saved at Ad_value while (1) {printf ("%04x\n", Ad_value);
	Lcd_showxnum (156,130,ad_value,4,16,0);//Display ADC value Delay_ms (200);		//while (1)//{//Adcx=get_adc_average (adc_channel_1,10);//Lcd_showxnum (156,130,adcx,4,16,0);//Show ADC value///
temp= (float) adcx* (3.3/4096);
Adcx=temp;
Lcd_showxnum (156,150,adcx,1,16,0);//display voltage value//TEMP-=ADCX;
temp*=1000; Lcd_showxnum (172,150,TEMP,3,16,0X80); led0=!
LED0;	
Delay_ms (250);
 //	}
 }



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.