实例
不多说了,直接贴代码,实现的功能为使用内部参考电压测VCC电压值。
void main(void)
{
uint16_t BandGap_H, BandGap_L, AIN3_H, AIN3_L;
uint16_t BandGap_v;
uint32_t AIN3_v = 0;
uint16_t voltage = 0;
while(1)
{
Enable_ADC_BandGap;
clr_ADCF;
set_ADCS; // Each time ADC start trig signal
while(ADCF == 0);
clr_ADCF;
set_ADCS; // Each time ADC start trig signal
while(ADCF == 0);
clr_ADCF;
set_ADCS; // Each time ADC start trig signal
while(ADCF == 0);
BandGap_H = ADCRH;
BandGap_L = ADCRL;
BandGap_v = BandGap_H << 4 | BandGap_L;
//printf ("\n Value = 0x%bx",ADCRH);
//printf ("\n Value = 0x%bx",ADCRL);
Enable_ADC_AIN3;
clr_ADCF;
set_ADCS; // Each time ADC start trig signal
while(ADCF == 0);
AIN3_H = ADCRH;
AIN3_L = ADCRL;
AIN3_v = AIN3_H << 4 | AIN3_L;
//printf ("\n AIN3 Value = 0x%bx",ADCRH);
//printf ("\n AIN3 Value = 0x%bx",ADCRL);
voltage = (uint16_t)((uint32_t)AIN3_v * 1220 / BandGap_v );
Timer0_Delay1ms(1000);
}
}
|