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

Please help with switch

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



Joined: 20 Jun 2014
Posts: 14

View user's profile Send private message

Please help with switch
PostPosted: Fri Jun 20, 2014 11:29 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 12:32 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 12:34 pm     Reply with quote

int16 r; //0-65000
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 12:36 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 12:44 pm     Reply with quote

i used it .
program nothing do with it or not
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 1:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 1:15 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 3:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 20, 2014 4:04 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jun 21, 2014 12:59 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jun 21, 2014 1:05 am     Reply with quote

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

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