📄 hd61202.cpp
字号:
/*//////////////////////////////////////////////////////////////////////////
Orgnization: Emtronix Incorporated
Filename: HD61202.CPP
Compiler: BC3.1
Authors: CS & ZHL
Date: April, 2002
Revised: ...
Description: function implementations of LCD module based on HD61202
///////////////////////////////////////////////////////////////////////////*/
#include <dos.h>
#include "hd61202.h"
#define CmdPort0 0x7c
#define DatPort0 0x7d
#define CmdPort1 0x7a
#define DatPort1 0x7b
#define ColBankSize 64
#define MAXX 128 //change 192
#define MAXY 64
unsigned int CmdPort[2] = { CmdPort0, CmdPort1 };
unsigned int DatPort[2] = { DatPort0, DatPort1 };
/*//////////////////////////////////////////////////////
DOT LCD support routines
//////////////////////////////////////////////////////*/
// return = 0x8000: LCD Controller[] unexisted
// = 0x0080: LCD Controller[] existed, Busy
// = 0x0020: LCD Controller[] existed, in OFF
// = 0x0010: LCD Controller[] existed, in RESET
// = 0x0000: LCD Controller[] ready
// = 0xffff: wrong input paramter
unsigned int ControllerStatus( int Idx )
{
unsigned char State, ss;
if( (Idx<0) || (Idx>1) ) return 0xffff;
State = inportb( CmdPort[Idx] );
if( State == 0x00 ) return 0;
ss = State & 0x80;
if( ss != 0 ) return 0x0080; // in Busy!
ss = State & 0x20;
if( ss != 0 ) return 0x0020; // in turn off!
ss = State & 0x10;
if( ss != 0 ) return 0x0010; // in reset!
return 0x8000; // unexisted!
}
// return = 0x8000: LCD unexisted
// = 0x0080: LCD existed, Busy
// = 0x0020: LCD turn off
// = 0x0010: LCD in reset
// = 0x0000: LCD ready
unsigned int CheckStatus( )
{
unsigned int i, status;
for( i=0; i<2; i++ ) //change 3
{
status = ControllerStatus( i );
if( status != 0 ) return status;
}
return 0;
}
// return = 0: ok
// = -1: LCD error
int WaitUntilReady( int Idx )
{
unsigned int i, status;
for( i=0; i<10000; i++ )
{
status = ControllerStatus( Idx );
switch( status )
{
case 0x8000: // LCD unexisted
return -1;
case 0x0020: // LCD turn off
//outp( CmdPort[Idx], 0x3f );
case 0x0000: // LCD ready
return 0;
default:
break;
}
}
return -1;
}
// return = 0: init ok
// = -1: init fail
int InitLCD( )
{
int i, k;
unsigned int status;
// wait LCD ready
for( i=0; i<11; i++ )
{
//DelayCall( 1, Milliseconds );
status = CheckStatus( );
if( status == 0x8000 ) return -1;
if( status == 0x0080 ) continue;
break;
}
if( i >= 11 ) return -1;
for( k=0; k<2; k++ ) //change 3
{
// turn on LCD
if( WaitUntilReady( k ) != 0 ) return -1;
outportb( CmdPort[k], 0x3f );
// set display start row
if( WaitUntilReady( k ) != 0 ) return -1;
outportb( CmdPort[k], 0xc0 );
// set active page => page 0
if( WaitUntilReady( k ) != 0 ) return -1;
outportb( CmdPort[k], 0xb8 );
// set active column => column 0
if( WaitUntilReady( k ) != 0 ) return -1;
outportb( CmdPort[k], 0x40 );
}
return ClearScreen( );
}
// return = 0: init ok
// = -1: LCD fail
// = -2: Not Init yet!
int ClearScreen( )
{
unsigned int i, PgIdx, k;
for( k=0; k<2; k++ ) //change 3
{
for( PgIdx=0; PgIdx<8; PgIdx++ )
{
if( WaitUntilReady( k ) != 0 ) return -1;
outportb( CmdPort[k], 0xb8+PgIdx );
if( WaitUntilReady( k ) != 0 ) return -1;
outportb( CmdPort[k], 0x40 );
for( i=0; i<ColBankSize; i++ )
{
if( WaitUntilReady( k ) != 0 ) return -1;
outportb( DatPort[k], 0 );
}
}
}
// set start line as 0
for( k=0; k<2; k++ ) //change 3
{
if( WaitUntilReady( k ) != 0 ) return -1;
outportb( CmdPort[k], 0xc0 );
}
return 0;
}
///////////////////////////////////////////////////
// input: x = 0 - 127
// y = 0 - 63
// (0,0)=topleft; (127,63)=bottomright
//
// input: DotColor = 0: write 0;
// = 1: write 1;
// = 0x81: XOR write
// return = 0: ok
// = -1: LCD fail
int WritePixel( int x, int y, int DotColor )
{
unsigned int Idx, PgNum, LineOffset, ColNum;
unsigned char abyte, MaskBit;
if( (x<0) || (x>=128) ) return -1;
if( (y<0) || (y>=64) ) return -1;
Idx = x / ColBankSize;
ColNum = x % ColBankSize;
PgNum = y / 8;
LineOffset = y % 8;
MaskBit = 1<<LineOffset;
// goto (x, y)
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( CmdPort[Idx], 0xb8+PgNum );
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( CmdPort[Idx], 0x40+ColNum );
// get data at (x, y)
if( WaitUntilReady( Idx ) != 0 ) return -1;
abyte = inportb( DatPort[Idx] );
if( WaitUntilReady( Idx ) != 0 ) return -1;
abyte = inportb( DatPort[Idx] );
// bit operation
if( DotColor == 1 ) abyte |= MaskBit;
else if( DotColor == 0 ) abyte &= ~MaskBit;
else abyte ^= MaskBit;
// write data back to (x,y)
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( CmdPort[Idx], 0x40+ColNum );
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( DatPort[Idx], abyte );
return 0;
}
// input x = 0 -- 127
// PgIdx = 0,1,...,7
// return = 0: ok
// = -1: LCD fail
// = -2: invalid parameter
int WriteByte( int x, int PgIdx, unsigned char aByte )
{
unsigned int Idx, ColNum;
if( (x<0) || (x>127) ) return -2; //change 191
if( (PgIdx<0) || (PgIdx>7) ) return -2;
Idx = x / ColBankSize;
ColNum = x % ColBankSize;
// goto (x, y)
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( CmdPort[Idx], 0xb8+PgIdx );
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( CmdPort[Idx], 0x40+ColNum );
// write data to (x, PgIdx)
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( DatPort[Idx], aByte );
return 0;
}
// input x = 0 -- 127
// PgIdx = 0,1,...,7
// return = 0: ok
// = -1: LCD fail
int ReadByte( int x, int PgIdx )
{
unsigned int Idx, ColNum;
unsigned char abyte;
if( (x<0) || (x>127) ) return -1; //change 191
if( (PgIdx<0) || (PgIdx>7) ) return -1;
Idx = x / ColBankSize;
ColNum = x % ColBankSize;
// goto (x, y)
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( CmdPort[Idx], 0xb8+PgIdx );
if( WaitUntilReady( Idx ) != 0 ) return -1;
outportb( CmdPort[Idx], 0x40+ColNum );
// write data to (x, PgIdx)
if( WaitUntilReady( Idx ) != 0 ) return -1;
abyte = inportb( DatPort[Idx] );
if( WaitUntilReady( Idx ) != 0 ) return -1; //MUST BE twice to get
abyte = inportb( DatPort[Idx] ); //correct data
return abyte;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -