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

use preproccer #if to set column width on lcd

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



Joined: 02 Nov 2003
Posts: 18

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

use preproccer #if to set column width on lcd
PostPosted: Sun Feb 22, 2004 9:36 pm     Reply with quote

I always seem to have a need for different lcds with longer or shorter character lengths. I tried to use the preproccer #if to make things easier so I didn't have to rewrite a bunch of x values in my lcd_goto_xy() functions. But the following doesn't seem to work. I am sure it has something to do with x being a variable. So, has anyone out there done this before?

Code:
#if LINE_SIZE == 16
  x = (x-4)
#elseif LINE_SIZE == 24
  x = (x+4)
#endif
void lcd_goto_xy(byte x, byte y)
{
 byte address;

  if(y!=1)
     address=lcd_row_offset;
    else
      address=0;
      address+=x-1;
      lcd_send_byte(0,0x80|address);
}

Thanks in advance

Ed Arnold
Code:
jds-pic2
Guest







Re: use preproccer #if to set column width on lcd
PostPosted: Sun Feb 22, 2004 9:55 pm     Reply with quote

Ed Arnold wrote:
But the following doesn't seem to work...


"doesn't work" doesn't help us.

doesn't compile?
doesn't output correctly for sizes other than 20?
doesn't output correctly for all sizes?
output is the same for all sizes?

now that i have remanded you appropriately, your problem is not with the code but in your construction thereof. it's actually a common error that has bitten us all a few times.

your code should be as such:

Code:

void lcd_goto_xy(byte x, byte y)
{
 byte address;

  if(y!=1)
     address=lcd_row_offset;
    else {
      address=0;
      address+=x-1;
      lcd_send_byte(0,0x80|address);
      }
}


notice that the else is now inclusive of the 3 following statements, whereas before it only included the "address=0;" statement. this is one reason why you may want to *always* include brackets with if and else constructs since it prevents coding errors when you revise you code.

jds-pic
Ed Arnold



Joined: 02 Nov 2003
Posts: 18

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

to clearify myself
PostPosted: Sun Feb 22, 2004 10:18 pm     Reply with quote

Sorry, I wasn't very clear. The code itself compiles and works only for the x and y varialbes passed to it. As for the missing brackets, that was an oversite, not on my part, but CCS. But you are correct in seeing the need for them. The part of the code I want to focus on id the preprocessor #if #elseif #endif statements which would allow me to use different size LCD modules without having to change the lcd_goto_xy() values x and y every time we change an LCD module.
Ed Arnold



Joined: 02 Nov 2003
Posts: 18

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

Maybe this will work instead
PostPosted: Sun Feb 22, 2004 10:58 pm     Reply with quote

After a closer look, I find that the CCS example wasn't flawed, just not clearly written. There wasn't a need for brackets. But I am tring a new approach on my code. Not sure if it will work until I get to my shop tomorrow. If anyone wants to take a look at this code and tell me if I made any mistakes or want to give me any ideas on a different approach, I will gladly read on.

Code:
#if LINE_SIZE == 16
  #define lcd_col_offset 0
#elseif LINE_SIZE == 20
  #define lcd_col_offset 4
#elseif LINE_SIZE == 24
  #define lcd_col_offset 8 
#endif
void lcd_goto_xy(byte x, byte y)
{
 byte addr;
   
  if(y!=1)addr=lcd_row_offset;
    else address=0;
  if(!lcd_col_offset)addr+=x-1;
    else address+=(x+lcd_col_offset)-1;
  lcd_send_byte(0,0x80|addr); 
}


Thanks

Ed Arnold
Guest








Re: to clearify myself
PostPosted: Sun Feb 22, 2004 10:59 pm     Reply with quote

ok, so your code is supposed to look like this?

Code:

#if LINE_SIZE == 16
  x = (x-4)
#elseif LINE_SIZE == 24
  x = (x+4)
#endif
void lcd_goto_xy(byte x, byte y)
{
 byte address;

  if(y!=1)
     address=lcd_row_offset;
    else
      address=0;
  address+=x-1;
  lcd_send_byte(0,0x80|address);
}


but now i'm more confused, i think (!)... Confused

your mainline code obviously has a function call as such:
lcd_goto_xy(1, 8);
which is supposed to position the cursor at x=1 and y=8.

however, the entrance to your lcd_goto_xy() function does NOT begin at any of the #defines prior to the function. in fact, who knows what the cpp/compiler is doing with those lines.

ergo, whatever logic you want to use to discriminate the LCD size must be INSIDE the function lcd_goto_xy().

Code:

void lcd_goto_xy(byte x, byte y)
{
 byte address;
#if LINE_SIZE == 16
  x = (x-4)
#elseif LINE_SIZE == 24
  x = (x+4)
#endif
  if(y!=1)
     address=lcd_row_offset;
    else
      address=0;
  address+=x-1;
  lcd_send_byte(0,0x80|address);
}


note:
there is a better way to do this, one that is much more maintainable. just modify the function lcd_goto_xy() to accept the LCD size as parameters.

lcd_goto_xy(int LCD_COLS, int LCD_ROWS, int x, int y)

now then, if this change causes you lots of other grief, consider having lcd_goto_xy() call a more specific function in turn (be careful of the stack depth limit in CCS however). e.g.

Code:

#define LCD_COLS 20
#define LCD_ROWS 2

// mainline code
lcd_goto_xy(1,2);

// function defs
lcd_goto_xy(int x, int y) {
  lcd_goto_xy_generic(LCD_COLS, LCD_ROWS, x, y)
}

lcd_goto_xy_generic(int cols, int rows, int x, int y) {
// code that auto-calcs and auto-bounds cursor postioning
// based on the physical geometry of the display
}


using the above example, you just need to change the #defines when you change the display, for old code and new alike.

jds-pic
Ed Arnold



Joined: 02 Nov 2003
Posts: 18

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

preprocessor inside a function
PostPosted: Sun Feb 22, 2004 11:11 pm     Reply with quote

guest

Thanks for your quick response and your ideas are very helpful. Lookng at your first example, I thought preprocessor directives were only allowed outside of functions. I may be mistaken and this approach might be the right one and also your second example also has merit. Thanks again. Please look at my re-write I did prior to your post and post what you think.

Ed Arnold Smile
Ed Arnold



Joined: 02 Nov 2003
Posts: 18

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

I made a mistake, imagine that
PostPosted: Sun Feb 22, 2004 11:42 pm     Reply with quote

I made a mistake on the code posted earlier. Once I figured it out, it now compiles and I hope it runs. Here is the code again.

Code:
#if LINE_SIZE == 16
  #define lcd_col_offset 0
#elif LINE_SIZE == 20
  #define lcd_col_offset 4
#elif LINE_SIZE == 24
  #define lcd_col_offset 8 
#endif

void lcd_goto_xy(byte x, byte y)
{
 byte addr;
   
  if(y!=1)addr=lcd_row_offset;
    else addr=0;
  if(!lcd_col_offset)addr+=x-1;  // 16 char display--no offset
    else addr+=(x+lcd_col_offset)-1;  // >16 char display--use offset
  lcd_send_byte(0,0x80|addr);
}


Thanks again

Ed Arnold
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