set_tris_a(0b0000111);
setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports(sAN0|sAN1|sAN2);
delay_ms(100);
while(TRUE)
{
set_adc_channel(0);// Battery Voltage Sense
set_adc_channel(2);// mains Voltage Sense
adc_value=read_adc();
batt_volt=adc_value/20.449;
mns_volt=adc_value/20.449;
//batt_volt=adc_value*(5/1023);//20.449;
if (batt_volt >= 13.8 )//<=14.0)// && batt_volt <= 1.0 )
{
delay_ms(3000);
output_high(LED);
}
if (mns_volt <= 1.0 )//<=3.0)
{
delay_ms(3000);
output_high(RELAY);
}
else
{
output_low(RELAY);
delay_ms(3000);
output_low(LED);
}
}
}
_________________ heroswap1981
Ttelmah
Joined: 11 Mar 2010 Posts: 19587
Posted: Sat Aug 21, 2021 3:31 am
You have to work like this:
Code:
set_adc_channel(0);// Battery Voltage Sense
delay_us(10); //The channel has to be selected long enough
//for the signal to acquire -> Tacq. Check the data sheet for what this
//needs to be (9.55uSec min on your chip).
adc_value=read_adc(); //This now reads the battery voltage
set_adc_channel(2);// mains Voltage Sense
delay_us(10);
adc_value=read_adc(); //This now reads the main voltage sense.
Sequence is always select channel, wait for signal to acquire, then sample
this.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum