基于51单片机的内含汉字库的LCM(二)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
128x64内含汉字库的液晶显示模块(LCM),具有并行和串行接口方式。有关8位并行接口,在上一篇博文中已经做过介绍,本文将介绍它串行接口方式的应用。
⒈ LCM的串行应用
本人使用的LCM型号为HXMG12864-GB,若要使用它的串行接口方式,需将板子上J8与VCC的连接断开,而将J8与GND接通(有些型号的模块需将J3断开而接通J4,一般需将与VCC的连接断开而连通GND)。
LCM在串行模式下,将使用两根引脚——时钟线(SCLK)和串行数据线(SID),来完成串行数据的传送。
当需要同时连接多个LCM时,引脚CS用于使能选择。当CS接高电平时,此模块才可以进行数据传输,如果CS接低电平,则模块的内部串行传输计数器与串行数据都将被重置,此时不能进行串行传输。如果系统中只有一个LCM,此时只需将CS接高电平即可。
从一个完整的串行数据传输过程来看,一开始需要先传输5个连续的1,作为开始标志,此时传输计数器将被重置并且串行数据将被同步;然后再传送传输方向位RW和寄存器选择位RS,最后第8位为0(向LCM发送指令—0XF8,向LCM传送数据—0XFA)。在接收到同步信号及RW和RS的开始信号后,每一个8位的指令(或数据)将被分为两个字节来传输。较高4位(DB7~DB4)的指令(数据)将被放在第一个字节的低4位,而较低4位(DB3~DB0)的指令(数据)将被放在第二个字节的低4位。相隔的其余4位都为0。串行数据传输的时序图如下所示。
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />
⒉ 硬件设计
这里采用AT89S52单片机作为控制器,当然也可以使用其它51系列单片机。而128x64内含汉字库的LCM工作于串形接口方式,完整的电原理图如下所示。
⒊ 软件设计
128x64内含汉字库的LCM工作于串形接口方式时,显示一首唐诗和一幅图形的流程图如下所示。
详细的C51程序如下所下。
/************************************
串行12864模块,SID-P2.1,SCLK-P2.2
AT89S52 12MHz
*************************************/
#include "reg51.h"
#define uchar unsigned char
#define uint unsigned int
sbit SID="P2"^1;
sbit SCLK="P2"^2;
/************************************************************
*朝辞白帝彩云间,千里江陵一日还,两岸猿声啼不住,轻舟已过万重山.
*************************************************************/
unsigned char ts[4][16]=
{
{
0xb3,0xaf,0xb4,0xc7,0xb0,0xd7,0xb5,0xdb,0xb2,0xca,0xd4,0xc6,0xbc,0xe4,0xa3,0xac
},
{
0xc7,0xa7,0xc0,0xef,0xbd,0xad,0xc1,0xea,0xd2,0xbb,0xc8,0xd5,0xbb,0xb9,0xa3,0xac
},
{
0xc1,0xbd,0xb0,0xb6,0xd4,0xb3,0xc9,0xf9,0xcc,0xe4,0xb2,0xbb,0xd7,0xa1,0xa3,0xac
},
{
0xc7,0xe1,0xd6,0xdb,0xd2,0xd1,0xb9,0xfd,0xcd,0xf2,0xd6,0xd8,0xc9,0xbd,0xa1,0xa3
}
};
/****1ms延时函数*******************************/
void delay1ms(uint t)
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<120;j++)
;
}
/*****发送指令子函数************/
void send_command(uchar command_data)
{
uchar i;
uchar i_data;
i_data=0xf8;
delay1ms(10); // 延时约10ms
//CS=1;
SCLK=0;
for(i=0;i<8;i++)
{ //开始标志,同步,写,命令
if(i_data&0x80)
SID=1;
else
SID=0;
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
i_data=command_data;
i_data&=0xf0;
for(i=0;i<8;i++)
{ //高4位
if(i_data&0x80)
SID=1;
else
SID=0;
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
i_data=command_data;
i_data=i_data<<4;
for(i=0;i<8;i++)
{ //低4位
if(i_data&0x80)
SID=1;
else
SID=0;
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
//CS=0;
}
/*****发送数据子函数************/
void send_data(uchar command_data)
{
uchar i;
uchar i_data;
i_data=0xfa;
delay1ms(10); // 延时约10ms
//CS=1;
for(i=0;i<8;i++)
{ ////开始标志,同步,写,数据
if(i_data&0x80)
SID=1;
else
SID=0;
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
i_data=command_data;
i_data&=0xf0;
for(i=0;i<8;i++)
{ //高4位
if(i_data&0x80)
SID=1;
else
SID=0;
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
i_data=command_data;
i_data=i_data<<4;
for(i=0;i<8;i++)
{ //低4位
if(i_data&0x80)
SID=1;
else
SID=0;
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
//CS=0;
}
/*****唐诗显示子函数************/
void display_cpubbs()
{
uchar command_data,m,n;
for(m=1;m<5;m++)
{
switch(m)
{
case 1:command_data=0x80; break;
case 2:command_data=0x90; break;
case 3:command_data=0x88; break;
case 4:command_data=0x98; break;
}
send_command(command_data);
for(n=0;n<16;n++)
send_data(ts[m-1][n]); //该行的唐诗
}
}
/********电脑桌面数据********************/
uchar code dnzm[]=
{
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xF0,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x18,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBF,0xFF,0xDC,0x00,0x01,
0x80,0x00,0x1F,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xBF,0xFF,0xBC,0x00,0x01,
0x80,0x00,0x30,0x00,0x01,0x80,0x00,0x00,0x00,0x00,0x01,0x7F,0xFF,0xB8,0x00,0x01,
0x80,0x00,0x30,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x01,0x7F,0xFF,0x78,0x00,0x01,
0x80,0x00,0x19,0xFF,0xFE,0xC0,0x00,0x00,0x00,0x00,0x02,0xFF,0xFF,0x70,0x00,0x01,
0x80,0x00,0x18,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x02,0xFF,0xFE,0xF0,0x00,0x01,
0x80,0x00,0x0C,0x01,0xFF,0x60,0x00,0x00,0x00,0x00,0x05,0xFF,0xFE,0xE0,0x00,0x01,
0x80,0x00,0x0C,0x7F,0xD0,0x20,0x00,0x00,0x00,0x00,0x05,0xFF,0xFD,0xE0,0x00,0x01,
0x80,0x00,0x06,0x00,0x0F,0xB0,0x00,0x00,0x00,0x00,0x0B,0xFF,0xFD,0xC0,0x00,0x01,
0x80,0x00,0x06,0x03,0xFC,0x10,0x00,0x00,0x00,0x00,0x0B,0xFF,0xFB,0xC0,0x00,0x01,
0x80,0x00,0x03,0x1F,0x00,0x18,0x00,0x00,0x00,0x00,0x17,0xFF,0xFB,0x80,0x00,0x01,
0x80,0x00,0x03,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x17,0xFF,0xF7,0x80,0x00,0x01,
0x80,0x00,0x01,0x80,0x00,0x0C,0x00,0x00,0x00,0x00,0x17,0xFF,0xF7,0x00,0x00,0x01,
0x80,0x00,0x01,0x80,0x00,0x04,0x00,0x00,0x00,0x00,0x09,0xFF,0xEF,0x00,0x00,0x01,
0x80,0x00,0x00,0xC0,0x00,0xF6,0x00,0x00,0x00,0x00,0x06,0x7F,0xEF,0x00,0x00,0x01,
0x80,0x00,0x00,0xC0,0x03,0xC2,0x00,0x00,0x00,0x00,0x01,0x9F,0xDE,0x00,0x00,0x01,
0x80,0x00,0x00,0x60,0x00,0x3B,0x00,0x00,0x00,0x00,0x00,0x67,0xDF,0x00,0x00,0x01,
0x80,0x00,0x00,0x60,0x00,0xE3,0x00,0x00,0x00,0x00,0x00,0x19,0xBF,0x00,0x00,0x01,
0x80,0x00,0x00,0x30,0x03,0x8E,0x00,0x00,0x00,0x00,0x00,0x06,0x3F,0x00,0x00,0x01,
0x80,0x00,0x00,0x30,0x06,0x3C,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0x01,
0x80,0x00,0x00,0x18,0x00,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0xEF,0x00,0x00,0x01,
0x80,0x00,0x00,0x18,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x0F,0x1F,0x80,0x00,0x01,
0x80,0x00,0x00,0x0C,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x1F,0xC0,0x00,0x01,
0x80,0x00,0x00,0x0C,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x07,0xE0,0x00,0x01,
0x80,0x00,0x00,0x06,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x1F,0xE0,0x00,0x01,
0x80,0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x3F,0xC0,0x00,0x01,
0x80,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0xFF,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFC,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xF0,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x05,0x40,0x88,0x04,0x01,0x08,0x00,0x00,0x0A,0x81,0x10,0x04,0x00,0x20,0x01,
0x80,0x79,0x21,0x08,0x02,0x21,0x49,0x00,0x00,0xF2,0x42,0x10,0x04,0x47,0x10,0x01,
0x80,0x09,0x07,0xDF,0x7F,0xF7,0xAA,0x00,0x00,0x12,0x0F,0xBE,0x7F,0xE5,0xFE,0x01,
0x80,0x7F,0xF4,0x51,0x08,0x81,0x2C,0x00,0x00,0xFF,0xE8,0xA2,0x44,0x47,0x00,0x01,
0x80,0x09,0x04,0x61,0x08,0x81,0x7F,0x00,0x00,0x12,0x08,0xC2,0x7F,0xC5,0x4A,0x01,
0x80,0x0B,0x24,0x51,0x08,0x83,0x81,0x00,0x00,0x16,0x48,0xA2,0x44,0x45,0x6A,0x01,
0x80,0x1D,0x47,0xC9,0x05,0x05,0x01,0x00,0x00,0x3A,0x8F,0x92,0x44,0x47,0x52,0x01,
0x80,0x68,0x84,0x49,0x05,0x05,0x7F,0x00,0x00,0xD1,0x08,0x92,0x7F,0xC5,0x6A,0x01,
0x80,0x09,0x94,0x41,0x02,0x01,0x01,0x00,0x00,0x13,0x28,0x82,0x44,0x05,0x4A,0x01,
0x80,0x0E,0x57,0xC1,0x0D,0x81,0x01,0x00,0x00,0x1C,0xAF,0x82,0x04,0x29,0x42,0x01,
0x80,0x38,0x34,0x4E,0x70,0x71,0x7F,0x00,0x00,0x70,0x68,0x9C,0x03,0xEB,0x7E,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
/*********显示图形子函数**********/
void display_photo()
{
uchar i,j,k=0x80;
send_command(0x3c); //设置8BIT控制,绘图OFF
for(i=0;i<32;i++)
{
send_command(k); //设置绘图区的Y地址坐标
k=k+1;
send_command(0x80); //设置绘图区的X地址坐标
for(j=0;j<16;j++)
send_data(dnzm[i*16+j]); //将点阵数据写入上半屏GDRAM
}
k=0x80;
for(i=0;i<32;i++)
{
send_command(k); //设置绘图区的Y地址坐标
k=k+1;
send_command(0x88); //设置绘图区的X地址坐标
for(j=0;j<16;j++)
send_data(dnzm[512+i*16+j]); //将点阵数据写入下半屏GDRAM
}
send_command(0x30);
send_command(0x01);
send_command(0x3e); //设置8BIT控制,绘图ON(显示图形)
}
// 主函数(程序入口)
main(void)
{
delay1ms(100); // 延时约100ms
send_command(0x30); //设置8位数据,基本指令集
send_command(0x0c); //开显示
for (;;)
{
send_command(0x30); //设置8位数据,基本指令集
send_command(0x01); //清DDRAM
display_cpubbs(); //调唐诗显示子函数
display_photo(); //调图形显示子函数
delay1ms(3000);
}
}
启动MedWin V3.0,执行菜单命令“项目管理”→“新建项目”创建新项目(或利用原有项目),然后再执行菜单命令“项目管理”→“新建文件”,文件名为“HLCDC”,并点选“C语言程序”,再点击“完成”,然后在编辑窗口中输入上述源程序,经编译无误后,在构建窗口中将显示无错误。注意:需执行菜单命令“设置”→“设置编译工具”→点选“指定路径下的编译工具”(该软件默认的是汇编语言),将C51文件夹放在适当的路径下(可利用Keil中的C51)。
⒈ 仿真调试
本人使用的硬件仿真器是万利公司的“ME-52HU”,将仿真头插入目标板中的CPU插座,先后接通目标板和仿真器的电源,点击快捷按钮“产生代码并装入”,将出现十几个调试的快捷按钮,此时可以点击某个快捷按钮进行调试。当点击“全速运行”按钮时,将会先后显示一首唐诗和电脑桌面图形,如下所示。
本人邮箱:txxyc104@163.com,欢迎来信讨论.
文章评论(0条评论)
登录后参与讨论