STM8L使用ADC内部参考电压通道测量VDD电压
STM8L内部含有一个12位的ADC,拥有25个输入通道,包括一个内部温度传感器,一个内部参考电压
由上图可知,STM8L内部还有一个内部参考电压,这个内部参考电压的电压值是可知的,且是精确的。由数据手册可知,内部参考电压为1.225V。
ADC选择内部参考电压作为测量通道,可以测量到内部电压的转化值Nadc。此时,已经VDD的转化值4096,VREFINT电压值1.225V及对应的转化值Nadc。这些值满足比例关系:
VDD/4096=VREFINT/Nadc
VDD/4096 =1.225/Nadc
VDD=1.225*4096/Nadc
STM8L15x系列单片机的相关库函数配置过程如下:
This section provides functions allowing to enable/ disable the internal connections between the ADC and the Temperature Sensor and the Vrefint source.
A typical configuration to get the Temperature sensor or/and Vrefint channels voltages is done following these steps :
1. Enable the internal connection of Temperature sensor or/and Vrefint sources with the ADC channels:
- for the Temperature sensor usingADC_TempSensorCmd() function.
- for the Internal Voltage reference usingADC_VrefintCmd() function.
2. Enable the ADC_Channel_TempSensor and/orADC_Channel_Vrefint channels usingADC_ChannelCmd()function.
3. Get the voltage values, usingADC_GetConversionValue().
(1)使能或失能内部参考电压ADC_VrefintCmd()
* @brief Enables or disables the Internal Voltage reference.
* @param NewState : new state of the Internal Voltage reference.
* This parameter can be: ENABLE or DISABLE.
* @retval None
void ADC_VrefintCmd(FunctionalState NewState);
参数:ENABLE(使能)或DISABLE(失能)
(2)选择内部参考电压通道ADC_ChannelCmd()
* @brief Enables or disables the selected ADC channel(s).
* @param ADCx where x can be 1 to select the specified ADC peripheral.
* @param ADC_Channels: specifies the ADC channels to be initialized
This parameter can be one of the following values:
@arg ADC_Channel_0: Channel 0
@arg ADC_Channel_1: Channel 1
@arg ADC_Channel_2: Channel 2
@arg ADC_Channel_3: Channel 3
@arg ADC_Channel_4: Channel 4
@arg ADC_Channel_5: Channel 5
@arg ADC_Channel_6: Channel 6
@arg ADC_Channel_7: Channel 7
@arg ADC_Channel_8: Channel 8
@arg ADC_Channel_9: Channel 9
@arg ADC_Channel_10: Channel 10
@arg ADC_Channel_11: Channel 11
@arg ADC_Channel_12: Channel 12
@arg ADC_Channel_13: Channel 13
@arg ADC_Channel_14: Channel 14
@arg ADC_Channel_15: Channel 15
@arg ADC_Channel_16: Channel 16
@arg ADC_Channel_17: Channel 17
@arg ADC_Channel_18: Channel 18
@arg ADC_Channel_19: Channel 19
@arg ADC_Channel_20: Channel 20
@arg ADC_Channel_21: Channel 21
@arg ADC_Channel_22: Channel 22
@arg ADC_Channel_23: Channel 23
@arg ADC_Channel_24: Channel 24
@arg ADC_Channel_25: Channel 25
@arg ADC_Channel_26: Channel 26
@arg ADC_Channel_27: Channel 27
@arg ADC_Channel_Vrefint: Vrefint Channel
@arg ADC_Channel_TempSensor: Temperature sensor Channel
@arg ADC_Channel_00To07: select from channel00 to channel07
@arg ADC_Channel_08To15: select from channel08 to channel15
@arg ADC_Channel_16To23: select from channel16 to channel23
@arg ADC_Channel_24To27: select from channel24 to channel27
@param NewState : new state of the specified ADC channel(s).
This parameter can be: ENABLE or DISABLE.
void ADC_ChannelCmd(ADC_TypeDef* ADCx, ADC_Channel_TypeDef ADC_Channels, FunctionalState NewState);
(3)获得转换数值ADC_GetConversionValue()
u16 u16_adc1_value;
u16_adc1_value = ADC1_GetConversionValue();
程序:
adc.h头文件:
#ifndef _adc_H
#define _adc_H
#include "stm8l15x.h"
void Adc_Init(void);
#endif
adc.c源文件:
#include "adc.h"
void Adc_Init(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1,ENABLE);//开启ADC1时钟
ADC_VrefintCmd(ENABLE); //使能内部参考电压
ADC_Init(ADC1,ADC_ConversionMode_Continuous,ADC_Resolution_12Bit,ADC_Prescaler_1);//连续转换,12位,转换时钟1分频
ADC_ChannelCmd(ADC1,ADC_Channel_Vrefint,ENABLE);//使能内部参考电压通道
ADC_Cmd(ADC1,ENABLE);//ADC使能
}
主函数:
#include "stm8l15x.h"
#include "adc.h"
int main( void )
{
u16 adc1_value=0;
float value=0;
Adc_Init();
while(1)
{
ADC_SoftwareStartConv(ADC1); //开启软件转换
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//等待转换结束
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);//清除对应标志
adc1_value=ADC_GetConversionValue(ADC1); //获取转换值
value=1.225*4096/adc1_value; //获得VDD电压,单位V
if(value<2.8)
{
GPIO_ResetBits(GPIOD,GPIO_Pin_4);
GPIO_ToggleBits(GPIOD,GPIO_Pin_5);
}
}
}
对上述IAR工程进行编译,并下载到STM8L15x板子中,分别右击变量adc1_value和变量value选择“Add to watch”出现如下窗口(查看相关变量的值):
在程序中设置相关断点,以便查看变量值
运行程序可得:
故测得STM32L15x板子的VDD=3.32291388V
- Keil MDK破解过程详解[18年05月20日 11:11]
- STM32F407之模拟I2C(二)之24C128[18年05月20日 12:18]
- STM32F1使用I/0模拟I2C接口[18年05月20日 12:17]
- STM32模拟I2C程序[18年05月20日 12:13]
- STM8S 模拟I2C程序[18年05月20日 12:14]
- 普通IO口模拟实现I2C通信及应用解析[18年05月20日 12:12]