View previous topic :: View next topic |
Author |
Message |
peterzatko
Joined: 20 Jun 2014 Posts: 14
|
Please help with switch |
Posted: Fri Jun 20, 2014 11:29 am |
|
|
I´m new in programming with C.
I need help with function switch.
My problem :
I have analog input on AN3 (its a 10 bit adc)
I read analog value from 0-1023
If analog value is 1 then variable(d)= 10000
If analog value is 1023 then variable(d)= 50000
If analog value is other(default) then variable(d)= 100000
variable "d" use in my code to change delay_us
i don't know what is wrong in my code!!!!!!
Code: |
void main()
{
int16 r;
int16 d;
setup_adc_ports(sAN3);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(3);
delay_ms(10);
while (True)
{
r = read_adc();
int r;
switch (r) {
case 1:d = 10000;break;
case 1023:d = 5000;break;
default :d = 100000;break;
}
output_high(PIN_A0);
delay_us(d);
output_low(PIN_A0);
delay_us(d);
}
} |
|
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Fri Jun 20, 2014 12:32 pm |
|
|
Things I see, what is the size of r inside the While loop?
Where was it declared?
Don't know if the ADC setup is correct since you don't say what CHIP... _________________ Google and Forum Search are some of your best tools!!!! |
|
|
peterzatko
Joined: 20 Jun 2014 Posts: 14
|
|
Posted: Fri Jun 20, 2014 12:34 pm |
|
|
int16 r; //0-65000 |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Fri Jun 20, 2014 12:36 pm |
|
|
What is this?
Code: | while (True)
{
r = read_adc();
int r; <===============
switch (r) {
|
_________________ Google and Forum Search are some of your best tools!!!! |
|
|
peterzatko
Joined: 20 Jun 2014 Posts: 14
|
|
Posted: Fri Jun 20, 2014 12:44 pm |
|
|
i used it .
program nothing do with it or not |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Fri Jun 20, 2014 1:01 pm |
|
|
You have two different variables "r" - the one declared in main and the one you declare again AFTER your read the adc. There are two issues - one is that you should only declare variables at the start of a function, not in the middle (I seem to remember hearing cases of CCS getting confused) - the most likely thing is that after you read the ADC, "r" is again declared - not sure what the state of uninitialized variables is, but that may also be causing strange things. You are also trying to declare "r" as both an int16 and an int (which is "int8"). See the data definitions section in the user manual.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri Jun 20, 2014 1:15 pm |
|
|
Hi,
In addition to the things already mentioned, your code strategy is really flawed. For example, expecting the A/D result to be exactly 1 or exactly 1023 is pretty unrealistic. Do something like this instead:
Code: |
case (r < 10):
d = 10000;
break;
case (r > 1010):
d = 5000;
break;
|
John |
|
|
peterzatko
Joined: 20 Jun 2014 Posts: 14
|
|
Posted: Fri Jun 20, 2014 3:01 pm |
|
|
i use these
ezflyr wrote: | Hi,
In addition to the things already mentioned, your code strategy is really flawed. For example, expecting the A/D result to be exactly 1 or exactly 1023 is pretty unrealistic. Do something like this instead:
Code: |
case (r < 10):
d = 10000;
break;
case (r > 1010):
d = 5000;
break;
|
John |
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Jun 20, 2014 4:04 pm |
|
|
ezflyr wrote: |
Code: |
case (r < 10):
d = 10000;
break;
case (r > 1010):
d = 5000;
break;
|
|
Does that even work in C?
I'm looking at my old K&R C and it states "const expr" for case.
I would just use IF's.
Code: |
if (r < 10) {
r = some value;
} else if (r > 1010) {
r = some value;
}
|
_________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
peterzatko
Joined: 20 Jun 2014 Posts: 14
|
|
Posted: Sat Jun 21, 2014 12:59 am |
|
|
i need about 60 case
bkamen wrote: | ezflyr wrote: |
Code: |
case (r < 10):
d = 10000;
break;
case (r > 1010):
d = 5000;
break;
|
|
Does that even work in C?
I'm looking at my old K&R C and it states "const expr" for case.
I would just use IF's.
Code: |
if (r < 10) {
r = some value;
} else if (r > 1010) {
r = some value;
}
|
|
|
|
|
peterzatko
Joined: 20 Jun 2014 Posts: 14
|
|
Posted: Sat Jun 21, 2014 1:05 am |
|
|
if i compile writte error expression must evaluate to a constant
peterzatko wrote: | i need about 60 case
bkamen wrote: | ezflyr wrote: |
Code: |
case (r < 10):
d = 10000;
break;
case (r > 1010):
d = 5000;
break;
|
|
Does that even work in C?
I'm looking at my old K&R C and it states "const expr" for case.
I would just use IF's.
Code: |
if (r < 10) {
r = some value;
} else if (r > 1010) {
r = some value;
}
|
|
|
|
|
|
|