Hello!
You should avoid having implementation in a header file. This should be in lcd.c.
lcd.h should contain only the functions' prototypes.
Excellent. I've never thought before to make header file by my own.
You should get used to it. Here is a quick tutorial: for example write a function
that calculates a sum.
The h file (to be saved for instance in "adder.h"):
Code:
#ifndef _ADDER_H_
#define _ADDER_H_
uint32 add(uint32 x, uint32 y);
#endif
The .c file (to be saved for instance in "adder.c") :
Code:
#include "adder.h"
uint32 add(uint32 x, uint32 y) {
uint32 result = x+y;
return resukt;
}
Note that if you put everything in the h file, it will work.
But there is a big inconvenience to this way of proceeding:
Suppose that your main.c calls functions from lcd.h. If you modify main.c, then the
compiler will also recompile what is implemented in lcd.h. Therefore, if your program
gets large, you will recompile everything everytime, and this can really take time.
The program I'm working on right now includes about 50 modules (maybe 100)...
It takes about 3 minutes for a full compile. So if I have to wait 3 minutes at every
tiny modification, my productivity would really drop.
By separating into modules (lcd.c / lcd.h), the compiler will never recompile lcd.c
if you don't modify it.
Your LCD.h file should look like this:
Code:
#ifndef _LCD_H_
#define _LCD_H_
void delay_ms(unsigned int i);
void cmd_lcd(unsigned char c);
void init_lcd(void);
void write_lcd(unsigned char c);
void display_lcd(unsigned char *s);
void integer_lcd(int n);
#endif
Now some comments about this header:
1. You should choose function names wisely.
If you read your program next year, do you think you will understand the difference
between display_lcd and write_lcd? Why not naming them for instance:
display_string(char * s);
display_char(char c);
display_int(int n);
2. You should use #define statements. Well, this LCD is simple and its initialization is
pretty standard. But one of these days if you use a graphic LCD, you will have to tune
dozens of parameters, and it would be a good idea to get used to this.
Example :
#define MODE_4_5x7_16x2 0x28
#define LCD_CURSOR_ON 0x0C
#define CURSOR_AUTO_SHIFT 0x06
#define CLEAR_LCD 0x01
then in your code:
cmd_lcd(MODE_4_5x7_16x2);
cmd_lcd(LCD_CURSOR_ON);
cmd_lcd(CURSOR_AUTO_SHIFT);
cmd_lcd(CLEAR_LCD);
And you don't even need to write comments, the functionality of these commands is
self-explanatory.
Dora.