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 support@ccsinfo.com

Returning a value from a function outside main program.

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







Returning a value from a function outside main program.
PostPosted: Fri Apr 25, 2003 9:17 pm     Reply with quote

<html>
<body>
<pre>

I am trying to return a value in a function located in a driver
file to the calling function in the main program. what I have below
should work but doesn't. The driver file is included at the top of
the main program file. When I compile the main program I get the
following error 'Undefined identifier value' and the word value
is highlighted in the 'value = read_adc_value(); line below.



Loop in main file
-----------------

do {
value = read_adc_value(); // Goto read ADC value sub-routine
lcd_gotoxy(7,1); // goto position 7 on line 1
printf(lcd_putc,"\%lu ",value); // display raw ADC value on line 1
Delay_ms(500); // Delay 1/2 second
} While (TRUE);


Function in driver file
-----------------------

// READ CS5516 ADC VALUE
int16 read_adc_value() {
int16 value; // define value variable
byte i; // define counter variable

While (!input(ADC_DI)); // WAIT FOR ADC_DI TO GO HIGH
while (input(ADC_DI)); // WAIT FOR ADC_DI TO GO LOW

for(i=1;i<=8;++i) { // repeat 8 times
output_high(ADC_CLK); // set adc clock pin high
output_low(ADC_CLK); // set adc clock pin low
}
for(i=1;i<=16;++i) { // repeat 16 times
shift_left(&value,2,input(ADC_DI)); // shift bit into value
output_high(ADC_CLK); // set adc clock pin high
output_low(ADC_CLK); // set adc clock pin low
}
for(i=1;i<=8;++i) { // repeat 8 times
output_high(ADC_CLK); // set adc clock pin high
output_low(ADC_CLK); // set adc clock pin low
}

return value;
}



</body>
___________________________
This message was ported from CCS's old forum
Original Post ID: 14005
Dave Yeatman
Guest







Re: Returning a value from a function outside main program.
PostPosted: Fri Apr 25, 2003 9:53 pm     Reply with quote

<font face="Courier New" size=-1>Do you have Value defined anywhere in the main program? I only see it defined as local in the Function then you are trying to assign a value to it outside the function,

Dave

:=<html>
:=<body>
:=<pre>
:=
:=I am trying to return a value in a function located in a driver
:=file to the calling function in the main program. what I have below
:=should work but doesn't. The driver file is included at the top of
:=the main program file. When I compile the main program I get the
:=following error 'Undefined identifier value' and the word value
:=is highlighted in the 'value = read_adc_value(); line below.
:=
:=
:=
:=Loop in main file
:=-----------------
:=
:=do {
:= value = read_adc_value(); // Goto read ADC value sub-routine
:= lcd_gotoxy(7,1); // goto position 7 on line 1
:= printf(lcd_putc,"\%lu ",value); // display raw ADC value on line 1
:= Delay_ms(500); // Delay 1/2 second
:= } While (TRUE);
:=
:=
:=Function in driver file
:=-----------------------
:=
:=// READ CS5516 ADC VALUE
:=int16 read_adc_value() {
:= int16 value; // define value variable
:= byte i; // define counter variable
:=
:= While (!input(ADC_DI)); // WAIT FOR ADC_DI TO GO HIGH
:= while (input(ADC_DI)); // WAIT FOR ADC_DI TO GO LOW
:=
:= for(i=1;i<=8;++i) { // repeat 8 times
:= output_high(ADC_CLK); // set adc clock pin high
:= output_low(ADC_CLK); // set adc clock pin low
:= }
:= for(i=1;i<=16;++i) { // repeat 16 times
:= shift_left(&value,2,input(ADC_DI)); // shift bit into value
:= output_high(ADC_CLK); // set adc clock pin high
:= output_low(ADC_CLK); // set adc clock pin low
:= }
:= for(i=1;i<=8;++i) { // repeat 8 times
:= output_high(ADC_CLK); // set adc clock pin high
:= output_low(ADC_CLK); // set adc clock pin low
:= }
:=
:= return value;
:=}
:=
:=
:=
:= :=</body></font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 14006
whmeade10
Guest







Re: Returning a value from a function outside main program.
PostPosted: Sat Apr 26, 2003 7:53 am     Reply with quote

I defined the variable in the calling function and I am getting a garbled number back. The number is jumping all over the place. There must still be something defined wrong somewhere.

:=<font face="Courier New" size=-1>Do you have Value defined anywhere in the main program? I only see it defined as local in the Function then you are trying to assign a value to it outside the function,
:=
:=Dave
:=
:=:=<html>
:=:=<body>
:=:=<pre>
:=:=
:=:=I am trying to return a value in a function located in a driver
:=:=file to the calling function in the main program. what I have below
:=:=should work but doesn't. The driver file is included at the top of
:=:=the main program file. When I compile the main program I get the
:=:=following error 'Undefined identifier value' and the word value
:=:=is highlighted in the 'value = read_adc_value(); line below.
:=:=
:=:=
:=:=
:=:=Loop in main file
:=:=-----------------
:=:=
:=:=do {
:=:= value = read_adc_value(); // Goto read ADC value sub-routine
:=:= lcd_gotoxy(7,1); // goto position 7 on line 1
:=:= printf(lcd_putc,"\%lu ",value); // display raw ADC value on line 1
:=:= Delay_ms(500); // Delay 1/2 second
:=:= } While (TRUE);
:=:=
:=:=
:=:=Function in driver file
:=:=-----------------------
:=:=
:=:=// READ CS5516 ADC VALUE
:=:=int16 read_adc_value() {
:=:= int16 value; // define value variable
:=:= byte i; // define counter variable
:=:=
:=:= While (!input(ADC_DI)); // WAIT FOR ADC_DI TO GO HIGH
:=:= while (input(ADC_DI)); // WAIT FOR ADC_DI TO GO LOW
:=:=
:=:= for(i=1;i<=8;++i) { // repeat 8 times
:=:= output_high(ADC_CLK); // set adc clock pin high
:=:= output_low(ADC_CLK); // set adc clock pin low
:=:= }
:=:= for(i=1;i<=16;++i) { // repeat 16 times
:=:= shift_left(&value,2,input(ADC_DI)); // shift bit into value
:=:= output_high(ADC_CLK); // set adc clock pin high
:=:= output_low(ADC_CLK); // set adc clock pin low
:=:= }
:=:= for(i=1;i<=8;++i) { // repeat 8 times
:=:= output_high(ADC_CLK); // set adc clock pin high
:=:= output_low(ADC_CLK); // set adc clock pin low
:=:= }
:=:=
:=:= return value;
:=:=}
:=:=
:=:=
:=:=
:=:= :=:=</body></font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 14008
liche
Guest







Re: Returning a value from a function outside main program.
PostPosted: Sat Apr 26, 2003 10:11 am     Reply with quote

You need to define the value in the main program as well. I would also call the variables different names. From a compiler stand-point, it doesn't matter, but it will be easier to keep track of which values are what. Here is an example.

...

int valueAD1; // Define as global variable

main {
valueAD1 = GetAD1 (value);
}

int GetAD1 (int value)
{
int value
value = 666;
return value;
}

Liche




:=I defined the variable in the calling function and I am getting a garbled number back. The number is jumping all over the place. There must still be something defined wrong somewhere.
:=
:=:=<font face="Courier New" size=-1>Do you have Value defined anywhere in the main program? I only see it defined as local in the Function then you are trying to assign a value to it outside the function,
:=:=
:=:=Dave
:=:=
:=:=:=<html>
:=:=:=<body>
:=:=:=<pre>
:=:=:=
:=:=:=I am trying to return a value in a function located in a driver
:=:=:=file to the calling function in the main program. what I have below
:=:=:=should work but doesn't. The driver file is included at the top of
:=:=:=the main program file. When I compile the main program I get the
:=:=:=following error 'Undefined identifier value' and the word value
:=:=:=is highlighted in the 'value = read_adc_value(); line below.
:=:=:=
:=:=:=
:=:=:=
:=:=:=Loop in main file
:=:=:=-----------------
:=:=:=
:=:=:=do {
:=:=:= value = read_adc_value(); // Goto read ADC value sub-routine
:=:=:= lcd_gotoxy(7,1); // goto position 7 on line 1
:=:=:= printf(lcd_putc,"\%lu ",value); // display raw ADC value on line 1
:=:=:= Delay_ms(500); // Delay 1/2 second
:=:=:= } While (TRUE);
:=:=:=
:=:=:=
:=:=:=Function in driver file
:=:=:=-----------------------
:=:=:=
:=:=:=// READ CS5516 ADC VALUE
:=:=:=int16 read_adc_value() {
:=:=:= int16 value; // define value variable
:=:=:= byte i; // define counter variable
:=:=:=
:=:=:= While (!input(ADC_DI)); // WAIT FOR ADC_DI TO GO HIGH
:=:=:= while (input(ADC_DI)); // WAIT FOR ADC_DI TO GO LOW
:=:=:=
:=:=:= for(i=1;i<=8;++i) { // repeat 8 times
:=:=:= output_high(ADC_CLK); // set adc clock pin high
:=:=:= output_low(ADC_CLK); // set adc clock pin low
:=:=:= }
:=:=:= for(i=1;i<=16;++i) { // repeat 16 times
:=:=:= shift_left(&value,2,input(ADC_DI)); // shift bit into value
:=:=:= output_high(ADC_CLK); // set adc clock pin high
:=:=:= output_low(ADC_CLK); // set adc clock pin low
:=:=:= }
:=:=:= for(i=1;i<=8;++i) { // repeat 8 times
:=:=:= output_high(ADC_CLK); // set adc clock pin high
:=:=:= output_low(ADC_CLK); // set adc clock pin low
:=:=:= }
:=:=:=
:=:=:= return value;
:=:=:=}
:=:=:=
:=:=:=
:=:=:=
:=:=:= :=:=:=</body></font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 14009
Carlos Barberis
Guest







Re: Returning a value from a function outside main program.
PostPosted: Sat Apr 26, 2003 10:16 am     Reply with quote

:=<html>
:=<body>
:=<pre>
:=
:=I am trying to return a value in a function located in a driver
:=file to the calling function in the main program. what I have below
:=should work but doesn't. The driver file is included at the top of
:=the main program file. When I compile the main program I get the
:=following error 'Undefined identifier value' and the word value
:=is highlighted in the 'value = read_adc_value(); line below.
:=
:=
:=
:=Loop in main file
:=-----------------
:=
:=do {
:= value = read_adc_value(); // Goto read ADC value sub-routine
:= lcd_gotoxy(7,1); // goto position 7 on line 1
:= printf(lcd_putc,"\%lu ",value); // display raw ADC value on line 1
:= Delay_ms(500); // Delay 1/2 second
:= } While (TRUE);
:=
:=
:=Function in driver file
:=-----------------------
:=
:=// READ CS5516 ADC VALUE
:=int16 read_adc_value() {
:= int16 value; // define value variable
:= byte i; // define counter variable
:=
:= While (!input(ADC_DI)); // WAIT FOR ADC_DI TO GO HIGH
:= while (input(ADC_DI)); // WAIT FOR ADC_DI TO GO LOW
:=
:= for(i=1;i<=8;++i) { // repeat 8 times
:= output_high(ADC_CLK); // set adc clock pin high
:= output_low(ADC_CLK); // set adc clock pin low
:= }
:= for(i=1;i<=16;++i) { // repeat 16 times
:= shift_left(&value,2,input(ADC_DI)); // shift bit into value
:= output_high(ADC_CLK); // set adc clock pin high
:= output_low(ADC_CLK); // set adc clock pin low
:= }
:= for(i=1;i<=8;++i) { // repeat 8 times
:= output_high(ADC_CLK); // set adc clock pin high
:= output_low(ADC_CLK); // set adc clock pin low
:= }
:=
:= return value;
:=}
:=
:=
:=
:= :=</body>
Hopefully you have declared a variable named value; such as int16 value within the main() routine
Also, I,m not sure wether it matters, but I ussually use the following syntax to my return value: return(value);
Other than that I cannot see why the above would not work.
___________________________
This message was ported from CCS's old forum
Original Post ID: 14010
whmeade10
Guest







Re: Returning a value from a function outside main program.
PostPosted: Sat Apr 26, 2003 8:58 pm     Reply with quote

<html>
<body>
<pre>

I ended up getting it working by defining 'value' at the top
of the driver file only. I even took the declaration out of
the function and everything is working great. Thanks to
everyone who responded to my question.

</pre>
</body>
</html>



:=You need to define the value in the main program as well. I would also call the variables different names. From a compiler stand-point, it doesn't matter, but it will be easier to keep track of which values are what. Here is an example.
:=
:=...
:=
:=int valueAD1; // Define as global variable
:=
:=main {
:=valueAD1 = GetAD1 (value);
:=}
:=
:=int GetAD1 (int value)
:={
:=int value
:=value = 666;
:=return value;
:=}
:=
:=Liche
:=
:=
:=
:=
:=:=I defined the variable in the calling function and I am getting a garbled number back. The number is jumping all over the place. There must still be something defined wrong somewhere.
:=:=
:=:=:=<font face="Courier New" size=-1>Do you have Value defined anywhere in the main program? I only see it defined as local in the Function then you are trying to assign a value to it outside the function,
:=:=:=
:=:=:=Dave
:=:=:=
:=:=:=:=<html>
:=:=:=:=<body>
:=:=:=:=<pre>
:=:=:=:=
:=:=:=:=I am trying to return a value in a function located in a driver
:=:=:=:=file to the calling function in the main program. what I have below
:=:=:=:=should work but doesn't. The driver file is included at the top of
:=:=:=:=the main program file. When I compile the main program I get the
:=:=:=:=following error 'Undefined identifier value' and the word value
:=:=:=:=is highlighted in the 'value = read_adc_value(); line below.
:=:=:=:=
:=:=:=:=
:=:=:=:=
:=:=:=:=Loop in main file
:=:=:=:=-----------------
:=:=:=:=
:=:=:=:=do {
:=:=:=:= value = read_adc_value(); // Goto read ADC value sub-routine
:=:=:=:= lcd_gotoxy(7,1); // goto position 7 on line 1
:=:=:=:= printf(lcd_putc,"\%lu ",value); // display raw ADC value on line 1
:=:=:=:= Delay_ms(500); // Delay 1/2 second
:=:=:=:= } While (TRUE);
:=:=:=:=
:=:=:=:=
:=:=:=:=Function in driver file
:=:=:=:=-----------------------
:=:=:=:=
:=:=:=:=// READ CS5516 ADC VALUE
:=:=:=:=int16 read_adc_value() {
:=:=:=:= int16 value; // define value variable
:=:=:=:= byte i; // define counter variable
:=:=:=:=
:=:=:=:= While (!input(ADC_DI)); // WAIT FOR ADC_DI TO GO HIGH
:=:=:=:= while (input(ADC_DI)); // WAIT FOR ADC_DI TO GO LOW
:=:=:=:=
:=:=:=:= for(i=1;i<=8;++i) { // repeat 8 times
:=:=:=:= output_high(ADC_CLK); // set adc clock pin high
:=:=:=:= output_low(ADC_CLK); // set adc clock pin low
:=:=:=:= }
:=:=:=:= for(i=1;i<=16;++i) { // repeat 16 times
:=:=:=:= shift_left(&value,2,input(ADC_DI)); // shift bit into value
:=:=:=:= output_high(ADC_CLK); // set adc clock pin high
:=:=:=:= output_low(ADC_CLK); // set adc clock pin low
:=:=:=:= }
:=:=:=:= for(i=1;i<=8;++i) { // repeat 8 times
:=:=:=:= output_high(ADC_CLK); // set adc clock pin high
:=:=:=:= output_low(ADC_CLK); // set adc clock pin low
:=:=:=:= }
:=:=:=:=
:=:=:=:= return value;
:=:=:=:=}
:=:=:=:=
:=:=:=:=
:=:=:=:=
:=:=:=:= :=:=:=:=</body></font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 14016
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