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

📄 lcd.c

📁 HOT51开发板TFT彩屏的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <reg51.h>
#include "TM128128_LCD.H"
#include "LCD_dis.H"
#include "LCD.H"
#include "LCD_type.H"

#define LCDCOM 0
#define LCDDAT 1

sbit LED_RD = P2^5;
sbit LED_CS = P2^7;
sbit LED_WR = P2^4;
sbit LED_A0 = P2^3;
sbit REST = P2^6;

void delay(INT16U a)
{
	INT16U i, j;
	for(i = 0; i < a; i++)
		for(j = 0; j < 114; j++);
}
/*======================================================
彩屏读/写命令/数据
=======================================================*/
void LCDWR( INT8U cmd_dt, INT8U value )
{
	LED_RD = 1;
	LED_CS = 1;
	LED_WR = 1;
	if(cmd_dt)
	{
		LED_A0 = 1;
	}
	else
	{
		LED_A0 = 0;	
	}
	P0 = value;
	LED_CS = 0;
	LED_WR = 0;
	LED_WR = 1;
	LED_CS = 1;
}
/*======================================================
LCD初始化
=======================================================*/
void LCD_Init()
{

/*	LCDWR(LCDCOM, SOFT_RESET);  //恢复出厂设置
	delay(150);
	LCDWR(LCDCOM, SLEEP_OUT);
	LCDWR(LCDCOM, DISPLAY_ON);
	LCDWR(LCDCOM, NORMAL_MODE_ON);//全屏显示
	LCDWR(LCDCOM, INVERSION_OFF);
	LCDWR(LCDCOM, PIXELS_OFF);
	LCDWR(LCDCOM, SET_CONTRAST);
	LCDWR(LCDDAT, 0x07);
	LCDWR(LCDCOM, MEM_CONTROL);	 //寄存器应用控制
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDCOM, SET_X_ADDR);
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDDAT, 0x7f);
	LCDWR(LCDCOM, SET_Y_ADDR);
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDDAT, 0x9f);
	LCDWR(LCDCOM, PARTIAL_AREA);
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDDAT, 0x00);
	LCDWR(LCDDAT, 0xaa);
	LCDWR(LCDDAT, 0xaa);
	LCDWR(LCDCOM, COLOR_INTERFACE);
//	LCDWR(LCDDAT, COLOR_16_BIT); */ 

//	LCDWR(DATE, 0x9f);
//	LCDWR(COM, 0xe0); //eeprom读写模式
//	LCDWR(DATE, 0x00); //读otp模式读eeprom
//	LCDWR(COM, 0x10); //写eeprom
//	LCDWR(COM, 0xe1); //退出读写控制模式
//	LCDWR(COM, 0xe2); //写eeprom
//	LCDWR(COM, 0xe3); //读eeprom
//	LCDWR(COM, DISPLAY_OFF); //关闭显示
		LCDWR( LCDCOM, 0x01);//software reset
		delay(150);       //delay 150ms
		LCDWR( LCDCOM, 0x11);//software reset

		LCDWR( LCDCOM,0x26); // Auto load set
		LCDWR ( LCDDAT,0x04);//auto load disable//0x9f
		LCDWR( LCDCOM,0xf2);  //EE read/write mode
		LCDWR ( LCDDAT,0x00); //set read  mode
		LCDWR( LCDCOM,0xb1);   
		LCDWR ( LCDDAT,0x08);
		LCDWR ( LCDDAT,0x14); 		
		LCDWR( LCDCOM,0xc0);   
		LCDWR ( LCDDAT,0x0a); 
		LCDWR ( LCDDAT,0x05); 
		LCDWR( LCDCOM,0xc1); 
		LCDWR ( LCDDAT,0x02);
		LCDWR( LCDCOM,0xec); 
		LCDWR ( LCDDAT,0x44);
		LCDWR ( LCDDAT,0x52);
		LCDWR( LCDCOM,0xc7); 
		LCDWR ( LCDDAT,0xbf);

//////////


		LCDWR( LCDCOM,0x2a);  //display off
		LCDWR ( LCDDAT,0x00);
		LCDWR ( LCDDAT,0x00);
		LCDWR ( LCDDAT,0x00);
		LCDWR ( LCDDAT,0x7f);
		LCDWR( LCDCOM,0x2b);  //sleep out
		LCDWR ( LCDDAT,0x00);
		LCDWR ( LCDDAT,0x00);
		LCDWR ( LCDDAT,0x00);
		LCDWR ( LCDDAT,0x7f);
		LCDWR( LCDCOM,0x36); 
		LCDWR ( LCDDAT,0xc8);
		LCDWR( LCDCOM,0x3a); 
		LCDWR ( LCDDAT,0x55);
		
                //////////////////////////////////////////////////////
		/////VOP set///////////
		LCDWR( LCDCOM,0xf2);  //vo voltage set   
		LCDWR ( LCDDAT,0x01);///////////f5/////  08//00

        LCDWR( LCDCOM,0xe0);   //write contrast for mobile
		LCDWR ( LCDDAT,0x3f);
		LCDWR ( LCDDAT,0x1b);
		LCDWR ( LCDDAT,0x19);
		LCDWR ( LCDDAT,0x27);
		LCDWR ( LCDDAT,0x1f);

		LCDWR ( LCDDAT,0x0d);
		LCDWR ( LCDDAT,0x45);
		LCDWR ( LCDDAT,0xb8);
		LCDWR ( LCDDAT,0x30);

		LCDWR ( LCDDAT,0x17);
		LCDWR ( LCDDAT,0x0f);
		LCDWR ( LCDDAT,0x05);
		LCDWR ( LCDDAT,0x13);

		LCDWR ( LCDDAT,0x02);
		LCDWR ( LCDDAT,0x00);

		//-----------OTPB SET----------------------//
		LCDWR( LCDCOM,0xe1);// bias set
		LCDWR ( LCDDAT,0x00);    //1/12
		LCDWR ( LCDDAT,0x24);

		LCDWR ( LCDDAT,0x26);
		LCDWR ( LCDDAT,0x08);
		LCDWR ( LCDDAT,0x10);
		LCDWR ( LCDDAT,0x12);

		LCDWR ( LCDDAT,0x3a);
		LCDWR ( LCDDAT,0x74);
		LCDWR ( LCDDAT,0x4f);
		LCDWR ( LCDDAT,0x08);

		LCDWR ( LCDDAT,0x20);
		LCDWR ( LCDDAT,0x2a);
		LCDWR ( LCDDAT,0x3c);
		LCDWR ( LCDDAT,0x3d);
		LCDWR ( LCDDAT,0x3f);
		delay(150);  
		LCDWR( LCDCOM,0x29);   //booster set
		delay(150);  
		LCDWR( LCDCOM,0x2c);
}

/*======================================================
设置显示范围
=======================================================*/
void LCDSetArea( INT16U x1, INT16U y1, INT16U x2, INT16U y2 )
{
	x1+=2;
	x2+=2;
	y1+=3;
	y2+=3;	

    LCDWR ( LCDCOM, 0x2A);
	LCDWR ( LCDDAT,  x1>>8);
	LCDWR ( LCDDAT, x1);
	LCDWR ( LCDDAT,  x2>>8);
	LCDWR ( LCDDAT, x2 + 0); 

	LCDWR ( LCDCOM, 0x2B);
	LCDWR ( LCDDAT,  y1>>8);
	LCDWR ( LCDDAT, y1); 
	LCDWR ( LCDDAT,  y2>>8);
	LCDWR ( LCDDAT, y2); 
	LCDWR ( LCDCOM, 0x2C);
}
/*======================================================
画一个特殊的范围
=======================================================*/
void	LCDDrawDollop( DOLLOP* dollop )
{
	INT8U x,y;

	LCDSetArea(dollop->xs, dollop->ys, dollop->xe, dollop->ye);   	//Set a area at the screen
	for( x = 0; x < dollop->xe - dollop->xs + 1; x ++ )					//Display rows
	{
	 	for( y = 0; y < dollop->ye - dollop->ys + 1; y ++ )	 			//Display columns
		{
		 	LCDWR( LCDDAT, dollop->Color>>8 );
			LCDWR( LCDDAT, dollop->Color );
		}
	}
}
/*======================================================
画点
=======================================================*/
void LCDDrawPoint( POINT* pPoint )	  //画点
{
    LCDSetArea( pPoint->x, pPoint->y, pPoint->x, pPoint->y );
	LCDWR ( LCDDAT,  ( pPoint->Color >> 8 ) & 0xff );
	LCDWR ( LCDDAT,  pPoint->Color & 0xff );
}

/*======================================================
画直线
=======================================================*/
void LCDDrawHRLine( LINE* pLine )	  //画直线
{
	INT8U x0, x1, y0, y1;
	if( pLine->xs != pLine->xe && pLine->ys != pLine->ye )   return;
	if( pLine->ys > pLine->ye )
	{
		y0 = pLine->ye;
		y1 = pLine->ys;
	}
	else
	{
		y0 = pLine->ys;
		y1 = pLine->ye;
	}
	if( pLine->xs > pLine->xe )
	{
		x0 = pLine->xe;
		x1 = pLine->xs;
	}
	else
	{
		x0 = pLine->xs;
		x1 = pLine->xe;
	}
	LCDSetArea( x0, y0, x1, y1 );
	x0 = x1 - x0;
	if( x0 == 0 ) x0 = y1 - y0;
	for( y0 = 0; y0 < x0; y0 ++ )
	{
		LCDWR( LCDDAT, pLine->Color >> 8 );
		LCDWR( LCDDAT, pLine->Color );
	}
}

/*======================================================
画一个特殊的
=======================================================*/
void	PrintBitBlock( BitBlock *pBitBlock )
{
    INT8U	Row, Column;
	INT32U	BytesAbs;
	INT8U	RowBytes;

	LCDSetArea( pBitBlock->xs, pBitBlock->ys,
		pBitBlock->xs + pBitBlock->Width - 1, pBitBlock->ys + pBitBlock->Height - 1 );

	RowBytes = pBitBlock->Width >> 3;
	if( pBitBlock->Width & 0x07 )
	{
	 	RowBytes ++;
	}
	for( Row = 0; Row <  pBitBlock->Height; Row ++ )
	{
	 	for( Column = 0; Column < pBitBlock->Width; Column ++ )
		{
		 	BytesAbs = Row * RowBytes + ( Column >> 3 )	;
			if( *( pBitBlock->pData + BytesAbs )	& ( 1<<( Column & 0x07 )) )
			{
			 	LCDWR( LCDDAT, pBitBlock->Color >> 8 );
				LCDWR( LCDDAT, pBitBlock->Color );
			}
			else
			{
			 	LCDWR( LCDDAT, pBitBlock->BackColor >> 8 );
				LCDWR( LCDDAT, pBitBlock->BackColor );
			}
		}
	}
}
/*

/*======================================================
显示图像
=======================================================*/
void GUI_Image( IMAGE *pImage )
{
    INT8U x, y;
    INT16U datacount = 0;
    LCDSetArea( pImage->xs, pImage->ys, pImage->xs + pImage->length - 1, pImage->ys + pImage->height - 1 );

    for( x = 0; x < pImage->length; x ++ )
    {
        for( y = 0; y < pImage->height; y ++ )
        {
            LCDWR( LCDDAT, *( pImage->pData + datacount++ ) );
            LCDWR( LCDDAT, *( pImage->pData + datacount++ ) );
        }
    }
}




void GUI_DrawRectangle( RECT* pRect )  //矩形
{
	LINE line;

	line.xs = pRect->xs;
	line.xe = pRect->xe;
	line.ys = pRect->ys;
	line.ye = pRect->ys;
	line.Color = pRect->Color;
	LCDDrawHRLine( &line );

	line.xe = pRect->xs;
	line.ye = pRect->ye;
	LCDDrawHRLine( &line );

	line.xs = pRect->xe;
	line.ys = pRect->ye;
	LCDDrawHRLine( &line );

	line.xe = pRect->xe;
	line.ye = pRect->ys;
	LCDDrawHRLine( &line );
}

