1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
/*
Copyright (C) 2000 T. Scott Dattalo
This file is part of gpsim.
gpsim is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
gpsim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with gpsim; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include "lcd.h"
using namespace std;
/*
lcdengine.cc
This is where the behavioral simulation of the LCD is performed.
The LCD is simulated with a state machine. The states are defined
POWERON,
ST_INITIALIZED,
ST_COMMAND_PH0,
ST_DATA_PH0,
*/
/****************************************************************
*
* Now the details for simulating the LCD
*/
/*
* LCD Command "Set Display Data RAM Address" = 10000000
*/
static const int LCD_CMD_SET_DDRAM = 0x80;
static const int LCD_MASK_SET_DDRAM = 0x80;
/*
* LCD Command "Set Display Character Generator RAM Address" = 01aaaaaa
*/
static const int LCD_CMD_SET_CGRAM = 0x40;
static const int LCD_MASK_SET_CGRAM = 0xc0;
/*
* LCD Command Function Set = 001dnfxx
* d = 1 for 8-bit interface or 0 for 4-bit interface
* n = for 2 line displays, n=1 allows both lines to be displayed
* while n=0 only allows the first.
* f = font size. f=1 is for 5x11 dots while f=0 is for 5x8 dots.
*/
static const int LCD_CMD_FUNC_SET = 0x20; // LCD Command "Function Set"
static const int LCD_MASK_FUNC_SET = 0xe0; //
static const int LCD_4bit_MODE = 0x00; // d=0
static const int LCD_8bit_MODE = 0x10; // d=1
static const int LCD_1_LINE = 0x00; // n=0
static const int LCD_2_LINES = 0x08; // n=1
static const int LCD_SMALL_FONT = 0x00; // f=0
static const int LCD_LARGE_FONT = 0x04; // f=1
/*
* LCD Command "Cursor Display" = 0001sdxx
* s = 1 Sets cursor-move or display-shift
* d = 1 Shift right 0 = shift left
*/
static const int LCD_CMD_CURSOR_DISPLAY = 0x10; // LCD Command "Cursor Display"
static const int LCD_MASK_CURSOR_DISPLAY = 0xf0; //
/*
* LCD Command Display Control = 00001dcb
* d = 1 turn display on or 0 to turn display off
* c = 1 turn cursor on or 0 to turn cursor off
* b = 1 blinking cursor or 0 non-blinking cursor
*/
static const int LCD_CMD_DISPLAY_CTRL = 0x08; // LCD Command "Display Control"
static const int LCD_MASK_DISPLAY_CTRL = 0xf8; //
static const int LCD_DISPLAY_OFF = 0x00; // d=0
static const int LCD_DISPLAY_ON = 0x04; // d=1
static const int LCD_CURSOR_OFF = 0x00; // c=0
static const int LCD_CURSOR_ON = 0x02; // c=1
static const int LCD_BLINK_OFF = 0x00; // b=0
static const int LCD_BLINK_ON = 0x01; // b=1
/*
* LCD Command "Entry Mode" = 000001is
* i = 1 to increment or 0 to decrement the DDRAM address after each DDRAM access.
* s = 1 to scroll the display in the direction specified by the i-bit when the
* cursor reaches the edge of the display window.
*/
static const int LCD_CMD_ENTRY_MODE = 0x04; // LCD Command "Entry Mode"
static const int LCD_MASK_ENTRY_MODE = 0xfc; //
static const int LCD_DEC_CURSOR_POS = 0x00; // i=0
static const int LCD_INC_CURSOR_POS = 0x02; // i=1
static const int LCD_NO_SCROLL = 0x00; // s=0
static const int LCD_SCROLL = 0x01; // s=1
/*
* LCD Command "Cursor Home" = 0000001x
*/
static const int LCD_CMD_CURSOR_HOME = 0x02; // LCD Command "Cursor Home"
static const int LCD_MASK_CURSOR_HOME = 0xfe; //
// LCD Command Clear Display = 00000001
static const int LCD_CMD_CLEAR_DISPLAY = 0x01;
static const int LCD_MASK_CLEAR_DISPLAY = 0xff;
//
// PowerON
//
// Power has just been applied to the LCD. Now it's waiting
// for a command to switch it into either 4 or 8 bit mode
//
/*
void LcdDisplay::STPowerON( Event e)
{
switch (e) {
case EWC:
start_command();
break;
default:
break;
}
}
*/
void LcdDisplay::start_data(void)
{
if(in_8bit_mode())
data_latch = data_port->value & 0xff;
else {
// 4-bit mode.
data_latch = ( (data_latch << 4) | ((data_port->value & 0xf0)>>4) ) & 0xff;
data_latch_phase ^= 1;
}
newState(ST_COMMAND_PH0);
}
void LcdDisplay::execute_command(void)
{
if(debug)
cout << "execute command: ";
//
// Determine the command type
//
if( (data_latch & LCD_MASK_SET_DDRAM) == LCD_CMD_SET_DDRAM) {
if(debug)
cout << "LCD_CMD_SET_DDRAM\n";
write_ddram_address(data_latch & 0x7f);
}
else if( (data_latch & LCD_MASK_SET_CGRAM) == LCD_CMD_SET_CGRAM) {
cout << "LCD_CMD_SET_CGRAM\n";
cout << "NOT SUPPORTED\n";
}
else if( (data_latch & LCD_MASK_FUNC_SET) == LCD_CMD_FUNC_SET) {
if(debug)
cout << "LCD_CMD_FUNC_SET\n";
//
// Check the bits in the command
//
if(data_latch & LCD_8bit_MODE)
set_8bit_mode();
else
set_4bit_mode();
if(data_latch & LCD_2_LINES)
set_2line_mode();
else
set_1line_mode();
if(data_latch & LCD_LARGE_FONT)
set_large_font_mode();
else
set_small_font_mode();
}
else if( (data_latch & LCD_MASK_CURSOR_DISPLAY) == LCD_CMD_CURSOR_DISPLAY) {
cout << "LCD_CMD_CURSOR_DISPLAY\n";
cout << "NOT SUPPORTED\n";
}
else if( (data_latch & LCD_MASK_DISPLAY_CTRL) == LCD_CMD_DISPLAY_CTRL) {
if(debug)
cout << "LCD_CMD_DISPLAY_CTRL\n";
if(data_latch & LCD_DISPLAY_ON)
set_display_on();
else
set_display_off();
if(data_latch & LCD_CURSOR_ON)
set_cursor_on();
else
set_cursor_off();
if(data_latch & LCD_BLINK_ON)
set_blink_on();
else
set_blink_off();
}
else if( (data_latch & LCD_MASK_ENTRY_MODE) == LCD_CMD_ENTRY_MODE) {
cout << "LCD_CMD_ENTRY_MODE\n";
cout << "NOT SUPPORTED\n";
}
else if( (data_latch & LCD_MASK_CURSOR_HOME) == LCD_CMD_CURSOR_HOME) {
cout << "LCD_CMD_CURSOR_HOME\n";
cout << "NOT SUPPORTED\n";
}
else if( (data_latch & LCD_MASK_CLEAR_DISPLAY) == LCD_CMD_CLEAR_DISPLAY) {
if(debug)
cout << "LCD_CMD_CLEAR_DISPLAY\n";
clear_display();
}
else
cout << "UNKOWN command : 0x" << hex << data_latch << '\n';
// If the lcd is in '4-bit' mode, then this flag
// will tell us which four bits are being written.
data_latch_phase = 1;
}
//--------------------------------------------------
//
// new_command
//
// The state machine will call `new_cmmand' when it enters
// the eWC state.
void LcdDisplay::new_command(void)
{
if(in_8bit_mode())
execute_command();
else {
// 4-bit mode
if(data_latch_phase & 1)
execute_command();
}
newState(ST_INITIALIZED);
}
//--------------------------------------------------
//
// new_data
//
void LcdDisplay::new_data(void)
{
if(in_8bit_mode())
write_data(data_latch);
else {
// 4-bit mode
if(data_latch_phase & 1)
write_data(data_latch);
}
newState(ST_INITIALIZED);
}
//--------------------------------------------------
//
// newState
//
void LcdDisplay::newState(State s)
{
previous_state = current_state;
current_state = s;
}
//--------------------------------------------------
//
// revertState
//
void LcdDisplay::revertState(void)
{
current_state = previous_state;
}
//--------------------------------------------------
//
// advanceState
//
void LcdDisplay::advanceState( Event e)
{
last_event = e;
//cout << "Event: " << getEventName(e) << '\n';
//cout << " from State: " << getStateName(current_state) << '\n';
switch(current_state) {
case ST_INITIALIZED:
case POWERON:
if(e == EWC)
start_data();
if(e == EWD)
start_data();
break;
case ST_COMMAND_PH0:
if(e == eWC)
new_command();
if(e == eWD)
new_data();
break;
case ST_DATA_PH0:
if(e == eWD)
new_data();
else
;
break;
default:
break;
}
//cout << " to State: " << getStateName(current_state) << '\n';
}
char * LcdDisplay::getStateName(State s)
{
switch(s) {
case POWERON:
return "power on";
case ST_INITIALIZED:
return "initialized";
case ST_COMMAND_PH0:
return "command start";
case ST_DATA_PH0:
return "data start";
default:
break;
}
return "unknown state";
}
char *LcdDisplay::getEventName( Event e)
{
switch(e) {
case eRD:
return "eRD";
case eRC:
return "eRC";
case eWD:
return "eWD";
case eWC:
return "eWC";
case ERD:
return "ERD";
case ERC:
return "ERC";
case EWD:
return "EWD";
case EWC:
return "EWC";
case DataChange:
return "Data Change";
default:
break;
}
return "unknown state";
}
void LcdDisplay::viewInternals(int verbosity)
{
cout << "Lcd Display: " << name() << '\n';
cout << "Current state: " << getStateName(current_state) << '\n';
cout << "Last event: " << getEventName(last_event) << '\n';
if(verbosity>1) {
cout << " " << ( (in_8bit_mode()) ? '8' : '4') << "bit mode\n";
cout << " " << ( (in_2line_mode()) ? '2' : '1') << "-line mode\n";
cout << " " << ( (in_large_font_mode()) ? "large" : "small") << " font\n";
}
}
void LcdDisplay::InitStateMachine(void)
{
ControlEvents[0].init(eWC, "eWC");
ControlEvents[1].init(eWD, "eWD");
ControlEvents[2].init(eRC, "eRC");
ControlEvents[3].init(eRD, "eRD");
ControlEvents[4].init(EWC, "EWC");
ControlEvents[5].init(EWD, "EWD");
ControlEvents[6].init(ERC, "ERC");
ControlEvents[7].init(ERD, "ERD");
current_state = POWERON;
int j;
for(j=0; j<cols; j++)
ch_data[0][j] = 255;
for(j=0; j<cols; j++)
ch_data[1][j] = 0;
if(debug)
test();
}
void LcdDisplay::test(void)
{
set_8bit_mode();
data_port->value = LCD_CMD_FUNC_SET | LCD_8bit_MODE;
advanceState(EWC);
advanceState(eWC);
data_port->value = LCD_CMD_FUNC_SET | LCD_4bit_MODE;
advanceState(EWC);
advanceState(eWC);
data_port->value = LCD_CMD_FUNC_SET | LCD_4bit_MODE | LCD_2_LINES | LCD_SMALL_FONT;
advanceState(EWC);
advanceState(eWC);
data_port->value = (LCD_CMD_FUNC_SET | LCD_4bit_MODE | LCD_2_LINES | LCD_SMALL_FONT)<<4;
advanceState(EWC);
advanceState(eWC);
data_port->value = LCD_CMD_DISPLAY_CTRL | LCD_DISPLAY_ON; //LCD_CURSOR_OFF | LCD_BLINK_OFF
advanceState(EWC);
advanceState(eWC);
data_port->value = (LCD_CMD_DISPLAY_CTRL | LCD_DISPLAY_ON)<<4;
advanceState(EWC);
advanceState(eWC);
data_port->value = LCD_CMD_CLEAR_DISPLAY;
advanceState(EWC);
advanceState(eWC);
data_port->value = LCD_CMD_CLEAR_DISPLAY << 4;
advanceState(EWC);
advanceState(eWC);
char *s ="ASHLEY & AMANDA";
int l = strlen(s);
for(int i=0; i<l; i++) {
data_port->value = s[i];
advanceState(EWD);
advanceState(eWD);
data_port->value = s[i] << 4;
advanceState(EWD);
advanceState(eWD);
}
viewInternals(0xff);
}
|