1 /*-------------------------------------------------------------------------
2    lcd.c - lcd routines for the DS80C390 (tested on TINI)
3 
4    Copyright (C) 2001, Johan Knol <johan.knol AT iduna.nl>
5 
6    This library is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this library; see the file COPYING. If not, write to the
18    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19    MA 02110-1301, USA.
20 
21    As a special exception, if you link this library with other files,
22    some of which are compiled with SDCC, to produce an executable,
23    this library does not by itself cause the resulting executable to
24    be covered by the GNU General Public License. This exception does
25    not however invalidate any other reasons why the executable file
26    might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
28 
29 #include <tinibios.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 
33 #define LCD_COLLUMNS 20
34 #define LCD_ROWS 4
35 
36 /* set the dd ram addresses for the rows
37    this one is for a 20x4 LCD
38 */
39 static unsigned char lcdLinesStart[LCD_ROWS]={0, 0x40, 0x14, 0x54};
40 
41 /* Commercial TINI stick connector boards don't support rw
42    control for the lcd-display.
43    My own board does, and it makes it much faster.
44 */
45 
46 //#define LCD_RW
47 
48 __xdata __at (0x380002) static unsigned char lcdIwr;
49 __xdata __at (0x38000a) static unsigned char lcdDwr;
50 
51 #ifdef LCD_RW
52 
53 __xdata __at (0x380003) static unsigned char lcdIrd;
54 __xdata __at (0x38000b) static unsigned char lcdDrd;
55 
56 #define LcdWait { while (lcdIrd&0x80) ; }
57 
58 #else // ifdef LCD_RW
59 
60 // wait for 100us
61 #define LcdWait { ClockMicroSecondsDelay(100) ; }
62 
63 #endif // ifdef LCD_RW
64 
LcdInit()65 void LcdInit() {
66 
67   ClockMilliSecondsDelay(16); // >15 ms
68 
69   lcdIwr=0x38 ;
70   ClockMilliSecondsDelay(5); // >4.1 ms
71 
72   lcdIwr=0x38;
73   ClockMicroSecondsDelay(101); // >100 us
74 
75   lcdIwr=0x38;
76   ClockMicroSecondsDelay(101); // >100 us
77 
78   lcdIwr=0x38; // interface 8 bit
79   ClockMicroSecondsDelay(41); // >40 us
80 
81   lcdIwr=0x0c; // display on
82   ClockMicroSecondsDelay(41); // >40 us
83 
84   LcdClear();
85 }
86 
LcdOn()87 void LcdOn() {
88   lcdIwr=0x0c; // display on
89   LcdWait;
90 }
91 
LcdOff()92 void LcdOff() {
93   lcdIwr=0x08; // display off
94   LcdWait;
95 }
96 
LcdCursorOn()97 void LcdCursorOn() {
98   // TODO
99 }
100 
LcdCursorOff()101 void LcdCursorOff() {
102   // TODO
103 }
104 
LcdScrollOn()105 void LcdScrollOn() {
106   // TODO
107 }
108 
LcdScrollOff()109 void LcdScrollOff() {
110   // TODO
111 }
112 
LcdCharDefine()113 void LcdCharDefine() {
114   // TODO
115 }
116 
LcdClear()117 void LcdClear() {
118   lcdIwr=0x01; // display clear
119   ClockMilliSecondsDelay(6); // > 5ms
120 }
121 
LcdHome()122 void LcdHome() {
123   lcdIwr=0x80; // set dd ram address 0
124   LcdWait;
125 }
126 
LcdGoto(unsigned int collumnRow)127 void LcdGoto(unsigned int collumnRow) { // msb=collumn, lsb=row
128   lcdIwr=0x80 + \
129     lcdLinesStart[collumnRow&0xff] + (collumnRow>>8);
130   LcdWait;
131 }
132 
LcdPutChar(char c)133 void LcdPutChar(char c) {
134   lcdDwr=c;
135   LcdWait;
136 }
137 
LcdPutString(char * string)138 void LcdPutString (char *string) {
139   char c;
140   while (c=*string++) {
141     LcdPutChar (c);
142   }
143 }
144 
LcdLPutString(unsigned int collumnRow,char * string)145 void LcdLPutString (unsigned int collumnRow, char *string) {
146   LcdGoto(collumnRow);
147   LcdPutString(string);
148 }
149 
150 // let's hope that no one ever printf's more than the display width,
151 // however they will :), so to be sure
152 static char lcdPrintfBuffer[LCD_COLLUMNS*4];
153 
LcdPrintf(const char * format,...)154 void LcdPrintf (const char *format, ...) __reentrant {
155   va_list arg;
156 
157   va_start (arg, format);
158   vsprintf (lcdPrintfBuffer, format, arg);
159   puts (lcdPrintfBuffer);
160   LcdPutString(lcdPrintfBuffer);
161 
162   va_end (arg);
163 }
164 
LcdLPrintf(unsigned int collumnRow,const char * format,...)165 void LcdLPrintf (unsigned int collumnRow, const char *format, ...) __reentrant {
166   va_list arg;
167 
168   LcdGoto(collumnRow);
169 
170   // we can not just call LcdPrintf since we have no idea what is on the stack,
171   // so we have to do it all over again
172   va_start (arg, format);
173   vsprintf (lcdPrintfBuffer, format, arg);
174 
175   LcdPutString(lcdPrintfBuffer);
176 
177   va_end (arg);
178 }
179