STM8 gets voltage value

Publisher:平凡的梦想Latest update time:2019-01-29 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STM8 gets voltage value

 

(1) Port definition

static void ADC_POWER_Config()

{

  /*  Init GPIO for ADC1 */

  GPIO_Init(GPIOB, GPIO_PIN_2, GPIO_MODE_IN_FL_NO_IT); //Variable resistor

  ADC1_DeInit();

  ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_2, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL0, ENABLE);

  ADC1_Cmd(ENABLE);

}

 

(2) Obtain the voltage value (here the voltage value is collected five times and the average value is taken)

#define BATTERY_CHECK_COUNT_PER_COLLECT 5 //Number of times each collection

u16 Get_ADC_POWER_Value(void)

{

   u16 nBattery_Check_Value = 0;

   u16 nNum = 0;

 

  for(nNum = 0; nNum < BATTERY_CHECK_COUNT_PER_COLLECT; nNum++) // Collect BATTERY_CHECK_COUNT_PER_COLLECT times and accumulate the collected power values

  {

    

    ADC1_StartConversion(); //Start conversion

    nBattery_Check_Value += ADC1_GetConversionValue(); //Accumulate and read the conversion value of adc  

  }

  

  nBattery_Check_Value = nBattery_Check_Value/(BATTERY_CHECK_COUNT_PER_COLLECT*10); //Calculate the average power

  return nBattery_Check_Value;

}

 

(3) main function

void main(void)

{

     ADC_POWER_Config();

    

u16 nBattery_Check_Value2 = 0;

nBattery_Check_Value2 = Get_ADC_POWER_Value(); //Read the power

printf("nBattery_Check_Value2 = %d\n",nBattery_Check_Value2);

}

 

Keywords:STM8 Reference address:STM8 gets voltage value

Previous article:Several key programs for MCU to simulate I2C bus communication
Next article:STM8L15x series MCU determines the level of the specified I/O pin

Recommended ReadingLatest update time:2024-11-15 14:49

[STM8] - External clock + internal clock switching
STM8S103K3: External clock + internal clock switching. First, take a look at the clock structure diagram of STM8S103K3, which can help you understand it well.    There are several clocks here, which are the clocks marked in the figure, and you need to clarify them: fHSE: External high-speed crystal oscillator cloc
[Microcontroller]
STM8的wait for interrupt
If I wrap wait forinterrupt(WFI) with disable interrupt and enable interrupt, do you think WFI can still be woken up? Have you ever thought about it? Last night I took the STM8L151K4 development board and tested it. This usage is completely fine. From this, I guess that inside the chip, the core should have a dedica
[Microcontroller]
STM8的wait for interrupt
STM8 study notes (II): GPIO input
I was a bit busy these two days. I soldered four buttons and realized GPIO input based on the LED light. This time we use the official library of ST company The key is to understand the functions of several GPIO registers. At the same time, you should understand the function definition and usage in its official librar
[Microcontroller]
STM8 IO input independent key detection program
System functions Use STM8 to detect three independent buttons, and give an indication immediately once a button is detected! hardware design For details about the I/O structure and related introduction of STM8, please refer to the Datasheet. Here we only briefly introduce some of them. The following is the I/O p
[Microcontroller]
STM8 IO input independent key detection program
STM8 study notes---external interrupt implementation
Use the button as an interrupt trigger. When the button is pressed once, the LED light flips once. First, initialize the IO port. The button is connected to the PC4 port. By default, it is at a high level, and it is at a low level when the button is pressed. The initialization code is as follows: void EXTI_GPIO_In
[Microcontroller]
STM8 study notes---external interrupt implementation
How to use the built-in bootloader of STM8
1. First, confirm whether the STM8 you are using has its own bootloader. Refer to the table below. 2. STM8 empty devices can directly use their own bootloader. 3. After STM8 is burned using SWIM, if you want to continue to use its own bootloader, you must set the optional byte to 0x55AA. As shown below: The method
[Microcontroller]
How to use the built-in bootloader of STM8
STM8 MCU learning summary 03
The physical welding and the corresponding driver program have been debugged. The video address is: Homemade Bluetooth lamp operation video Network address: http://v.youku.com/v_show/id_XMjk0ODk1MjUyMA==.html; Physical picture 01 is: Physical picture 02 is: --------------- All program codes are implemented i
[Microcontroller]
STM8 MCU learning summary 03
stm8——Implementation of serial port printf
Serial port communication is one of the most basic and important functions of microcontroller learning. The serial port can be used indirectly as a debugging interface to achieve communication between the microcontroller and the computer. It can also communicate with some modules (such as Bluetooth, WiFi), and can als
[Microcontroller]
Latest Microcontroller Articles
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号