STM8L內(nèi)部含有一個(gè)12位的ADC,擁有25個(gè)輸入通道,包括一個(gè)內(nèi)部溫度傳感器,一個(gè)內(nèi)部參考電壓
由上圖可知,STM8L內(nèi)部還有一個(gè)內(nèi)部參考電壓,這個(gè)內(nèi)部參考電壓的電壓值是可知的,且是精確的。由數(shù)據(jù)手冊(cè)可知,內(nèi)部參考電壓為1.225V。
ADC選擇內(nèi)部參考電壓作為測(cè)量通道,可以測(cè)量到內(nèi)部電壓的轉(zhuǎn)化值Nadc。此時(shí),已經(jīng)VDD的轉(zhuǎn)化值4096,VREFINT電壓值1.225V及對(duì)應(yīng)的轉(zhuǎn)化值Nadc。這些值滿足比例關(guān)系:
VDD/4096=VREFINT/Nadc
VDD/4096 =1.225/Nadc
VDD=1.225*4096/Nadc
STM8L15x系列單片機(jī)的相關(guān)庫(kù)函數(shù)配置過(guò)程如下:
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)使能或失能內(nèi)部參考電壓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);
參數(shù):ENABLE(使能)或DISABLE(失能)
(2)選擇內(nèi)部參考電壓通道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)獲得轉(zhuǎn)換數(shù)值A(chǔ)DC_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);//開(kāi)啟ADC1時(shí)鐘
ADC_VrefintCmd(ENABLE); //使能內(nèi)部參考電壓
ADC_Init(ADC1,ADC_ConversionMode_Continuous,ADC_Resolution_12Bit,ADC_Prescaler_1);//連續(xù)轉(zhuǎn)換,12位,轉(zhuǎn)換時(shí)鐘1分頻
ADC_ChannelCmd(ADC1,ADC_Channel_Vrefint,ENABLE);//使能內(nèi)部參考電壓通道
ADC_Cmd(ADC1,ENABLE);//ADC使能
}
主函數(shù):
#include "stm8l15x.h"
#include "adc.h"
int main( void )
{
u16 adc1_value=0;
float value=0;
Adc_Init();
while(1)
{
ADC_SoftwareStartConv(ADC1); //開(kāi)啟軟件轉(zhuǎn)換
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//等待轉(zhuǎn)換結(jié)束
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);//清除對(duì)應(yīng)標(biāo)志
adc1_value=ADC_GetConversionValue(ADC1); //獲取轉(zhuǎn)換值
value=1.225*4096/adc1_value; //獲得VDD電壓,單位V
if(value<2.8)
{
GPIO_ResetBits(GPIOD,GPIO_Pin_4);
GPIO_ToggleBits(GPIOD,GPIO_Pin_5);
}
}
}
對(duì)上述IAR工程進(jìn)行編譯,并下載到STM8L15x板子中,分別右擊變量adc1_value和變量value選擇“Add to watch”出現(xiàn)如下窗口(查看相關(guān)變量的值):
在程序中設(shè)置相關(guān)斷點(diǎn),以便查看變量值
運(yùn)行程序可得:
故測(cè)得STM32L15x板子的VDD=3.32291388V