Project

General

Profile

Revision 1345

Added by Rich Hong over 11 years ago

Final spline code for master/slave

updated outdated libdragonfly and libwireless

View differences:

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
FontLookup - a lookup table for all characters
57
  FontLookup - a lookup table for all characters
226 58
*/
227 59
static const unsigned char FontLookup [][5] =
228
{
60
  {
229 61
    { 0x00, 0x00, 0x00, 0x00, 0x00 },  // sp
230 62
    { 0x00, 0x00, 0x5f, 0x00, 0x00 },   // !
231 63
    { 0x00, 0x07, 0x00, 0x07, 0x00 },   // "
......
322 154
    { 0x41, 0x41, 0x36, 0x08, 0x00 },   // }
323 155
    { 0x02, 0x01, 0x01, 0x02, 0x01 },   // ~
324 156
    { 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // del
325
};
157
  };
326 158

  
327 159

  
328 160
/**
......
338 170
 * Initializes the LCD. Must be called before any other
339 171
 * LCD functions.
340 172
 **/
341
void lcd_init(void)
342
{
343
	LCDDDR |= (SCE | SDI | SCK);
344
	LCDRESETDDR |= (RST|D_C);
173
void lcd_init(void) {
174
  LCDDDR |= (SCE | SDI | SCK);
175
  LCDRESETDDR |= (RST|D_C);
345 176

  
346
	LCDPORT &= ~( SCE | SDI | SCK);
347
	LCDRESETPORT &=~(D_C);
177
  LCDPORT &= ~( SCE | SDI | SCK);
178
  LCDRESETPORT &=~(D_C);
348 179
  
349
	SPCR |= 0x50;//0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2
350
	SPSR |= 0x01;       // a continuation of the above
180
  SPCR |= 0x50;//0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2
181
  SPSR |= 0x01;       // a continuation of the above
351 182

  
352
	LCDRESETPORT |= RST;
353
	delay_ms(10);
354
	LCDRESETPORT &= (~RST);
355
	delay_ms(100);
356
	LCDRESETPORT |= RST;
183
  LCDRESETPORT |= RST;
184
  delay_ms(10);
185
  LCDRESETPORT &= (~RST);
186
  delay_ms(100);
187
  LCDRESETPORT |= RST;
357 188
	
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.
189
  lcd_putbyte( 0x21 );  // LCD Extended Commands.
190
  lcd_putbyte( 0xC8 );  // Set LCD Vop (Contrast).
191
  lcd_putbyte( 0x06 );  // Set Temp coefficent.
192
  lcd_putbyte( 0x13 );  // LCD bias mode 1:48.
193
  lcd_putbyte( 0x20 );  // LCD Standard Commands, Horizontal addressing mode.
194
  lcd_putbyte( 0x0C );  // LCD in normal mode.
364 195
	
365
	LCDRESETPORT |= D_C;		//put it in init instead of main
196
  LCDRESETPORT |= D_C;		//put it in init instead of main
366 197
	
367
	lcd_clear_screen();
198
  lcd_clear_screen();
368 199
}
369 200

  
370 201
/**
......
373 204
 * @see lcd_init
374 205
 **/
375 206
void lcd_clear_screen( void ) {
376
	int i;
377
	for (i = 0; i < 504; i++)
378
			lcd_putbyte(0x0);
207
  int i;
208
  for (i = 0; i < 504; i++)
209
    lcd_putbyte(0x0);
379 210
	
380
	lcd_gotoxy(0,0);
211
  lcd_gotoxy(0,0);
381 212
}
382 213

  
383 214
/**
......
388 219
 *
389 220
 * @see lcd_init
390 221
 **/
391
void lcd_putc(char c)
392
{
393
	int i;
222
void lcd_putc(char c) {
223
  int i;
394 224
	
395
	for (i = 0; i < 5; i++)
396
		lcd_putbyte(FontLookup[c-32][i]);
397
	lcd_putbyte(0);
225
  for (i = 0; i < 5; i++)
226
    lcd_putbyte(FontLookup[c-32][i]);
227
  lcd_putbyte(0);
398 228
}
399 229

  
400 230
/*
401
print an entire string to the lcd
231
  print an entire string to the lcd
402 232
*/
403
void lcd_puts(char *s)
404
{
405
	char *t = s;
406
	while (*t != 0)
407
	{
408
		lcd_putc(*t);
409
		t++;
410
	}
233
void lcd_puts(char *s) {
234
  char *t = s;
235
  while (*t != 0) {
236
    lcd_putc(*t);
237
    t++;
238
  }
411 239
}
412 240

  
413 241
/*
414
go to coordinate x, y
415
y: vertically - 1 char
416
x: horizontally - 1 pixel
242
  go to coordinate x, y
243
  y: vertically - 1 char
244
  x: horizontally - 1 pixel
417 245

  
418
multiply x by 6 if want to move 1 entire character
246
  multiply x by 6 if want to move 1 entire character
419 247

  
420
origin (0,0) is at top left corner of lcd screen
248
  origin (0,0) is at top left corner of lcd screen
421 249
*/
250

  
422 251
/**
423 252
 * Move the current cursor position to the one specified.
424 253
 * lcd_init must be called before this function may be used.
......
428 257
 *
429 258
 * @see lcd_init
430 259
 **/
431
void lcd_gotoxy(int x, int y)
432
{
260
void lcd_gotoxy(int x, int y) {
433 261
  LCDRESETPORT &= ~(D_C);
434 262
  lcd_putbyte(0x40 | (y & 0x07));
435 263
  lcd_putbyte(0x80 | (x & 0x7f));
......
437 265
}
438 266

  
439 267
/*
440
prints an int to the lcd
268
  prints an int to the lcd
441 269

  
442
code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
270
  code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
443 271
*/
272

  
444 273
/**
445 274
 * Print an integer to the LCD screen.
446 275
 * lcd_init must be called before this function may be used.
......
450 279
 * @see lcd_init
451 280
 **/
452 281
void lcd_puti(int value ) {
453
	unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10; 
282
  unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10; 
454 283

  
455
        /* convert int to ascii  */ 
456
        if(value<0) { lcd_putc('-'); value=-value; }    
457
        do { position--; *(lcd_data+position)=(value%radix)+'0'; value/=radix;  } while(value); 
284
  /* convert int to ascii  */ 
285
  if(value<0) { 
286
    lcd_putc('-'); 
287
    value=-value; 
288
  }    
289
  do { 
290
    position--; 
291
    *(lcd_data+position)=(value%radix)+'0'; 
292
    value/=radix;  
293
  } while(value); 
458 294

  
459 295
    
460
        /* start displaying the number */
461
        for(;position<=(sizeof(lcd_data)-1);position++)
462
          {
463
            
464
            lcd_putc(lcd_data[position]);
465
          }
296
  /* start displaying the number */
297
  for(;position<=(sizeof(lcd_data)-1);position++)
298
    lcd_putc(lcd_data[position]);
466 299

  
467
	return;
300
  return;
468 301
}
469 302

  
470 303
/** @} **/ //end defgroup
471 304

  
472
void lcd_putbyte(unsigned char b)
473
{
474
	SPDR = b;
475
	while (!(SPSR & 0x80)); /* Wait until SPI transaction is complete */
305
void lcd_putbyte(unsigned char b) {
306
  SPDR = b;
307
  while (!(SPSR & 0x80)); /* Wait until SPI transaction is complete */
476 308
}
477

  

Also available in: Unified diff