標(biāo)題:
lcd12864液晶驅(qū)動 畫點(diǎn) 畫線 寫入7*5像素?cái)?shù)字 漢字
[打印本頁]
作者:
51hei學(xué)習(xí)技術(shù)中
時(shí)間:
2017-3-6 03:29
標(biāo)題:
lcd12864液晶驅(qū)動 畫點(diǎn) 畫線 寫入7*5像素?cái)?shù)字 漢字
0.png
(62.37 KB, 下載次數(shù): 66)
下載附件
2017-3-6 03:24 上傳
下載:
12864b-資料.zip
(2.75 MB, 下載次數(shù): 76)
2017-3-6 03:29 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
主程序:
#include <reg52.h>
#include <math.h>
#include <INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
#define DataPort P0
#define DELAYMS 80
sbit RS_Port = P1^0;
sbit RW_Port = P1^1;
sbit E_Port = P2^5;
sbit PSB_Port = P1^2;
sbit RST_Port = P1^4;
xchar CorpInf[]=
{
"自定義的漢字?jǐn)?shù)組"
"自定義的漢字?jǐn)?shù)組"
"自定義的漢字?jǐn)?shù)組"
"自定義的漢字?jǐn)?shù)組"
};
//用來顯示數(shù)字0-9,每個(gè)數(shù)字7行*5列,將一豎排儲存為一個(gè)字節(jié),最高位為0
unsigned char xdata ucxFigure[10][5]=
{
{0x3E,0x51,0x49,0x45,0x3E},//0
{0x00,0x42,0x7F,0x40,0x00},//1
{0x42,0x61,0x51,0x49,0x46},//2
{0x21,0x41,0x45,0x2B,0x31},//3
{0x18,0x14,0x12,0x7F,0x10},//4
{0x27,0x45,0x45,0x45,0x39},//5
{0x3C,0x4A,0x49,0x49,0x30},//6
{0x01,0x71,0x09,0x05,0x03},//7
{0x36,0x49,0x49,0x49,0x36},//8
{0x06,0x49,0x49,0x29,0x1E},//9
};
/*******************************************************
* 函 數(shù) 名:void delay(uchar uc_dly)
* 功能描述:短延時(shí)函數(shù)
* 函數(shù)說明:用于彌補(bǔ)MPU和單片機(jī)在執(zhí)行時(shí)序上的差異
* 輸入?yún)?shù):(1)uchar ui_dly
* 返 回 值:無
*******************************************************/
#pragma disable //在函數(shù)前聲明,只對一個(gè)函數(shù)有效,該函數(shù)在調(diào)用過程中將不可被中斷。
void delay(uint ui_dly)
{
while (ui_dly--);
}
/*******************************************************
* 函 數(shù) 名:void delays()
* 功能描述:長延時(shí)函數(shù)
* 函數(shù)說明:測試時(shí)兩畫面之間的延時(shí)
* 輸入?yún)?shù):無
* 返 回 值:無
*******************************************************/
#pragma disable
void delays()
{
uchar uc_dly,uc_dly1,uc_dly2;
uc_dly =DELAYMS;
while (uc_dly --)
{
for (uc_dly1=0;uc_dly1<50;uc_dly1++)
for (uc_dly2=0;uc_dly2<50;uc_dly2++);
};
}
/*******************************************************
* 函 數(shù) 名:void rdbf(void)
* 功能描述:讀忙狀態(tài)
* 函數(shù)說明:讀忙狀態(tài)
* 輸入?yún)?shù):無
* 返 回 值:無
*******************************************************/
void rdbf(void)
{
unsigned int nTimeOut = 0 ;
RS_Port=0;
RW_Port=1;
E_Port=0;
E_Port=1;
while( ( DataPort & 0x80 ) && ( ++nTimeOut != 0 ) ) ;
E_Port=0;
RS_Port=0;
RW_Port=1;
}
/*******************************************************
* 函 數(shù) 名:void wr_cmd(uchar cmd)
* 功能描述:寫指令
* 函數(shù)說明:往12864b寫指令
* 輸入?yún)?shù):uchar cmd
* 返 回 值:無
*******************************************************/
#pragma disable
void wr_cmd(uchar cmd)
{
rdbf();
RS_Port=0;
RW_Port=0;
E_Port=1;
DataPort=cmd;
E_Port=0;
}
/*******************************************************
* 函 數(shù) 名:void wr_dat(uchar dat)
* 功能描述:寫數(shù)據(jù)
* 函數(shù)說明:往12864b寫數(shù)據(jù)
* 輸入?yún)?shù):uchar dat
* 返 回 值:無
*******************************************************/
#pragma disable
void wr_dat(uchar dat)
{
rdbf();
RS_Port=1;
RW_Port=0;
E_Port=1;
DataPort=dat;
E_Port=0;
}
/*******************************************************
* 函 數(shù) 名:unsigned char rd_dat( void )
* 功能描述:讀數(shù)據(jù)
* 函數(shù)說明:從12864b讀數(shù)據(jù)
* 輸入?yún)?shù):無
* 返 回 值:unsigned char byReturnValue
*******************************************************/
#pragma disable
unsigned char rd_dat( void )
{
unsigned char byReturnValue ;
DataPort = 0xff ;
rdbf();
RS_Port=1;
RW_Port=1;
E_Port=0;
E_Port=1;
byReturnValue = DataPort ;
E_Port=0;
return byReturnValue ;
}
/*******************************************************
* 函 數(shù) 名:void DrawPoint( unsigned char X, unsigned char Y, unsigned char Color )
* 功能描述:畫點(diǎn)
* 函數(shù)說明:在制定位置畫一個(gè)像素的點(diǎn)
* 輸入?yún)?shù):unsigned char X, unsigned char Y, unsigned char Color
* X為列,Y為行,COLOR為不顯示、顯示或者翻轉(zhuǎn)(0、1、2)
* 返 回 值:無
*******************************************************/
void DrawPoint( unsigned char X, unsigned char Y, unsigned char Color )
{
unsigned char Row , Tier , Tier_bit ;
unsigned char ReadOldH, ReadOldL ;
Tier = X >> 4 ;
Tier_bit = X & 0x0f ;
if( Y < 32 )
{
Row = Y ;
}
else
{
Row = Y - 32 ;
Tier += 8 ;
}
wr_cmd(Row + 0x80); //寫入行坐標(biāo)
wr_cmd(Tier + 0x80); //寫入列坐標(biāo)
rd_dat();
ReadOldH = rd_dat() ; //讀出原始數(shù)據(jù)
ReadOldL = rd_dat() ;
wr_cmd(Row + 0x80);
wr_cmd(Tier + 0x80);
if( Tier_bit < 8 )
{
switch( Color)
{
case 0 : ReadOldH &=( ~( 0x01 << ( 7 - Tier_bit ))) ; break ; //不顯示
case 1 : ReadOldH |= ( 0x01 << ( 7 - Tier_bit )) ; break ; //顯示
case 2 : ReadOldH ^= ( 0x01 << ( 7 - Tier_bit )) ; break ; //翻轉(zhuǎn)
default : break ;
}
wr_dat(ReadOldH); //寫入新數(shù)據(jù)
wr_dat(ReadOldL);
}
else
{
switch(Color)
{
case 0 : ReadOldL &= (~( 0x01 << ( 15 - Tier_bit ))) ; break ;
case 1 : ReadOldL |= ( 0x01 << ( 15 - Tier_bit )) ; break ;
case 2 : ReadOldL ^= ( 0x01 << ( 15 - Tier_bit )) ; break ;
default : break ;
}
wr_dat(ReadOldH);
wr_dat(ReadOldL);
}
}
/*******************************************************
* 函 數(shù) 名:void DrawFigure( unsigned char X, unsigned char Y, unsigned char Figure )
* 功能描述:在制定位置畫數(shù)字,7*5個(gè)像素點(diǎn)
* 函數(shù)說明:在制定位置畫數(shù)字
* 輸入?yún)?shù):unsigned char X, unsigned char Y, unsigned char Color
* X為列,Y為行,F(xiàn)igure為待顯示的數(shù)字
* 返 回 值:無
*******************************************************/
void DrawFigure( unsigned char X, unsigned char Y, unsigned char Figure )
{
unsigned char i=0,j=0;
for(j=0;j<5;j++)
{
for(i=0;i<7;i++)
{
DrawPoint(X+i,Y-j,(ucxFigure[Figure][j]>>i)&0x01);
}
}
}
/*******************************************************
* 函 數(shù) 名:void DrawLineX( unsigned char X0, unsigned char X1, unsigned char Y, unsigned char Color )
* 功能描述:畫一條水平線
* 函數(shù)說明:在制定位置畫一條水平線
* 輸入?yún)?shù):unsigned char X0, unsigned char X1, unsigned char Y, unsigned char Color
* X0列的起始位置,X1列的終止位置,Y行位置,COLOR
* 返 回 值:無
*******************************************************/
void DrawLineX( unsigned char X0, unsigned char X1, unsigned char Y, unsigned char Color )
{ unsigned char Temp ;
if( X0 > X1 )
{
Temp = X1 ;
X1 = X0 ;
X0 = Temp ;
}
for( ; X0 <= X1 ; X0++ )
DrawPoint( X0, Y, Color ) ;
}
/*******************************************************
* 函 數(shù) 名:void DrawLineY( unsigned char X, unsigned char Y0, unsigned char Y1, unsigned char Color )
* 功能描述:畫一條垂直線
* 函數(shù)說明:在制定位置畫一條垂直線
* 輸入?yún)?shù):unsigned char X, unsigned char Y0, unsigned char Y1, unsigned char Color
* X列位置,Y0行的起始位置,Y1行的終止位置,COLOR
* 返 回 值:無
*******************************************************/
void DrawLineY( unsigned char X, unsigned char Y0, unsigned char Y1, unsigned char Color )
{
unsigned char Temp ;
if( Y0 > Y1 )
{
Temp = Y1 ;
Y1 = Y0 ;
Y0 = Temp ;
}
for(; Y0 <= Y1 ; Y0++)
DrawPoint( X, Y0, Color);
}
/*******************************************************
* 函 數(shù) 名:void DrawLine( unsigned char StartX, unsigned char StartY, unsigned char EndX, unsigned char EndY, unsigned char Color )
* 功能描述:畫一條任意的線
* 函數(shù)說明:在起點(diǎn),終點(diǎn)之間畫一條直線
* 輸入?yún)?shù):unsigned char StartX, unsigned char StartY, unsigned char EndX, unsigned char EndY, unsigned char Color
* 返 回 值:無
*******************************************************/
void DrawLine( unsigned char StartX, unsigned char StartY, unsigned char EndX, unsigned char EndY, unsigned char Color )
{
int t, distance; /*根據(jù)屏幕大小改變變量類型(如改為int型)*/
int x = 0 , y = 0 , delta_x, delta_y ;
char incx, incy ;
delta_x = EndX - StartX ;
delta_y = EndY - StartY ;
if( delta_x > 0 )
{
incx = 1;
}
else if( delta_x == 0 )
{
DrawLineY( StartX, StartY, EndY, Color ) ;
return ;
}
else
{
incx = -1 ;
}
if( delta_y > 0 )
{
incy = 1 ;
}
else if(delta_y == 0 )
{
DrawLineX( StartX, EndX, StartY, Color ) ;
return ;
}
else
{
incy = -1 ;
}
delta_x = abs( delta_x );
delta_y = abs( delta_y );
if( delta_x > delta_y )
{
distance = delta_x ;
}
else
{
distance = delta_y ;
}
DrawPoint( StartX, StartY, Color ) ;
/* Draw Line*/
for( t = 0 ; t <= distance+1 ; t++ )
{
DrawPoint( StartX, StartY, Color ) ;
x += delta_x ;
y += delta_y ;
if( x > distance )
{
x -= distance ;
StartX += incx ;
}
if( y > distance )
{
y -= distance ;
StartY += incy ;
}
}
}
/*******************************************************
* 函 數(shù) 名:void DisGBStr(xchar *CorpInf)
* 功能描述:文本顯示
* 函數(shù)說明:寫入漢字
* 輸入?yún)?shù):xchar *CorpInf
* 返 回 值:無
*******************************************************/
void DisGBStr(unsigned char code *CorpInf)
{
uchar uc_GBCnt;
wr_cmd(0x30); //DL=1:8-BIT interface
wr_cmd(0x30); //RE=0:basic instruction
wr_cmd(0x0C); //Display ON,Cursor OFF,Cursor position blink OFF 。整體顯示開,光標(biāo)及其設(shè)置關(guān)。
wr_cmd(0x80);
for (uc_GBCnt=0;uc_GBCnt<16;uc_GBCnt++)
{
wr_dat(CorpInf[2 * uc_GBCnt]);
wr_dat(CorpInf[2 * uc_GBCnt + 1]);
};
wr_cmd(0x90);
for (uc_GBCnt=0;uc_GBCnt<16;uc_GBCnt++)
{
wr_dat(CorpInf[2 * uc_GBCnt + 32]);
wr_dat(CorpInf[2 * uc_GBCnt + 33]);
};
delays();
}
/*******************************************************
* 函 數(shù) 名:void Cram_Off()
* 功能描述:清除文本顯示
* 函數(shù)說明:清除文本顯示
* 輸入?yún)?shù):無
* 返 回 值:無
*******************************************************/
void Cram_Off(void)
{
wr_cmd(0x30); //DL=1:8-BIT interface
wr_cmd(0x30); //RE=0:basic instruction
wr_cmd(0x08); //Display OFF,Cursor OFF,Cursor position blink OFF
wr_cmd(0x01); //CLEAR
delay(250);
}
/*******************************************************
* 函 數(shù) 名:void Clr_GDRAM(void)
* 功能描述:清除繪圖顯示
* 函數(shù)說明:清除繪圖顯示
* 輸入?yún)?shù):無
* 返 回 值:無
*******************************************************/
void Clr_GDRAM(void)
{
uchar x,y;
for(y=0;y<64;y++)
for(x=0;x<16;x++)
{
wr_cmd(0x34);
wr_cmd(0x36);//開顯示
wr_cmd(y+0x80);
wr_cmd(x+0x80);
wr_dat(0x00);
wr_dat(0x00);
}
}
/*******************************************************
* 函 數(shù) 名:void Lcd_Init(void)
* 功能描述:12864初始化
* 函數(shù)說明:初始化
* 輸入?yún)?shù):無
* 返 回 值:無
*******************************************************/
void Lcd_Init(void)
{
PSB_Port =1;//并行接口
_nop_();
_nop_();
RST_Port =0;
delay(250);
delay(250);
delay(250);
delay(250);
RST_Port =1;
delay(250);
delay(250);
delay(250);
delay(250);
wr_cmd(0x30); //DL=1:8-BIT interface。功能設(shè)定,RE=0,基本指令集
wr_cmd(0x30); //RE=0:basic instruction
wr_cmd(0x06); //Entire display shift right by 1。光標(biāo)右移
Cram_Off(); //清空文本顯示
Clr_GDRAM(); //清空繪圖顯示
}
復(fù)制代碼
為大家提供一份12864的程序,是我花了很長時(shí)間根據(jù)網(wǎng)上程序精簡而成,比網(wǎng)上的版本運(yùn)行速度快了很多,并增加部分功能及注釋,很穩(wěn)定好用,希望能幫到大家。 包含了如下功能:
①在任意位置畫一個(gè)像素點(diǎn)
②畫一條任意長度的水平/垂直線
③畫一條任意線
④寫入漢字
⑤在任意位置寫入7*5個(gè)像素點(diǎn)的數(shù)字(0-9)
用12864時(shí),需要了解一點(diǎn),寫漢字和繪圖是兩個(gè)分開的功能!在說明文檔里一個(gè)叫文本顯示,一個(gè)叫繪圖,它倆使用的RAM也不一樣,一個(gè)是DDRAM,一個(gè)是GDRAM,使用時(shí)的相關(guān)命令及地址也都是不同的,包括清屏,也得分開清除。再有一點(diǎn)是,不知道是不是我的12864有些問題,個(gè)別漢字顯示錯(cuò)誤,比如我寫的“過”,它顯示的就是“!。
作者:
bbxyzzj
時(shí)間:
2017-3-6 08:04
感謝分享!
作者:
yangwen
時(shí)間:
2017-3-6 13:15
謝謝分享
作者:
xiuyueyuan2013
時(shí)間:
2017-3-6 13:52
感謝樓主分享
作者:
gaozhaohong
時(shí)間:
2017-3-6 14:49
非常感謝。。!
作者:
imxuheng
時(shí)間:
2017-3-6 15:38
謝謝分享,有值得借鑒的地方,不過DrawLine函數(shù)似乎不太好,推薦用Bresenham法來生成直線
作者:
lyl_420819
時(shí)間:
2017-5-17 10:11
好資料,下載學(xué)習(xí)了,謝謝分享。
作者:
實(shí)習(xí)生呢
時(shí)間:
2017-7-17 19:59
請問樓主,為什么,在高八位寫哪一點(diǎn),第八位就會寫哪一點(diǎn),高低是連體的嗎
作者:
似水流年jie
時(shí)間:
2018-5-7 16:09
很好,謝謝樓主,最近正在找這個(gè)資料呢,希望有幫助
作者:
545455
時(shí)間:
2018-5-8 14:29
感謝樓主
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1