📄 ca12864.c
字号:
/*********************************************************************
【程序名称】12864液晶显示器串行操作关键函数
【编写人】王兵洋
【个人档】任职于平安力合有限公司——硬件工程师
【版权声明】欢迎转载,请注明原作者
【本程序提供的外部函数】
extern void Lcd_Write_Str(UCHAR l_line,UCHAR l_site,UCHAR * words);//写字符串
extern void Lcd_Init(void);//初始化
extern void Lcd_Write_Bmp(unsigned char *puts); //写图片
void Lcd_Clr_Scr(void); //清屏
*********************************************************************/
#include <reg52.h>
//数据类型定义
typedef unsigned char UCHAR;
typedef unsigned int UINT;
//功能管脚定义
sbit LCM_CS = P1^0; //片选
sbit LCM_SCK = P1^1; //时钟
sbit LCM_SID = P1^2; //数据
sbit LCM_BLK = P1^3; //背光
sbit LCM_RST = P1^4; //复位
/***************************************************************************
【功能说明】串行发送一个字节
【输入参数】欲发送字节,输出参数:无
***************************************************************************/
void Send_Byte(UCHAR lcd_byte)
{
register UCHAR i;
LCM_SCK = 0;
for(i=0;i<8;i++)
{
lcd_byte <<= 1; //左移一位
LCM_SID = CY; //移出的位给SID
LCM_SCK = 1;
LCM_SCK = 0;
}
}
/***************************************************************************
【功能说明】串行读取一个命令字节
【输入参数】无,输出参数:读取的字节数据
****************************************************************************/
UCHAR Lcd_Read_Cmd(void)
{
register UCHAR i;
register UCHAR temp;
LCM_CS = 0;
LCM_SID = 0;
LCM_SCK = 0;
LCM_CS = 1;
Send_Byte(0xfc);//首先发送读命令
//11111,RW(1),RS(0),0
temp = 0;
for (i=0;i<8;i++)
{
LCM_SCK = 1;
temp <<=1;
temp |= LCM_SID;
LCM_SCK = 0;
} //读出高四位
temp >>= 4;
for (i=0;i<4;i++)
{
LCM_SCK = 1;
temp <<=1;
temp |= LCM_SID;
LCM_SCK = 0;
} //读出第四位
for (i=0;i<4;i++)
{
LCM_SCK = 1;
LCM_SCK = 0;
}
LCM_CS = 0;
LCM_SID = 0;
LCM_SCK = 0;
return temp;
}
/***************************************************************************
【功能说明】串行写入一个字节
【输入参数】lcd_data-欲写入字节,cmd_data-数据/命令选择位
【输出参数】无
****************************************************************************/
void Lcd_Write_Byte(UCHAR lcd_data,bit cmd_data)
{
UCHAR lcdopt;
register UCHAR temp;
EA = 0; //禁用所有中断
lcdopt = 0;
do
{
lcdopt = Lcd_Read_Cmd();
}
while (lcdopt&0x80);
LCM_CS = 0;
LCM_SID = 0;
LCM_SCK = 0;
LCM_CS = 1;
// 1111 1 RW RS 0
//RW为0,选择写;RS为1,选择数据;RS为0,选择指令
if(cmd_data==1)
Send_Byte(0xfa);//发送数据
else
Send_Byte(0xf8);//发送指令
temp = lcd_data&0xf0;
Send_Byte(temp); //传输高四位
temp = (lcd_data<<4)&0xf0;
Send_Byte(temp); //传输低四位
LCM_CS = 0;
LCM_SID = 0;
LCM_SCK = 0;
EA=1; //恢复中断
}
/***************************************************************************
【功能说明】写入一个命令
【输入参数】写入命令字节,输出参数:无
***************************************************************************/
void Lcd_Write_Cmd(UCHAR lcd_data)
{
Lcd_Write_Byte(lcd_data,0);
}
/***************************************************************************
【功能说明】:写入一个数据
【输入参数】:欲写入数据,输出参数:无
***************************************************************************/
void Lcd_Write_Data(UCHAR lcd_data)
{
Lcd_Write_Byte(lcd_data,1);
}
/***************************************************************************
【功能说明】12864初始化
【输入输出参数】无
***************************************************************************/
void Lcd_Init(void)
{
LCM_BLK = 0;//打开背光
Lcd_Write_Cmd(0x30); //基本命令集
Lcd_Write_Cmd(0x30);
Lcd_Write_Cmd(0x0C); //显示ON,游标OFF,游标位反白OFF
Lcd_Write_Cmd(0x06); //写入时,游标右移动
Lcd_Write_Cmd(0x01); //清除显示
//Os_Wait2(K_TMO,1);
Lcd_Write_Cmd(0x02); //位址归位
Lcd_Write_Cmd(0x06); //写入时,游标右移动
//Os_Wait2(K_TMO,1); //根据所用单片机晶振加入等待
}
/***************************************************************************
【功能说明】:写入字符串
【输入参数】:l_line:行0-3; l_site:列 0-7; words:中英字符
【输出参数】:无
***************************************************************************/
void Lcd_Write_Str(UCHAR l_line,UCHAR l_site,UCHAR * words)
{
UCHAR c;
c = 0;
l_site = l_site % 8; //使列在0-7范围
if(l_line>1) //若行=2,3
{
l_line = l_line % 2; //2行变0行,3行变1行
l_site = l_site + 8; //列加8
}
l_site = (l_line<<4) + l_site; //行*16+列
l_site = (l_site&0x3f)|0x80;
Lcd_Write_Cmd(l_site); //DDRAM地址
while(*words != 0) //写字符
{
UCHAR tempdata;
tempdata = *words;
Lcd_Write_Data(tempdata);
words++;
l_site++;
c++;
}
if (c%2) //写完数据,若最后一个字符列号是奇数则补空格
{
Lcd_Write_Data(0x20);
}
}
/***************************************************************************
【功能说明】写入一副图像
【输入参数】puts——图像数据起点
【图像数据格式要求】图片尺寸:128*64,横向取模左高位,数据排列:从左到右从上到下
【输出参数】无
****************************************************************************/
void Lcd_Write_Bmp(unsigned char *puts)
{
unsigned int x=0;
unsigned char i,j;
Lcd_Write_Cmd(0x34);
Lcd_Write_Cmd(0x34); //8Bit扩充指令集,绘图OFF,要写两次
for(i=0;i<32;i++) //12864实际为256x32
{
Lcd_Write_Cmd(i|0x80); //行位置
Lcd_Write_Cmd(0x80); //列位置
for(j=0;j<16;j++) //先写上半帧//再写下半帧
{ //列位置每行自动增加
Lcd_Write_Data(puts[x]);
x++;
}
x-=16;
for(j=0;j<16;j++) //先写上半帧//再写下半帧
{ //列位置每行自动增加
Lcd_Write_Data(puts[x+0x200]);
x++;
}
}
Lcd_Write_Cmd(0x36);
Lcd_Write_Cmd(0x36); //绘图ON,基本指令集里面36H不能开绘图
Lcd_Write_Cmd(0x30);
Lcd_Write_Cmd(0x30); //恢复基本指令集
}
/***************************************************************************
【功能说明】:清屏
【输入输出参数】:无
***************************************************************************/
void Lcd_Clr_Scr(void)
{
Lcd_Write_Cmd(0x01); //清屏
}
/*******************************************************************/
/*************************中华人民共和国****************************/
/*******************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -