12864液晶屏驅動程序分享
#include《absacc.h》
#include《reg52.h》
#include《stdarg.h》
#include《stdio.h》
#defineulongunsignedlong
#defineuintunsignedint
#defineucharunsignedchar
#defineSTX0x02
#defineETX0x03
#defineEOT0x04
#defineENQ0x05
#defineBS0x08
#defineCR0x0D
#defineLF0x0A
#defineDLE0x10
#defineETB0x17
#defineSPACE0x20
#defineCOMMA0x2C
#defineTRUE1
#defineFALSE0
#defineHIGH1
#defineLOW0
//T6963C端口定義
#defineLCMDWXBYTE[0x5000]//數據口
#defineLCMCWXBYTE[0x5002]//命令口
//T6963C命令定義
#defineLC_CUR_POS0x21//光標位置設置
#defineLC_CGR_POS0x22//CGRAM偏置地址設置
#defineLC_ADD_POS0x24//地址指針位置
#defineLC_TXT_STP0x40//文本區首址
#defineLC_TXT_WID0x41//文本區寬度
#defineLC_GRH_STP0x42//圖形區首址
#defineLC_GRH_WID0x43//圖形區寬度
#defineLC_MOD_OR0x80//顯示方式:邏輯“或”
#defineLC_MOD_XOR0x81//顯示方式:邏輯“異或”
#defineLC_MOD_AND0x82//顯示方式:邏輯“與”
#defineLC_MOD_TCH0x83//顯示方式:文本特征
#defineLC_DIS_SW0x90//顯示開關:D0=1/0:光標閃爍啟用/禁用;
//D1=1/0:光標顯示啟用/禁用;
//D2=1/0:文本顯示啟用/禁用;
//D3=1/0:圖形顯示啟用/禁用;
#defineLC_CUR_SHP0xA0//光標形狀選擇:0xA0-0xA7表示光標占的行數
#defineLC_AUT_WR0xB0//自動寫設置
#defineLC_AUT_RD0xB1//自動讀設置
#defineLC_AUT_OVR0xB2//自動讀/寫結束
#defineLC_INC_WR0xC0//數據一次寫,地址加1
#defineLC_INC_RD0xC1//數據一次讀,地址加1
#defineLC_DEC_WR0xC2//數據一次寫,地址減1
#defineLC_DEC_RD0xC3//數據一次讀,地址減1
#defineLC_NOC_WR0xC4//數據一次寫,地址不變
#defineLC_NOC_RD0xC5//數據一次讀,地址不變
#defineLC_SCN_RD0xE0//屏讀
#defineLC_SCN_CP0xE8//屏拷貝
#defineLC_BIT_OP0xF0//位操作:D0-D2:定義D0-D7位;D3:1置位;0:清除
codeucharconstuPowArr[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
//ASCII字模寬度及高度定義
#defineASC_CHR_WIDTH8
#defineASC_CHR_HEIGHT12
//ASCII字模,顯示為8*16
charcodeASC_MSK[96*12]={
//Terminal9;此字體下對應的點陣為:寬x高=8x12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,//《0x20時,打印此字
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//‘’
0x00,0x0C,0x1E,0x1E,0x1E,0x0C,0x0C,0x00,0x0C,0x0C,0x00,0x00,//‘!’
0x00,0x66,0x66,0x66,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//‘“’
0x00,0x36,0x36,0x7F,0x36,0x36,0x36,0x7F,0x36,0x36,0x00,0x00,//‘#’
0x0C,0x0C,0x3E,0x03,0x03,0x1E,0x30,0x30,0x1F,0x0C,0x0C,0x00,//‘$’
0x00,0x00,0x00,0x23,0x33,0x18,0x0C,0x06,0x33,0x31,0x00,0x00,//‘%’
0x00,0x0E,0x1B,0x1B,0x0E,0x5F,0x7B,0x33,0x3B,0x6E,0x00,0x00,//‘&’
0x00,0x0C,0x0C,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//‘’‘
0x00,0x30,0x18,0x0C,0x06,0x06,0x06,0x0C,0x18,0x30,0x00,0x00,//’(‘
0x00,0x06,0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x06,0x00,0x00,//’)‘
0x00,0x00,0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00,0x00,0x00,//’*‘
0x00,0x00,0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00,0x00,0x00,//’+‘
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x1C,0x06,0x00,//’,‘
0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,//’-‘
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x1C,0x00,0x00,//’。‘
0x00,0x00,0x40,0x60,0x30,0x18,0x0C,0x06,0x03,0x01,0x00,0x00,//’/‘
0x00,0x3E,0x63,0x73,0x7B,0x6B,0x6F,0x67,0x63,0x3E,0x00,0x00,//’0‘
0x00,0x08,0x0C,0x0F,0x0C,0x0C,0x0C,0x0C,0x0C,0x3F,0x00,0x00,//’1‘
0x00,0x1E,0x33,0x33,0x30,0x18,0x0C,0x06,0x33,0x3F,0x00,0x00,//’2‘
0x00,0x1E,0x33,0x30,0x30,0x1C,0x30,0x30,0x33,0x1E,0x00,0x00,//’3‘
0x00,0x30,0x38,0x3C,0x36,0x33,0x7F,0x30,0x30,0x78,0x00,0x00,//’4‘
0x00,0x3F,0x03,0x03,0x03,0x1F,0x30,0x30,0x33,0x1E,0x00,0x00,//’5‘
0x00,0x1C,0x06,0x03,0x03,0x1F,0x33,0x33,0x33,0x1E,0x00,0x00,//’6‘
0x00,0x7F,0x63,0x63,0x60,0x30,0x18,0x0C,0x0C,0x0C,0x00,0x00,//’7‘
0x00,0x1E,0x33,0x33,0x37,0x1E,0x3B,0x33,0x33,0x1E,0x00,0x00,//’8‘
0x00,0x1E,0x33,0x33,0x33,0x3E,0x18,0x18,0x0C,0x0E,0x00,0x00,//’9‘
0x00,0x00,0x00,0x1C,0x1C,0x00,0x00,0x1C,0x1C,0x00,0x00,0x00,//’:‘
0x00,0x00,0x00,0x1C,0x1C,0x00,0x00,0x1C,0x1C,0x18,0x0C,0x00,//’;‘
0x00,0x30,0x18,0x0C,0x06,0x03,0x06,0x0C,0x18,0x30,0x00,0x00,//’《‘
0x00,0x00,0x00,0x00,0x7E,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,//’=‘
0x00,0x06,0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x06,0x00,0x00,//’》‘
0x00,0x1E,0x33,0x30,0x18,0x0C,0x0C,0x00,0x0C,0x0C,0x00,0x00,//’?‘
0x00,0x3E,0x63,0x63,0x7B,0x7B,0x7B,0x03,0x03,0x3E,0x00,0x00,//’@‘
0x00,0x0C,0x1E,0x33,0x33,0x33,0x3F,0x33,0x33,0x33,0x00,0x00,//’A‘
0x00,0x3F,0x66,0x66,0x66,0x3E,0x66,0x66,0x66,0x3F,0x00,0x00,//’B‘
0x00,0x3C,0x66,0x63,0x03,0x03,0x03,0x63,0x66,0x3C,0x00,0x00,//’C‘
0x00,0x1F,0x36,0x66,0x66,0x66,0x66,0x66,0x36,0x1F,0x00,0x00,//’D‘
0x00,0x7F,0x46,0x06,0x26,0x3E,0x26,0x06,0x46,0x7F,0x00,0x00,//’E‘
0x00,0x7F,0x66,0x46,0x26,0x3E,0x26,0x06,0x06,0x0F,0x00,0x00,//’F‘
0x00,0x3C,0x66,0x63,0x03,0x03,0x73,0x63,0x66,0x7C,0x00,0x00,//’G‘
0x00,0x33,0x33,0x33,0x33,0x3F,0x33,0x33,0x33,0x33,0x00,0x00,//’H‘
0x00,0x1E,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x1E,0x00,0x00,//’I‘
0x00,0x78,0x30,0x30,0x30,0x30,0x33,0x33,0x33,0x1E,0x00,0x00,//’J‘
0x00,0x67,0x66,0x36,0x36,0x1E,0x36,0x36,0x66,0x67,0x00,0x00,//’K‘
0x00,0x0F,0x06,0x06,0x06,0x06,0x46,0x66,0x66,0x7F,0x00,0x00,//’L‘
0x00,0x63,0x77,0x7F,0x7F,0x6B,0x63,0x63,0x63,0x63,0x00,0x00,//’M‘
0x00,0x63,0x63,0x67,0x6F,0x7F,0x7B,0x73,0x63,0x63,0x00,0x00,//’N‘
0x00,0x1C,0x36,0x63,0x63,0x63,0x63,0x63,0x36,0x1C,0x00,0x00,//’O‘
0x00,0x3F,0x66,0x66,0x66,0x3E,0x06,0x06,0x06,0x0F,0x00,0x00,//’P‘
0x00,0x1C,0x36,0x63,0x63,0x63,0x73,0x7B,0x3E,0x30,0x78,0x00,//’Q‘
0x00,0x3F,0x66,0x66,0x66,0x3E,0x36,0x66,0x66,0x67,0x00,0x00,//’R‘
0x00,0x1E,0x33,0x33,0x03,0x0E,0x18,0x33,0x33,0x1E,0x00,0x00,//’S‘
0x00,0x3F,0x2D,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x1E,0x00,0x00,//’T‘
0x00,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x1E,0x00,0x00,//’U‘
0x00,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x1E,0x0C,0x00,0x00,//’V‘
0x00,0x63,0x63,0x63,0x63,0x6B,0x6B,0x36,0x36,0x36,0x00,0x00,//’W‘
0x00,0x33,0x33,0x33,0x1E,0x0C,0x1E,0x33,0x33,0x33,0x00,0x00,//’X‘
0x00,0x33,0x33,0x33,0x33,0x1E,0x0C,0x0C,0x0C,0x1E,0x00,0x00,//’Y‘
0x00,0x7F,0x73,0x19,0x18,0x0C,0x06,0x46,0x63,0x7F,0x00,0x00,//’Z‘
0x00,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00,0x00,//’[‘
0x00,0x00,0x01,0x03,0x06,0x0C,0x18,0x30,0x60,0x40,0x00,0x00,//’\‘
0x00,0x3C,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x3C,0x00,0x00,//’]‘
0x08,0x1C,0x36,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//’^‘
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,//’_‘
0x0C,0x0C,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//’`‘
0x00,0x00,0x00,0x00,0x1E,0x30,0x3E,0x33,0x33,0x6E,0x00,0x00,//’a‘
0x00,0x07,0x06,0x06,0x3E,0x66,0x66,0x66,0x66,0x3B,0x00,0x00,//’b‘
0x00,0x00,0x00,0x00,0x1E,0x33,0x03,0x03,0x33,0x1E,0x00,0x00,//’c‘
0x00,0x38,0x30,0x30,0x3E,0x33,0x33,0x33,0x33,0x6E,0x00,0x00,//’d‘
0x00,0x00,0x00,0x00,0x1E,0x33,0x3F,0x03,0x33,0x1E,0x00,0x00,//’e‘
0x00,0x1C,0x36,0x06,0x06,0x1F,0x06,0x06,0x06,0x0F,0x00,0x00,//’f‘
0x00,0x00,0x00,0x00,0x6E,0x33,0x33,0x33,0x3E,0x30,0x33,0x1E,//’g‘
0x00,0x07,0x06,0x06,0x36,0x6E,0x66,0x66,0x66,0x67,0x00,0x00,//’h‘
0x00,0x18,0x18,0x00,0x1E,0x18,0x18,0x18,0x18,0x7E,0x00,0x00,//’i‘
0x00,0x30,0x30,0x00,0x3C,0x30,0x30,0x30,0x30,0x33,0x33,0x1E,//’j‘
0x00,0x07,0x06,0x06,0x66,0x36,0x1E,0x36,0x66,0x67,0x00,0x00,//’k‘
0x00,0x1E,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x7E,0x00,0x00,//’l‘
0x00,0x00,0x00,0x00,0x3F,0x6B,0x6B,0x6B,0x6B,0x63,0x00,0x00,//’m‘
0x00,0x00,0x00,0x00,0x1F,0x33,0x33,0x33,0x33,0x33,0x00,0x00,//’n‘
0x00,0x00,0x00,0x00,0x1E,0x33,0x33,0x33,0x33,0x1E,0x00,0x00,//’o‘
0x00,0x00,0x00,0x00,0x3B,0x66,0x66,0x66,0x66,0x3E,0x06,0x0F,//’p‘
0x00,0x00,0x00,0x00,0x6E,0x33,0x33,0x33,0x33,0x3E,0x30,0x78,//’q‘
0x00,0x00,0x00,0x00,0x37,0x76,0x6E,0x06,0x06,0x0F,0x00,0x00,//’r‘
0x00,0x00,0x00,0x00,0x1E,0x33,0x06,0x18,0x33,0x1E,0x00,0x00,//’s‘
0x00,0x00,0x04,0x06,0x3F,0x06,0x06,0x06,0x36,0x1C,0x00,0x00,//’t‘
0x00,0x00,0x00,0x00,0x33,0x33,0x33,0x33,0x33,0x6E,0x00,0x00,//’u‘
0x00,0x00,0x00,0x00,0x33,0x33,0x33,0x33,0x1E,0x0C,0x00,0x00,//’v‘
0x00,0x00,0x00,0x00,0x63,0x63,0x6B,0x6B,0x36,0x36,0x00,0x00,//’w‘
0x00,0x00,0x00,0x00,0x63,0x36,0x1C,0x1C,0x36,0x63,0x00,0x00,//’x‘
0x00,0x00,0x00,0x00,0x66,0x66,0x66,0x66,0x3C,0x30,0x18,0x0F,//’y‘
0x00,0x00,0x00,0x00,0x3F,0x31,0x18,0x06,0x23,0x3F,0x00,0x00,//’z‘
0x00,0x38,0x0C,0x0C,0x06,0x03,0x06,0x0C,0x0C,0x38,0x00,0x00,//’{‘
0x00,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x00,0x00,//’|‘
0x00,0x07,0x0C,0x0C,0x18,0x30,0x18,0x0C,0x0C,0x07,0x00,0x00,//’}‘
0x00,0xCE,0x5B,0x73,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//’~‘
};
typedefstructtypFNT_GB16//漢字字模顯示數據結構
{
charIndex[2];
charMsk[32];
};
structtypFNT_GB16xdataGB_16[]={//顯示為16*16
”中“,0x01,0x00,0x01,0x00,0x21,0x08,0x3F,0xFC,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,
”文“,0x02,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x08,0x20,0x08,0x20,0x08,0x20,0x04,0x40,0x04,0x40,0x02,0x80,0x01,0x00,0x02,0x80,0x04,0x60,0x18,0x1E,0xE0,0x08,0x00,0x00,
”測“,0x40,0x02,0x27,0xC2,0x24,0x42,0x84,0x52,0x45,0x52,0x55,0x52,0x15,0x52,0x25,0x52,0x25,0x52,0x25,0x52,0xC5,0x52,0x41,0x02,0x42,0x82,0x42,0x42,0x44,0x4A,0x48,0x04,
”試“,0x00,0x20,0x40,0x28,0x20,0x24,0x30,0x24,0x27,0xFE,0x00,0x20,0xE0,0x20,0x27,0xE0,0x21,0x20,0x21,0x10,0x21,0x10,0x21,0x0A,0x29,0xCA,0x36,0x06,0x20,0x02,0x00,0x00,
};
uchargCurRow,gCurCol;//當前行、列存儲,行高16點,列寬8點
ucharfnGetRow(void)
{
returngCurRow;
}
ucharfnGetCol(void)
{
returngCurCol;
}
ucharfnST01(void)//狀態位STA1,STA0判斷(讀寫指令和讀寫數據)
{
uchari;
for(i=10;i》0;i--)
{
if((LCMCW&0x03)==0x03)
break;
}
returni;//若返回零,說明錯誤
}
ucharfnST2(void)//狀態位ST2判斷(數據自動讀狀態)
{
uchari;
for(i=10;i》0;i--)
{
if((LCMCW&0x04)==0x04)
break;
}
returni;//若返回零,說明錯誤
}
ucharfnST3(void)//狀態位ST3判斷(數據自動寫狀態)
{
uchari;
for(i=10;i》0;i--)
{
if((LCMCW&0x08)==0x08)
break;
}
returni;//若返回零,說明錯誤
}
ucharfnST6(void)//狀態位ST6判斷(屏讀/屏拷貝狀態)
{
uchari;
for(i=10;i》0;i--)
{
if((LCMCW&0x40)==0x40)
break;
}
returni;//若返回零,說明錯誤
}
ucharfnPR1(ucharuCmd,ucharuPar1,ucharuPar2)//寫雙參數的指令
{
if(fnST01()==0)
return1;
LCMDW=uPar1;
if(fnST01()==0)
return2;
LCMDW=uPar2;
if(fnST01()==0)
return3;
LCMCW=uCmd;
return0;//返回0成功
}
ucharfnPR11(ucharuCmd,ucharuPar1)//寫單參數的指令
{
if(fnST01()==0)
return1;
LCMDW=uPar1;
if(fnST01()==0)
return2;
LCMCW=uCmd;
return0;//返回0成功
}
ucharfnPR12(ucharuCmd)//寫無參數的指令
{
if(fnST01()==0)
return1;
LCMCW=uCmd;
return0;//返回0成功
}
ucharfnPR13(ucharuData)//寫數據
{
if(fnST3()==0)
return1;
LCMDW=uData;
return0;//返回0成功
}
ucharfnPR2(void)//讀數據
{
if(fnST01()==0)
return1;
returnLCMDW;
}
//設置當前地址
voidfnSetPos(ucharurow,ucharucol)
{
uintiPos;
iPos=urow*30+ucol;
fnPR1(LC_ADD_POS,iPos&0xFF,iPos/256);
gCurRow=urow;
gCurCol=ucol;
}
//設置當前顯示行、列
voidcursor(ucharuRow,ucharuCol)
{
fnSetPos(uRow*16,uCol);
}
//清屏
voidcls(void)
{
uinti;
fnPR1(LC_ADD_POS,0x00,0x00);//置地址指針
fnPR12(LC_AUT_WR);//自動寫
for(i=0;i《240*30;i++)
{
fnST3();
fnPR13(0x00);//寫數據
}
fnPR12(LC_AUT_OVR);//自動寫結束
fnPR1(LC_ADD_POS,0x00,0x00);//重置地址指針
gCurRow=0;//置地址指針存儲變量
gCurCol=0;
}
//LCM初始化
charfnLCMInit(void)
{
if(fnPR1(LC_TXT_STP,0x00,0x00)!=0)//文本顯示區首地址
return-1;
fnPR1(LC_TXT_WID,0x1E,0x00);//文本顯示區寬度
fnPR1(LC_GRH_STP,0x00,0x00);//圖形顯示區首地址
fnPR1(LC_GRH_WID,0x1E,0x00);//圖形顯示區寬度
fnPR12(LC_CUR_SHP|0x01);//光標形狀
fnPR12(LC_MOD_OR);//顯示方式設置
fnPR12(LC_DIS_SW|0x08);//顯示開關設置
return0;
}
//ASCII(8*16)及漢字(16*16)顯示函數
uchardprintf(char*fmt,。。.)
{
va_listarg_ptr;
charc1,c2,cData;
chartmpBuf[64];//LCD顯示數據緩沖區
uchari=0,j,uLen,uRow,uCol;
uintk;
va_start(arg_ptr,fmt);
uLen=(uchar)vsprintf(tmpBuf,fmt,arg_ptr);
va_end(arg_ptr);
while(i《uLen)
{
c1=tmpBuf[i];
c2=tmpBuf[i+1];
uRow=fnGetRow();
uCol=fnGetCol();
if(c1》=0)
{//ASCII
if(c1《0x20)
{
switch(c1)
{
caseCR:
caseLF://回車或換行
i++;
if(uRow《112)
fnSetPos(uRow+16,0);
else
fnSetPos(0,0);
continue;
caseBS://退格
if(uCol》0)
uCol--;
fnSetPos(uRow,uCol);
cData=0x00;
break;
default://其他
c1=0x1f;
}
}
for(j=0;j《16;j++)
{
fnPR12(LC_AUT_WR);//寫數據
if(c1》=0x1f)
{
if(j《(16-ASC_CHR_HEIGHT))
fnPR13(0x00);
else
fnPR13(ASC_MSK[(c1-0x1f)*ASC_CHR_HEIGHT+j-(16-ASC_CHR_HEIGHT)]);
}
else
fnPR13(cData);
fnPR12(LC_AUT_OVR);
fnSetPos(uRow+j+1,uCol);
}
if(c1!=BS)//非退格
uCol++;
}
else
{//中文
for(j=0;j《sizeof(GB_16)/sizeof(GB_16[0]);j++)
{
if(c1==GB_16[j].Index[0]&&c2==GB_16[j].Index[1])
break;
}
for(k=0;k《sizeof(GB_16[0].Msk)/2;k++)
{
fnSetPos(uRow+k,uCol);
fnPR12(LC_AUT_WR);//寫數據
if(j《sizeof(GB_16)/sizeof(GB_16[0]))
{
fnPR13(GB_16[j].Msk[k*2]);
fnPR13(GB_16[j].Msk[k*2+1]);
}
else//未找到該字
{
if(k《sizeof(GB_16[0].Msk)/4)
{
fnPR13(0x00);
fnPR13(0x00);
}
else
{
fnPR13(0xff);
fnPR13(0xff);
}
}
fnPR12(LC_AUT_OVR);
}
uCol+=2;
i++;
}
if(uCol》=30)//光標后移
{
uRow+=16;
if(uRow《0x80)
uCol-=30;
else
{
uRow=0;
uCol=0;
}
}
fnSetPos(uRow,uCol);
i++;
}
returnuLen;
}
voidmain(void)//測試用
{
fnLCMInit();
cls();
cursor(0,0);
dprintf(”%s“,”Thisisatest:中文測試“);
}
評論
查看更多