?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 6

Line No. Rev Author Line
1 6 kaklik <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3 <title>Procyon AVRlib: lcd.c Source File</title>
4 <link href="dox.css" rel="stylesheet" type="text/css">
5 </head><body>
6 <!-- Generated by Doxygen 1.4.2 -->
7 <div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
8 <h1>lcd.c</h1><a href="lcd_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file lcd.c \brief Character LCD driver for HD44780/SED1278 displays. */</span>
9 00002 <span class="comment">//*****************************************************************************</span>
10 00003 <span class="comment">//</span>
11 00004 <span class="comment">// File Name : 'lcd.c'</span>
12 00005 <span class="comment">// Title : Character LCD driver for HD44780/SED1278 displays</span>
13 00006 <span class="comment">// (usable in mem-mapped, or I/O mode)</span>
14 00007 <span class="comment">// Author : Pascal Stang</span>
15 00008 <span class="comment">// Created : 11/22/2000</span>
16 00009 <span class="comment">// Revised : 4/30/2002</span>
17 00010 <span class="comment">// Version : 1.1</span>
18 00011 <span class="comment">// Target MCU : Atmel AVR series</span>
19 00012 <span class="comment">// Editor Tabs : 4</span>
20 00013 <span class="comment">//</span>
21 00014 <span class="comment">// This code is distributed under the GNU Public License</span>
22 00015 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>
23 00016 <span class="comment">//</span>
24 00017 <span class="comment">//*****************************************************************************</span>
25 00018
26 00019 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>
27 00020 <span class="preprocessor">#include &lt;avr/pgmspace.h&gt;</span>
28 00021
29 00022 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
30 00023 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>
31 00024
32 00025 <span class="preprocessor">#include "<a class="code" href="lcd_8h.html">lcd.h</a>"</span>
33 00026
34 00027 <span class="comment">// custom LCD characters</span>
35 00028 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> __attribute__ ((progmem)) LcdCustomChar[] =
36 00029 {
37 00030 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, <span class="comment">// 0. 0/5 full progress block</span>
38 00031 0x00, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x1F, 0x00, <span class="comment">// 1. 1/5 full progress block</span>
39 00032 0x00, 0x1F, 0x18, 0x18, 0x18, 0x18, 0x1F, 0x00, <span class="comment">// 2. 2/5 full progress block</span>
40 00033 0x00, 0x1F, 0x1C, 0x1C, 0x1C, 0x1C, 0x1F, 0x00, <span class="comment">// 3. 3/5 full progress block</span>
41 00034 0x00, 0x1F, 0x1E, 0x1E, 0x1E, 0x1E, 0x1F, 0x00, <span class="comment">// 4. 4/5 full progress block</span>
42 00035 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, <span class="comment">// 5. 5/5 full progress block</span>
43 00036 0x03, 0x07, 0x0F, 0x1F, 0x0F, 0x07, 0x03, 0x00, <span class="comment">// 6. rewind arrow</span>
44 00037 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, <span class="comment">// 7. stop block</span>
45 00038 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x00, <span class="comment">// 8. pause bars</span>
46 00039 0x18, 0x1C, 0x1E, 0x1F, 0x1E, 0x1C, 0x18, 0x00, <span class="comment">// 9. fast-forward arrow</span>
47 00040 0x00, 0x04, 0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x00, <span class="comment">// 10. scroll up arrow</span>
48 00041 0x00, 0x1F, 0x1F, 0x0E, 0x0E, 0x04, 0x04, 0x00, <span class="comment">// 11. scroll down arrow</span>
49 00042 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, <span class="comment">// 12. blank character</span>
50 00043 0x00, 0x0E, 0x19, 0x15, 0x13, 0x0E, 0x00, 0x00, <span class="comment">// 13. animated play icon frame 0</span>
51 00044 0x00, 0x0E, 0x15, 0x15, 0x15, 0x0E, 0x00, 0x00, <span class="comment">// 14. animated play icon frame 1</span>
52 00045 0x00, 0x0E, 0x13, 0x15, 0x19, 0x0E, 0x00, 0x00, <span class="comment">// 15. animated play icon frame 2</span>
53 00046 0x00, 0x0E, 0x11, 0x1F, 0x11, 0x0E, 0x00, 0x00, <span class="comment">// 16. animated play icon frame 3</span>
54 00047 };
55 00048
56 00049 <span class="comment">/*************************************************************/</span>
57 00050 <span class="comment">/********************** LOCAL FUNCTIONS **********************/</span>
58 00051 <span class="comment">/*************************************************************/</span>
59 00052
60 00053 <span class="keywordtype">void</span> lcdInitHW(<span class="keywordtype">void</span>)
61 00054 {
62 00055 <span class="comment">// initialize I/O ports</span>
63 00056 <span class="comment">// if I/O interface is in use</span>
64 00057 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>
65 00058 <span class="preprocessor"></span> <span class="comment">// initialize LCD control lines</span>
66 00059 cbi(LCD_CTRL_PORT, LCD_CTRL_RS);
67 00060 cbi(LCD_CTRL_PORT, LCD_CTRL_RW);
68 00061 cbi(LCD_CTRL_PORT, LCD_CTRL_E);
69 00062 <span class="comment">// initialize LCD control lines to output</span>
70 00063 sbi(LCD_CTRL_DDR, LCD_CTRL_RS);
71 00064 sbi(LCD_CTRL_DDR, LCD_CTRL_RW);
72 00065 sbi(LCD_CTRL_DDR, LCD_CTRL_E);
73 00066 <span class="comment">// initialize LCD data port to input</span>
74 00067 <span class="comment">// initialize LCD data lines to pull-up</span>
75 00068 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
76 00069 <span class="preprocessor"></span> outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&amp;0x0F); <span class="comment">// set data I/O lines to input (4bit)</span>
77 00070 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); <span class="comment">// set pull-ups to on (4bit)</span>
78 00071 <span class="preprocessor"> #else</span>
79 00072 <span class="preprocessor"></span> outb(LCD_DATA_DDR, 0x00); <span class="comment">// set data I/O lines to input (8bit)</span>
80 00073 outb(LCD_DATA_POUT, 0xFF); <span class="comment">// set pull-ups to on (8bit)</span>
81 00074 <span class="preprocessor"> #endif</span>
82 00075 <span class="preprocessor"></span><span class="preprocessor">#else</span>
83 00076 <span class="preprocessor"></span> <span class="comment">// enable external memory bus if not already enabled</span>
84 00077 sbi(MCUCR, SRE); <span class="comment">// enable bus interface</span>
85 00078 <span class="preprocessor">#endif</span>
86 00079 <span class="preprocessor"></span>}
87 00080
88 00081 <span class="keywordtype">void</span> lcdBusyWait(<span class="keywordtype">void</span>)
89 00082 {
90 00083 <span class="comment">// wait until LCD busy bit goes to zero</span>
91 00084 <span class="comment">// do a read from control register</span>
92 00085 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>
93 00086 <span class="preprocessor"></span> cbi(LCD_CTRL_PORT, LCD_CTRL_RS); <span class="comment">// set RS to "control"</span>
94 00087 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
95 00088 <span class="preprocessor"></span> outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&amp;0x0F); <span class="comment">// set data I/O lines to input (4bit)</span>
96 00089 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); <span class="comment">// set pull-ups to on (4bit)</span>
97 00090 <span class="preprocessor"> #else</span>
98 00091 <span class="preprocessor"></span> outb(LCD_DATA_DDR, 0x00); <span class="comment">// set data I/O lines to input (8bit)</span>
99 00092 outb(LCD_DATA_POUT, 0xFF); <span class="comment">// set pull-ups to on (8bit)</span>
100 00093 <span class="preprocessor"> #endif</span>
101 00094 <span class="preprocessor"></span> sbi(LCD_CTRL_PORT, LCD_CTRL_RW); <span class="comment">// set R/W to "read"</span>
102 00095 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
103 00096 LCD_DELAY; <span class="comment">// wait</span>
104 00097 <span class="keywordflow">while</span>(inb(LCD_DATA_PIN) &amp; 1&lt;&lt;LCD_BUSY)
105 00098 {
106 00099 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
107 00100 LCD_DELAY; <span class="comment">// wait</span>
108 00101 LCD_DELAY; <span class="comment">// wait</span>
109 00102 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
110 00103 LCD_DELAY; <span class="comment">// wait</span>
111 00104 LCD_DELAY; <span class="comment">// wait</span>
112 00105 <span class="preprocessor"> #ifdef LCD_DATA_4BIT // do an extra clock for 4 bit reads</span>
113 00106 <span class="preprocessor"></span> cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
114 00107 LCD_DELAY; <span class="comment">// wait</span>
115 00108 LCD_DELAY; <span class="comment">// wait</span>
116 00109 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
117 00110 LCD_DELAY; <span class="comment">// wait</span>
118 00111 LCD_DELAY; <span class="comment">// wait</span>
119 00112 <span class="preprocessor"> #endif</span>
120 00113 <span class="preprocessor"></span> }
121 00114 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
122 00115 <span class="comment">// leave data lines in input mode so they can be most easily used for other purposes</span>
123 00116 <span class="preprocessor">#else</span>
124 00117 <span class="preprocessor"></span> <span class="comment">// memory bus read</span>
125 00118 <span class="comment">// sbi(MCUCR, SRW); // enable RAM waitstate</span>
126 00119 <span class="comment">// wait until LCD busy bit goes to zero</span>
127 00120 <span class="keywordflow">while</span>( (*((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *) (LCD_CTRL_ADDR))) &amp; (1&lt;&lt;LCD_BUSY) );
128 00121 <span class="comment">// cbi(MCUCR, SRW); // disable RAM waitstate</span>
129 00122 <span class="preprocessor">#endif</span>
130 00123 <span class="preprocessor"></span>}
131 00124
132 00125 <span class="keywordtype">void</span> lcdControlWrite(u08 data)
133 00126 {
134 00127 <span class="comment">// write the control byte to the display controller</span>
135 00128 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>
136 00129 <span class="preprocessor"></span> lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>
137 00130 cbi(LCD_CTRL_PORT, LCD_CTRL_RS); <span class="comment">// set RS to "control"</span>
138 00131 cbi(LCD_CTRL_PORT, LCD_CTRL_RW); <span class="comment">// set R/W to "write"</span>
139 00132 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
140 00133 <span class="preprocessor"></span> <span class="comment">// 4 bit write</span>
141 00134 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
142 00135 outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)|0xF0); <span class="comment">// set data I/O lines to output (4bit)</span>
143 00136 outb(LCD_DATA_POUT, (inb(LCD_DATA_POUT)&amp;0x0F) | (data&amp;0xF0) ); <span class="comment">// output data, high 4 bits</span>
144 00137 LCD_DELAY; <span class="comment">// wait</span>
145 00138 LCD_DELAY; <span class="comment">// wait</span>
146 00139 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
147 00140 LCD_DELAY; <span class="comment">// wait</span>
148 00141 LCD_DELAY; <span class="comment">// wait</span>
149 00142 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
150 00143 outb(LCD_DATA_POUT, (inb(LCD_DATA_POUT)&amp;0x0F) | (data&lt;&lt;4) ); <span class="comment">// output data, low 4 bits</span>
151 00144 LCD_DELAY; <span class="comment">// wait</span>
152 00145 LCD_DELAY; <span class="comment">// wait</span>
153 00146 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
154 00147 <span class="preprocessor"> #else</span>
155 00148 <span class="preprocessor"></span> <span class="comment">// 8 bit write</span>
156 00149 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
157 00150 outb(LCD_DATA_DDR, 0xFF); <span class="comment">// set data I/O lines to output (8bit)</span>
158 00151 outb(LCD_DATA_POUT, data); <span class="comment">// output data, 8bits</span>
159 00152 LCD_DELAY; <span class="comment">// wait</span>
160 00153 LCD_DELAY; <span class="comment">// wait</span>
161 00154 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
162 00155 <span class="preprocessor"> #endif</span>
163 00156 <span class="preprocessor"></span> <span class="comment">// leave data lines in input mode so they can be most easily used for other purposes</span>
164 00157 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
165 00158 <span class="preprocessor"></span> outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&amp;0x0F); <span class="comment">// set data I/O lines to input (4bit)</span>
166 00159 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); <span class="comment">// set pull-ups to on (4bit)</span>
167 00160 <span class="preprocessor"> #else</span>
168 00161 <span class="preprocessor"></span> outb(LCD_DATA_DDR, 0x00); <span class="comment">// set data I/O lines to input (8bit)</span>
169 00162 outb(LCD_DATA_POUT, 0xFF); <span class="comment">// set pull-ups to on (8bit)</span>
170 00163 <span class="preprocessor"> #endif</span>
171 00164 <span class="preprocessor"></span><span class="preprocessor">#else</span>
172 00165 <span class="preprocessor"></span> <span class="comment">// memory bus write</span>
173 00166 <span class="comment">//sbi(MCUCR, SRW); // enable RAM waitstate</span>
174 00167 lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>
175 00168 *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *) (LCD_CTRL_ADDR)) = data;
176 00169 <span class="comment">//cbi(MCUCR, SRW); // disable RAM waitstate</span>
177 00170 <span class="preprocessor">#endif</span>
178 00171 <span class="preprocessor"></span>}
179 00172
180 00173 u08 lcdControlRead(<span class="keywordtype">void</span>)
181 00174 {
182 00175 <span class="comment">// read the control byte from the display controller</span>
183 00176 <span class="keyword">register</span> u08 data;
184 00177 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>
185 00178 <span class="preprocessor"></span> lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>
186 00179 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
187 00180 <span class="preprocessor"></span> outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&amp;0x0F); <span class="comment">// set data I/O lines to input (4bit)</span>
188 00181 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); <span class="comment">// set pull-ups to on (4bit)</span>
189 00182 <span class="preprocessor"> #else</span>
190 00183 <span class="preprocessor"></span> outb(LCD_DATA_DDR, 0x00); <span class="comment">// set data I/O lines to input (8bit)</span>
191 00184 outb(LCD_DATA_POUT, 0xFF); <span class="comment">// set pull-ups to on (8bit)</span>
192 00185 <span class="preprocessor"> #endif</span>
193 00186 <span class="preprocessor"></span> cbi(LCD_CTRL_PORT, LCD_CTRL_RS); <span class="comment">// set RS to "control"</span>
194 00187 sbi(LCD_CTRL_PORT, LCD_CTRL_RW); <span class="comment">// set R/W to "read"</span>
195 00188 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
196 00189 <span class="preprocessor"></span> <span class="comment">// 4 bit read</span>
197 00190 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
198 00191 LCD_DELAY; <span class="comment">// wait</span>
199 00192 LCD_DELAY; <span class="comment">// wait</span>
200 00193 data = inb(LCD_DATA_PIN)&amp;0xF0; <span class="comment">// input data, high 4 bits</span>
201 00194 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
202 00195 LCD_DELAY; <span class="comment">// wait</span>
203 00196 LCD_DELAY; <span class="comment">// wait</span>
204 00197 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
205 00198 LCD_DELAY; <span class="comment">// wait</span>
206 00199 LCD_DELAY; <span class="comment">// wait</span>
207 00200 data |= inb(LCD_DATA_PIN)&gt;&gt;4; <span class="comment">// input data, low 4 bits</span>
208 00201 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
209 00202 <span class="preprocessor"> #else</span>
210 00203 <span class="preprocessor"></span> <span class="comment">// 8 bit read</span>
211 00204 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
212 00205 LCD_DELAY; <span class="comment">// wait</span>
213 00206 LCD_DELAY; <span class="comment">// wait</span>
214 00207 data = inb(LCD_DATA_PIN); <span class="comment">// input data, 8bits</span>
215 00208 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
216 00209 <span class="preprocessor"> #endif</span>
217 00210 <span class="preprocessor"></span> <span class="comment">// leave data lines in input mode so they can be most easily used for other purposes</span>
218 00211 <span class="preprocessor">#else</span>
219 00212 <span class="preprocessor"></span> <span class="comment">//sbi(MCUCR, SRW); // enable RAM waitstate</span>
220 00213 lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>
221 00214 data = *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *) (LCD_CTRL_ADDR));
222 00215 <span class="comment">//cbi(MCUCR, SRW); // disable RAM waitstate</span>
223 00216 <span class="preprocessor">#endif</span>
224 00217 <span class="preprocessor"></span> <span class="keywordflow">return</span> data;
225 00218 }
226 00219
227 00220 <span class="keywordtype">void</span> lcdDataWrite(u08 data)
228 00221 {
229 00222 <span class="comment">// write a data byte to the display</span>
230 00223 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>
231 00224 <span class="preprocessor"></span> lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>
232 00225 sbi(LCD_CTRL_PORT, LCD_CTRL_RS); <span class="comment">// set RS to "data"</span>
233 00226 cbi(LCD_CTRL_PORT, LCD_CTRL_RW); <span class="comment">// set R/W to "write"</span>
234 00227 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
235 00228 <span class="preprocessor"></span> <span class="comment">// 4 bit write</span>
236 00229 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
237 00230 outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)|0xF0); <span class="comment">// set data I/O lines to output (4bit)</span>
238 00231 outb(LCD_DATA_POUT, (inb(LCD_DATA_POUT)&amp;0x0F) | (data&amp;0xF0) ); <span class="comment">// output data, high 4 bits</span>
239 00232 LCD_DELAY; <span class="comment">// wait</span>
240 00233 LCD_DELAY; <span class="comment">// wait</span>
241 00234 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
242 00235 LCD_DELAY; <span class="comment">// wait</span>
243 00236 LCD_DELAY; <span class="comment">// wait</span>
244 00237 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
245 00238 outb(LCD_DATA_POUT, (inb(LCD_DATA_POUT)&amp;0x0F) | (data&lt;&lt;4) ); <span class="comment">// output data, low 4 bits</span>
246 00239 LCD_DELAY; <span class="comment">// wait</span>
247 00240 LCD_DELAY; <span class="comment">// wait</span>
248 00241 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
249 00242 <span class="preprocessor"> #else</span>
250 00243 <span class="preprocessor"></span> <span class="comment">// 8 bit write</span>
251 00244 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
252 00245 outb(LCD_DATA_DDR, 0xFF); <span class="comment">// set data I/O lines to output (8bit)</span>
253 00246 outb(LCD_DATA_POUT, data); <span class="comment">// output data, 8bits</span>
254 00247 LCD_DELAY; <span class="comment">// wait</span>
255 00248 LCD_DELAY; <span class="comment">// wait</span>
256 00249 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
257 00250 <span class="preprocessor"> #endif</span>
258 00251 <span class="preprocessor"></span> <span class="comment">// leave data lines in input mode so they can be most easily used for other purposes</span>
259 00252 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
260 00253 <span class="preprocessor"></span> outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&amp;0x0F); <span class="comment">// set data I/O lines to input (4bit)</span>
261 00254 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); <span class="comment">// set pull-ups to on (4bit)</span>
262 00255 <span class="preprocessor"> #else</span>
263 00256 <span class="preprocessor"></span> outb(LCD_DATA_DDR, 0x00); <span class="comment">// set data I/O lines to input (8bit)</span>
264 00257 outb(LCD_DATA_POUT, 0xFF); <span class="comment">// set pull-ups to on (8bit)</span>
265 00258 <span class="preprocessor"> #endif</span>
266 00259 <span class="preprocessor"></span><span class="preprocessor">#else</span>
267 00260 <span class="preprocessor"></span> <span class="comment">// memory bus write</span>
268 00261 <span class="comment">//sbi(MCUCR, SRW); // enable RAM waitstate</span>
269 00262 lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>
270 00263 *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *) (LCD_DATA_ADDR)) = data;
271 00264 <span class="comment">//cbi(MCUCR, SRW); // disable RAM waitstate</span>
272 00265 <span class="preprocessor">#endif</span>
273 00266 <span class="preprocessor"></span>}
274 00267
275 00268 u08 lcdDataRead(<span class="keywordtype">void</span>)
276 00269 {
277 00270 <span class="comment">// read a data byte from the display</span>
278 00271 <span class="keyword">register</span> u08 data;
279 00272 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>
280 00273 <span class="preprocessor"></span> lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>
281 00274 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
282 00275 <span class="preprocessor"></span> outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&amp;0x0F); <span class="comment">// set data I/O lines to input (4bit)</span>
283 00276 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); <span class="comment">// set pull-ups to on (4bit)</span>
284 00277 <span class="preprocessor"> #else</span>
285 00278 <span class="preprocessor"></span> outb(LCD_DATA_DDR, 0x00); <span class="comment">// set data I/O lines to input (8bit)</span>
286 00279 outb(LCD_DATA_POUT, 0xFF); <span class="comment">// set pull-ups to on (8bit)</span>
287 00280 <span class="preprocessor"> #endif</span>
288 00281 <span class="preprocessor"></span> sbi(LCD_CTRL_PORT, LCD_CTRL_RS); <span class="comment">// set RS to "data"</span>
289 00282 sbi(LCD_CTRL_PORT, LCD_CTRL_RW); <span class="comment">// set R/W to "read"</span>
290 00283 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>
291 00284 <span class="preprocessor"></span> <span class="comment">// 4 bit read</span>
292 00285 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
293 00286 LCD_DELAY; <span class="comment">// wait</span>
294 00287 LCD_DELAY; <span class="comment">// wait</span>
295 00288 data = inb(LCD_DATA_PIN)&amp;0xF0; <span class="comment">// input data, high 4 bits</span>
296 00289 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
297 00290 LCD_DELAY; <span class="comment">// wait</span>
298 00291 LCD_DELAY; <span class="comment">// wait</span>
299 00292 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
300 00293 LCD_DELAY; <span class="comment">// wait</span>
301 00294 LCD_DELAY; <span class="comment">// wait</span>
302 00295 data |= inb(LCD_DATA_PIN)&gt;&gt;4; <span class="comment">// input data, low 4 bits</span>
303 00296 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
304 00297 <span class="preprocessor"> #else</span>
305 00298 <span class="preprocessor"></span> <span class="comment">// 8 bit read</span>
306 00299 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>
307 00300 LCD_DELAY; <span class="comment">// wait</span>
308 00301 LCD_DELAY; <span class="comment">// wait</span>
309 00302 data = inb(LCD_DATA_PIN); <span class="comment">// input data, 8bits</span>
310 00303 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
311 00304 <span class="preprocessor"> #endif</span>
312 00305 <span class="preprocessor"></span> <span class="comment">// leave data lines in input mode so they can be most easily used for other purposes</span>
313 00306 <span class="preprocessor">#else</span>
314 00307 <span class="preprocessor"></span> <span class="comment">// memory bus read</span>
315 00308 <span class="comment">//sbi(MCUCR, SRW); // enable RAM waitstate</span>
316 00309 lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>
317 00310 data = *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *) (LCD_DATA_ADDR));
318 00311 <span class="comment">//cbi(MCUCR, SRW); // disable RAM waitstate</span>
319 00312 <span class="preprocessor">#endif</span>
320 00313 <span class="preprocessor"></span> <span class="keywordflow">return</span> data;
321 00314 }
322 00315
323 00316
324 00317
325 00318 <span class="comment">/*************************************************************/</span>
326 00319 <span class="comment">/********************* PUBLIC FUNCTIONS **********************/</span>
327 00320 <span class="comment">/*************************************************************/</span>
328 00321
329 00322 <span class="keywordtype">void</span> lcdInit()
330 00323 {
331 00324 <span class="comment">// initialize hardware</span>
332 00325 lcdInitHW();
333 00326 <span class="comment">// LCD function set</span>
334 00327 lcdControlWrite(LCD_FUNCTION_DEFAULT);
335 00328 <span class="comment">// clear LCD</span>
336 00329 lcdControlWrite(1&lt;&lt;LCD_CLR);
337 00330 delay(60000); <span class="comment">// wait 60ms</span>
338 00331 <span class="comment">// set entry mode</span>
339 00332 lcdControlWrite(1&lt;&lt;LCD_ENTRY_MODE | 1&lt;&lt;LCD_ENTRY_INC);
340 00333 <span class="comment">// set display to on</span>
341 00334 <span class="comment">//lcdControlWrite(1&lt;&lt;LCD_ON_CTRL | 1&lt;&lt;LCD_ON_DISPLAY | 1&lt;&lt;LCD_ON_BLINK);</span>
342 00335 lcdControlWrite(1&lt;&lt;LCD_ON_CTRL | 1&lt;&lt;LCD_ON_DISPLAY );
343 00336 <span class="comment">// move cursor to home</span>
344 00337 lcdControlWrite(1&lt;&lt;LCD_HOME);
345 00338 <span class="comment">// set data address to 0</span>
346 00339 lcdControlWrite(1&lt;&lt;LCD_DDRAM | 0x00);
347 00340
348 00341 <span class="comment">// load the first 8 custom characters</span>
349 00342 lcdLoadCustomChar((u08*)LcdCustomChar,0,0);
350 00343 lcdLoadCustomChar((u08*)LcdCustomChar,1,1);
351 00344 lcdLoadCustomChar((u08*)LcdCustomChar,2,2);
352 00345 lcdLoadCustomChar((u08*)LcdCustomChar,3,3);
353 00346 lcdLoadCustomChar((u08*)LcdCustomChar,4,4);
354 00347 lcdLoadCustomChar((u08*)LcdCustomChar,5,5);
355 00348 lcdLoadCustomChar((u08*)LcdCustomChar,6,6);
356 00349 lcdLoadCustomChar((u08*)LcdCustomChar,7,7);
357 00350 }
358 00351
359 00352 <span class="keywordtype">void</span> lcdHome(<span class="keywordtype">void</span>)
360 00353 {
361 00354 <span class="comment">// move cursor to home</span>
362 00355 lcdControlWrite(1&lt;&lt;LCD_HOME);
363 00356 }
364 00357
365 00358 <span class="keywordtype">void</span> lcdClear(<span class="keywordtype">void</span>)
366 00359 {
367 00360 <span class="comment">// clear LCD</span>
368 00361 lcdControlWrite(1&lt;&lt;LCD_CLR);
369 00362 }
370 00363
371 00364 <span class="keywordtype">void</span> lcdGotoXY(u08 x, u08 y)
372 00365 {
373 00366 <span class="keyword">register</span> u08 DDRAMAddr;
374 00367
375 00368 <span class="comment">// remap lines into proper order</span>
376 00369 <span class="keywordflow">switch</span>(y)
377 00370 {
378 00371 <span class="keywordflow">case</span> 0: DDRAMAddr = LCD_LINE0_DDRAMADDR+x; <span class="keywordflow">break</span>;
379 00372 <span class="keywordflow">case</span> 1: DDRAMAddr = LCD_LINE1_DDRAMADDR+x; <span class="keywordflow">break</span>;
380 00373 <span class="keywordflow">case</span> 2: DDRAMAddr = LCD_LINE2_DDRAMADDR+x; <span class="keywordflow">break</span>;
381 00374 <span class="keywordflow">case</span> 3: DDRAMAddr = LCD_LINE3_DDRAMADDR+x; <span class="keywordflow">break</span>;
382 00375 <span class="keywordflow">default</span>: DDRAMAddr = LCD_LINE0_DDRAMADDR+x;
383 00376 }
384 00377
385 00378 <span class="comment">// set data address</span>
386 00379 lcdControlWrite(1&lt;&lt;LCD_DDRAM | DDRAMAddr);
387 00380 }
388 00381
389 00382 <span class="keywordtype">void</span> lcdLoadCustomChar(u08* lcdCustomCharArray, u08 romCharNum, u08 lcdCharNum)
390 00383 {
391 00384 <span class="keyword">register</span> u08 i;
392 00385 u08 saveDDRAMAddr;
393 00386
394 00387 <span class="comment">// backup the current cursor position</span>
395 00388 saveDDRAMAddr = lcdControlRead() &amp; 0x7F;
396 00389
397 00390 <span class="comment">// multiply the character index by 8</span>
398 00391 lcdCharNum = (lcdCharNum&lt;&lt;3); <span class="comment">// each character occupies 8 bytes</span>
399 00392 romCharNum = (romCharNum&lt;&lt;3); <span class="comment">// each character occupies 8 bytes</span>
400 00393
401 00394 <span class="comment">// copy the 8 bytes into CG (character generator) RAM</span>
402 00395 <span class="keywordflow">for</span>(i=0; i&lt;8; i++)
403 00396 {
404 00397 <span class="comment">// set CG RAM address</span>
405 00398 lcdControlWrite((1&lt;&lt;LCD_CGRAM) | (lcdCharNum+i));
406 00399 <span class="comment">// write character data</span>
407 00400 lcdDataWrite( pgm_read_byte(lcdCustomCharArray+romCharNum+i) );
408 00401 }
409 00402
410 00403 <span class="comment">// restore the previous cursor position</span>
411 00404 lcdControlWrite(1&lt;&lt;LCD_DDRAM | saveDDRAMAddr);
412 00405
413 00406 }
414 00407
415 00408 <span class="keywordtype">void</span> lcdPrintData(<span class="keywordtype">char</span>* data, u08 nBytes)
416 00409 {
417 00410 <span class="keyword">register</span> u08 i;
418 00411
419 00412 <span class="comment">// check to make sure we have a good pointer</span>
420 00413 <span class="keywordflow">if</span> (!data) <span class="keywordflow">return</span>;
421 00414
422 00415 <span class="comment">// print data</span>
423 00416 <span class="keywordflow">for</span>(i=0; i&lt;nBytes; i++)
424 00417 {
425 00418 lcdDataWrite(data[i]);
426 00419 }
427 00420 }
428 00421
429 00422 <span class="keywordtype">void</span> lcdProgressBar(u16 progress, u16 maxprogress, u08 length)
430 00423 {
431 00424 u08 i;
432 00425 u32 pixelprogress;
433 00426 u08 c;
434 00427
435 00428 <span class="comment">// draw a progress bar displaying (progress / maxprogress)</span>
436 00429 <span class="comment">// starting from the current cursor position</span>
437 00430 <span class="comment">// with a total length of "length" characters</span>
438 00431 <span class="comment">// ***note, LCD chars 0-5 must be programmed as the bar characters</span>
439 00432 <span class="comment">// char 0 = empty ... char 5 = full</span>
440 00433
441 00434 <span class="comment">// total pixel length of bargraph equals length*PROGRESSPIXELS_PER_CHAR;</span>
442 00435 <span class="comment">// pixel length of bar itself is</span>
443 00436 pixelprogress = ((progress*(length*PROGRESSPIXELS_PER_CHAR))/maxprogress);
444 00437
445 00438 <span class="comment">// print exactly "length" characters</span>
446 00439 <span class="keywordflow">for</span>(i=0; i&lt;length; i++)
447 00440 {
448 00441 <span class="comment">// check if this is a full block, or partial or empty</span>
449 00442 <span class="comment">// (u16) cast is needed to avoid sign comparison warning</span>
450 00443 <span class="keywordflow">if</span>( ((i*(u16)PROGRESSPIXELS_PER_CHAR)+5) &gt; pixelprogress )
451 00444 {
452 00445 <span class="comment">// this is a partial or empty block</span>
453 00446 <span class="keywordflow">if</span>( ((i*(u16)PROGRESSPIXELS_PER_CHAR)) &gt; pixelprogress )
454 00447 {
455 00448 <span class="comment">// this is an empty block</span>
456 00449 <span class="comment">// use space character?</span>
457 00450 c = 0;
458 00451 }
459 00452 <span class="keywordflow">else</span>
460 00453 {
461 00454 <span class="comment">// this is a partial block</span>
462 00455 c = pixelprogress % PROGRESSPIXELS_PER_CHAR;
463 00456 }
464 00457 }
465 00458 <span class="keywordflow">else</span>
466 00459 {
467 00460 <span class="comment">// this is a full block</span>
468 00461 c = 5;
469 00462 }
470 00463
471 00464 <span class="comment">// write character to display</span>
472 00465 lcdDataWrite(c);
473 00466 }
474 00467
475 00468 }
476 00469
477 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by&nbsp;
478 <a href="http://www.doxygen.org/index.html">
479 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
480 </body>
481 </html>
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3