参考电压为5V时,ADC引脚设置为RA3
测的值如下:(存放在EEPROM中察看) -- 已修改10位正确值
5.00V --> 0x03ff
3.91V --> 0x031D
2.80V --> 0x023A
2.22V --> 0x01C6
程序如下:
#include <16F877.h>
#pragma device adc=10
#fuses XT,WDT,PROTECT,PUT
#use delay(clock=4000000)
#define MAXEEPROM 0xff
unsigned int8 cEEpromAddr;
void Clear_EEprom();
main()
{
unsigned int16 iVolt;
float fVolt;
//启动看门狗
setup_wdt(WDT_2304MS);
Clear_EEprom(); //清空EEPROM
setup_adc_ports(RA0_RA1_RA3_ANALOG); //把A0,A1,A3 设置为ADC模拟引脚, 设置 +5v 为参考电压
setup_adc(ADC_CLOCK_INTERNAL); //启动ADC
set_adc_channel(3); //设置ADC通道
delay_ms(10);
cEEpromAddr=0;
while (1)
{
restart_wdt();
iVolt= read_adc(); //读adc的值
if (cEEpromAddr < MAXEEPROM )
{
write_eeprom( cEEpromAddr++, 0x02); //把ADC值写入EEPROM ,0x02为附加标志
write_eeprom( cEEpromAddr++, iVolt >> 8 ); //把ADC值(高位)写入EEPROM
write_eeprom( cEEpromAddr++, iVolt ); //把ADC值(低位)写入EEPROM
}
//fVolt = 5.0 * iVolt / 1024; //换算成实际电压值
delay_ms(500);
}
}
void Clear_EEprom()
{
unsigned int8 i;
restart_wdt();
for (i=0; i<MAXEEPROM; i++)
{
write_eeprom( i, 0x00);
}
write_eeprom( i, 0x00);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
确实是漏了高2位,加上就ok了 (已在上面修改)
一周热门 更多>