View previous topic :: View next topic |
Author |
Message |
wannabe Guest
|
function problem |
Posted: Tue Jan 05, 2010 9:04 pm |
|
|
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
|
|
Posted: Tue Jan 05, 2010 9:09 pm |
|
|
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
|
|
Posted: Tue Jan 05, 2010 9:18 pm |
|
|
Try this:
return ((read_adc()*5/65500)-2.5); |
|
|
Guest
|
|
Posted: Tue Jan 05, 2010 9:21 pm |
|
|
You may also need to prototype the function.
math might also be a reserved word. |
|
|
wanabe Guest
|
|
Posted: Tue Jan 05, 2010 10:35 pm |
|
|
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
|
|
Posted: Wed Jan 06, 2010 12:18 am |
|
|
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
|
Re: function problem |
Posted: Wed Jan 06, 2010 3:18 am |
|
|
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
|
|
Posted: Wed Jan 06, 2010 8:45 am |
|
|
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
|
|
Posted: Wed Jan 06, 2010 9:32 am |
|
|
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
|
|
Posted: Thu Jan 07, 2010 12:11 am |
|
|
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
}
|
|
|
|
|