View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
How to change AD channel "in fly" ?? |
Posted: Mon Oct 07, 2019 5:52 am |
|
|
Hello,
I'm trying to change AD-channel from channel 0 to channel 1 but I can't make it work.
Please help me... I'm tried it about three hours today and nothing seems to help.
Code: |
#include <16F88.h>
#device ADC=10
.
.
.
//I THINK THIS WORKS ,,,
void init_FIRST(void){
set_pwm1_duty(0);
led_demo();
// pin A0 measures temperature,pin A1 measures battery voltage:
//I WANT VREF LOW TO BE PIN A2 TO BE LOW REFERENCE IT'S GROUNDED AND
//PIN A3 HIGH REFERENCE IT'S VOLTAGE COMES FROM REFERENCE IC REF02 TO PIN A3
//IS THIS OK FOR MY PURPOSE ?????
setup_adc_ports(sAN0 | sAN1 | VREF_VREF);
//It is now set to use the two external Vref pins and allow readings
//from AN0 and AN1.
setup_adc(ADC_CLOCK_DIV_16);
.
.
.
//THIS WORKS
float meas_temp(void){
float PV;
set_adc_channel(sAN0);
delay_ms(20);
PV = read_ADC();
return PV;
}
//BUT THIS DON'T WORK !!!!!!!!
//MY battery_V is measured about 2.5 V but lowbat_led is allways ON
//
//battery_V = 900; this make it OFF ok
void test_battery(void){
int16 battery_V;
setup_adc_ports(sAN0 | sAN1 | VREF_VREF);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(sAN1);
delay_ms(20);
battery_V = read_ADC();
delay_ms(20);
//battery_V = 900;
if(battery_V <= 10) //battery voltage below 8 V turn lowbat_led_ON
lowbat_led_ON;
if(battery_V > 10) //battery voltage more than 8 V turn lowbat_led_OFF
lowbat_led_OFF;
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Oct 07, 2019 7:13 am |
|
|
Two things:
First you only need to do the actual 'setup', once. So just have:
setup_adc_ports(sAN0 | sAN1 | VREF_VREF);
setup_adc(ADC_CLOCK_DIV_16);
Once at the start of the code. This is not needed again.
The reason it is not working, is the wrong value for the set_adc_channel
command. This doesn't want/take the defines sAN0 etc. It just takes the
channel number. So you just want:
set_adc_channel(0);
and
set_adc_channel(1);
|
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Mon Oct 07, 2019 7:37 am |
|
|
Ttelmah wrote: | Two things:
First you only need to do the actual 'setup', once. So just have:
setup_adc_ports(sAN0 | sAN1 | VREF_VREF);
setup_adc(ADC_CLOCK_DIV_16);
Once at the start of the code. This is not needed again.
The reason it is not working, is the wrong value for the set_adc_channel
command. This doesn't want/take the defines sAN0 etc. It just takes the
channel number. So you just want:
set_adc_channel(0);
and
set_adc_channel(1);
|
Thank you very much ... now it works fine !!
all the best
-arto-
|
|
|
|