View previous topic :: View next topic |
Author |
Message |
runpigeon
Joined: 09 Mar 2013 Posts: 3
|
Problem using numerical expressions for ADC converter |
Posted: Mon Mar 18, 2013 11:14 am |
|
|
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: 9221 Location: Greensville,Ontario
|
|
Posted: Mon Mar 18, 2013 11:27 am |
|
|
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
|
|
Posted: Mon Mar 18, 2013 11:31 am |
|
|
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: 9221 Location: Greensville,Ontario
|
|
Posted: Mon Mar 18, 2013 11:37 am |
|
|
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: 19498
|
|
Posted: Mon Mar 18, 2013 12:00 pm |
|
|
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 |
|
|
|