Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / libdragonfly / lcd.c @ 1076

History | View | Annotate | Download (8.38 KB)

1
/**
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
    { 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
  };
158

    
159

    
160
void lcd_init(void) {
161
  LCDDDR |= (SCE | SDI | SCK);
162
  LCDRESETDDR |= (RST|D_C);
163

    
164
  LCDPORT &= ~( SCE | SDI | SCK);
165
  LCDRESETPORT &=~(D_C);
166
  
167
  SPCR |= 0x50;//0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2
168
  SPSR |= 0x01;       // a continuation of the above
169

    
170
  LCDRESETPORT |= RST;
171
  delay_ms(10);
172
  LCDRESETPORT &= (~RST);
173
  delay_ms(100);
174
  LCDRESETPORT |= RST;
175
        
176
  lcd_putbyte( 0x21 );  // LCD Extended Commands.
177
  lcd_putbyte( 0xC8 );  // Set LCD Vop (Contrast).
178
  lcd_putbyte( 0x06 );  // Set Temp coefficent.
179
  lcd_putbyte( 0x13 );  // LCD bias mode 1:48.
180
  lcd_putbyte( 0x20 );  // LCD Standard Commands, Horizontal addressing mode.
181
  lcd_putbyte( 0x0C );  // LCD in normal mode.
182
        
183
  LCDRESETPORT |= D_C;                //put it in init instead of main
184
        
185
  lcd_clear_screen();
186
}
187

    
188
void lcd_clear_screen( void ) {
189
  int i;
190
  for (i = 0; i < 504; i++)
191
    lcd_putbyte(0x0);
192
        
193
  lcd_gotoxy(0,0);
194
}
195

    
196
void lcd_putc(char c) {
197
  int i;
198
        
199
  for (i = 0; i < 5; i++)
200
    lcd_putbyte(FontLookup[c-32][i]);
201
  lcd_putbyte(0);
202
}
203

    
204
void lcd_puts(char *s) {
205
  char *t = s;
206
  while (*t != 0) {
207
    lcd_putc(*t);
208
    t++;
209
  }
210
}
211

    
212
/*
213
  go to coordinate x, y
214
  y: vertically - 1 char
215
  x: horizontally - 1 pixel
216

217
  multiply x by 6 if want to move 1 entire character
218

219
  origin (0,0) is at top left corner of lcd screen
220
*/
221

    
222
void lcd_gotoxy(int x, int y) {
223
  LCDRESETPORT &= ~(D_C);
224
  lcd_putbyte(0x40 | (y & 0x07));
225
  lcd_putbyte(0x80 | (x & 0x7f));
226
  LCDRESETPORT |= D_C;
227
}
228

    
229
/*
230
  prints an int to the lcd
231

232
  code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
233
*/
234

    
235
void lcd_puti(int value ) {
236
  unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10; 
237

    
238
  /* convert int to ascii  */ 
239
  if(value<0) { 
240
    lcd_putc('-'); 
241
    value=-value; 
242
  }    
243
  do { 
244
    position--; 
245
    *(lcd_data+position)=(value%radix)+'0'; 
246
    value/=radix;  
247
  } while(value); 
248

    
249
    
250
  /* start displaying the number */
251
  for(;position<=(sizeof(lcd_data)-1);position++)
252
    lcd_putc(lcd_data[position]);
253

    
254
  return;
255
}
256

    
257
void lcd_putbyte(unsigned char b) {
258
  SPDR = b;
259
  while (!(SPSR & 0x80)); /* Wait until SPI transaction is complete */
260
}
261