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

function problem

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







function problem
PostPosted: Tue Jan 05, 2010 9:04 pm     Reply with quote

greetings...

I tried to compile a simple function prog... but it give some compile error
.... Undefined identifier.
Code:

main ()
{
   set_ADC_channel(0);
   delay_us(20);
   math ();
}

math ()
{
return (read_adc()*5/65500)-2.5;
}
wannabe
Guest







PostPosted: Tue Jan 05, 2010 9:09 pm     Reply with quote

Sorry.
The compile error is at the function math().

I tried using int16 for the function math but it give new error
............a numeric expression must appear here -1 is not 0..255.
I change to float and it give same error.
Guest








PostPosted: Tue Jan 05, 2010 9:18 pm     Reply with quote

Try this:

return ((read_adc()*5/65500)-2.5);
Guest








PostPosted: Tue Jan 05, 2010 9:21 pm     Reply with quote

You may also need to prototype the function.

math might also be a reserved word.
wanabe
Guest







PostPosted: Tue Jan 05, 2010 10:35 pm     Reply with quote

Anonymous wrote:
You may also need to prototype the function.

math might also be a reserved word.


how to do this?
mbradley



Joined: 11 Jul 2009
Posts: 118
Location: California, USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Jan 06, 2010 12:18 am     Reply with quote

just curious:

((read_adc()*5/65500)-2.5);

lets say the max adc is 4096, let step through:

1) 4096 * 5 = 20480
2) 20480 / 65500 = .312671756
3) .312671756 - 2.5 = -2.87328244

We end up with a negative number

I dont know if this helps or not
_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

Re: function problem
PostPosted: Wed Jan 06, 2010 3:18 am     Reply with quote

wannabe wrote:
greetings...

I tried to compile a simple function prog... but it give some compile error
.... Undefined identifier.
Code:

main ()
{
   set_ADC_channel(0);
   delay_us(20);
   math ();
}

math ()
{
return (read_adc()*5/65500)-2.5;
}


First of all the math function you have written should not return a value:-
try
Code:

float math()
{
  return (read_adc()*5/65500)-2.5;
}


Next as stated you may have to prototype the function OR define the function before it is used e.g. put the function before main.
Having to do this is standard C, in C++ and prob some C compilers you don't have to.

Next, I am a little concerned about the equation.
read_adc returns either an 8bit or 16bit int and the first part only uses 16 bit values so as mbradley shows :-
1) 4096 * 5 = 20480
2) 20480 / 65500 = .312671756
3) .312671756 - 2.5 = -2.87328244

The value being negative is not the problem it is step 2. With 16 bit int math :-

1) 4096 * 5 = 20480
2) 20480 / 65500 = 0 (16 bit math)
3) 0 - 2.5 = -2.5 (will hopefully convert to float)

If this is the case you can fix it with type casting :-
Code:
return ((float)(read_adc()*5)/65500)-2.5;

or even
Code:
return (read_adc()*5.0f/65500)-2.5;


Hope this helps.
wanabe
Guest







PostPosted: Wed Jan 06, 2010 8:45 am     Reply with quote

I change it a bit.
Now I get this error after I tried to compile the code..>>
...unknown device type --try PCM
I'm using PCW compiler.
Code:

#include<16F877A.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)

#include <STDLIB.H>

int16 math ()

void main ()

{

setup_adc_ports( ANALOG_RA3_REF );
setup_adc( ADC_CLOCK_DIV_32 );

while (1)
{
   set_ADC_channel(0);
   delay_us(20);
   math();
   delay_us(5);
   
}

float math()
{
  return (read_adc()*5/1023)-2.5;
}

}
Ttelmah
Guest







PostPosted: Wed Jan 06, 2010 9:32 am     Reply with quote

PCW, is the 'windows front end' for the compiler. Near the top of the screen, above the text 'Lookup part', you have which compiler is going to be used. PCB, PCM, PCH, PCD etc., for the different chip families.
Select PCM here.

Your function prototype, must _match_ what the function actually returns. Currently you have the prototype saying it returns an int16, and the actual function returning a float....

You are still not casting the arithmetic on the incoming value to a float, so will lose most values.

Best Wishes
takumi



Joined: 07 Mar 2009
Posts: 14

View user's profile Send private message

PostPosted: Thu Jan 07, 2010 12:11 am     Reply with quote

Code:

#include<16F877A.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)

#include <STDLIB.H>

int16 math () ; // a function prototype must have ; after ()

void main(){

//your code

}

int16 math(){

//your code
}
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