Revision 322
Copied new analog code into recharging branch.
lcd.c | ||
---|---|---|
1 |
/* |
|
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 |
**/ |
|
2 | 25 |
|
3 |
lcd-ar2.c - implementation for lcd functions |
|
4 | 26 |
|
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 |
|
|
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 |
**/ |
|
13 | 35 |
#include <avr/io.h> |
14 | 36 |
#include <lcd.h> |
15 | 37 |
#include <time.h> |
16 | 38 |
|
17 |
#define LCD_RS PB4 // Command/Data |
|
18 |
#define LCD_RSTB PE2 // reset line |
|
19 |
#define LCD_CS PB0 |
|
20 |
|
|
21 |
//////lcd defines |
|
39 |
//LCD defines |
|
22 | 40 |
#define RST _BV(4) // pd4 (GPIO) |
23 | 41 |
#define SCE _BV(0) // pb0 (~SS) |
24 | 42 |
#define D_C _BV(5) // pd5 (GPIO?) |
... | ... | |
31 | 49 |
#define LCDRESETPORT PORTD |
32 | 50 |
#define LCDRESETDDR DDRD |
33 | 51 |
|
34 |
// Internal Function Prototypes |
|
35 |
// inialize OLED and SPI |
|
36 |
void OLED_init(void); |
|
37 |
|
|
38 |
// reset Controller |
|
39 |
void Reset_SSD1339(void); |
|
40 |
|
|
41 |
// write command or data |
|
42 |
void write_c(unsigned char out_command); |
|
43 |
void write_d(unsigned char out_data); |
|
44 |
|
|
45 |
// these write data to the OLED on 8 bit data bus, depends on MCU |
|
46 |
void LCD_out(unsigned char cmd); |
|
47 |
|
|
48 |
// these functions set / clear pins for OLED control lines. they accecpt a 0 or 1 |
|
49 |
void DC(char stat); |
|
50 |
void RES(char stat); |
|
51 |
void CS(char stat); |
|
52 |
|
|
53 | 52 |
void lcd_putbyte(unsigned char b); |
54 | 53 |
|
55 |
void crazycircle (void) { |
|
56 |
/* int i; |
|
57 |
// draw 100 random circles |
|
58 |
for(i = 0;i < 100;i++){ |
|
59 |
write_c(0x86); // draw circle command |
|
60 |
write_d(rand() % 130); |
|
61 |
write_d(rand() % 130); |
|
62 |
write_d(rand() % 64); |
|
63 |
write_d(rand()); |
|
64 |
write_d(rand()); |
|
65 |
write_d(rand()); |
|
66 |
write_d(rand()); |
|
67 | 54 |
|
68 |
delay_ms(10); |
|
69 |
} |
|
70 |
*/ |
|
71 |
} |
|
72 |
|
|
73 |
void OLEDtest (void) { |
|
74 |
//int i = 0; |
|
75 |
|
|
76 |
// Initialize |
|
77 |
//Initialize(); |
|
78 |
OLED_init(); |
|
79 |
|
|
80 |
delay_ms(120); |
|
81 |
|
|
82 |
write_c(0x8e); // clear window command |
|
83 |
write_d(0); |
|
84 |
write_d(0); |
|
85 |
write_d(130); |
|
86 |
write_d(130); |
|
87 |
|
|
88 |
delay_ms(100); |
|
89 |
|
|
90 |
write_c(0x92); // fill enable command |
|
91 |
write_d(0x01); |
|
92 |
|
|
93 |
delay_ms(10); |
|
94 |
|
|
95 |
crazycircle(); |
|
96 |
/* |
|
97 |
// write directly to ram, this fills up bottom 1/3 of display with color pattern |
|
98 |
write_c(0x5c); |
|
99 |
for (i = 0; i < 2000; i++){ |
|
100 |
write_c(0x5c); |
|
101 |
write_d(i); |
|
102 |
write_d(i); |
|
103 |
write_d(i); |
|
104 |
} |
|
105 |
*/ |
|
106 |
} |
|
107 |
|
|
108 |
/********************************************************** |
|
109 |
Initialize |
|
110 |
**********************************************************/ |
|
111 |
|
|
112 |
void OLED_init(void) |
|
113 |
{ |
|
114 |
// Setup SPI here |
|
115 |
SPCR = 0x5D; // enable SPI, master, SPI mode 3 |
|
116 |
DDRB |= 0x06; // enable MOSI and SCK as outputs |
|
117 |
|
|
118 |
LCD_out(0); |
|
119 |
DC(0); |
|
120 |
CS(0); |
|
121 |
Reset_SSD1339(); |
|
122 |
write_c(0xa0); // Set Re-map / Color Depth |
|
123 |
write_d(0x34);//0xb4); // 262K 8bit R->G->B |
|
124 |
write_c(0xa1); // Set display start line |
|
125 |
write_d(0x00); // 00h start |
|
126 |
//write_c(0xa2); // Set display offset |
|
127 |
//write_d(0x80); // 80h start |
|
128 |
write_c(0xA6); // Normal display |
|
129 |
write_c(0xad); // Set Master Configuration |
|
130 |
write_d(0x8e); // DC-DC off & external VcomH voltage & external pre-charge voltage |
|
131 |
write_c(0xb0); // Power saving mode |
|
132 |
write_d(0x05); |
|
133 |
write_c(0xb1); // Set pre & dis_charge |
|
134 |
write_d(0x11); // pre=1h dis=1h |
|
135 |
write_c(0xb3); // clock & frequency |
|
136 |
write_d(0xf0); // clock=Divser+1 frequency=fh |
|
137 |
write_c(0xbb); // Set pre-charge voltage of color A B C |
|
138 |
write_d(0xff); // color A was 1c |
|
139 |
write_d(0xff); // color B was 1c |
|
140 |
write_d(0xff); // color C was 1c |
|
141 |
write_c(0xbe); // Set VcomH |
|
142 |
write_d(0x1f); // |
|
143 |
write_c(0xc1); // Set contrast current for A B C |
|
144 |
write_d(0xCa); // Color A was AA |
|
145 |
write_d(0xD4); // Color B was B4 |
|
146 |
write_d(0xF8); // Color C was C8 |
|
147 |
write_c(0xc7); // Set master contrast |
|
148 |
write_d(0x0f); // no change |
|
149 |
write_c(0xca); // Duty |
|
150 |
write_d(0x7f); // 127+1 |
|
151 |
write_c(0xaf); // Display on |
|
152 |
} |
|
153 |
|
|
154 |
void Reset_SSD1339(void) |
|
155 |
{ |
|
156 |
RES(0); |
|
157 |
delay_ms(100); |
|
158 |
RES(1); |
|
159 |
} |
|
160 |
void write_c(unsigned char out_command) |
|
161 |
{ |
|
162 |
DC(0);CS(0); |
|
163 |
//delay_ms(1); |
|
164 |
|
|
165 |
LCD_out(out_command); |
|
166 |
// delay_ms(1); |
|
167 |
|
|
168 |
DC(1); |
|
169 |
// potentially add NOP |
|
170 |
CS(1); |
|
171 |
|
|
172 |
} |
|
173 |
|
|
174 |
void write_d(unsigned char out_data) |
|
175 |
{ |
|
176 |
DC(1);CS(0); |
|
177 |
// delay_ms(1); |
|
178 |
|
|
179 |
LCD_out(out_data); |
|
180 |
// delay_ms(1); |
|
181 |
DC(1); |
|
182 |
//potentially add NOP |
|
183 |
CS(1); |
|
184 |
} |
|
185 |
|
|
186 |
// these functions set / clear pins for LCD control lines. they accecpt a 0 or 1 |
|
187 |
void DC(char stat) |
|
188 |
{ |
|
189 |
DDRB |= _BV(LCD_RS); // RS is P0.30, set to output |
|
190 |
if (stat) |
|
191 |
PORTB |= _BV(LCD_RS); |
|
192 |
else |
|
193 |
PORTB &= ~(_BV(LCD_RS)); |
|
194 |
} |
|
195 |
|
|
196 |
void RES(char stat) |
|
197 |
{ |
|
198 |
DDRE |= _BV(LCD_RSTB); // RSTB is P0.7, set to output |
|
199 |
if (stat) |
|
200 |
PORTE |= _BV(LCD_RSTB); |
|
201 |
else |
|
202 |
PORTE &= ~(_BV(LCD_RSTB)); |
|
203 |
} |
|
204 |
|
|
205 |
void CS(char stat) |
|
206 |
{ |
|
207 |
DDRB |= _BV(LCD_CS); // RSTB is P0.7, set to output |
|
208 |
if (stat) |
|
209 |
PORTB |= _BV(LCD_CS); |
|
210 |
else |
|
211 |
PORTB &= ~(_BV(LCD_CS)); |
|
212 |
} |
|
213 |
|
|
214 |
// send to the LCD |
|
215 |
void LCD_out(unsigned char cmd) |
|
216 |
{ |
|
217 |
SPDR = cmd; |
|
218 |
// could also do this via interrupts |
|
219 |
loop_until_bit_is_set(SPSR, SPIF); |
|
220 |
} |
|
221 |
|
|
222 |
|
|
223 | 55 |
//**************************************Old shit below!***************** |
224 | 56 |
/* |
225 | 57 |
FontLookup - a lookup table for all characters |
... | ... | |
355 | 187 |
delay_ms(100); |
356 | 188 |
LCDRESETPORT |= RST; |
357 | 189 |
|
358 |
lcd_putbyte( 0x21 ); // LCD Extended Commands.
|
|
359 |
lcd_putbyte( 0xC8 ); // Set LCD Vop (Contrast).
|
|
360 |
lcd_putbyte( 0x06 ); // Set Temp coefficent.
|
|
361 |
lcd_putbyte( 0x13 ); // LCD bias mode 1:48.
|
|
362 |
lcd_putbyte( 0x20 ); // LCD Standard Commands, Horizontal addressing mode.
|
|
363 |
lcd_putbyte( 0x0C ); // LCD in normal mode.
|
|
190 |
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.
|
|
364 | 196 |
|
365 | 197 |
LCDRESETPORT |= D_C; //put it in init instead of main |
366 | 198 |
|
... | ... | |
375 | 207 |
void lcd_clear_screen( void ) { |
376 | 208 |
int i; |
377 | 209 |
for (i = 0; i < 504; i++) |
378 |
lcd_putbyte(0x0);
|
|
210 |
lcd_putbyte(0x0); |
|
379 | 211 |
|
380 | 212 |
lcd_gotoxy(0,0); |
381 | 213 |
} |
... | ... | |
419 | 251 |
|
420 | 252 |
origin (0,0) is at top left corner of lcd screen |
421 | 253 |
*/ |
254 |
|
|
422 | 255 |
/** |
423 | 256 |
* Move the current cursor position to the one specified. |
424 | 257 |
* lcd_init must be called before this function may be used. |
... | ... | |
430 | 263 |
**/ |
431 | 264 |
void lcd_gotoxy(int x, int y) |
432 | 265 |
{ |
433 |
LCDRESETPORT &= ~(D_C);
|
|
434 |
lcd_putbyte(0x40 | (y & 0x07));
|
|
435 |
lcd_putbyte(0x80 | (x & 0x7f));
|
|
436 |
LCDRESETPORT |= D_C;
|
|
266 |
LCDRESETPORT &= ~(D_C);
|
|
267 |
lcd_putbyte(0x40 | (y & 0x07));
|
|
268 |
lcd_putbyte(0x80 | (x & 0x7f));
|
|
269 |
LCDRESETPORT |= D_C;
|
|
437 | 270 |
} |
438 | 271 |
|
439 | 272 |
/* |
... | ... | |
441 | 274 |
|
442 | 275 |
code adapted from Chris Efstathiou's code (hendrix@otenet.gr) |
443 | 276 |
*/ |
277 |
|
|
444 | 278 |
/** |
445 | 279 |
* Print an integer to the LCD screen. |
446 | 280 |
* lcd_init must be called before this function may be used. |
... | ... | |
449 | 283 |
* |
450 | 284 |
* @see lcd_init |
451 | 285 |
**/ |
452 |
void lcd_puti(int value ) { |
|
286 |
void lcd_puti(int value ) |
|
287 |
{ |
|
453 | 288 |
unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10; |
454 | 289 |
|
455 | 290 |
/* convert int to ascii */ |
... | ... | |
459 | 294 |
|
460 | 295 |
/* start displaying the number */ |
461 | 296 |
for(;position<=(sizeof(lcd_data)-1);position++) |
462 |
{ |
|
463 |
|
|
464 |
lcd_putc(lcd_data[position]); |
|
465 |
} |
|
297 |
lcd_putc(lcd_data[position]); |
|
466 | 298 |
|
467 | 299 |
return; |
468 | 300 |
} |
Also available in: Unified diff