STM32 ADC Note Single Conversion Tested

Publisher:书香墨意Latest update time:2017-02-16 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The following are two examples of library functions and direct register manipulation. I have tested them all.

Before using this program, you must set the GPIO to analog input mode.

=====================================Library Function Version==========================================

void AD_CONFIG_SINGLE(void)
{
//Configure the IO port first:

ADC_InitTypeDef adcInitStruct;
// //PB1 is used as the analog channel input pin                       
 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Enable ADC and GPIOC clock
//Configure ADC:
//Configure ADC to non-scanning mode (that is, not all ports in this group will be scanned each time processing), because there is only one register to save the adc result when using the rule group; single mode.
adcInitStruct.ADC_Mode = ADC_Mode_Independent;
adcInitStruct.ADC_ScanConvMode = DISABLE;
adcInitStruct.ADC_ContinuousConvMode = DISABLE;
adcInitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
adcInitStruct.ADC_DataAlign = ADC_DataAlign_Right;
InitStruct.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &adcInitStruct);

       ADC_Cmd(ADC1, ENABLE);
       ADC_TempSensorVrefintCmd(ENABLE);

 ADC_ResetCalibration(ADC1);
 while((ADC_GetResetCalibrationStatus(ADC1)));// && (timeOut--));

 ADC_StartCalibration(ADC1);
 while((ADC_GetCalibrationStatus(ADC1)));// && (timeOut--));

}
//Call function during each scan:
u16 AD_sysGetAdcResult(void)  
{
  u16 ad;
 ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);
 ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
   ADC_SoftwareStartConvCmd(ADC1, ENABLE); 
   while(A DC_GetFlagStatus(ADC1, ADC_FLAG_EOC)==0);
   ad = ADC_GetConversionValue(ADC1);
       
   //ADC_TempSensorVrefintCmd(DISABLE);
   //ADC_SoftwareStartConvCmd(ADC1, DISABLE);
   //ADC_Cmd(ADC1, DISABLE);
   return ad;
 
}

=====================================Register Version==========================================


void AD_CONFIG_SINGLE(void)
{        
       //Initialize IO port first
       //RCC->APB2ENR|=1<<2; //Enable PORTA port clock  
       //GPIOA->CRL&=0XFFFF0000; //PA0 1 2 3 anolog input
       //Channel 10/11 setting                     
       RCC->APB2ENR|=1<<9; //ADC1 clock enable     
       RCC->APB2RSTR|=1<<9; //ADC1 reset
       RCC->APB2RSTR&=~(1<<9); //Reset end          
       RCC->CFGR&=~(3<<14); //Division factor cleared    
       //SYSCLK/DIV2=12M ADC clock is set to 12M, ADC maximum clock cannot exceed 14M!
       //Otherwise, the ADC accuracy will decrease!  
       RCC->CFGR|=2<<14;                     
 
       ADC1->CR1&=0XF0FFFF; //Working mode clear
       ADC1->CR1|=0<<16; //Independent working mode    
       ADC1->CR1&=~(1<<8); //Non-scanning mode     
       ADC1->CR2&=~(1<<1); //Single conversion mode
       ADC1->CR2&=~(7<<17);           
       ADC1->CR2|=7<<17; //Software control conversion    
       ADC1->CR2|=1<<20; //Use external trigger (SWSTART)!!! An event must be used to trigger
       ADC1->CR2&=~(1<<11); //Right-align
 
       ADC1->SQR1&=~(0XF<<20);
       ADC1->SQR1&=0<<20; //1 conversion in regular sequence, that is, only convert regular sequence 1       
              
       //Set the sampling time of channels 0~3
       ADC1->SMPR1|=7; //Channel 10 239.5 cycles, increasing the sampling time can improve the accuracy      
 
       ADC1->CR2|=1<<0; //Turn on AD Converter    
       ADC1->CR2|=1<<3; //Enable reset calibration    
       while(ADC1->CR2&1<<3); //Wait for calibration to end                      
       //This bit is set by software and cleared by hardware. This bit will be cleared after the calibration register is initialized.                
       ADC1->CR2|=1<<2; //Start AD calibration          
       while(ADC1->CR2&1<<2); //Wait for calibration to end
       //This bit is set by software to start calibration and cleared by hardware when calibration ends    
}                                  

