CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to change AD channel "in fly" ??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

How to change AD channel "in fly" ??
PostPosted: Mon Oct 07, 2019 5:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 07, 2019 7:13 am     Reply with quote

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);

Smile
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Mon Oct 07, 2019 7:37 am     Reply with quote

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);

Smile


Thank you very much ... now it works fine !!

all the best


-arto-
Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group