lcd.c

gehe zur Dokumentation dieser Datei
00001 
00026 /*************************************************************************************************************************************************** 
00027  * Library to control LCD over I²c
00028  *
00029  * LCD Library
00030  *
00031  * This Library is designed for HD44870 based LCDs with I2C expander PCF8574p. It requires I²C Master Software from Peter Fleury.
00032  * PIN-Assignment: P0-P3 -> DB4-DB7, P4 -> RS, P5 -> R/w, P7 -> Enable.
00033  * For Use with ASURO Robot
00034  *
00035  * Inspiration from I²C-LCD library by "Nico Eichelmann, Thomas Eichelmann"
00036  *
00037  * --------------------------------------------------------------------------------------------------------------------------------------------------
00038  *
00039  * Copyright (c) 2006 Rizqi Ahmad (raid_ox)
00040  *
00041  * This software is a free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the
00042  * Free Software Foundation; either version 2 of the License, or (at your option) any later version. You should have received a copy of the GNU
00043  * General Public License along with asurolib; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00044  * 02110-1301  USA
00045  *
00046  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00047  *
00048  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00049  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00050  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00051  * DEALINGS IN THE SOFTWARE.
00052  **************************************************************************************************************************************************/
00053 
00054 #include "asuro.h"
00055 #include "lcd.h"
00056 #include "i2c.h"
00057 
00062 void InitLCD(void)
00063 {
00064   unsigned char init[] = LCD_INIT;
00065   unsigned char i = 0;
00066 
00067   SetIOLCD(OFF, LCD_EN);              // Start LCD Control, EN=0
00068   Msleep(1);                          // Wait LCD Ready
00069 
00070   // Initialize LCD
00071   CommandLCD( LCD_8BIT | (LCD_8BIT >> 4) );
00072   CommandLCD( LCD_8BIT | (LCD_4BIT >> 4) );
00073 
00074   while (init[i] != 0x00)
00075   {
00076     CommandLCD(init[i]);
00077     i++;
00078   }
00079 
00080   CommandLCD( LCD_DISPLAYON );        // Display on/off Control (Entry Display,Cursor off,Cursor not Blink)
00081   CommandLCD( LCD_INCREASE );         // Entry Mode Set (I/D=1 Increment,S=0 Cursor Shift)
00082   CommandLCD( LCD_CLEAR );            // Clear Display
00083   CommandLCD( LCD_HOME );             // Home Cursor
00084   Msleep(1);                          // Wait Initial Complete
00085 }
00086 
00097 void BacklightLCD(unsigned char state)
00098 {
00099   SetIOLCD(state, LCD_BL);
00100 }
00101 
00108 void SetDataLCD(unsigned char data)
00109 {
00110   unsigned char dataPins;             // Pin Compatibility
00111 
00112   // Set First Nibble Data to DataPins on PCF8574
00113   dataPins &= 0x00;
00114   dataPins |= ((data & 0x80) >> 7) << LD7;
00115   dataPins |= ((data & 0x40) >> 6) << LD6;
00116   dataPins |= ((data & 0x20) >> 5) << LD5;
00117   dataPins |= ((data & 0x10) >> 4) << LD4;
00118 
00119   SetIOLCD(OFF, LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7);     // Clear old LCD Data (Bit[7..4])
00120   SetIOLCD(ON, dataPins);             // Strobe High Nibble Command
00121   SetIOLCD(ON, LCD_EN);               // Enable ON
00122   Msleep(1);
00123   SetIOLCD(OFF, LCD_EN);              // Enable OFF
00124 
00125   // Set Second Nibble Data to DataPins on PCF8574
00126   dataPins &= 0x00;
00127   dataPins |= ((data & 0x08) >> 3) << LD7;
00128   dataPins |= ((data & 0x04) >> 2) << LD6;
00129   dataPins |= ((data & 0x02) >> 1) << LD5;
00130   dataPins |= ((data & 0x01) >> 0) << LD4;
00131 
00132   SetIOLCD(OFF, LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7);     // Clear old LCD Data (Bit[7..4])
00133   SetIOLCD(ON, dataPins);             // Strobe Low Nibble Command
00134   SetIOLCD(ON, LCD_EN);               // Enable ON
00135   Msleep(1);
00136   SetIOLCD(OFF, LCD_EN);              // Enable OFF
00137 
00138   Msleep(1);                          // Wait LCD Busy
00139 }
00140 
00148 void SetIOLCD(unsigned char setCommand, unsigned char bits)
00149 {
00150   if (setCommand == ON)
00151     portLCD |= bits;
00152   else
00153     portLCD &= ~bits;
00154   StartI2C(LCD_DEV);
00155   WriteI2C(portLCD);
00156   StopI2C();
00157 }
00158 
00165 unsigned char GetIOLCD(void)
00166 {
00167   unsigned char data = 0x00;
00168   StartI2C(LCD_DEV+1);
00169   data = ReadI2C(0);
00170   StopI2C();
00171   return data;
00172 }
00173 
00181 void SetCursorLCD(unsigned char cursor, unsigned char line)
00182 {
00183   cursorLCD   = cursor;
00184   lineLCD   = line;
00185 
00186   if (line == 0)
00187     line = LCD_LINE1;
00188 #if LCD_LINES>=2
00189   else if (line == 1)
00190     line = LCD_LINE2;
00191 #endif
00192 #if LCD_LINES>=3
00193   else if (line == 2)
00194     line = LCD_LINE3;
00195 #endif
00196 #if LCD_LINES>=4
00197   else if (line == 3)
00198     line = LCD_LINE4;
00199 #endif
00200   else
00201     line = LCD_LINE1;
00202 
00203   CommandLCD(LCD_DDRAM | (line+cursor));
00204 }
00205 
00212 void CommandLCD(unsigned char command)
00213 {
00214   if (command == LCD_HOME)
00215     lineLCD = cursorLCD = 0x00;
00216   SetIOLCD(OFF, LCD_RS);
00217   SetDataLCD(command);
00218 }
00219 
00225 void ClearLCD(void)
00226 {
00227   CommandLCD(LCD_CLEAR);
00228   CommandLCD(LCD_HOME);
00229 }
00230 
00238 void WriteLCD(unsigned char data)
00239 {
00240   SetIOLCD(ON, LCD_RS);
00241   SetDataLCD(data);
00242   cursorLCD++;
00243 }
00244 
00252 void PrintLCD(char *string, unsigned char wrap)
00253 {
00254   unsigned char i = 0;
00255   while (string[i] != 0x00)
00256   {
00257     if (cursorLCD >= LCD_CHARS)
00258     {
00259       if (wrap)
00260         SetCursorLCD(0, lineLCD+1);
00261       else
00262         break;
00263     }
00264     WriteLCD(string[i]);
00265     i++;
00266   }
00267 }
00268 
00277 void PrintSetLCD(unsigned char cursor, unsigned char line, char *string)
00278 {
00279   SetCursorLCD(cursor, line);
00280   PrintLCD(string, OFF);
00281 }
00282 
00289 void PrintIntLCD(int value)
00290 {
00291   char text[6];
00292   itoa(value,text,10);
00293   PrintLCD(text, OFF);
00294 }
00295 
00307 void PrintAlignLCD(unsigned char alignment, unsigned char line, char *string)
00308 {
00309   unsigned char i = 0;
00310   while (string[i] != 0x00)
00311     i++;
00312   if (alignment == RIGHT)
00313     PrintSetLCD(LCD_CHARS-i, line, string);
00314   else if (alignment == CENTER)
00315     PrintSetLCD((LCD_CHARS-i)/2, line, string);
00316   else
00317     PrintSetLCD(0, line, string);
00318 }
00319 
00320 
00321 

Erzeugt am Sun Nov 18 18:24:52 2007 für ASURO Library von  doxygen 1.5.1-p1