📄 lcd_8c-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: lcd.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.2 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div><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>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'lcd.c'</span>00005 <span class="comment">// Title : Character LCD driver for HD44780/SED1278 displays</span>00006 <span class="comment">// (usable in mem-mapped, or I/O mode)</span>00007 <span class="comment">// Author : Pascal Stang</span>00008 <span class="comment">// Created : 11/22/2000</span>00009 <span class="comment">// Revised : 4/30/2002</span>00010 <span class="comment">// Version : 1.1</span>00011 <span class="comment">// Target MCU : Atmel AVR series</span>00012 <span class="comment">// Editor Tabs : 4</span>00013 <span class="comment">//</span>00014 <span class="comment">// This code is distributed under the GNU Public License</span>00015 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00016 <span class="comment">//</span>00017 <span class="comment">//*****************************************************************************</span>00018 00019 <span class="preprocessor">#include <avr/io.h></span>00020 <span class="preprocessor">#include <avr/pgmspace.h></span>00021 00022 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00023 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00024 00025 <span class="preprocessor">#include "<a class="code" href="lcd_8h.html">lcd.h</a>"</span>00026 00027 <span class="comment">// custom LCD characters</span>00028 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> __attribute__ ((progmem)) LcdCustomChar[] =00029 {00030 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, <span class="comment">// 0. 0/5 full progress block</span>00031 0x00, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x1F, 0x00, <span class="comment">// 1. 1/5 full progress block</span>00032 0x00, 0x1F, 0x18, 0x18, 0x18, 0x18, 0x1F, 0x00, <span class="comment">// 2. 2/5 full progress block</span>00033 0x00, 0x1F, 0x1C, 0x1C, 0x1C, 0x1C, 0x1F, 0x00, <span class="comment">// 3. 3/5 full progress block</span>00034 0x00, 0x1F, 0x1E, 0x1E, 0x1E, 0x1E, 0x1F, 0x00, <span class="comment">// 4. 4/5 full progress block</span>00035 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, <span class="comment">// 5. 5/5 full progress block</span>00036 0x03, 0x07, 0x0F, 0x1F, 0x0F, 0x07, 0x03, 0x00, <span class="comment">// 6. rewind arrow</span>00037 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, <span class="comment">// 7. stop block</span>00038 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x00, <span class="comment">// 8. pause bars</span>00039 0x18, 0x1C, 0x1E, 0x1F, 0x1E, 0x1C, 0x18, 0x00, <span class="comment">// 9. fast-forward arrow</span>00040 0x00, 0x04, 0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x00, <span class="comment">// 10. scroll up arrow</span>00041 0x00, 0x1F, 0x1F, 0x0E, 0x0E, 0x04, 0x04, 0x00, <span class="comment">// 11. scroll down arrow</span>00042 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, <span class="comment">// 12. blank character</span>00043 0x00, 0x0E, 0x19, 0x15, 0x13, 0x0E, 0x00, 0x00, <span class="comment">// 13. animated play icon frame 0</span>00044 0x00, 0x0E, 0x15, 0x15, 0x15, 0x0E, 0x00, 0x00, <span class="comment">// 14. animated play icon frame 1</span>00045 0x00, 0x0E, 0x13, 0x15, 0x19, 0x0E, 0x00, 0x00, <span class="comment">// 15. animated play icon frame 2</span>00046 0x00, 0x0E, 0x11, 0x1F, 0x11, 0x0E, 0x00, 0x00, <span class="comment">// 16. animated play icon frame 3</span>00047 };00048 00049 <span class="comment">/*************************************************************/</span>00050 <span class="comment">/********************** LOCAL FUNCTIONS **********************/</span>00051 <span class="comment">/*************************************************************/</span>00052 00053 <span class="keywordtype">void</span> lcdInitHW(<span class="keywordtype">void</span>)00054 {00055 <span class="comment">// initialize I/O ports</span>00056 <span class="comment">// if I/O interface is in use</span>00057 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>00058 <span class="preprocessor"></span> <span class="comment">// initialize LCD control lines</span>00059 cbi(LCD_CTRL_PORT, LCD_CTRL_RS);00060 cbi(LCD_CTRL_PORT, LCD_CTRL_RW);00061 cbi(LCD_CTRL_PORT, LCD_CTRL_E);00062 <span class="comment">// initialize LCD control lines to output</span>00063 sbi(LCD_CTRL_DDR, LCD_CTRL_RS);00064 sbi(LCD_CTRL_DDR, LCD_CTRL_RW);00065 sbi(LCD_CTRL_DDR, LCD_CTRL_E);00066 <span class="comment">// initialize LCD data port to input</span>00067 <span class="comment">// initialize LCD data lines to pull-up</span>00068 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>00069 <span class="preprocessor"></span> outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F); <span class="comment">// set data I/O lines to input (4bit)</span>00070 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); <span class="comment">// set pull-ups to on (4bit)</span>00071 <span class="preprocessor"> #else</span>00072 <span class="preprocessor"></span> outb(LCD_DATA_DDR, 0x00); <span class="comment">// set data I/O lines to input (8bit)</span>00073 outb(LCD_DATA_POUT, 0xFF); <span class="comment">// set pull-ups to on (8bit)</span>00074 <span class="preprocessor"> #endif</span>00075 <span class="preprocessor"></span><span class="preprocessor">#else</span>00076 <span class="preprocessor"></span> <span class="comment">// enable external memory bus if not already enabled</span>00077 sbi(MCUCR, SRE); <span class="comment">// enable bus interface</span>00078 <span class="preprocessor">#endif</span>00079 <span class="preprocessor"></span>}00080 00081 <span class="keywordtype">void</span> lcdBusyWait(<span class="keywordtype">void</span>)00082 {00083 <span class="comment">// wait until LCD busy bit goes to zero</span>00084 <span class="comment">// do a read from control register</span>00085 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>00086 <span class="preprocessor"></span> cbi(LCD_CTRL_PORT, LCD_CTRL_RS); <span class="comment">// set RS to "control"</span>00087 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>00088 <span class="preprocessor"></span> outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F); <span class="comment">// set data I/O lines to input (4bit)</span>00089 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); <span class="comment">// set pull-ups to on (4bit)</span>00090 <span class="preprocessor"> #else</span>00091 <span class="preprocessor"></span> outb(LCD_DATA_DDR, 0x00); <span class="comment">// set data I/O lines to input (8bit)</span>00092 outb(LCD_DATA_POUT, 0xFF); <span class="comment">// set pull-ups to on (8bit)</span>00093 <span class="preprocessor"> #endif</span>00094 <span class="preprocessor"></span> sbi(LCD_CTRL_PORT, LCD_CTRL_RW); <span class="comment">// set R/W to "read"</span>00095 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>00096 LCD_DELAY; <span class="comment">// wait</span>00097 <span class="keywordflow">while</span>(inb(LCD_DATA_PIN) & 1<<LCD_BUSY)00098 {00099 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>00100 LCD_DELAY; <span class="comment">// wait</span>00101 LCD_DELAY; <span class="comment">// wait</span>00102 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>00103 LCD_DELAY; <span class="comment">// wait</span>00104 LCD_DELAY; <span class="comment">// wait</span>00105 <span class="preprocessor"> #ifdef LCD_DATA_4BIT // do an extra clock for 4 bit reads</span>00106 <span class="preprocessor"></span> cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>00107 LCD_DELAY; <span class="comment">// wait</span>00108 LCD_DELAY; <span class="comment">// wait</span>00109 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>00110 LCD_DELAY; <span class="comment">// wait</span>00111 LCD_DELAY; <span class="comment">// wait</span>00112 <span class="preprocessor"> #endif</span>00113 <span class="preprocessor"></span> }00114 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>00115 <span class="comment">// leave data lines in input mode so they can be most easily used for other purposes</span>00116 <span class="preprocessor">#else</span>00117 <span class="preprocessor"></span> <span class="comment">// memory bus read</span>00118 <span class="comment">// sbi(MCUCR, SRW); // enable RAM waitstate</span>00119 <span class="comment">// wait until LCD busy bit goes to zero</span>00120 <span class="keywordflow">while</span>( (*((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *) (LCD_CTRL_ADDR))) & (1<<LCD_BUSY) );00121 <span class="comment">// cbi(MCUCR, SRW); // disable RAM waitstate</span>00122 <span class="preprocessor">#endif</span>00123 <span class="preprocessor"></span>}00124 00125 <span class="keywordtype">void</span> lcdControlWrite(u08 data) 00126 {00127 <span class="comment">// write the control byte to the display controller</span>00128 <span class="preprocessor">#ifdef LCD_PORT_INTERFACE</span>00129 <span class="preprocessor"></span> lcdBusyWait(); <span class="comment">// wait until LCD not busy</span>00130 cbi(LCD_CTRL_PORT, LCD_CTRL_RS); <span class="comment">// set RS to "control"</span>00131 cbi(LCD_CTRL_PORT, LCD_CTRL_RW); <span class="comment">// set R/W to "write"</span>00132 <span class="preprocessor"> #ifdef LCD_DATA_4BIT</span>00133 <span class="preprocessor"></span> <span class="comment">// 4 bit write</span>00134 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>00135 outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)|0xF0); <span class="comment">// set data I/O lines to output (4bit)</span>00136 outb(LCD_DATA_POUT, (inb(LCD_DATA_POUT)&0x0F) | (data&0xF0) ); <span class="comment">// output data, high 4 bits</span>00137 LCD_DELAY; <span class="comment">// wait</span>00138 LCD_DELAY; <span class="comment">// wait</span>00139 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>00140 LCD_DELAY; <span class="comment">// wait</span>00141 LCD_DELAY; <span class="comment">// wait</span>00142 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>00143 outb(LCD_DATA_POUT, (inb(LCD_DATA_POUT)&0x0F) | (data<<4) ); <span class="comment">// output data, low 4 bits</span>00144 LCD_DELAY; <span class="comment">// wait</span>00145 LCD_DELAY; <span class="comment">// wait</span>00146 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>00147 <span class="preprocessor"> #else</span>00148 <span class="preprocessor"></span> <span class="comment">// 8 bit write</span>00149 sbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// set "E" line</span>00150 outb(LCD_DATA_DDR, 0xFF); <span class="comment">// set data I/O lines to output (8bit)</span>00151 outb(LCD_DATA_POUT, data); <span class="comment">// output data, 8bits</span>00152 LCD_DELAY; <span class="comment">// wait</span>00153 LCD_DELAY; <span class="comment">// wait</span>00154 cbi(LCD_CTRL_PORT, LCD_CTRL_E); <span class="comment">// clear "E" line</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -