1269 |
kakl |
1 |
/* --------------------------------------------------------------------------- |
|
|
2 |
* AVR_MLIB - HD 44780 LCD Display Driver |
|
|
3 |
* www.mlab.cz miho 2008 |
|
|
4 |
* --------------------------------------------------------------------------- |
|
|
5 |
* LCD display driver for standard Hitachi 1/2/4 line character LCD modules |
|
|
6 |
* for AVR processors. It uses 4 or 8 bit interface without readback. |
|
|
7 |
* In the Examples section there is a demo application for this library. |
|
|
8 |
* --------------------------------------------------------------------------- |
|
|
9 |
* 00.00 2008/03/28 First Version |
|
|
10 |
* --------------------------------------------------------------------------- |
|
|
11 |
*/ |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
// What should be set and done before here |
|
|
15 |
// --------------------------------------- |
|
|
16 |
// |
|
|
17 |
// #include <stdio.h> // If you want to use printf, ... |
|
|
18 |
// |
|
|
19 |
// #define LCD_DATA B // 4 or 8 bits field (lsb bit of the port) |
|
|
20 |
// #define LCD_DATA_BIT 4 |
|
|
21 |
// |
|
|
22 |
// #define LCD_RS D // Register Select (port and bit) |
|
|
23 |
// #define LCD_RS_BIT 4 |
|
|
24 |
// |
|
|
25 |
// #define LCD_E D // Enable (port and bit) |
|
|
26 |
// #define LCD_E_BIT 3 |
|
|
27 |
// |
|
|
28 |
// |
|
|
29 |
// // LCD Display Parameters |
|
|
30 |
// #define LCD_INTERFACE_BITS 4 // 4 or 8 bit interface |
|
|
31 |
// #define LCD_LINES 1 // 1 or 2 or 4 lines |
|
|
32 |
// #define LCD_CHARS 20 // usualy 16 or 20, important for 4 line display only |
|
|
33 |
// |
|
|
34 |
// #include "lcd_hd44780.h" // Use LCD Library |
|
|
35 |
// |
|
|
36 |
// |
|
|
37 |
// How to use the library |
|
|
38 |
// ---------------------- |
|
|
39 |
// |
|
|
40 |
// void lcd_init(void) // Init LCD Display |
|
|
41 |
// |
|
|
42 |
// void lcd_home() // Goto Home |
|
|
43 |
// |
|
|
44 |
// void lcd_clear() // Clear Display |
|
|
45 |
// |
|
|
46 |
// void lcd_clear_home() // Clear Display and Goto Home with no Cursor |
|
|
47 |
// |
|
|
48 |
// void lcd_cursor_on() // Switch Cursor On |
|
|
49 |
// |
|
|
50 |
// void lcd_cursor_off() // Switch Cursor Off |
|
|
51 |
// |
|
|
52 |
// void lcd_cursor_left() // Move Cursor Left |
|
|
53 |
// |
|
|
54 |
// void lcd_cursor_right() // Move Cursor Right |
|
|
55 |
// |
|
|
56 |
// void lcd_gotoxy(uint8_t x, uint8_t y) // Move to Position (1,1 is the first position) |
|
|
57 |
// |
|
|
58 |
// int lcd_putc(char c) // LCD Char Output |
|
|
59 |
// |
|
|
60 |
// int lcd_putc_stream(char c, FILE *unused) // LCD Char Output (for Stream Library) |
|
|
61 |
// |
|
|
62 |
// |
|
|
63 |
// How to use printf |
|
|
64 |
// ----------------- |
|
|
65 |
// |
|
|
66 |
// 1) Define FILE structure |
|
|
67 |
// |
|
|
68 |
// static FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putc_stream, NULL, _FDEV_SETUP_WRITE); |
|
|
69 |
// |
|
|
70 |
// 2) Connect it with standard output |
|
|
71 |
// |
|
|
72 |
// stdout = &lcd_stream; // Connect stdout to LCD Stream |
|
|
73 |
// |
|
|
74 |
// 3) Use printf |
|
|
75 |
// |
|
|
76 |
// printf("\fHello World!\n------------"); |
|
|
77 |
// |
|
|
78 |
// 4) Use special chars |
|
|
79 |
// |
|
|
80 |
// \f - clear display and goto home |
|
|
81 |
// \n - goto the beginning of the next line |
|
|
82 |
// \r - goto to the beginning of curent line |
|
|
83 |
// \b - backspace |
|
|
84 |
// \v - start and end definition of user defined char |
|
|
85 |
// |
|
|
86 |
// |
|
|
87 |
// How to use User Defined symbols |
|
|
88 |
// ------------------------------- |
|
|
89 |
// |
|
|
90 |
// That is easy. Just print the definition to lcd. Look at the example |
|
|
91 |
// |
|
|
92 |
// printf("\v" "\x10" LCD_CHAR_BAT50 "\v"); // definition (redefines CGRAM content of the LCD) |
|
|
93 |
// printf("Battery Status \x10"); // usage |
|
|
94 |
// |
|
|
95 |
// \v starts the definition |
|
|
96 |
// \x10 first (of eight) user defined char |
|
|
97 |
// LCD_CHAR_BAT50 half battery symbol, you can define more symbols here (up to 8) |
|
|
98 |
// \v end of definition |
|
|
99 |
// |
|
|
100 |
|
|
|
101 |
#include "global.h" |
|
|
102 |
#include <avr/io.h> // Device Specific Defines |
|
|
103 |
#include <stdio.h> |
|
|
104 |
#include <util/delay.h> // Delay Routines |
|
|
105 |
|
|
|
106 |
// IO Pins MACROS |
|
|
107 |
#define GLUE(a,b) a##b |
|
|
108 |
#define PORT(a) GLUE(PORT,a) |
|
|
109 |
#define PIN(a) GLUE(PIN,a) |
|
|
110 |
#define DDR(a) GLUE(DDR,a) |
|
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
// LCD Port Settings |
|
|
115 |
#define LCD_DATA A // 4 or 8 bits field (lsb port) |
|
|
116 |
#define LCD_DATA_BIT 4 |
|
|
117 |
|
|
|
118 |
#define LCD_RS C // Register Select |
|
|
119 |
#define LCD_RS_BIT 2 |
|
|
120 |
|
|
|
121 |
#define LCD_E C // Enable |
|
|
122 |
#define LCD_E_BIT 4 |
|
|
123 |
|
|
|
124 |
// LCD Display Parameters |
|
|
125 |
#define LCD_INTERFACE_BITS 4 // 4 or 8 bit interface |
|
|
126 |
#define LCD_LINES 2 // 1 or 2 or 4 lines |
|
|
127 |
#define LCD_CHARS 18 // usualy 16 or 20, important for 4 line display only |
|
|
128 |
|
|
|
129 |
// Goto Home |
|
|
130 |
void lcd_home(void); |
|
|
131 |
|
|
|
132 |
// Clear Display |
|
|
133 |
void lcd_clear(void); |
|
|
134 |
|
|
|
135 |
// Switch Cursor On |
|
|
136 |
void lcd_cursor_on(void); |
|
|
137 |
|
|
|
138 |
// Switch Cursor Off |
|
|
139 |
void lcd_cursor_off(void); |
|
|
140 |
|
|
|
141 |
// Clear Display and Goto Home with no Cursor |
|
|
142 |
void lcd_clear_home(void); |
|
|
143 |
|
|
|
144 |
// Move to Position (1,1 is the first position) |
|
|
145 |
void lcd_gotoxy(uint8_t x, uint8_t y); |
|
|
146 |
|
|
|
147 |
// Increment Position |
|
|
148 |
void lcd_inc_pos(void); |
|
|
149 |
|
|
|
150 |
// Decrement Position |
|
|
151 |
void lcd_dec_pos(void); |
|
|
152 |
|
|
|
153 |
// Move Cursor Left |
|
|
154 |
void lcd_cursor_left(void); |
|
|
155 |
|
|
|
156 |
// Move Cursor Right |
|
|
157 |
void lcd_cursor_right(void); |
|
|
158 |
|
|
|
159 |
// Init LCD Display |
|
|
160 |
void lcd_init(void); |
|
|
161 |
|
|
|
162 |
// LCD Char Output |
|
|
163 |
int lcd_putc(char c); |
|
|
164 |
|