?? 12864zk.c
字號:
/*******************************************
* 文件名 : 12864ZK.C
* 功能 : 12864ZK驅(qū)動模塊
* 占用資源: 若干IO口
* 程序員 : wyl(wyl-e@163.com)
* 日期 : 2007.07
* 包含文件: 12864ZK.H
* 說明 : 本模塊支持12864ZK的串行及并行驅(qū)動.
********************************************/
#include"DELAY.H"
#include"12864ZK.H"
#include"_REG52_.H"
//---------------------------------------
#ifdef Paralell_Operation_Mode
/*******************************************
* 函數(shù)功能 :LCD忙標(biāo)志檢測
* 入口參數(shù) : (void)
* 占用資源 : (void)
* 返回參數(shù) : (void)
********************************************/
static void Busy_Check(void) //判忙標(biāo)志.
{
DatPort=0xff;
DI = 0; //command
RW = 1; //read
E = 1; //flip_latch enable
_nop_(); _nop_();
while(DatPort&0x80);
E = 0;
}
/*******************************************
* 函數(shù)功能 :LCD寫字節(jié)數(shù)據(jù)
* 入口參數(shù) : (uchar dat)
* 占用資源 : (void)
* 返回參數(shù) : (void)
********************************************/
static void Lcd_Write_Dat(uchar f_dat) //寫數(shù)據(jù).
{
Busy_Check();
DI = 1; //send dat.
RW = 0; //writing.
E = 1; //flip_latch enable.
DatPort=f_dat;
_nop_(); _nop_();
E = 0; //latch.
DatPort=0xff;
}
/*******************************************
* 函數(shù)功能 :LCD命令字節(jié)寫入
* 入口參數(shù) : (uchar com)
* 占用資源 : (void)
* 返回參數(shù) : (void)
********************************************/
static void Lcd_Write_Command(uchar f_com)
{
Busy_Check();
DI = 0;
RW = 0;
E = 1;
DatPort=f_com;
_nop_(); _nop_();
E = 0;
DatPort=0xff;
}
#endif
/***************串行工作模式******************/
#ifdef Series_Operation_Mode
/*******************************************
* 函數(shù)功能 :LCD串行寫數(shù)據(jù)或命令
* 入口參數(shù) : (uchar dat,bit di)
* 占用資源 : (void)
* 返回參數(shù) : (void)
********************************************/
#define READ 1
#define WRITE 0
#define DAT 1
#define INST 0
//---------------------------------------
static void Lcd_Write_DI(uchar f_dat,bit di)
{
uchar i;
uchar dat;
dat = f_dat;
SDA_12864 = 0;
CS_12864 = 1;
SDA_12864 = 1;
SCK_12864 = 0;
for(i=0;i<5;i++)
{
SCK_12864 = 1;
SCK_12864 = 0;
}
SDA_12864 = WRITE;
SCK_12864 = 1;
SCK_12864 = 0;
SDA_12864 = di;
SCK_12864 = 1;
SCK_12864 = 0;
SDA_12864 = 0;
SCK_12864 = 1;
SCK_12864 = 0;
for(i=0;i<4;i++)
{
if(dat&0x80)SDA_12864 = 1;
else SDA_12864 = 0;
SCK_12864 = 1;
SCK_12864 = 0;
dat<<=1;
}
SDA_12864 = 0;
for(i=0;i<4;i++)
{
SCK_12864 = 1;
SCK_12864 = 0;
}
for(i=0;i<4;i++)
{
if(dat&0x80)SDA_12864 = 1;
else SDA_12864 = 0;
SCK_12864 = 1;
SCK_12864 = 0;
dat<<=1;
}
SDA_12864 = 0;
for(i=0;i<4;i++) //四個空閑脈沖
{
SCK_12864 = 1;
SCK_12864 = 0;
}
CS_12864 = 0;
SDA_12864 = 1;
}
/*******************************************
* 函數(shù)功能 :LCD串行寫字節(jié)數(shù)據(jù)
* 入口參數(shù) : (uchar dat)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 操作時間 : 72us
********************************************/
void Lcd_Write_Dat(uchar formal_dat)
{
Lcd_Write_DI(formal_dat,DAT);
Delay_10us(8);//延時80us
}
/*******************************************
* 函數(shù)功能 :LCD串行寫字節(jié)命令
* 入口參數(shù) : (uchar com)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 操作時間 : 72us
********************************************/
void Lcd_Write_Command(uchar f_com)
{
Lcd_Write_DI(f_com,INST);
Delay_10us(8);//延時80us
}
#endif
/*****************基本指令集******************/
#define Basic_Instruction_Enable
//--------------------------------
#ifdef Basic_Instruction_Enable
//--------------------------------
/*******************************************
* 函數(shù)功能 :LCD功能設(shè)定
* 入口參數(shù) : (void)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
功能設(shè)定(36H/30H/34H) 0 0 1 1 X RE G 0
RE=1: 擴充指令集動作,RE=0: 基本指令集動作,
G=1 :繪圖顯示ON ,G=0 :繪圖顯示OFF
********************************************/
#define Basic_Instruction_Com 0x30 //精簡指令
#define Expand_Instruction_Com 0x34 //擴充指令
#define GraphRAM_On_Com 0x36 //繪圖功能開
//---------------------------------------
static void Function_Setting(void)
{
Lcd_Write_Command(Basic_Instruction_Com); //8bits format,basic instruction set,graph function disable.
Lcd_Write_Command(Basic_Instruction_Com);
}
/*******************************************
* 函數(shù)功能 :LCD顯示與游標(biāo)開關(guān)
* 入口參數(shù) : (void)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
顯示狀態(tài) 開/關(guān)(08H/0CH/0EH/0FH)
L L L L L L H D C B
D=1;整體顯示ON C=1;游標(biāo)ON B=1;游標(biāo)位置ON
********************************************/
#define All_On_Com 0x0f
#define Display_On_Only_Com 0x0c
#define Display_On_Cursor_On_Com 0x0e
#define All_Off_Com 0x08
//--------------------------------------
void NoniusOn(void)
{
Lcd_Write_Command(Basic_Instruction_Com);
Lcd_Write_Command(All_On_Com);
}
//---------------------------------------
void NoniusOff(void)
{
Lcd_Write_Command(Basic_Instruction_Com);
Lcd_Write_Command(Display_On_Only_Com);
}
/*******************************************
* 函數(shù)功能 :LCD狀態(tài)設(shè)置
* 入口參數(shù) : (void)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
********************************************/
//---------------------------------------
static void Display_State(void)
{
NoniusOff(); //display enable,cursor off,cursor position off.
}
/*******************************************
* 函數(shù)功能 :LCD設(shè)置光標(biāo)及移動方向
* 入口參數(shù) : (void)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
點設(shè)定(07H/04H/05H/06H)
L L L L L L L H I/D S
I/D=1 光標(biāo)右移,I/D=0 光標(biāo)左移。
SH=1 且DDRAM 為寫狀態(tài):整體顯示移動,
方向由I/D 決定(I/D=1 左移,I/D=0 右移)
SH=0 或DDRAM 為讀狀態(tài):整體顯示不移動
********************************************/
#define Cursor_Right_No_Move_Com 0x06
#define Cursor_Right_Move_Left_Com 0x07
#define Cursor_Left_No_Move_Com 0x04
#define Cursor_Left_Move_Right_Com 0x05
//---------------------------------------
static void Entry_Mode(void)
{
Lcd_Write_Command(Basic_Instruction_Com);
Lcd_Write_Command(Cursor_Right_No_Move_Com);
}
/*******************************************
* 函數(shù)功能 :LCD清屏
* 入口參數(shù) : (void)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 操作時間 : 4.2ms
********************************************/
void DisplayClear(void)
{
Lcd_Write_Command(Basic_Instruction_Com);
Lcd_Write_Command(0x01);
#ifdef Series_Operation_Mode
Delay_ms(5);//延時5ms
#endif
}
/*******************************************
* 函數(shù)功能 :LCD設(shè)置XY地址
* 入口參數(shù) : (uchar x,uchar y)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
********************************************/
void GotoXY(uchar fx,uchar fy)
{
uchar x,y,xy;
Lcd_Write_Command(Basic_Instruction_Com);
x = fx;
y = fy;
x &= 0x07;
y &= 0x03;
//xy coordinate is 0x80 in line 0,0x90 in line 1,0x88 in line 2,0x98 ine line 3.
if(y==0) xy=0x80;
else if(y==1) xy=0x90;
else if(y==2) xy=0x88;
else if(y==3) xy=0x98;
else xy=0x80;
xy += x;
Lcd_Write_Command(xy);
}
/*******************************************
* 函數(shù)功能 :LCD數(shù)據(jù)串輸出
* 入口參數(shù) : (uchar x,uchar y,uchar *str)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
********************************************/
void Print(uchar x,uchar y,uchar *str)
{
uchar i=0;
GotoXY(x,y);
Lcd_Write_Command(Basic_Instruction_Com);
while(((*str)!='\0')&&(i<16))
{
Lcd_Write_Dat(*str++);
i++;
}
}
/*******************************************
* 函數(shù)功能 :LCD數(shù)據(jù)串輸出
* 入口參數(shù) : (uchar x,uchar y,uchar *str,uchar cnt)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 : 限制輸出字符個數(shù)
********************************************/
void PrintLimit(uchar x,uchar y,uchar *str,uchar cnt)
{
uchar i=0;
GotoXY(x,y);
Lcd_Write_Command(Basic_Instruction_Com);
while(((*str)!='\0')&&(i<cnt))
{
Lcd_Write_Dat(*str++);
i++;
}
}
/*******************************************
* 函數(shù)功能 :LCD初始化
* 入口參數(shù) : (void)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
********************************************/
void LcdInitial(void)
{
Function_Setting(); //function setting.
Display_State(); //display state key.
DisplayClear(); //display clear
Entry_Mode(); //entry mode set
}
//---------------------------------------
#endif
//---------------------------------------
#ifdef Expand_Instruction_Enable
/************************************以下為擴充指令下的函數(shù)命令***********************/
/*******************************************
* 函數(shù)功能 :LCD反白選擇
* 入口參數(shù) : (bit line)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
反白選擇 0 0 0 0 0 0 0 1 R1 R0
選擇一、三行同時作反白顯示,
或者二、四行同時作反白顯示
line=0,reverse the line 1&3
else reverse line2&4
********************************************/
void ReverseDisplay(bit line)
{
Lcd_Write_Command(Expand_Instruction_Com);
line &= 0x01;
Lcd_Write_Command(0x04|line);
}
/*******************************************
* 函數(shù)功能 :LCD卷動允許
* 入口參數(shù) : (void)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
卷動地址或IRAM 地址選擇
0 0 0 0 0 0 0 0 1 SR
SR=1:允許輸入垂直卷動地址
SR=0:允許輸入IRAM 地址
********************************************/
#define Roll_Addr_Enable_Com 0x03
#define IRAM_Addr_Enable_Com 0x02
//---------------------------------------
void RollAddrEnable(void)
{
Lcd_Write_Command(Expand_Instruction_Com);
Lcd_Write_Command(Roll_Addr_Enable_Com);
}
/*******************************************
* 函數(shù)功能 :LCD設(shè)定IRAM地址或卷動地址
* 入口參數(shù) : (uchar addr)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
設(shè)定IRAM地址或卷動地址
0 1 AC5 AC4 AC3 AC2 AC1 AC0
SR=1:AC5—AC0 為垂直卷動地址
SR=0:AC3—AC0 為ICON IRAM 地址
********************************************/
void RollAddr(uchar f_addr)
{
Lcd_Write_Command(Expand_Instruction_Com);
Lcd_Write_Command(0x40|f_addr);
}
/*******************************************
* 函數(shù)功能 :LCD睡眠模式選擇
* 入口參數(shù) : (bit sleep)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
睡眠模式 0 0 0 0 0 0 1 SL X X
SL=1:脫離睡眠模式
SL=0:進入睡眠模式
********************************************/
void Sleep(bit sleep)
{
Lcd_Write_Command(Expand_Instruction_Com);
if(sleep)Lcd_Write_Command(0x08);
else Lcd_Write_Command(0x0c);
}
/*******************************************
* 函數(shù)功能 :LCD設(shè)定繪圖RAM 地址
* 入口參數(shù) : (uchar addr)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
設(shè)定繪圖RAM 地址
0 0 1 AC6 AC5 AC4 AC3 AC2 AC1 AC0
設(shè)定CGRAM 地址到地址計數(shù)器(AC)
********************************************/
/*
void CGRAMAddr(uchar f_addr)
{
Lcd_Write_Command(Expand_Instruction_Com);
Lcd_Write_Command(0x80|f_addr);
}
*/
/*******************************************
* 函數(shù)功能 :LCD左半屏繪圖
* 入口參數(shù) : (uchar *p)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
//取模方法:從右往左逐行取模.
15 ..8..1 15..8.1
1 2 3 4
********************************************/
void LeftPrintGraph(uchar *p)
{
uchar x,y,z;
for(z=0;z<9;z+=8)
for(y=0;y<0x20;y++)
for(x=0;x<4;x++)
{
Lcd_Write_Command(GraphRAM_On_Com);
Lcd_Write_Command(y+0x80); //Y,行地址
Lcd_Write_Command(x+0x80+z);
Lcd_Write_Command(Basic_Instruction_Com);
Lcd_Write_Dat(*p++);
Lcd_Write_Dat(*p++);
}
}
/*******************************************
* 函數(shù)功能 :LCD右半屏繪圖
* 入口參數(shù) : (uchar *p)
* 占用資源 : (void)
* 返回參數(shù) : (void)
* 說明 :
//取模方法:從右往左逐行取模.
15 ..8..1 15..8.1
1 2 3 4
********************************************/
void RightPrintGraph(uchar *p)
{
uchar x,y,z;
for(z=0;z<9;z+=8)
for(y=0;y<0x20;y++)
for(x=4;x<8;x++)
{
Lcd_Write_Command(GraphRAM_On_Com);
Lcd_Write_Command(y+0x80); //Y,行地址
Lcd_Write_Command(x+0x80+z);
Lcd_Write_Command(Basic_Instruction_Com);
Lcd_Write_Dat(*p++);
Lcd_Write_Dat(*p++);
}
}
#endif
/*******************************************
*** The END ***
********************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -