?? lcd.c
字號:
#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 + -