/*================================================================================
画直线
================================================================================*/
void GUI_DrawLine( LINE* pLine )
{
	INT32S   dx;						// 直线x轴差值变量
	INT32S   dy;          	// 直线y轴差值变量
	INT32S    dx_sym;				// x轴增长方向,为-1时减值方向,为1时增值方向
	INT32S    dy_sym;				// y轴增长方向,为-1时减值方向,为1时增值方向
	INT32S   dx_x2;					// dx*2值变量,用于加快运算速度
	INT32S   dy_x2;					// dy*2值变量,用于加快运算速度
	INT32S   di;						// 决策变量

	POINT    point;
	LINE     line;

	line.xs = pLine->xs;
	line.ys = pLine->ys;
	line.xe = pLine->xe;
	line.ye = pLine->ye;
	line.Color = pLine->Color;

  point.Color = pLine->Color;

	dx = line.xe - line.xs;
  dy = line.ye - line.ys;

 /* 判断增长方向,或是否为水平线、垂直线、点 */
	if( dx > 0 )					// 判断x轴方向
	{
		dx_sym = 1;					// dx>0,设置dx_sym=1
	}
	else
	{
		if( dx < 0 )
		{
			dx_sym = -1;		  // dx<0,设置dx_sym=-1
		}
		else
		{
			LCDDrawHRLine( &line );
			return;
		}
	}

	if( dy > 0 )							// 判断y轴方向
	{
		dy_sym = 1;					// dy>0,设置dy_sym=1
	}
	else
	{
		if( dy < 0 )
		{
			dy_sym = -1;				// dy<0,设置dy_sym=-1
		}
		else
		{  // dy==0,画水平线,或一点
			LCDDrawHRLine( &line );
			return;
		}
	}

	/* 将dx、dy取绝对值 */
	dx = dx_sym * dx;
	dy = dy_sym * dy;

	/* 计算2倍的dx及dy值 */
	dx_x2 = dx*2;
	dy_x2 = dy*2;

 /* 使用Bresenham法进行画直线 */
	if( dx >= dy )						// 对于dx>=dy,则使用x轴为基准
	{
		di = dy_x2 - dx;
    while( line.xs != line.xe )
    {
			point.x = line.xs;
			point.y = line.ys;
			LCDDrawPoint( &point );
			line.xs += dx_sym;
			if( di < 0 )
			{
				di += dy_x2;			// 计算出下一步的决策值
			}
			else
			{
				di += dy_x2 - dx_x2;
				line.ys += dy_sym;
			}
    }
		LCDDrawPoint( &point );		// 显示最后一点
	}
	else								// 对于dx<dy,则使用y轴为基准
	{
		di = dx_x2 - dy;
    while( line.ys != line.ye )
    {
			point.x = line.xs;
			point.y = line.ys;
			LCDDrawPoint( &point );
			line.ys += dy_sym;
			if(di<0)
			{
				di += dx_x2;
			}
			else
			{
				di += dx_x2 - dy_x2;
				line.xs += dx_sym;
			}
    }
		LCDDrawPoint( &point );		// 显示最后一点
	}
}

