Generally, the ADC must be used together with the DMA. Here, the ADC application is simply configured with the library to continuously scan the ADC.
First, configure the clock of gpio and ADC:
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 2 MB.
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; // The conversion is not determined by the outside world.
Adc_initstructure.adc_dataalign = adc_dataalign_right; // right-aligned
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. The sampling time is 1.5 cycles, 1 indicates that the rule Channel is 1st. What does this 1 mean? I am not sure that the ADC is normal only when it is 1.
Adc_cmd (adc1, enable); // enables or disables the specified ADC
Adc_softwarestartconvcmd (adc1, enable); // enable or disable the software conversion startup function of the specified ADC
Here I am using the 9-channel PB1 pin of adc1.
Some default configuration functions are also the same as those of gpio, such as 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: To correctly configure each ADC channel, after you call adc_init (), you must call adc_channelconfig () to configure the conversion sequence and sampling time for each channel.
Then there is continuous reading;
B2testadc (void)
{
B2adc;
While (adc_getflagstatus (adc1, adc_flag_eoc) = reset); // check whether the specified ADC flag position is 1 or not. adc_flag_eoc indicates the end of the conversion.
ADC = adc_getconversionvalue (adc1 );
Returnadc; // returns the Conversion Result of the last adcx rule group.
}
The 8-Bit Single-Chip Microcomputer style of this program is very heavy, and the real ADC must be put in DMA or interrupted.
This article is reproduced in
Simple implementation of ADC modulus Conversion