STM32 ADC single channel single routine

Publisher:CyborgDreamerLatest update time:2019-03-08 Source: eefocusKeywords:STM32  ADC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "stm32f10x.h"


/*RCC clock configuration*/

void RCC_config(void)

 ErrorStatus HSEStartUpStatus;


 /* RCC registers are set to default configuration */

 RCC_DeInit();

 /* Turn on external high speed clock */

 RCC_HSEConfig(RCC_HSE_ON);

 /* Wait for the external high-speed clock to stabilize*/

 HSEStartUpStatus = RCC_WaitForHSEStartUp();

 if(HSEStartUpStatus == SUCCESS) 

 { 

  /* Set HCLK = SYSCLK */

  RCC_HCLKConfig(RCC_SYSCLK_Div1);

  /* Set PCLK2 = HCLK */

  RCC_PCLK2Config(RCC_HCLK_Div1);

  /* Set PCLK1 = HCLK / 2 */

  RCC_PCLK1Config(RCC_HCLK_Div2);

// /* Set FLASH code delay */

//  FLASH_SetLatency(FLASH_Latency_2);

// /* Enable prefetch cache */

//  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  /* Set the PLL clock source to HSE multiplier 9 72MHz */

  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  /* Enable PLL */

  RCC_PLLCmd(ENABLE);

  /* Wait for PLL to stabilize */

  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

  /* Set PLL as system clock source */

  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  /* Wait for the system clock source to switch to PLL */

  while(RCC_GetSYSCLKSource() != 0x08);

 }

}


/* Millisecond delay function */

void delay_ms(uint16_t time)

{    

 uint16_t i = 0; 


 while(time--)

 {

  i = 12000;

  while(i--);    

 }

}


/* GPIO configuration */

void GPIO_config(void)

{

 GPIO_InitTypeDef GPIO_InitStructure;


 /* Clock configuration */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);


 /* Analog input */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

}


/* ADC configuration */

void ADC_config(void)

{

 ADC_InitTypeDef ADC_InitStructure;


 /* Enable clock */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

 

 /* Configure ADC clock 12MHz */

 RCC_ADCCLKConfig(RCC_PCLK2_Div6);

 

 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; /* Independent mode*/

 ADC_InitStructure.ADC_ScanConvMode = DISABLE; /* Single channel mode*/

 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; /* Single conversion mode*/

 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; /* Software trigger*/

 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /* Data right aligned*/

 ADC_InitStructure.ADC_NbrOfChannel = 1; /* 1 channel */

 ADC_Init(ADC1, &ADC_InitStructure);

 

 /* Analog channel 0, sampling sequence 1, sampling period 55.5 */

 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);

 

 /* Enable ADC */

 ADC_Cmd(ADC1, ENABLE);

 

 /* Reset calibration registers */

 ADC_ResetCalibration(ADC1);

 while(ADC_GetResetCalibrationStatus(ADC1));

 

 /* Start calibration */

 ADC_StartCalibration(ADC1);

 while(ADC_GetCalibrationStatus(ADC1));

}


/* Main function */

int main(void)

{

 uint32_t value;

 volatile float V;

 

 /*RCC clock configuration*/

 RCC_config();


 /* IO placement */

 GPIO_config();

 

 /* ADC configuration */

 ADC_config();

 

 while(1)

 {

  /* Start conversion */

  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

  /* Wait for the conversion to finish */

  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

  

  /* Get the conversion value */

  value = ADC_GetConversionValue(ADC1);

  

  /* Convert digital quantity to voltage value*/

  V = (float)value / 4096 * 3.3;

  

  delay_ms(1000);

 }

}

Keywords:STM32  ADC Reference address:STM32 ADC single channel single routine

Previous article:STM32 SPI slave DMA routine
Next article:STM32 ADC principle

Recommended ReadingLatest update time:2024-11-16 13:30

A brief description of pull-up and pull-down inputs of STM32 IO ports
         Pull-up input: with pull-up resistor                                 When the button is not pressed, the port is connected to a high level, that is, high level 1 state---3.3v                                 When the button is pressed, the port is connected to a low level, that is, a low level 0 state-------
[Microcontroller]
Classic STM32 ADC multi-channel conversion
STM32 ADC multi-channel conversion   Description: Use ADC to continuously collect 11 analog signals and transfer them to memory through DMA. ADC is configured to scan and continuously convert mode, and the ADC clock is configured to 12MHZ. After each conversion, DMA transfers the converted data to memory in a loop.
[Microcontroller]
About the CPU usage of STM32 ~ bare metal without system
1. If you don't run the operating system, the CPU will definitely be 100% used all the time. Even if you are waiting for a delay, the CPU will always execute the empty statement nop, because there is a CPU in the STM32. 2. For stm32, it is always 100%. It just depends on how much free time you have and how much time
[Microcontroller]
Several common questions about STM32 peripheral configuration
In the process of developing STM32 applications, we often encounter various problems, many of which are related to the configuration of peripherals and related GPIOs. Let's summarize and discuss these issues here. The current STM32 chip is a programmable microprocessor based on the ARM core. We can simply call every
[Microcontroller]
Several common questions about STM32 peripheral configuration
STM32 Experience (I)
The relevant experience and experience are as follows:   Summary: 1. After configuring the TX completion interrupt of USART, the TX completion interrupt function is automatically entered without sending data. 2. After the TX configuration is completed, the corresponding TC flag is automatically set to 1, and then the
[Microcontroller]
STM32 Experience (I)
Principle and Application of High-Performance ∑-Δ ADC
1 Overview MAX1403 is an 18-bit, over-sampling ADC chip that uses a ∑-Δ modulator and a digital filter to achieve true 16-bit conversion accuracy. In applications, in order to obtain a high output data rate, the digital filter factor can be selected and the conversion resolution can be reduced. The samp
[Power Management]
Principle and Application of High-Performance ∑-Δ ADC
STM32 Study Notes - External Interrupts
Interrupt, as the name implies, means that a continuous process is interrupted. When the processor is processing a task, there is a task that needs to be processed immediately, so it needs to stop the task at hand and execute this task first. The process of configuring external interrupts is similar to that of confi
[Microcontroller]
Using a switching power supply to power a high-speed analog-to-digital converter
System designers are often asked to reduce overall power consumption to reduce the impact on our environment while reducing capital and operating costs. They are also required to increase circuit density to achieve smaller electronic systems and operate in more harsh environments. Unfortunately, when high power soluti
[Power Management]
Using a switching power supply to power a high-speed analog-to-digital converter
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号