Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libdragonfly / lcd.c @ 277

History | View | Annotate | Download (8.86 KB)

1 276 emarinel
/**
2
 * Copyright (c) 2007 Colony Project
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25
26
27
/**
28
 * @file lcd.c
29
 * @brief LCD
30
 *
31
 * Implementation of functions for using the LCD.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35
#include <avr/io.h>
36
#include <lcd.h>
37
#include <time.h>
38
39
//LCD defines
40
#define RST _BV(4)  // pd4 (GPIO)
41
#define SCE _BV(0)  // pb0 (~SS)
42
#define D_C _BV(5)  // pd5 (GPIO?)
43
#define SDI _BV(2)  // pb2 (MOSI)
44
#define SCK _BV(1) // pb1 (SCK)
45
46
#define LCDPORT PORTB
47
#define LCDDDR DDRB
48
49
#define LCDRESETPORT PORTD
50
#define LCDRESETDDR DDRD
51
52
void lcd_putbyte(unsigned char b);
53
54
55
//**************************************Old shit below!*****************
56
/*
57
FontLookup - a lookup table for all characters
58
*/
59
static const unsigned char FontLookup [][5] =
60
{
61 277 emarinel
  { 0x00, 0x00, 0x00, 0x00, 0x00 },  // sp
62
  { 0x00, 0x00, 0x5f, 0x00, 0x00 },   // !
63
  { 0x00, 0x07, 0x00, 0x07, 0x00 },   // "
64
  { 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // #
65
  { 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $
66
  { 0x23, 0x13, 0x08, 0x64, 0x62 },   // %
67
  { 0x36, 0x49, 0x55, 0x22, 0x50 },   // &
68
  { 0x00, 0x05, 0x03, 0x00, 0x00 },   // '
69
  { 0x00, 0x1c, 0x22, 0x41, 0x00 },   // (
70
  { 0x00, 0x41, 0x22, 0x1c, 0x00 },   // )
71
  { 0x14, 0x08, 0x3E, 0x08, 0x14 },   // *
72
  { 0x08, 0x08, 0x3E, 0x08, 0x08 },   // +
73
  { 0x00, 0x00, 0x50, 0x30, 0x00 },   // ,
74
  { 0x10, 0x10, 0x10, 0x10, 0x10 },   // -
75
  { 0x00, 0x60, 0x60, 0x00, 0x00 },   // .
76
  { 0x20, 0x10, 0x08, 0x04, 0x02 },   // /
77
  { 0x3E, 0x51, 0x49, 0x45, 0x3E },   // 0
78
  { 0x00, 0x42, 0x7F, 0x40, 0x00 },   // 1
79
  { 0x42, 0x61, 0x51, 0x49, 0x46 },   // 2
80
  { 0x21, 0x41, 0x45, 0x4B, 0x31 },   // 3
81
  { 0x18, 0x14, 0x12, 0x7F, 0x10 },   // 4
82
  { 0x27, 0x45, 0x45, 0x45, 0x39 },   // 5
83
  { 0x3C, 0x4A, 0x49, 0x49, 0x30 },   // 6
84
  { 0x01, 0x71, 0x09, 0x05, 0x03 },   // 7
85
  { 0x36, 0x49, 0x49, 0x49, 0x36 },   // 8
86
  { 0x06, 0x49, 0x49, 0x29, 0x1E },   // 9
87
  { 0x00, 0x36, 0x36, 0x00, 0x00 },   // :
88
  { 0x00, 0x56, 0x36, 0x00, 0x00 },   // ;
89
  { 0x08, 0x14, 0x22, 0x41, 0x00 },   // <
90
  { 0x14, 0x14, 0x14, 0x14, 0x14 },   // =
91
  { 0x00, 0x41, 0x22, 0x14, 0x08 },   // >
92
  { 0x02, 0x01, 0x51, 0x09, 0x06 },   // ?
93
  { 0x32, 0x49, 0x59, 0x51, 0x3E },   // @
94
  { 0x7E, 0x11, 0x11, 0x11, 0x7E },   // A
95
  { 0x7F, 0x49, 0x49, 0x49, 0x36 },   // B
96
  { 0x3E, 0x41, 0x41, 0x41, 0x22 },   // C
97
  { 0x7F, 0x41, 0x41, 0x22, 0x1C },   // D
98
  { 0x7F, 0x49, 0x49, 0x49, 0x41 },   // E
99
  { 0x7F, 0x09, 0x09, 0x09, 0x01 },   // F
100
  { 0x3E, 0x41, 0x49, 0x49, 0x7A },   // G
101
  { 0x7F, 0x08, 0x08, 0x08, 0x7F },   // H
102
  { 0x00, 0x41, 0x7F, 0x41, 0x00 },   // I
103
  { 0x20, 0x40, 0x41, 0x3F, 0x01 },   // J
104
  { 0x7F, 0x08, 0x14, 0x22, 0x41 },   // K
105
  { 0x7F, 0x40, 0x40, 0x40, 0x40 },   // L
106
  { 0x7F, 0x02, 0x0C, 0x02, 0x7F },   // M
107
  { 0x7F, 0x04, 0x08, 0x10, 0x7F },   // N
108
  { 0x3E, 0x41, 0x41, 0x41, 0x3E },   // O
109
  { 0x7F, 0x09, 0x09, 0x09, 0x06 },   // P
110
  { 0x3E, 0x41, 0x51, 0x21, 0x5E },   // Q
111
  { 0x7F, 0x09, 0x19, 0x29, 0x46 },   // R
112
  { 0x46, 0x49, 0x49, 0x49, 0x31 },   // S
113
  { 0x01, 0x01, 0x7F, 0x01, 0x01 },   // T
114
  { 0x3F, 0x40, 0x40, 0x40, 0x3F },   // U
115
  { 0x1F, 0x20, 0x40, 0x20, 0x1F },   // V
116
  { 0x3F, 0x40, 0x38, 0x40, 0x3F },   // W
117
  { 0x63, 0x14, 0x08, 0x14, 0x63 },   // X
118
  { 0x07, 0x08, 0x70, 0x08, 0x07 },   // Y
119
  { 0x61, 0x51, 0x49, 0x45, 0x43 },   // Z
120
  { 0x00, 0x7F, 0x41, 0x41, 0x00 },   // [
121
  { 0x02, 0x04, 0x08, 0x10, 0x20 },   // backslash
122
  { 0x00, 0x41, 0x41, 0x7F, 0x00 },   // ]
123
  { 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^
124
  { 0x40, 0x40, 0x40, 0x40, 0x40 },   // _
125
  { 0x00, 0x01, 0x02, 0x04, 0x00 },   // '
126
  { 0x20, 0x54, 0x54, 0x54, 0x78 },   // a
127
  { 0x7F, 0x48, 0x44, 0x44, 0x38 },   // b
128
  { 0x38, 0x44, 0x44, 0x44, 0x20 },   // c
129
  { 0x38, 0x44, 0x44, 0x48, 0x7F },   // d
130
  { 0x38, 0x54, 0x54, 0x54, 0x18 },   // e
131
  { 0x08, 0x7E, 0x09, 0x01, 0x02 },   // f
132
  { 0x0C, 0x52, 0x52, 0x52, 0x3E },   // g
133
  { 0x7F, 0x08, 0x04, 0x04, 0x78 },   // h
134
  { 0x00, 0x44, 0x7D, 0x40, 0x00 },   // i
135
  { 0x20, 0x40, 0x44, 0x3D, 0x00 },   // j
136
  { 0x7F, 0x10, 0x28, 0x44, 0x00 },   // k
137
  { 0x00, 0x41, 0x7F, 0x40, 0x00 },   // l
138
  { 0x7C, 0x04, 0x18, 0x04, 0x78 },   // m
139
  { 0x7C, 0x08, 0x04, 0x04, 0x78 },   // n
140
  { 0x38, 0x44, 0x44, 0x44, 0x38 },   // o
141
  { 0x7C, 0x14, 0x14, 0x14, 0x08 },   // p
142
  { 0x08, 0x14, 0x14, 0x18, 0x7C },   // q
143
  { 0x7C, 0x08, 0x04, 0x04, 0x08 },   // r
144
  { 0x48, 0x54, 0x54, 0x54, 0x20 },   // s
145
  { 0x04, 0x3F, 0x44, 0x40, 0x20 },   // t
146
  { 0x3C, 0x40, 0x40, 0x20, 0x7C },   // u
147
  { 0x1C, 0x20, 0x40, 0x20, 0x1C },   // v
148
  { 0x3C, 0x40, 0x30, 0x40, 0x3C },   // w
149
  { 0x44, 0x28, 0x10, 0x28, 0x44 },   // x
150
  { 0x0C, 0x50, 0x50, 0x50, 0x3C },   // y
151
  { 0x44, 0x64, 0x54, 0x4C, 0x44 },    // z
152
  { 0x00, 0x08, 0x36, 0x41, 0x41 },   // {
153
  { 0x00, 0x00, 0x7F, 0x00, 0x00 },   // |
154
  { 0x41, 0x41, 0x36, 0x08, 0x00 },   // }
155
  { 0x02, 0x01, 0x01, 0x02, 0x01 },   // ~
156
  { 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // del
157 276 emarinel
};
158
159
160
/**
161
 * @defgroup lcd LCD
162
 * @brief Functions for the LCD
163
 * Functions for writing to the LCD.
164
 * All functions may be found in lcd.h.
165
 *
166
 * @{
167
 **/
168
169
/**
170
 * Initializes the LCD. Must be called before any other
171
 * LCD functions.
172
 **/
173
void lcd_init(void)
174
{
175 277 emarinel
  LCDDDR |= (SCE | SDI | SCK);
176
  LCDRESETDDR |= (RST|D_C);
177 276 emarinel
178 277 emarinel
  LCDPORT &= ~( SCE | SDI | SCK);
179
  LCDRESETPORT &=~(D_C);
180 276 emarinel
181 277 emarinel
  SPCR |= 0x50;//0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2
182
  SPSR |= 0x01;       // a continuation of the above
183 276 emarinel
184 277 emarinel
  LCDRESETPORT |= RST;
185
  delay_ms(10);
186
  LCDRESETPORT &= (~RST);
187
  delay_ms(100);
188
  LCDRESETPORT |= RST;
189 276 emarinel
190 277 emarinel
  lcd_putbyte( 0x21 );  // LCD Extended Commands.
191
  lcd_putbyte( 0xC8 );  // Set LCD Vop (Contrast).
192
  lcd_putbyte( 0x06 );  // Set Temp coefficent.
193
  lcd_putbyte( 0x13 );  // LCD bias mode 1:48.
194
  lcd_putbyte( 0x20 );  // LCD Standard Commands, Horizontal addressing mode.
195
  lcd_putbyte( 0x0C );  // LCD in normal mode.
196 276 emarinel
197 277 emarinel
  LCDRESETPORT |= D_C;    //put it in init instead of main
198 276 emarinel
199 277 emarinel
  lcd_clear_screen();
200 276 emarinel
}
201
202
/**
203
 * Clears the LCD screen. lcd_init must be called first.
204
 *
205
 * @see lcd_init
206
 **/
207
void lcd_clear_screen( void ) {
208 277 emarinel
  int i;
209
  for (i = 0; i < 504; i++) {
210
    lcd_putbyte(0x0);
211
  }
212 276 emarinel
213 277 emarinel
  lcd_gotoxy(0,0);
214 276 emarinel
}
215
216
/**
217
 * Prints a character on the LCD screen. lcd_init
218
 * must be called before this function may be used.
219
 *
220
 * @param c the character to print
221
 *
222
 * @see lcd_init
223
 **/
224
void lcd_putc(char c)
225
{
226 277 emarinel
  int i;
227 276 emarinel
228 277 emarinel
  for (i = 0; i < 5; i++) {
229
    lcd_putbyte(FontLookup[c-32][i]);
230
  }
231
232
  lcd_putbyte(0);
233 276 emarinel
}
234
235
/*
236
print an entire string to the lcd
237
*/
238
void lcd_puts(char *s)
239
{
240 277 emarinel
  char *t = s;
241
  while (*t != 0) {
242
    lcd_putc(*t);
243
    t++;
244
  }
245 276 emarinel
}
246
247
/*
248
go to coordinate x, y
249
y: vertically - 1 char
250
x: horizontally - 1 pixel
251

252
multiply x by 6 if want to move 1 entire character
253

254
origin (0,0) is at top left corner of lcd screen
255
*/
256
257
/**
258
 * Move the current cursor position to the one specified.
259
 * lcd_init must be called before this function may be used.
260
 *
261
 * @param x The x coordinate of the new position
262
 * @param y The y coordinate of the new position
263
 *
264
 * @see lcd_init
265
 **/
266
void lcd_gotoxy(int x, int y)
267
{
268 277 emarinel
  LCDRESETPORT &= ~(D_C);
269
  lcd_putbyte(0x40 | (y & 0x07));
270
  lcd_putbyte(0x80 | (x & 0x7f));
271
  LCDRESETPORT |= D_C;
272 276 emarinel
}
273
274
/*
275
prints an int to the lcd
276

277
code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
278
*/
279
280
/**
281
 * Print an integer to the LCD screen.
282
 * lcd_init must be called before this function may be used.
283
 *
284
 * @param value the integer to print
285
 *
286
 * @see lcd_init
287
 **/
288
void lcd_puti(int value )
289
{
290 277 emarinel
  unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10;
291 276 emarinel
292 277 emarinel
  /* convert int to ascii  */
293
  if (value<0) {
294
    lcd_putc('-');
295
    value=-value;
296
  }
297 276 emarinel
298 277 emarinel
  do { position--; *(lcd_data+position)=(value%radix)+'0'; value/=radix;  } while(value);
299 276 emarinel
300 277 emarinel
  /* start displaying the number */
301
  for(; position <= (sizeof(lcd_data)-1); position++) {
302
    lcd_putc(lcd_data[position]);
303
  }
304 276 emarinel
305 277 emarinel
  return;
306 276 emarinel
}
307
308
/** @} **/ //end defgroup
309
310
void lcd_putbyte(unsigned char b)
311
{
312 277 emarinel
  SPDR = b;
313
314
  /* Wait until SPI transaction is complete */
315
  while (!(SPSR & 0x80));
316 276 emarinel
}