⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcddriver.c

📁 tms320lf2407a 和带t6963控制器的LCD液晶显示源代码
💻 C
字号:
#include<stdlib.h>
#include<math.h>


#define LCDWIDTH 240
#define LCDWIDTHB 30
#define LCDHEIGHT 128

#define LCDDISPMODEGRAPHON 0x98
#define LCDDISPMODETEXTON 0x94
#define LCDDISPMODECURSORON 0x92
#define LCDDISPMODECURSORBLINK 0x91
#define LCDMODEICGROM 0x80
#define LCDMODEEXCGRAM 0x88
#define LCDMODEOR 0x80
#define LCDMODEEXOR 0x81
#define LCDMODEAND 0x82
#define LCDMODETEXTATTRIB 0x84
#define LCDCURSORDEF 0x0a0
#define LCDCURSORPOSITION 0x21
#define LCDCURSORSIZE 0x0a0
#define LCDSETTEXTHOME 0x40
#define LCDSETTEXTAREA 0x41
#define LCDSETGRAPHHOME 0x42
#define LCDSETGRAPHAREA 0x43
#define LCDSETADDRESS 0x24
#define LCDWRITEINCADD 0x0c0
#define LCDAUTOWRITE 0x0b0
#define LCDAUTORESET 0x0b2

ioport unsigned int port8000;
ioport unsigned int port8005;
ioport unsigned int port8001;
ioport unsigned int port8002;
ioport unsigned int port8003;
ioport unsigned int port8004;
ioport unsigned int port8007;

#define LCDSTATUS port8000 //port8000
#define LCDCOMMAND port8001 // port8001
#define LCDDATA port8002 // port8002

struct struLCDGraph
{
	void *pData;
	unsigned int uDataMode;
	unsigned int uDataLength;
	unsigned int uMaxValue;
	unsigned int uWindowX0,uWindowY0,uWindowX1,uWindowY1;
	int nOriginX,nOriginY;
	unsigned int uLineMode;
	unsigned int bShowXCcoordinate; /* 是否绘制x坐标轴 */
	unsigned int bShowYCcoordinate; /* 是否绘制y坐标轴 */
};

void LCDSetOrigin(int nX,int nY);
void LCDSetScreenBuffer(unsigned int *_pScreenBuffer);
void LCDTurnOn();
void LCDTurnOff();
void LCDCLS();
void LCDSetDelay(unsigned int nDelay);
void _Delay(unsigned int nTime);
void LCDRefreshScreen();
void LCDPutPixel(int x,int y,unsigned int color);
void LCDGraph(struct struLCDGraph *Gstru);
void LCDWriteBytes(unsigned int *pData,int x,int y,unsigned color);
void LCDPutCString(unsigned int *pData,int x,int y,unsigned int nCharNumber,unsigned color);
void LCDGraph2(struct struLCDGraph *Gstru,struct struLCDGraph *Gstru1);
void LCDDrawGraph(struct struLCDGraph *Gstru);
void LCDOff();
unsigned int LCDCheckStatus(unsigned int nLCDFunctionID);
void LCDCommand(unsigned int nLCDCommand);
void LCDData(unsigned int nLCDData);
void LCDSetCursorPosition(unsigned int x,unsigned int y);
void LCDSetCursorSize(unsigned int nLCDLineNumber);
void LCDSetTextHomeAddress(unsigned int nLCDPosition);
void LCDSetTextHomeArea(unsigned int nLCDArea);
void LCDSetGraphHomeAddress(unsigned int nLCDPosition);
void LCDSetGraphHomeArea(unsigned int nLCDArea);
void LCDSetAddress(unsigned int nLCDAddress);
void LCDWriteDataIncAdd(unsigned int nLCDData);


unsigned int _nOriginX=0,_nOriginY=0;
unsigned int *_pScreen;
unsigned int _nXLength=LCDWIDTH,_nYLength=LCDHEIGHT,_nXWord=LCDWIDTHB,_nYWord=LCDHEIGHT;
unsigned int _bRefreshMode=0;
unsigned int _nLCDDelay=0;
unsigned int _nScreenX0=0,_nScreenX1=LCDWIDTH-1,_nScreenY0=0,_nScreenY1=LCDHEIGHT-1;

void LCDSetDelay(unsigned int nDelay)
{
	_nLCDDelay=nDelay;
}

void LCDSetOrigin(int nX,int nY)
{
	_nOriginX=nX; _nOriginY=nY;
}

void LCDSetScreenBuffer(unsigned int *_pScreenBuffer)
{
	_pScreen=_pScreenBuffer;
}
 
void LCDCLS()
{
	int nLCDWork1,nLCDWork2;
	unsigned int *pLCDWork;
	int delay;
	pLCDWork=_pScreen;
	LCDSetAddress(0);
	LCDCommand(LCDAUTOWRITE);
	for ( nLCDWork1=0;nLCDWork1<LCDHEIGHT;nLCDWork1++ )
		for ( nLCDWork2=0;nLCDWork2<LCDWIDTHB;nLCDWork2++ )
		{
			LCDData(0); (*pLCDWork++)=0;
			//for(delay=0;delay<0x1000;delay++);
		}
	LCDCommand(LCDAUTORESET);
}

void LCDTurnOn()
{
	LCDCommand(LCDDISPMODEGRAPHON);
	LCDCommand(LCDMODEICGROM);
	LCDSetGraphHomeAddress(0); LCDSetGraphHomeArea(30);
	LCDSetAddress(0);
}

void LCDTurnOff()
{
	LCDOff();
}

void _Delay(unsigned int nDelay)
{
	int ii,jj,kk=0;
	for ( ii=0;ii<nDelay;ii++ )
	{
		for ( jj=0;jj<64;jj++ )
		{
			kk++;
		}
	}
}

void LCDRefreshScreen()
{
	unsigned int nLCDWork1,nLCDWork2;
	unsigned int *pLCDWork;
	
	pLCDWork=_pScreen;
	LCDSetAddress(0);
	LCDCommand(LCDAUTOWRITE);
	for ( nLCDWork1=0;nLCDWork1<LCDHEIGHT;nLCDWork1++ )
		for ( nLCDWork2=0;nLCDWork2<LCDWIDTHB;nLCDWork2++ )
			LCDData((*pLCDWork++));
	LCDCommand(LCDAUTORESET);
}

void LCDPutPixel(int x,int y,unsigned int color)
{
	unsigned int nLCDX,nLCDY,nLCDWork,nLCDWork1,nLCDWork2;
	unsigned *pLCDWork;
	
	nLCDX=x+_nOriginX; nLCDY=_nYLength-y-1;
	if ( nLCDX<_nScreenX0 || nLCDX>_nScreenX1 )	return;
	if ( nLCDY<_nScreenY0 || nLCDY>_nScreenY1 )	return;
	nLCDWork=nLCDX>>3; nLCDWork1=nLCDX&7; nLCDWork2=0x80;
	nLCDWork2>>=nLCDWork1;
	pLCDWork=_pScreen; pLCDWork+=nLCDWork; pLCDWork+=(nLCDY*LCDWIDTHB);
	nLCDWork=(*pLCDWork);
	if ( color==2 )	nLCDWork^=nLCDWork2;
	else	if ( color==0 )	{ nLCDWork2=(~nLCDWork2)&0x0ff; nLCDWork&=nLCDWork2; }
	else	nLCDWork|=nLCDWork2;
	nLCDWork&=0x0ff;
	(*pLCDWork)=nLCDWork;
}

