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 support@ccsinfo.com

Problem using numerical expressions for ADC converter

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



Joined: 09 Mar 2013
Posts: 3

View user's profile Send private message

Problem using numerical expressions for ADC converter
PostPosted: Mon Mar 18, 2013 11:14 am     Reply with quote

hi,

I am using an ADC converter to light 4 LEDs in sequence depending on the position of a variable resistor. But for some reason the numerical expressions I have created are not being recognized. The expression for case 0 and case 3 are recognized but 1 and 2 are not. Could anyone explain why and how I could fix this?

cheers

Code:

#include  <16f877a.h>
#device  ICD=TRUE
#fuses  HS,NOLVP,NOWDT,PUT
#use delay  (clock=20000000)

#define  FIRST_SIGNAL  PIN_B2
#define  SECOND_SIGNAL  PIN_B0

#define bottom 60
#define top 180
#define middle 120

void light_one_led(int led)
{
Output_low(FIRST_SIGNAL);
Output_low(SECOND_SIGNAL);
Output_low(THIRD_SIGNAL);
Output_low(FOURTH_SIGNAL);
Switch(led)
      {
      Case 0: output_high(FIRST_SIGNAL);break;
      Case 1: output_high(SECOND_SIGNAL);break;
      Case 2: output_high(THIRD_SIGNAL);break;
      Case 3: output_high(FOURTH_SIGNAL);break;
      }
}

void main()
{
int reading;
Setup_adc_ports(RA0_Analog);
Setup_adc (ADC_CLOCK_INTERNAL);
Set_adc_channel(0);

while(TRUE)
   {
   reading = read_adc();
   if(reading<bottom)
      light_one_led(0);
   else if (bottom<reading<middle)
      light_one_led(1);
   else if (middle<reading<top)
      light_one_led(2);
   else if (reading>top)
      light_one_led(3);
   }
}
temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 11:27 am     Reply with quote

some things to consider....

1) ICD=true may affect what you're 'seeing'.

2) no defines for 3rd or 4th signal(I assume and LED ?)

3) C coding for the logic of 'bottom<reading<middle' isn't correct.

4) same for the next 'comparison'.

5) setup adc clock is wrong...

6) overall code could be made a lot smaller,simpler to code and read

7) should have a delay in main 'loop' to better display the 'action'.

may be more but I need a coffee.....

hth
jay
runpigeon



Joined: 09 Mar 2013
Posts: 3

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 11:31 am     Reply with quote

temtronic,

I understand your points but my main concern here is understanding how to correctly code the logic statement bottom<reading<middle, as mathematically this gives me the range i require for this reading but I am not sure how to code this in C.....can you shed some light on it?
temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 11:37 am     Reply with quote

pressing F11 while your project is open will give you great access to the CCS C help files .......
they do show how to use 'relational operators'.

also

check out the examples within the EXAMPLES folder.
there's actual code in one of them that will show you how to compose the code

rats, coffee's cold....nuke time

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Mon Mar 18, 2013 12:00 pm     Reply with quote

Key point to understand is that a single logic statement just returns true/false. and evaluates left to right. So:

bottom<reading<middle

evaluates as bottom<reading giving 0/1 for true/false

then

0/1<middle

You need something like:

(bottom<reading) && (reading<middle)

Best Wishes
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