STM8L ADC

Publisher:清新微笑Latest update time:2019-12-23 Source: eefocusKeywords:STM8L  ADC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STM8L ADC

/***********************************************************************************

*Name: Hal_Comm_GetBatADC

*Function: Get battery power information

*Entry parameters: None

*Export parameter: AD value of power

*illustrate: 

***********************************************************************************/

unsigned int Hal_Comm_GetBatADC(void)

{

unsigned int Result = 0, value_adc = 0;

unsigned char index = 0;


  GPIO_Init(VOL_ADC_PORT,VOL_ADC_PIN,GPIO_Mode_In_FL_No_IT);//VOL_ADC

  ADC1->CR1 |= 0x01; // Enable AD

  ADC1->CR3 &= ~0x1F; // Select sampling time 384 cycles Channel clear 

  ADC1->CR3 |= 0x00; // channel 0 is selected 

  ADC1->SQR[0] = ADC1->SQR[1] = ADC1->SQR[2] = ADC1->SQR[3] = 0x00;

  ADC1->SQR[3] = 0x01; // Select channel 0 to scan  

  ADC1->SR &= 0x00; // conversion not complete 

  ADC1->SR = 0; // conversion flag cleared

  ADC1->CR1 |= 0x03; // Enable AD and start conversion 

  

  while(!(ADC1->SR & 0x01)); //waitloop bit0: 1 complete 0 not complete 

  

  for(index = 0; index < 10; index++)

  {

    ADC1->SR = 0; // conversion flag cleared 

    ADC1->CR1 |= 0x03; // Enable AD and start conversion

    while(!(ADC1->SR & 0x01));//waitloop conversion complete 

    Result = ADC1->DRH; //Calculate a total of 16 bits, but ADC is 12 bits long

    Result <<= 8; //ADC1->DRH is the high 8 bits,

    Result += ADC1->DRL; //ADC1->DRL is the lower 8 bits

    value_adc += Result;

if(index != 0)value_adc = value_adc/2; //Get multiple data and calculate the average value  

    index++;

  }



  ADC1->CR1 &= 0xFE; // turn off AD 

  ADC1->CR3 &= ~0x1F; //Select channel 

  ADC1->CR3 |= 0x11; 



return value_adc;

}





When designing schematics, we usually set the ADC input voltage range to 0~3.3v. If the ADC is set to 12 bits, then the 12-bit full scale corresponds to 3.3V, and the digital value corresponding to the 12-bit full scale is 2^12. The value 0 corresponds to 0V. If the converted ADC value is X, and the analog voltage corresponding to X is Y, then there will be such an equation: 2^12/3.3=X/Y, => Y=(3.3*X)/2^12



In our smart lock project, the full-scale Vref is 3.31V, total voltage (M6V)/test voltage (VOL_ADC) = 3

4096/3.31 = value_adc/Va



Va = value_adc * 3.31 / 4096



Total voltage = Va * 3



That is, vin in the program = value_adc * 3.31 * 3 / 4096 


Keywords:STM8L  ADC Reference address:STM8L ADC

Previous article:STM8 low power design
Next article:STM8S timer TIM4 timing 1kHz output

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号