//Get ADC value
//ch: channel value 0~3
u16 AD_sysGetAdcResult(void)    
{
       //Set conversion sequence                        
       ADC1->SQR3&=0XFFFFFFE0; //Rule sequence 1 channel ch
       ADC1->SQR3|=10;                                         
       ADC1->CR2|=1<<22; //Start rule conversion channel  
       while(!(ADC1->SR&1<<1)); //Wait for conversion to end                    
       return ADC1->DR; //Return adc value  
}


Keywords:STM32 Reference address:STM32 ADC Note Single Conversion Tested

Previous article:STM32 peripheral programming steps
Next article:STM32 HSE LSE crystal oscillator official recommendation

Recommended ReadingLatest update time:2024-11-17 04:25

STM32 GPIO settings
I just started learning STM32 recently, so I started from the most basic GPIO; first take a look at the simple introduction to the GPIO port on the STM32 datasheet: Each GPI/O port has two 32-bit configuration registers (GPIOx_CRL, GPIOx_CRH), two 32-bit data registers (GPIOx_IDR, GPIOx_ODR), a 32-bit set/reset regist
[Microcontroller]
STM32 uses DMA and FSMC to drive ISSI 25616 external SRAM successfully
Last year, I tried the DMA of STM32. I think I used the M2M mode. When testing, I used 32-bit width data from STM32's own FLASH to RAM. The test was successful. Then I used DMA to send data to DAC to generate square waves, triangle waves, sine waves, etc. After using DMA, I used FSMC to drive 9325TFT. At that time, I
[Microcontroller]
Question about STM32 ADC speed
The STM32F103xx series is called the enhanced product, and the maximum clock frequency of the enhanced product can reach 72MHz. The English name of the enhanced product is Performance Line. The STM32F101xx series is called the basic product, and the maximum clock frequency of the basic product can reach 36MHz. The Eng
[Microcontroller]
Detailed explanation of ucosii transplantation on stm32 4
    There is a question in Detailed Explanation 3 that has not been explained, that is, there is already a definition of SysTick interrupt function SysTick_Handler() in stm32f10x_it.c, why does the official version have to create OS_CPU_SysTickHandler(). The answer lies in the startup file. Generally, when we develop s
[Microcontroller]
How to process the main frequency of STM32 external 4-16MHz crystal oscillator
Since the STM32F10x library officially uses the default external 8MHz crystal, many users also use the 8MHz crystal. However, the 8MHz crystal is not necessary, and other frequency crystals are also feasible. You only need to make corresponding modifications in the library.     Many users on the forum reported that us
[Microcontroller]
arm-none-eabi-gcc compiles STM32 optimization and deletes code solution
A few days ago, I used arm-none-eabi-gcc to compile a STM32F103C8 program, which is a simple running light program. #include "stm32f10x.h" int main(void) {         int i;         RCC- APB2ENR=0xFFFFFFFF;         GPIOC- CRL=0X33333333;//outpp at 50M         GPIOC- CRH=0X33333333;         while(1)         {            
[Microcontroller]
STM32 Series Chapter 18 - ADC
ADC features: 12-bit successive approximation analog-to-digital converter With up to 3 ADC controllers Supports up to 18 channels and can measure up to 16 external and 2 internal signal sources Supports single and continuous conversion modes Automatic scanning mode from channel 0 to channel n Automatic calibrati
[Microcontroller]
STM32 Series Chapter 18 - ADC
A method to improve the ADC accuracy of digital processor
TMS320F2812 is a 32-bit high-performance digital signal processor ( DSP ) with a main frequency of up to 150 MHz launched by Texas Instruments (TI) , which integrates an ADC conversion module. The ADC module is a 12-bit, pipelined analog-to-digital converter with a built-in dual sample-and-hol
[Power Management]
A method to improve the ADC accuracy of digital processor
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号