|
|
View previous topic :: View next topic |
Author |
Message |
Ed Arnold
Joined: 02 Nov 2003 Posts: 18
|
use preproccer #if to set column width on lcd |
Posted: Sun Feb 22, 2004 9:36 pm |
|
|
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
|
|
|
jds-pic2 Guest
|
Re: use preproccer #if to set column width on lcd |
Posted: Sun Feb 22, 2004 9:55 pm |
|
|
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
|
to clearify myself |
Posted: Sun Feb 22, 2004 10:18 pm |
|
|
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
|
Maybe this will work instead |
Posted: Sun Feb 22, 2004 10:58 pm |
|
|
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 |
Posted: Sun Feb 22, 2004 10:59 pm |
|
|
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 (!)...
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
|
preprocessor inside a function |
Posted: Sun Feb 22, 2004 11:11 pm |
|
|
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 |
|
|
Ed Arnold
Joined: 02 Nov 2003 Posts: 18
|
I made a mistake, imagine that |
Posted: Sun Feb 22, 2004 11:42 pm |
|
|
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 |
|
|
|
|
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
|