/*================================================================================
填充显示的子函数
================================================================================*/
void	GUI_DisplayFont( INT8U	Xs, INT8U Ys, FONT* pFont, char Character )
{
	BitBlock	Block;
	INT32U	Bytes;
	INT8U DataBuffer[64];
	INT8U i;
	const unsigned char *offset;

	Block.Height = pFont->Height;
	Block.Width = pFont->Width;
	Block.Color = pFont->Color;
	Block.BackColor = pFont->BackColor;
	Block.xs = Xs;
	Block.ys = Ys;

	Bytes = pFont->Width >> 3;
	if( pFont->Width & 0x07 )
	{
	 	Bytes ++;
	}
	Bytes *= pFont->Height;
	Bytes *= Character - ' ';
//offset = (const unsigned char*)&FontLib_14;
/*
	if( pFont->Height == 18 )
	{
	 	offset = (const unsigned char*)&FontLib_18;
	}
	else if( pFont->Height == 14 )
	{
	 	offset = (const unsigned char*)&FontLib_14;
	}
	else
	{
		return;
	}
	*/
	offset += Bytes;
	for( i = 0; i < 36; i ++ )
	{
		DataBuffer[i] = *( offset + i );	
	}

	
	Block.pData = DataBuffer;

	PrintBitBlock( &Block );
}
/*
========================================================================================================
Name: DisplayStr
Function: Display a character at a special area
Input:
    1.Xs : Start position X
		2.Ys : Start position Y
		3.pFont : A pointer of a font structure
		4.Str : The start address of a string
Output: None
Note: The start position is inputted as a parameter, And the end position is calculated by the FONT
		structure.
Author: LiYong
Date  : 2008.08.09
========================================================================================================
*/
/*================================================================================
填充显示
================================================================================*/
void	GUI_DisplayStr( INT8U xs, INT8U ys, FONT* pFont, char* Str )
{
	while( *Str )
	{
		GUI_DisplayFont( xs, ys, pFont, *Str );
	 	Str ++;
		xs += pFont->Width;
	}
}

/*================================================================================
画空心的圆
================================================================================*/
/*void  GUI_DrawCircle( CIRCLE* pCircle )
{
   INT8S  draw_x0, draw_y0;			// 刽图点坐标变量
   INT8S  draw_x1, draw_y1;
   INT8S  draw_x2, draw_y2;
   INT8S  draw_x3, draw_y3;
   INT8S  draw_x4, draw_y4;
   INT8S  draw_x5, draw_y5;
   INT8S  draw_x6, draw_y6;
   INT8S  draw_x7, draw_y7;
   INT8S  xx, yy;					// 画圆控制变量

   INT8S  di;						// 决策变量
   POINT point;

   point.Color = pCircle->Color;

   // 参数过滤
   if(0 == pCircle->r ) return;

   // 计算出8个特殊点(0、45、90、135、180、225、270度),进行显示 
   point.x = draw_x0 = draw_x1 = pCircle->x;
   point.y = draw_y0 = draw_y1 = pCircle->y + pCircle->r;

   if( draw_y0 < GUI_LCM_YMAX ) LCDDrawPoint( &point );	// 90度

   point.x = draw_x2 = draw_x3 = pCircle->x;
   point.y = draw_y2 = draw_y3 = pCircle->y - pCircle->r;
   if( draw_y2 >= 0 ) LCDDrawPoint( &point );			// 270度


   point.x = draw_x4 = draw_x6 = pCircle->x + pCircle->r;
   point.y = draw_y4 = draw_y6 = pCircle->y;
   if(draw_x4<GUI_LCM_XMAX) LCDDrawPoint( &point );	// 0度

   point.x = draw_x5 = draw_x7 = pCircle->x - pCircle->r;
   point.y = draw_y5 = draw_y7 = pCircle->y;
   if(draw_x5>=0) LCDDrawPoint( &point );			// 180度
   if(1==pCircle->r) return;					// 若半径为1,则已圆画完


   //使用Bresenham法进行画圆 
   di = 3 - 2*pCircle->r;					// 初始化决策变量

   xx = 0;
   yy = pCircle->r;
   while(xx<yy)
   {  if(di<0)
	  {  di += 4*xx + 6;
	  }
	  else
	  {  di += 4*(xx - yy) + 10;

	     yy--;
		 draw_y0--;
		 draw_y1--;
		 draw_y2++;
		 draw_y3++;
		 draw_x4--;
		 draw_x5++;
		 draw_x6--;
		 draw_x7++;
	  }

	  xx++;
	  draw_x0++;
	  draw_x1--;
	  draw_x2++;
	  draw_x3--;
	  draw_y4++;
	  draw_y5++;
	  draw_y6--;
	  draw_y7--;

⌨️ 快捷键说明

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