void LCDDrawLine(int x0, int y0, int x1, int y1, unsigned char color)
{
	int x,y;
	int dx,dy,t,error,xd,yd;

	if ( (x0==x1)&&(y0==y1) ) LCDPutPixel(x0,y0,color);
	else	if ( y0==y1 )
	{	if ( x0>x1 )	{ t=x0; x0=x1; x1=t; t=y0; y0=y1; y1=t; }
	 	for ( x=x0;x<=x1;x++ )	LCDPutPixel(x,y0,color);
    } 
    else	if ( x0==x1 )
	{	if ( y0>y1 ) { t=x0; x0=x1; x1=t; t=y0; y0=y1; y1=t; }
	   	for ( y=y0;y<=y1;y++ ) LCDPutPixel(x0,y,color);
	}
	else
	{	xd=abs(x1-x0); yd=abs(y1-y0);
	   	if ( xd>=yd )
	    {	if ( x0>x1 ) { t=x0; x0=x1; x1=t; t=y0; y0=y1; y1=t; }
	       	error=(yd<<1)-(xd); LCDPutPixel(x0,y0,color);
	       	x=x0; y=y0; dy=(y1>y0)?1:(-1);
	       	while ( x<x1 )
		 	{	x++;
		   		if ( error<0 ) error+=(yd<<1); else { error+=((yd-xd)<<1); y+=dy; }
		   		LCDPutPixel(x,y,color);
		 	}
	    }
	  	else
	   	{	if ( y0>y1 ) { t=x0; x0=x1; x1=t; t=y0; y0=y1; y1=t; }
	     	error=(xd<<1)-(yd); LCDPutPixel(x0,y0,color);
	     	x=x0; y=y0; dx=(x1>x0)?1:(-1);
	     	while ( y<y1 )
	      	{	y++;
				if ( error<0 ) error+=(xd<<1); else { error+=((xd-yd)<<1); x+=dx; }
				LCDPutPixel(x,y,color);
	      	}
	   	}
	}
}

#define DATAINTMODE 0
#define DATAUINTMODE 1
#define LINEMODE 0
#define PIXELMODE 1
#define BARMODE 2

void LCDGraph(struct struLCDGraph *Gstru)
{
	LCDDrawGraph(Gstru);
	LCDRefreshScreen();
}

void LCDGraph2(struct struLCDGraph *Gstru,struct struLCDGraph *Gstru1)
{
	LCDDrawGraph(Gstru);
	LCDDrawGraph(Gstru1);
	LCDRefreshScreen();
}

void LCDDrawGraph(struct struLCDGraph *Gstru)
{
	unsigned int *pUint,uWork,sx0,sy0,sx1,sy1,*puWork;
	int *pInt,*pnWork,nWork,nx0,ny0,nx1,ny1;
	int i;
	float fWork,fxSample,fySample;
	
	if ( Gstru->bShowXCcoordinate )	LCDDrawLine(Gstru->nOriginX,Gstru->nOriginY,Gstru->nOriginX,Gstru->uWindowY1,1);
	if ( Gstru->bShowYCcoordinate )	LCDDrawLine(Gstru->nOriginX,Gstru->nOriginY,Gstru->uWindowX1,Gstru->nOriginY,1); 
	sx0=_nScreenX0; sx1=_nScreenX1; sy0=_nScreenY0; sy1=_nScreenY1;
	pInt=(int *)(Gstru->pData); pUint=(unsigned int *)(Gstru->pData);
	fySample=( Gstru->nOriginY==Gstru->uWindowY0 )?((float)(Gstru->uMaxValue)/abs(Gstru->uWindowY1-Gstru->uWindowY0)):((float)(Gstru->uMaxValue)/abs(Gstru->nOriginY-Gstru->uWindowY0));
	fySample=-fySample;
	fxSample=(float)(Gstru->uDataLength)/abs(Gstru->uWindowX1-Gstru->uWindowX0);
	pnWork=pInt; puWork=pUint;
	ny0=Gstru->nOriginY-(*pnWork)/fySample; nx0=Gstru->nOriginX; nx1=nx0+1;
	_nScreenX0=Gstru->uWindowX0; _nScreenX1=Gstru->uWindowX1; _nScreenY0=_nYLength-Gstru->uWindowY1; _nScreenY1=_nYLength-Gstru->uWindowY0;
	for ( i=1;i<Gstru->uWindowX1-Gstru->uWindowX0;i++,nx1++ )
	{
		uWork=i*fxSample;
		switch ( Gstru->uDataMode )
		{
			case DATAINTMODE:
				ny1=Gstru->nOriginY-(*(pnWork+uWork))/fySample;
				break;
			case DATAUINTMODE:
				ny1=Gstru->nOriginY-(*(puWork+uWork))/fySample;
				break;
		}
		switch ( Gstru->uLineMode )
		{
			case LINEMODE:
				LCDDrawLine(nx0,ny0,nx1,ny1,1);
				break;
			case PIXELMODE:
				LCDPutPixel(nx1,ny1,1);
				break;
			case BARMODE:
				LCDDrawLine(nx1,Gstru->nOriginY,nx1,ny1,1);
				break;
		}
		nx0=nx1; ny0=ny1;
	}
	_nScreenX0=sx0; _nScreenX1=sx1; _nScreenY0=sy0; _nScreenY1=sy1;
}

