Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / testing / robot_routine_reg_test / lcd.c @ 13

History | View | Annotate | Download (7.04 KB)

1
/*
2

3
lcd-ar2.c - implementation for lcd functions
4

5
Author: CMU Robotics Club, Colony Project
6

7
This uses SPI.
8

9
lcd_init MUST be called before anything can be used.
10

11
*/
12

    
13
#include <avr/io.h>
14
#include "lcd.h"
15
#include "time.h"
16

    
17
/*
18
FontLookup - a lookup table for all characters
19
*/
20
static const unsigned char FontLookup [][5] =
21
{
22
    { 0x00, 0x00, 0x00, 0x00, 0x00 },  // sp
23
    { 0x00, 0x00, 0x5f, 0x00, 0x00 },   // !
24
    { 0x00, 0x07, 0x00, 0x07, 0x00 },   // "
25
    { 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // #
26
    { 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $
27
        { 0x23, 0x13, 0x08, 0x64, 0x62 },   // %
28
    { 0x36, 0x49, 0x55, 0x22, 0x50 },   // &
29
    { 0x00, 0x05, 0x03, 0x00, 0x00 },   // '
30
    { 0x00, 0x1c, 0x22, 0x41, 0x00 },   // (
31
    { 0x00, 0x41, 0x22, 0x1c, 0x00 },   // )
32
    { 0x14, 0x08, 0x3E, 0x08, 0x14 },   // *
33
    { 0x08, 0x08, 0x3E, 0x08, 0x08 },   // +
34
    { 0x00, 0x00, 0x50, 0x30, 0x00 },   // ,
35
    { 0x10, 0x10, 0x10, 0x10, 0x10 },   // -
36
    { 0x00, 0x60, 0x60, 0x00, 0x00 },   // .
37
    { 0x20, 0x10, 0x08, 0x04, 0x02 },   // /
38
    { 0x3E, 0x51, 0x49, 0x45, 0x3E },   // 0
39
    { 0x00, 0x42, 0x7F, 0x40, 0x00 },   // 1
40
    { 0x42, 0x61, 0x51, 0x49, 0x46 },   // 2
41
    { 0x21, 0x41, 0x45, 0x4B, 0x31 },   // 3
42
    { 0x18, 0x14, 0x12, 0x7F, 0x10 },   // 4
43
    { 0x27, 0x45, 0x45, 0x45, 0x39 },   // 5
44
    { 0x3C, 0x4A, 0x49, 0x49, 0x30 },   // 6
45
    { 0x01, 0x71, 0x09, 0x05, 0x03 },   // 7
46
    { 0x36, 0x49, 0x49, 0x49, 0x36 },   // 8
47
    { 0x06, 0x49, 0x49, 0x29, 0x1E },   // 9
48
    { 0x00, 0x36, 0x36, 0x00, 0x00 },   // :
49
    { 0x00, 0x56, 0x36, 0x00, 0x00 },   // ;
50
    { 0x08, 0x14, 0x22, 0x41, 0x00 },   // <
51
    { 0x14, 0x14, 0x14, 0x14, 0x14 },   // =
52
    { 0x00, 0x41, 0x22, 0x14, 0x08 },   // >
53
    { 0x02, 0x01, 0x51, 0x09, 0x06 },   // ?
54
    { 0x32, 0x49, 0x59, 0x51, 0x3E },   // @
55
    { 0x7E, 0x11, 0x11, 0x11, 0x7E },   // A
56
    { 0x7F, 0x49, 0x49, 0x49, 0x36 },   // B
57
    { 0x3E, 0x41, 0x41, 0x41, 0x22 },   // C
58
    { 0x7F, 0x41, 0x41, 0x22, 0x1C },   // D
59
    { 0x7F, 0x49, 0x49, 0x49, 0x41 },   // E
60
    { 0x7F, 0x09, 0x09, 0x09, 0x01 },   // F
61
    { 0x3E, 0x41, 0x49, 0x49, 0x7A },   // G
62
    { 0x7F, 0x08, 0x08, 0x08, 0x7F },   // H
63
    { 0x00, 0x41, 0x7F, 0x41, 0x00 },   // I
64
    { 0x20, 0x40, 0x41, 0x3F, 0x01 },   // J
65
    { 0x7F, 0x08, 0x14, 0x22, 0x41 },   // K
66
    { 0x7F, 0x40, 0x40, 0x40, 0x40 },   // L
67
    { 0x7F, 0x02, 0x0C, 0x02, 0x7F },   // M
68
    { 0x7F, 0x04, 0x08, 0x10, 0x7F },   // N
69
    { 0x3E, 0x41, 0x41, 0x41, 0x3E },   // O
70
    { 0x7F, 0x09, 0x09, 0x09, 0x06 },   // P
71
    { 0x3E, 0x41, 0x51, 0x21, 0x5E },   // Q
72
    { 0x7F, 0x09, 0x19, 0x29, 0x46 },   // R
73
    { 0x46, 0x49, 0x49, 0x49, 0x31 },   // S
74
    { 0x01, 0x01, 0x7F, 0x01, 0x01 },   // T
75
    { 0x3F, 0x40, 0x40, 0x40, 0x3F },   // U
76
    { 0x1F, 0x20, 0x40, 0x20, 0x1F },   // V
77
    { 0x3F, 0x40, 0x38, 0x40, 0x3F },   // W
78
    { 0x63, 0x14, 0x08, 0x14, 0x63 },   // X
79
    { 0x07, 0x08, 0x70, 0x08, 0x07 },   // Y
80
    { 0x61, 0x51, 0x49, 0x45, 0x43 },   // Z
81
    { 0x00, 0x7F, 0x41, 0x41, 0x00 },   // [
82
    { 0x02, 0x04, 0x08, 0x10, 0x20 },   // backslash
83
    { 0x00, 0x41, 0x41, 0x7F, 0x00 },   // ]
84
    { 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^
85
    { 0x40, 0x40, 0x40, 0x40, 0x40 },   // _
86
    { 0x00, 0x01, 0x02, 0x04, 0x00 },   // '
87
    { 0x20, 0x54, 0x54, 0x54, 0x78 },   // a
88
    { 0x7F, 0x48, 0x44, 0x44, 0x38 },   // b
89
    { 0x38, 0x44, 0x44, 0x44, 0x20 },   // c
90
    { 0x38, 0x44, 0x44, 0x48, 0x7F },   // d
91
    { 0x38, 0x54, 0x54, 0x54, 0x18 },   // e
92
    { 0x08, 0x7E, 0x09, 0x01, 0x02 },   // f
93
    { 0x0C, 0x52, 0x52, 0x52, 0x3E },   // g
94
    { 0x7F, 0x08, 0x04, 0x04, 0x78 },   // h
95
    { 0x00, 0x44, 0x7D, 0x40, 0x00 },   // i
96
    { 0x20, 0x40, 0x44, 0x3D, 0x00 },   // j
97
    { 0x7F, 0x10, 0x28, 0x44, 0x00 },   // k
98
    { 0x00, 0x41, 0x7F, 0x40, 0x00 },   // l
99
    { 0x7C, 0x04, 0x18, 0x04, 0x78 },   // m
100
    { 0x7C, 0x08, 0x04, 0x04, 0x78 },   // n
101
    { 0x38, 0x44, 0x44, 0x44, 0x38 },   // o
102
    { 0x7C, 0x14, 0x14, 0x14, 0x08 },   // p
103
    { 0x08, 0x14, 0x14, 0x18, 0x7C },   // q
104
    { 0x7C, 0x08, 0x04, 0x04, 0x08 },   // r
105
    { 0x48, 0x54, 0x54, 0x54, 0x20 },   // s
106
    { 0x04, 0x3F, 0x44, 0x40, 0x20 },   // t
107
    { 0x3C, 0x40, 0x40, 0x20, 0x7C },   // u
108
    { 0x1C, 0x20, 0x40, 0x20, 0x1C },   // v
109
    { 0x3C, 0x40, 0x30, 0x40, 0x3C },   // w
110
    { 0x44, 0x28, 0x10, 0x28, 0x44 },   // x
111
    { 0x0C, 0x50, 0x50, 0x50, 0x3C },   // y
112
    { 0x44, 0x64, 0x54, 0x4C, 0x44 },    // z
113
    { 0x00, 0x08, 0x36, 0x41, 0x41 },   // {
114
    { 0x00, 0x00, 0x7F, 0x00, 0x00 },   // |
115
    { 0x41, 0x41, 0x36, 0x08, 0x00 },   // }
116
    { 0x02, 0x01, 0x01, 0x02, 0x01 },   // ~
117
    { 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // del
118
};
119

    
120

    
121
/*
122
initializes the LCD
123
*/
124
void lcd_init(void)
125
{
126
        LCDDDR |= (D_C | SCE | SDI | SCK);
127
        LCDRESETDDR |= RST;
128

    
129
        LCDPORT &= ~(D_C | SCE | SDI | SCK);
130
        
131
        SPCR |= 0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2
132
        SPSR |= 0x01;       // a continuation of the above
133

    
134
        LCDRESETPORT |= RST;
135
        delay_ms(10);
136
        LCDRESETPORT &= (~RST);
137
        delay_ms(100);
138
        LCDRESETPORT |= RST;
139
        
140
    lcd_putbyte( 0x21 );  // LCD Extended Commands.
141
    lcd_putbyte( 0xC8 );  // Set LCD Vop (Contrast).
142
    lcd_putbyte( 0x06 );  // Set Temp coefficent.
143
    lcd_putbyte( 0x13 );  // LCD bias mode 1:48.
144
    lcd_putbyte( 0x20 );  // LCD Standard Commands, Horizontal addressing mode.
145
    lcd_putbyte( 0x0C );  // LCD in normal mode.
146
        
147
        LCDPORT |= D_C;                //put it in init instead of main
148
        
149
        lcd_clear_screen();
150
        
151
}
152

    
153
/*
154
clear the lcd screen
155
*/
156
void lcd_clear_screen( void ) {
157
        int i;
158
        for (i = 0; i < 504; i++)
159
                        lcd_putbyte(0x0);
160
        
161
        lcd_gotoxy(0,0);
162

    
163
}
164

    
165

    
166
/*
167
print a byte on the lcd screen
168
*/
169
void lcd_putbyte(unsigned char b)
170
{
171
        SPDR = b;
172
        while (!(SPSR & 0x80)); /* Wait until SPI transaction is complete */
173
}
174

    
175
/*
176
print a character on the lcd
177
*/
178
void lcd_putchar(char c)
179
{
180
        int i;
181
        
182
        for (i = 0; i < 5; i++)
183
                lcd_putbyte(FontLookup[c-32][i]);
184
        lcd_putbyte(0);
185

    
186
}
187

    
188
/*
189
print an entire string to the lcd
190
*/
191
void lcd_putstr(char *s)
192
{
193
        char *t = s;
194
        while (*t != 0)
195
        {
196
                lcd_putchar(*t);
197
                t++;
198
        }
199
}
200

    
201
/*
202
go to coordinate x, y
203
y: vertically - 1 char
204
x: horizontally - 1 pixel
205

206
multiply x by 6 if want to move 1 entire character
207

208
origin (0,0) is at top left corner of lcd screen
209
*/
210
void lcd_gotoxy(int x, int y)
211
{
212
  LCDPORT &= ~(D_C);
213
  lcd_putbyte(0x40 | (y & 0x07));
214
  lcd_putbyte(0x80 | (x & 0x7f));
215
  LCDPORT |= D_C;
216
}
217

    
218
/*
219
prints an int to the lcd
220

221
code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
222
*/
223
void lcd_putint(int value ) {
224
        unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10; 
225

    
226
        /* convert int to ascii  */ 
227
        if(value<0) { lcd_putchar('-'); value=-value; }    
228
        do { position--; *(lcd_data+position)=(value%radix)+'0'; value/=radix;  } while(value); 
229

    
230
    
231
        /* start displaying the number */
232
        for(;position<=(sizeof(lcd_data)-1);position++)
233
          {
234
            
235
            lcd_putchar(lcd_data[position]);
236
          }
237

    
238
return;
239
}