Project

General

Profile

Statistics
| Revision:

root / demos / hunter_prey / lib / src / libdragonfly / lcd.c @ 1828

History | View | Annotate | Download (9.48 KB)

1 1828 emullini
/**
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 <avr/pgmspace.h>
37
38
#include "lcd.h"
39
#include "time.h"
40
41
//LCD defines
42
#define RST _BV(4)  // pd4 (GPIO)
43
#define SCE _BV(0)  // pb0 (~SS)
44
#define D_C _BV(5)  // pd5 (GPIO?)
45
#define SDI _BV(2)  // pb2 (MOSI)
46
#define SCK _BV(1) // pb1 (SCK)
47
48
#define LCDPORT PORTB
49
#define LCDDDR DDRB
50
51
#define LCDRESETPORT PORTD
52
#define LCDRESETDDR DDRD
53
54
void lcd_putbyte(unsigned char b);
55
56
57
//**************************************Old shit below!*****************
58
/*
59
  FontLookup - a lookup table for all characters
60
*/
61
static const unsigned char FontLookup [][5] PROGMEM =
62
  {
63
    { 0x00, 0x00, 0x00, 0x00, 0x00 },  // sp
64
    { 0x00, 0x00, 0x5f, 0x00, 0x00 },   // !
65
    { 0x00, 0x07, 0x00, 0x07, 0x00 },   // "
66
    { 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // #
67
    { 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $
68
    { 0x23, 0x13, 0x08, 0x64, 0x62 },   // %
69
    { 0x36, 0x49, 0x55, 0x22, 0x50 },   // &
70
    { 0x00, 0x05, 0x03, 0x00, 0x00 },   // '
71
    { 0x00, 0x1c, 0x22, 0x41, 0x00 },   // (
72
    { 0x00, 0x41, 0x22, 0x1c, 0x00 },   // )
73
    { 0x14, 0x08, 0x3E, 0x08, 0x14 },   // *
74
    { 0x08, 0x08, 0x3E, 0x08, 0x08 },   // +
75
    { 0x00, 0x00, 0x50, 0x30, 0x00 },   // ,
76
    { 0x10, 0x10, 0x10, 0x10, 0x10 },   // -
77
    { 0x00, 0x60, 0x60, 0x00, 0x00 },   // .
78
    { 0x20, 0x10, 0x08, 0x04, 0x02 },   // /
79
    { 0x3E, 0x51, 0x49, 0x45, 0x3E },   // 0
80
    { 0x00, 0x42, 0x7F, 0x40, 0x00 },   // 1
81
    { 0x42, 0x61, 0x51, 0x49, 0x46 },   // 2
82
    { 0x21, 0x41, 0x45, 0x4B, 0x31 },   // 3
83
    { 0x18, 0x14, 0x12, 0x7F, 0x10 },   // 4
84
    { 0x27, 0x45, 0x45, 0x45, 0x39 },   // 5
85
    { 0x3C, 0x4A, 0x49, 0x49, 0x30 },   // 6
86
    { 0x01, 0x71, 0x09, 0x05, 0x03 },   // 7
87
    { 0x36, 0x49, 0x49, 0x49, 0x36 },   // 8
88
    { 0x06, 0x49, 0x49, 0x29, 0x1E },   // 9
89
    { 0x00, 0x36, 0x36, 0x00, 0x00 },   // :
90
    { 0x00, 0x56, 0x36, 0x00, 0x00 },   // ;
91
    { 0x08, 0x14, 0x22, 0x41, 0x00 },   // <
92
    { 0x14, 0x14, 0x14, 0x14, 0x14 },   // =
93
    { 0x00, 0x41, 0x22, 0x14, 0x08 },   // >
94
    { 0x02, 0x01, 0x51, 0x09, 0x06 },   // ?
95
    { 0x32, 0x49, 0x59, 0x51, 0x3E },   // @
96
    { 0x7E, 0x11, 0x11, 0x11, 0x7E },   // A
97
    { 0x7F, 0x49, 0x49, 0x49, 0x36 },   // B
98
    { 0x3E, 0x41, 0x41, 0x41, 0x22 },   // C
99
    { 0x7F, 0x41, 0x41, 0x22, 0x1C },   // D
100
    { 0x7F, 0x49, 0x49, 0x49, 0x41 },   // E
101
    { 0x7F, 0x09, 0x09, 0x09, 0x01 },   // F
102
    { 0x3E, 0x41, 0x49, 0x49, 0x7A },   // G
103
    { 0x7F, 0x08, 0x08, 0x08, 0x7F },   // H
104
    { 0x00, 0x41, 0x7F, 0x41, 0x00 },   // I
105
    { 0x20, 0x40, 0x41, 0x3F, 0x01 },   // J
106
    { 0x7F, 0x08, 0x14, 0x22, 0x41 },   // K
107
    { 0x7F, 0x40, 0x40, 0x40, 0x40 },   // L
108
    { 0x7F, 0x02, 0x0C, 0x02, 0x7F },   // M
109
    { 0x7F, 0x04, 0x08, 0x10, 0x7F },   // N
110
    { 0x3E, 0x41, 0x41, 0x41, 0x3E },   // O
111
    { 0x7F, 0x09, 0x09, 0x09, 0x06 },   // P
112
    { 0x3E, 0x41, 0x51, 0x21, 0x5E },   // Q
113
    { 0x7F, 0x09, 0x19, 0x29, 0x46 },   // R
114
    { 0x46, 0x49, 0x49, 0x49, 0x31 },   // S
115
    { 0x01, 0x01, 0x7F, 0x01, 0x01 },   // T
116
    { 0x3F, 0x40, 0x40, 0x40, 0x3F },   // U
117
    { 0x1F, 0x20, 0x40, 0x20, 0x1F },   // V
118
    { 0x3F, 0x40, 0x38, 0x40, 0x3F },   // W
119
    { 0x63, 0x14, 0x08, 0x14, 0x63 },   // X
120
    { 0x07, 0x08, 0x70, 0x08, 0x07 },   // Y
121
    { 0x61, 0x51, 0x49, 0x45, 0x43 },   // Z
122
    { 0x00, 0x7F, 0x41, 0x41, 0x00 },   // [
123
    { 0x02, 0x04, 0x08, 0x10, 0x20 },   // backslash
124
    { 0x00, 0x41, 0x41, 0x7F, 0x00 },   // ]
125
    { 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^
126
    { 0x40, 0x40, 0x40, 0x40, 0x40 },   // _
127
    { 0x00, 0x01, 0x02, 0x04, 0x00 },   // '
128
    { 0x20, 0x54, 0x54, 0x54, 0x78 },   // a
129
    { 0x7F, 0x48, 0x44, 0x44, 0x38 },   // b
130
    { 0x38, 0x44, 0x44, 0x44, 0x20 },   // c
131
    { 0x38, 0x44, 0x44, 0x48, 0x7F },   // d
132
    { 0x38, 0x54, 0x54, 0x54, 0x18 },   // e
133
    { 0x08, 0x7E, 0x09, 0x01, 0x02 },   // f
134
    { 0x0C, 0x52, 0x52, 0x52, 0x3E },   // g
135
    { 0x7F, 0x08, 0x04, 0x04, 0x78 },   // h
136
    { 0x00, 0x44, 0x7D, 0x40, 0x00 },   // i
137
    { 0x20, 0x40, 0x44, 0x3D, 0x00 },   // j
138
    { 0x7F, 0x10, 0x28, 0x44, 0x00 },   // k
139
    { 0x00, 0x41, 0x7F, 0x40, 0x00 },   // l
140
    { 0x7C, 0x04, 0x18, 0x04, 0x78 },   // m
141
    { 0x7C, 0x08, 0x04, 0x04, 0x78 },   // n
142
    { 0x38, 0x44, 0x44, 0x44, 0x38 },   // o
143
    { 0x7C, 0x14, 0x14, 0x14, 0x08 },   // p
144
    { 0x08, 0x14, 0x14, 0x18, 0x7C },   // q
145
    { 0x7C, 0x08, 0x04, 0x04, 0x08 },   // r
146
    { 0x48, 0x54, 0x54, 0x54, 0x20 },   // s
147
    { 0x04, 0x3F, 0x44, 0x40, 0x20 },   // t
148
    { 0x3C, 0x40, 0x40, 0x20, 0x7C },   // u
149
    { 0x1C, 0x20, 0x40, 0x20, 0x1C },   // v
150
    { 0x3C, 0x40, 0x30, 0x40, 0x3C },   // w
151
    { 0x44, 0x28, 0x10, 0x28, 0x44 },   // x
152
    { 0x0C, 0x50, 0x50, 0x50, 0x3C },   // y
153
    { 0x44, 0x64, 0x54, 0x4C, 0x44 },    // z
154
    { 0x00, 0x08, 0x36, 0x41, 0x41 },   // {
155
    { 0x00, 0x00, 0x7F, 0x00, 0x00 },   // |
156
    { 0x41, 0x41, 0x36, 0x08, 0x00 },   // }
157
    { 0x02, 0x01, 0x01, 0x02, 0x01 },   // ~
158
    { 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // del
159
  };
160
161
162
/**
163
 * @defgroup lcd LCD
164
 * @brief Functions for the LCD
165
 * Functions for writing to the LCD.
166
 * All functions may be found in lcd.h.
167
 *
168
 * @{
169
 **/
170
171
/**
172
 * Initializes the LCD. Must be called before any other
173
 * LCD functions.
174
 **/
175
void lcd_init(void) {
176
  LCDDDR |= (SCE | SDI | SCK);
177
  LCDRESETDDR |= (RST|D_C);
178
179
  LCDPORT &= ~( SCE | SDI | SCK);
180
  LCDRESETPORT &=~(D_C);
181
182
  SPCR |= 0x50;//0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2
183
  SPSR |= 0x01;       // a continuation of the above
184
185
  LCDRESETPORT |= RST;
186
  delay_ms(10);
187
  LCDRESETPORT &= (~RST);
188
  delay_ms(100);
189
  LCDRESETPORT |= RST;
190
191
  lcd_putbyte( 0x21 );  // LCD Extended Commands.
192
  lcd_putbyte( 0xC8 );  // Set LCD Vop (Contrast).
193
  lcd_putbyte( 0x06 );  // Set Temp coefficent.
194
  lcd_putbyte( 0x13 );  // LCD bias mode 1:48.
195
  lcd_putbyte( 0x20 );  // LCD Standard Commands, Horizontal addressing mode.
196
  lcd_putbyte( 0x0C );  // LCD in normal mode.
197
198
  LCDRESETPORT |= D_C;                //put it in init instead of main
199
200
  lcd_clear_screen();
201
}
202
203
/**
204
 * Clears the LCD screen. lcd_init must be called first.
205
 *
206
 * @see lcd_init
207
 **/
208
void lcd_clear_screen( void ) {
209
  int i;
210
  for (i = 0; i < 504; i++)
211
    lcd_putbyte(0x0);
212
213
  lcd_gotoxy(0,0);
214
}
215
216
/**
217
 * Prints a character on the LCD screen. lcd_init
218
 * must be called before this function may be used.
219
 *
220
 * Note: pgm_read_byte() is untested
221
 *
222
 * @param c the character to print
223
 *
224
 * @see lcd_init
225
 **/
226
void lcd_putc(char c) {
227
  int i;
228
229
  for (i = 0; i < 5; i++)
230
    lcd_putbyte(pgm_read_byte(&(FontLookup[c-32][i])));
231
  lcd_putbyte(0);
232
}
233
234
/*
235
  print an entire string to the lcd
236
*/
237
void lcd_puts(char *s) {
238
  char *t = s;
239
  while (*t != 0) {
240
    lcd_putc(*t);
241
    t++;
242
  }
243
}
244
245
/*
246
  go to coordinate x, y
247
  y: vertically - 1 char
248
  x: horizontally - 1 pixel
249

250
  multiply x by 6 if want to move 1 entire character
251

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

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