a6b05ef509d624c4c62575914f2f807941068a38
[fw/sdcc] / device / examples / ds390 / clock390 / lcd.c
1 #define LCD_ROWS 4
2 #define LCD_COLLUMNS 20
3
4 xdata at 0x380002 static unsigned char lcdIwr;
5 xdata at 0x38000a static unsigned char lcdDwr;
6
7 #ifdef LCD_RW
8
9 xdata at 0x380003 static unsigned char lcdIrd;
10 xdata at 0x38000b static unsigned char lcdDrd;
11
12 #define LcdWait { while (lcdIrd&0x80) ; }
13
14 #else //#ifdef LCD_RW
15
16 // wait for 100us*time
17 // check this, _PLEASE_
18
19 static void Wait100u(long time) {
20   register long timeout=time*50;
21   while (timeout--)
22     ;
23 }
24
25 #define LcdWait { Wait100u(1);}
26
27 #endif //#ifdef LCD_RW
28
29 // set the dd ram addresses for the rows
30 // this one is for a 20x4 LCD
31 xdata static unsigned char lcdLinesStart[LCD_ROWS]={0, 0x40, 0x14, 0x54};
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include "lcd.h"
38
39 void LcdInit() {
40   
41   Wait100u(150); // >15 ms
42   
43   lcdIwr=0x38 ;
44   Wait100u(50); // >4.1 ms
45   
46   lcdIwr=0x38;
47   Wait100u(1); // >100 us
48   
49   lcdIwr=0x38;
50   Wait100u(1); // >100 u
51   
52   lcdIwr=0x38; // interface 8 bit
53   Wait100u(1); // >40u
54   
55   lcdIwr=0x0c; // display on
56   Wait100u(1); // >40u
57   LcdClear();
58   Wait100u(1); // >40u
59 }
60
61 void LcdOn() {
62   lcdIwr=0x0c; // display on
63   LcdWait;
64 }
65
66 void LcdOff() {
67   lcdIwr=0x08; // display off
68   LcdWait;
69 }
70
71 void LcdCursorOn() {
72   // TODO
73 }
74
75 void LcdCursorOff() {
76   // TODO
77 }
78
79 void LcdScrollOn() {
80   // TODO
81 }
82
83 void LcdScrollOff() {
84   // TODO
85 }
86
87 void LcdCharDefine() {
88   // TODO
89 }
90
91 void LcdClear() {
92   lcdIwr=0x01; // display clear
93   Wait100u(50);
94 }
95
96 void LcdHome() {
97   lcdIwr=0x80; // set dd ram address 0
98   LcdWait;
99 }
100
101 void LcdGoto(unsigned int collumnRow) { // msb=collumn, lsb=row
102   lcdIwr=0x80 + \
103     lcdLinesStart[collumnRow&0xff] + \
104     (collumnRow>>8);
105   LcdWait;
106 }
107
108 void LcdPutChar(char c) {
109   lcdDwr=c;
110   LcdWait;
111 }
112
113 void LcdPutString (char *string) {
114   char c;
115   while (c=*string++) {
116     LcdPutChar (c);
117   }
118 }
119
120 void LcdLPutString (unsigned int collumnRow, char *string) {
121   LcdGoto(collumnRow);
122   LcdPutString(string);
123 }
124
125 // let's hope that no one ever printf's more than the display width,
126 // however they will :), so to be sure
127 xdata static char lcdPrintfBuffer[LCD_COLLUMNS*4];
128
129 void LcdPrintf (xdata const char *format, ...) reentrant {
130   va_list arg;
131
132   va_start (arg, format);
133   vsprintf (lcdPrintfBuffer, format, arg);
134
135   LcdPutString(lcdPrintfBuffer);
136
137   va_end (arg);
138 }
139
140 void LcdLPrintf (unsigned int collumnRow, xdata const char *format, ...) reentrant {
141   va_list arg;
142
143   LcdGoto(collumnRow);
144
145   // we can not just call LcdPrintf since we have no idea what is on the stack,
146   // so we have to do it all over again
147   va_start (arg, format);
148   vsprintf (lcdPrintfBuffer, format, arg);
149
150   LcdPutString(lcdPrintfBuffer);
151
152   va_end (arg);
153 }
154
155 #if 0
156 // These don't belong here
157 void LedOn (void) {
158   _asm
159     setb P3.5
160   _endasm;
161 }
162
163 void LedOff (void) {
164   _asm
165     clr P3.5
166   _endasm;
167 }
168
169 void LedToggle (void) {
170   _asm
171     cpl P3.5
172   _endasm;
173 }
174 #endif