void LCDWriteBytes(unsigned int *pData,int x,int y,unsigned color)
{
	int i,j;
	unsigned int k,mcolor;
	
	for ( i=0;i<8;i++ )
	{
		k=1;
		for ( j=0;j<8;j++,k<<=1 )
		{
			if ( color==2 )	mcolor=2;
			else
			{
				mcolor=( pData[i]&k )?(1):(0);
				if ( color==0 )	mcolor=1-mcolor;
			}
			LCDPutPixel(x+i,y-j,mcolor);
		}
	}
	LCDRefreshScreen();
}

void LCDPutCString(unsigned int *pData,int x,int y,unsigned int nCharNumber,unsigned color)
{
	int i,j,l;
	unsigned int k,mcolor;
	
	for ( l=0;l<nCharNumber;l++ )
		for ( i=0;i<16;i++ )
		{
			k=1;
			for ( j=0;j<16;j++,k<<=1 )
			{
				if ( color==2 )	mcolor=2;
				else
				{
					mcolor=( pData[l*16+i]&k )?(1):(0);
					if ( color==0 )	mcolor=1-mcolor;
				}
				LCDPutPixel(x+l*16+i,y-j,mcolor);
			}
		}
	LCDRefreshScreen();
}

void LCDOff()
{
	LCDCheckStatus(0);
	LCDCOMMAND=0x90;
}

void LCDCommand(unsigned int nLCDCommand)
{
	LCDCheckStatus(0);
	LCDCOMMAND=nLCDCommand;
}

void LCDData(unsigned int nLCDData)
{
	LCDCheckStatus(0);
	LCDDATA=nLCDData;
}

unsigned int LCDCheckStatus(unsigned int nLCDFunctionID)
{
	unsigned int nLCDWork;
	
	do { nLCDWork=LCDSTATUS; } while ( nLCDFunctionID==0 && nLCDWork&3!=3  );
	return nLCDWork;
}

void LCDSetCursorPosition(unsigned int x,unsigned int y)
{
	LCDData(x);
	LCDData(y);
	LCDCommand(LCDCURSORPOSITION);
}

void LCDSetCursorSize(unsigned int nLCDLineNumber)
{
	LCDCommand(LCDCURSORSIZE|(nLCDLineNumber&7));
}

void LCDSetTextHomeAddress(unsigned int nLCDPosition)
{
	LCDData(nLCDPosition&0x0ff);
	LCDData((nLCDPosition>>8)&0x0ff);
	LCDCommand(LCDSETTEXTHOME);
}

void LCDSetTextHomeArea(unsigned int nLCDArea)
{
	LCDData(nLCDArea);
	LCDData(0);
	LCDCommand(LCDSETTEXTAREA);
}

void LCDSetGraphHomeAddress(unsigned int nLCDPosition)
{
	LCDData(nLCDPosition&0x0ff);
	LCDData((nLCDPosition>>8)&0x0ff);
	LCDCommand(LCDSETGRAPHHOME);
}

void LCDSetGraphHomeArea(unsigned int nLCDArea)
{
	LCDData(nLCDArea);
	LCDData(0);
	LCDCommand(LCDSETGRAPHAREA);
}

void LCDSetAddress(unsigned int nLCDAddress)
{
	LCDData(nLCDAddress&0x0ff);
	LCDData((nLCDAddress>>8)&0x0ff);
	LCDCommand(LCDSETADDRESS);
}

void LCDWriteDataIncAdd(unsigned int nLCDData)
{
	LCDData(nLCDData);
	LCDCommand(LCDWRITEINCADD);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -