uint16 read_vdd_value(uint8 channel, uint8 resolution)
{
unsigned int reading = 0;
uint8 i, resbits;
uint8 adcChannel = 1;
if (channel < 8)
{
for (i=0; i < channel; i++)
{
adcChannel <<= 1;
}
}
ADCCFG |= adcChannel;
switch (resolution)
{
case HAL_ADC_RESOLUTION_8:
resbits = HAL_ADC_DEC_064;
break;
case HAL_ADC_RESOLUTION_10:
resbits = HAL_ADC_DEC_128;
break;
case HAL_ADC_RESOLUTION_12:
resbits = HAL_ADC_DEC_256;
break;
case HAL_ADC_RESOLUTION_14:
default:
resbits = HAL_ADC_DEC_512;
break;
}
ADCCON3 = channel | resbits | HAL_ADC_REF_125V;//HAL_ADC_REF_VOLT;
while (!(ADCCON1 & HAL_ADC_EOC));
//ADCCFG寄存器的真正名字是 APCFG
ADCCFG &= (adcChannel ^ 0xFF);
reading = ADCL >> 2; //ADCL 寄存器低 2 位無效
reading |= (((uint16)(ADCH & 0x3f)) << 6);
return ((uint16)reading);
}
//顯示ADC的值跨琳,根據(jù)自己實(shí)際情況改
#define ADC_TO_CELSIUS(ADC_VALUE) ((float)ADC_VALUE*1.25/4096.0)
adc = read_vdd_value(HAL_ADC_CHN_VDD3,HAL_ADC_RESOLUTION_12);
adc_f = ADC_TO_CELSIUS(adc);
adc_f_vdd = adc_f*3;
如果需要讀取內(nèi)部芯片跌捆,還需要添加兩行亡哄,將通道改外#define ADC_EMP_SENS 0x0E,其他的就和普通ADC一樣枣抱。(好像芯片的內(nèi)部溫度偏差較大熔吗,但本人沒有試驗(yàn)。)
TR0=0x01; //設(shè)置為1來連接溫度傳感器到SOC_ADC
ATEST=0x01; //使能溫度傳感