Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Microchip C18 : Function Declaration

Status
Not open for further replies.
Hi,

Firstly, hope the thread title is not misleading (it probably is) but not sure how to title this question. I'm positioning a cursor on an LCD screen. Works ok, done it loads of times in previous programs. But trying to make the code a little 'neater / tighter'. This is what I have been doing thus far...

Code:
void LCD_data_init (void)
{
    clrscrn();
    
    csrpos = 0x8F;
    putcsr();
Code:
void putcsr (void)
{
    data2LCD = csrpos;        //Line x Coloumn x
    write2LCD();
}
I might be mistaken but I think I've seen something like this somewhere that would be neater...

Code:
    putcsr(0x8f);
Code:
void putcsr (char)
{
    data2LCD = putcsr;        //Line x Coloumn x
    write2LCD();
}
What's wrong with the second two code snippets? Any help greatly appreciated.

Thanks in advance.
 
Change the final code that you show to the following:

Code:
void putcsr (char pos)
{
   data2LCD = pos;
   write2LCD();
}

At the top of your c file (or in a header file included in any c file from which you call the function putcsr) add the following line:
Code:
void putcsr (char pos);

Also, consider what putcsr(0x8f) looks like. I can guess that you're putting the cursor somewhere, but I don't know where it is going. Will you know where it is going a year from now or will you have to look at the display datasheet? Consider using a #define

Code:
#define TOP_LEFT_CORNER  0x8f
void putcsr (char pos);

void LCD_data_init (void)
{
    clrscrn();
    putcsr( TOP_LEFT_CORNER );
}

void putcsr (char pos)
{
   data2LCD = pos;
   write2LCD();
}

or even better, do something like the following (I have NO idea if this is how the display works or not)
Code:
#define ROW_1  0x80
#define ROW_2 0x40
#define ROW_3 0x20
#define ROW_4 0x10
#define COL_1 0x00
#define COL_2 0x01
#define COL_3 0x02
.
.
.
#define COL_16 0x0F

void LCD_data_init (void)
{
    clrscrn();
    putcsr( ROW_1 | COL_16 );
}
 
It would be better to rewrite write2LCD to take a value,
Code:
void write2LCD(char dat){
   .....
}

    write2LCD(0x8f);
Using global variables to pass parameters is very wasteful and can easily lead to bugs.

Mike.
 
Using global variables to pass parameters is very wasteful and can easily lead to bugs.
Mike.

I agree entirely.. but write2LCD() might be at the lowest level and stack could be an issue! ( Yes!! I know in this case it won't, but caution needed, I don't know the ram size)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top