📄 12864zk.c
字号:
/*******************************************
* 文件名 : 12864ZK.C
* 功能 : 12864ZK驱动模块
* 占用资源: 若干IO口
* 程序员 : wyl(wyl-e@163.com)
* 日期 : 2007.07
* 包含文件: 12864ZK.H
* 说明 : 本模块支持12864ZK的串行及并行驱动.
********************************************/
#include"DELAY.H"
#include"12864ZK.H"
#include"_REG52_.H"
//---------------------------------------
#ifdef Paralell_Operation_Mode
/*******************************************
* 函数功能 :LCD忙标志检测
* 入口参数 : (void)
* 占用资源 : (void)
* 返回参数 : (void)
********************************************/
static void Busy_Check(void) //判忙标志.
{
DatPort=0xff;
DI = 0; //command
RW = 1; //read
E = 1; //flip_latch enable
_nop_(); _nop_();
while(DatPort&0x80);
E = 0;
}
/*******************************************
* 函数功能 :LCD写字节数据
* 入口参数 : (uchar dat)
* 占用资源 : (void)
* 返回参数 : (void)
********************************************/
static void Lcd_Write_Dat(uchar f_dat) //写数据.
{
Busy_Check();
DI = 1; //send dat.
RW = 0; //writing.
E = 1; //flip_latch enable.
DatPort=f_dat;
_nop_(); _nop_();
E = 0; //latch.
DatPort=0xff;
}
/*******************************************
* 函数功能 :LCD命令字节写入
* 入口参数 : (uchar com)
* 占用资源 : (void)
* 返回参数 : (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
/*******************************************
* 函数功能 :LCD串行写数据或命令
* 入口参数 : (uchar dat,bit di)
* 占用资源 : (void)
* 返回参数 : (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;
}
/*******************************************
* 函数功能 :LCD串行写字节数据
* 入口参数 : (uchar dat)
* 占用资源 : (void)
* 返回参数 : (void)
* 操作时间 : 72us
********************************************/
void Lcd_Write_Dat(uchar formal_dat)
{
Lcd_Write_DI(formal_dat,DAT);
Delay_10us(8);//延时80us
}
/*******************************************
* 函数功能 :LCD串行写字节命令
* 入口参数 : (uchar com)
* 占用资源 : (void)
* 返回参数 : (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
//--------------------------------
/*******************************************
* 函数功能 :LCD功能设定
* 入口参数 : (void)
* 占用资源 : (void)
* 返回参数 : (void)
* 说明 :
功能设定(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);
}
/*******************************************
* 函数功能 :LCD显示与游标开关
* 入口参数 : (void)
* 占用资源 : (void)
* 返回参数 : (void)
* 说明 :
显示状态 开/关(08H/0CH/0EH/0FH)
L L L L L L H D C B
D=1;整体显示ON C=1;游标ON B=1;游标位置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);
}
/*******************************************
* 函数功能 :LCD状态设置
* 入口参数 : (void)
* 占用资源 : (void)
* 返回参数 : (void)
* 说明 :
********************************************/
//---------------------------------------
static void Display_State(void)
{
NoniusOff(); //display enable,cursor off,cursor position off.
}
/*******************************************
* 函数功能 :LCD设置光标及移动方向
* 入口参数 : (void)
* 占用资源 : (void)
* 返回参数 : (void)
* 说明 :
点设定(07H/04H/05H/06H)
L L L L L L L H I/D S
I/D=1 光标右移,I/D=0 光标左移。
SH=1 且DDRAM 为写状态:整体显示移动,
方向由I/D 决定(I/D=1 左移,I/D=0 右移)
SH=0 或DDRAM 为读状态:整体显示不移动
********************************************/
#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);
}
/*******************************************
* 函数功能 :LCD清屏
* 入口参数 : (void)
* 占用资源 : (void)
* 返回参数 : (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
}
/*******************************************
* 函数功能 :LCD设置XY地址
* 入口参数 : (uchar x,uchar y)
* 占用资源 : (void)
* 返回参数 : (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);
}
/*******************************************
* 函数功能 :LCD数据串输出
* 入口参数 : (uchar x,uchar y,uchar *str)
* 占用资源 : (void)
* 返回参数 : (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++;
}
}
/*******************************************
* 函数功能 :LCD数据串输出
* 入口参数 : (uchar x,uchar y,uchar *str,uchar cnt)
* 占用资源 : (void)
* 返回参数 : (void)
* 说明 : 限制输出字符个数
********************************************/
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++;
}
}
/*******************************************
* 函数功能 :LCD初始化
* 入口参数 : (void)
* 占用资源 : (void)
* 返回参数 : (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
/************************************以下为扩充指令下的函数命令***********************/
/*******************************************
* 函数功能 :LCD反白选择
* 入口参数 : (bit line)
* 占用资源 : (void)
* 返回参数 : (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);
}
/*******************************************
* 函数功能 :LCD卷动允许
* 入口参数 : (void)
* 占用资源 : (void)
* 返回参数 : (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);
}
/*******************************************
* 函数功能 :LCD设定IRAM地址或卷动地址
* 入口参数 : (uchar addr)
* 占用资源 : (void)
* 返回参数 : (void)
* 说明 :
设定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);
}
/*******************************************
* 函数功能 :LCD睡眠模式选择
* 入口参数 : (bit sleep)
* 占用资源 : (void)
* 返回参数 : (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);
}
/*******************************************
* 函数功能 :LCD设定绘图RAM 地址
* 入口参数 : (uchar addr)
* 占用资源 : (void)
* 返回参数 : (void)
* 说明 :
设定绘图RAM 地址
0 0 1 AC6 AC5 AC4 AC3 AC2 AC1 AC0
设定CGRAM 地址到地址计数器(AC)
********************************************/
/*
void CGRAMAddr(uchar f_addr)
{
Lcd_Write_Command(Expand_Instruction_Com);
Lcd_Write_Command(0x80|f_addr);
}
*/
/*******************************************
* 函数功能 :LCD左半屏绘图
* 入口参数 : (uchar *p)
* 占用资源 : (void)
* 返回参数 : (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++);
}
}
/*******************************************
* 函数功能 :LCD右半屏绘图
* 入口参数 : (uchar *p)
* 占用资源 : (void)
* 返回参数 : (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 ***
********************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -