ASURO Library  2.80
lcd.c
gehe zur Dokumentation dieser Datei
1 
31 /***************************************************************************************************************************************************
32  * Library to control LCD over I²c
33  *
34  * LCD Library
35  *
36  * This Library is designed for HD44870 based LCDs with I2C expander PCF8574p. It requires I²C Master Software from Peter Fleury.
37  * PIN-Assignment: P0-P3 -> DB4-DB7, P4 -> RS, P5 -> R/w, P7 -> Enable.
38  * For Use with ASURO Robot
39  *
40  * Inspiration from I²C-LCD library by "Nico Eichelmann, Thomas Eichelmann"
41  *
42  * --------------------------------------------------------------------------------------------------------------------------------------------------
43  *
44  * Copyright (c) 2006 Rizqi Ahmad (raid_ox)
45  *
46  * 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
47  * 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
48  * General Public License along with asurolib; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
49  * 02110-1301 USA
50  *
51  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
52  *
53  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
54  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55  * 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
56  * DEALINGS IN THE SOFTWARE.
57  **************************************************************************************************************************************************/
58 
59 #include "asuro.h"
60 #include "lcd.h"
61 #include "i2c.h"
62 
67 void InitLCD(void)
68 {
69  unsigned char init[] = LCD_INIT;
70  unsigned char i = 0;
71 
72  SetIOLCD(OFF, LCD_EN); // Start LCD Control, EN=0
73  Msleep(1); // Wait LCD Ready
74 
75  // Initialize LCD
76  CommandLCD( LCD_8BIT | (LCD_8BIT >> 4) );
77  CommandLCD( LCD_8BIT | (LCD_4BIT >> 4) );
78 
79  while (init[i] != 0x00)
80  {
81  CommandLCD(init[i]);
82  i++;
83  }
84 
85  CommandLCD( LCD_DISPLAYON ); // Display on/off Control (Entry Display,Cursor off,Cursor not Blink)
86  CommandLCD( LCD_INCREASE ); // Entry Mode Set (I/D=1 Increment,S=0 Cursor Shift)
87  CommandLCD( LCD_CLEAR ); // Clear Display
88  CommandLCD( LCD_HOME ); // Home Cursor
89  Msleep(1); // Wait Initial Complete
90 }
91 
100 void BacklightLCD(unsigned char state)
101 {
102  SetIOLCD(state, LCD_BL);
103 }
104 
111 void SetDataLCD(unsigned char data)
112 {
113  unsigned char dataPins; // Pin Compatibility
114 
115  // Set First Nibble Data to DataPins on PCF8574
116  dataPins &= 0x00;
117  dataPins |= ((data & 0x80) >> 7) << LD7;
118  dataPins |= ((data & 0x40) >> 6) << LD6;
119  dataPins |= ((data & 0x20) >> 5) << LD5;
120  dataPins |= ((data & 0x10) >> 4) << LD4;
121 
122  SetIOLCD(OFF, LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7); // Clear old LCD Data (Bit[7..4])
123  SetIOLCD(ON, dataPins); // Strobe High Nibble Command
124  SetIOLCD(ON, LCD_EN); // Enable ON
125  Msleep(1);
126  SetIOLCD(OFF, LCD_EN); // Enable OFF
127 
128  // Set Second Nibble Data to DataPins on PCF8574
129  dataPins &= 0x00;
130  dataPins |= ((data & 0x08) >> 3) << LD7;
131  dataPins |= ((data & 0x04) >> 2) << LD6;
132  dataPins |= ((data & 0x02) >> 1) << LD5;
133  dataPins |= ((data & 0x01) >> 0) << LD4;
134 
135  SetIOLCD(OFF, LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7); // Clear old LCD Data (Bit[7..4])
136  SetIOLCD(ON, dataPins); // Strobe Low Nibble Command
137  SetIOLCD(ON, LCD_EN); // Enable ON
138  Msleep(1);
139  SetIOLCD(OFF, LCD_EN); // Enable OFF
140 
141  Msleep(1); // Wait LCD Busy
142 }
143 
151 void SetIOLCD(unsigned char setCommand, unsigned char bits)
152 {
153  if (setCommand == ON)
154  portLCD |= bits;
155  else
156  portLCD &= ~bits;
157  StartI2C(LCD_DEV);
158  WriteI2C(portLCD);
159  StopI2C();
160 }
161 
168 unsigned char GetIOLCD(void)
169 {
170  unsigned char data = 0x00;
171  StartI2C(LCD_DEV+1);
172  data = ReadI2C(0);
173  StopI2C();
174  return data;
175 }
176 
184 void SetCursorLCD(unsigned char cursor, unsigned char line)
185 {
186  cursorLCD = cursor;
187  lineLCD = line;
188 
189  if (line == 0)
190  line = LCD_LINE1;
191 #if LCD_LINES>=2
192  else if (line == 1)
193  line = LCD_LINE2;
194 #endif
195 #if LCD_LINES>=3
196  else if (line == 2)
197  line = LCD_LINE3;
198 #endif
199 #if LCD_LINES>=4
200  else if (line == 3)
201  line = LCD_LINE4;
202 #endif
203  else
204  line = LCD_LINE1;
205 
206  CommandLCD(LCD_DDRAM | (line+cursor));
207 }
208 
215 void CommandLCD(unsigned char command)
216 {
217  if (command == LCD_HOME)
218  lineLCD = cursorLCD = 0x00;
219  SetIOLCD(OFF, LCD_RS);
220  SetDataLCD(command);
221 }
222 
228 void ClearLCD(void)
229 {
232 }
233 
241 void WriteLCD(unsigned char data)
242 {
243  SetIOLCD(ON, LCD_RS);
244  SetDataLCD(data);
245  cursorLCD++;
246 }
247 
255 void PrintLCD(char *string, unsigned char wrap)
256 {
257  unsigned char i = 0;
258  while (string[i] != 0x00)
259  {
260  if (cursorLCD >= LCD_CHARS)
261  {
262  if (wrap)
263  SetCursorLCD(0, lineLCD+1);
264  else
265  break;
266  }
267  WriteLCD(string[i]);
268  i++;
269  }
270 }
271 
280 void PrintSetLCD(unsigned char cursor, unsigned char line, char *string)
281 {
282  SetCursorLCD(cursor, line);
283  PrintLCD(string, OFF);
284 }
285 
292 void PrintIntLCD(int value)
293 {
294  char text[6];
295  itoa(value,text,10);
296  PrintLCD(text, OFF);
297 }
298 
308 void PrintAlignLCD(unsigned char alignment, unsigned char line, char *string)
309 {
310  unsigned char i = 0;
311  while (string[i] != 0x00)
312  i++;
313  if (alignment == RIGHT)
314  PrintSetLCD(LCD_CHARS-i, line, string);
315  else if (alignment == CENTER)
316  PrintSetLCD((LCD_CHARS-i)/2, line, string);
317  else
318  PrintSetLCD(0, line, string);
319 }
320 
321 
330 void PrintLCD_p(const char *progmem_s)
331 {
332  register char c;
333 
334  while ( (c = pgm_read_byte(progmem_s++)) )
335  {
336  WriteLCD(c);
337  }
338 }
339 
340 
353 void SetCharLCD_p(unsigned char AsciiCode, const char *progmem_s)
354 {
355  unsigned char i;
356 
357  CommandLCD( 0x40|(AsciiCode<<3));
358  for(i=0;i<=7;i++)
359  {
360  WriteLCD(pgm_read_byte(progmem_s++));
361  }
362 }
363 
373 int PollSwitchLCD (void)
374 {
375  int key = 0;
376 
377  //taster pins auf input schalten
378  // OC2 PIN deaktivieren, aber 36kHz Timer weiterlaufen lassen (z.B. für Sleep(void) )
379 #if defined(__AVR_ATmega168__)
380  TCCR2A = _BV(WGM21);
381  TCCR2B = _BV(CS20);
382 #else
383  TCCR2 = (1 << WGM21) | (1 << CS20);
384 #endif
385  DDRD &= ~((1<<PD2)|(1<<PD6)); // roten und gelben Taster als Eingang definieren
386  DDRB &= ~(1<<PB3); // blauen Taster als Eingang definieren
387 
388  //taster abfragen
389 
390  key |= (PIND&LCD_KEY_YELLOW);
391  key |= (PIND&LCD_KEY_RED);
392  key |= (PINB&LCD_KEY_BLUE);
393 
394  //taster pins auf output schalten
395 #if defined(__AVR_ATmega168__)
396  TCCR2A = _BV(WGM20) | _BV(WGM21) | _BV(COM2A0) | _BV(COM2A1);
397  TCCR2B = _BV(CS20);
398 #else
399  TCCR2 = (1 << WGM20) | (1 << WGM21) | (1 << COM20) | (1 << COM21) | (1 << CS20);
400 #endif
401 
402  DDRD |= ((1<<PD2)|(1<<PD6)); // roten und gelben Taster als ausgang definieren
403  DDRB |= (1<<PB3); // blauen Taster als ausgang definieren
404 
405 
406  //tasterwert zurückgeben
407 
408  return key;
409 }
410