function return

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello,

i have a question about the return of this function:

unsigned char lcd_init(unsigned char lcd_columns)
{

return ???
}

i wanna know how i can find out what the return variable is. i can't find it in the datasheets. do i need to create one myself or how..?

the help function of codevision tells me:

'The function returns 1 if the LCD module is detected and 0 if it is not.
This is the first function that must be called before using the other high level LCD Functions.'

for example, this function:

unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;

the return valiable is here ADCH

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Where did you find this function?

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How Pavel calculates the return value from his library function is up to him.

All you need to know is that if the return value is 0 then the LCD is either not present or broken.

If the return value is 1 then everything is OK.

If you write a function yourself, then you just put your required result in the return statement.

int whose_name(char *s)
{
    if (strcmpf(s, "David") == 0) return 1;
    if (strcmpf(s, "Provak") == 0) return 2;
    return 0;
}

Any book on C will explain return expressions. The most important lesson is to ALWAYS make use of them.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's not going to be in any datasheet, because it has nothing to do with the AVR... it is part of the C language.

The return type is defined with the function. Notice the "unsigned char" before the function name, had this been "void" then there would be no return value.

As for what variable is returned, no variable is returned, only a value, and that value is what ever is in the contents of the variable after the word "return" or the direct value itself if you said "return 1" for example.

The calling code receives this value, just like it's reading a variable.

someVar = someFunc(someVal, someOtherVal);

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the typical function definition :

RETURN_TYPE FUNCTION_NAME ( INPUT VARS )
{
//just for example
RETURN SOMETHING;
}

if function type is VOID the function has no return type. just a little example

unsigned char double_it(unsigned char ucNumber)
{
     unsigned char ucReturnData = 0;
     ucReturnData = ucNumber * 2;

     return ucReturnData;
}

Then you can as mentioned in the post above in your code to use the function.

unsigned char ucDoubleNumber = double_it(2);

will give ucDoubleNumber = 4

Understand provac ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think he's just a bit confused about how variables are returned. What's important to know is that _you_ are going to define the variable for the return value ie:

unsigned char returnedValue;

returnedValue = lcd_init();

if(returnedValue == 1) {
    // it worked!
} else {
    // uhoh
}

The variable defined in the function is irrelevant.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

okey! But also imporant to remember if returnedValue is unsigned char, the function type also have to be the same.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks for the replies, some things are making sence, but not all.

in the codevision help function is writen:

unsigned char lcd_init(unsigned char lcd_columns)

initializes the LCD module, clears the display and sets the printing character position at row 0 and column 0. The numbers of columns of the LCD must be specified (e.g. 16). No cursor is displayed.
The function returns 1 if the LCD module is detected and 0 if it is not.
This is the first function that must be called before using the other high level LCD Functions.

what is the function that i need to write?

i was thinking about something like this:

unsigned char lcd_init(unsigned char 16)
  {   
  unsigned char lcd_ready;
  
  return lcd_ready
  }

but this ain't right :P. my question is how can i write the function that it gives me 1 or 0 when it detects an lcd.

thanks,

Robbin

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you just want to know how to use a library function ?

Or do you want to know how to write the equivalent library function yourself ?

In the first case, you read the documentation, call the function as advised. Make use of the return value appropriately. If you look at the CodeVision help files, you will see how this is done.

    char good_lcd = lcd_init(16);
    if (good_lcd == 0) {
         // assuming you have an UART working
         printf("Oops,  You have forgotten the LCD\n");
    }
    else {
         lcd_putsf("My LCD is working");
    }

If you want to write your own equivalent lcd_init() function, then you need to learn a lot more C. You can practice your C on the PC, or use any books on C.

Then you read the documentation for the LCD driver chip, and write your own code. When you understand someone else's code, you can begin to write your own version. (Hint. Pavel's code checks to see if the LCD responds to a read command. if no response then no LCD)

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you are missing what others above are saying. The lcd_init function returns a value, its up to you to do something with it. No need for another function to get the return value of lcd_init, only to then to turn around and return that value again to somewhere else.

They probably intend something like-

if (lcd_init(16)==0){
  //crap, someone disconnected the lcd again
  light_up_all_lights();
  sound_every_buzzer();
  //the lcd is so important, we will have
  //to wait for some user to plug in the lcd
  //and reset us
  cli();
  while(1);
}

but its not required to do anything with the return value, and if a non-functioning lcd is not that important, or you have no plan on what to do if it isn't working, then skip reading the return value.

The 'lcd_init(16)' can be thought of as - 'go do the lcd_init function with 16 passed to the function, and upon returning replace me with the returned value.

So 'lcd_init(16)' turns into a 0x01 or a 0x00 when its done, and then it can be compared to the 0 in the rest of the if statement.