title
收藏本站
联系站长
关于本站
首页 信息发布 产品宣传 论坛交流 学习文章 技术人生 项目源码 技术资料 人才收录
 今天是: 2011年8月21日 星期 日
欢迎光临!单片机群号:154389772 25930265 71062262 21829895 64584393 26583231  
热门文章推荐


当前位置:首页>>>单片机学习文章>> 1602液晶显示汉字程序
1602液晶显示汉字程序
作者:tamson给他留言 [转载] 字体:
发表于:
2009-08-09 13:47:46
利用1602显示汉字也不是新鲜的内容,今天不想颓废了。只是需要自己事先定义一下
汉字的字模,就是显示出来的汉子字体结构。 
要定义字模就需要知道1602的CG RAM地址和显示字符的地址 DD RAM 
图1: 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 
第一行第一个字符的地址是( 0x80) ,后面的字符地址以此类推 
第二行第一个字符的地址为 ( 0x80 + 0x40),需要在第一行的基地址后面加上一个偏移
量0x40,后面的地址在此基址上再依次加一。 
void delay(unsigned int time_i) 
{ int i; 
for (;tim>0;tim--) 


for (i=0;i<10;i++); 



void command_out (char out_data) 


RW=0; 
RS=0; 
EN=1; 
DB=out_data; 
EN=0; 
delay(10); 

void OUTD(char out_data) 

RS=1; 
RW=0; 
EN=1; 
DB= out_data; 
EN=0; 
delay(10); 

OUTI()是写入指令的函数, OUTD()是写入数据的函数。 
如: 
左上角第一个5*7的显示数据为: 
0x00,0x01,0x81,0x12,0x24,0x41,0x81,0x10 
蓝色区域的相应位为1 
这样就可以写出全部4个5*7点阵的显示数据了,把它封装在一个数组中,如数组,
hanzi[]={0x00,0x01,0x81,0x12,0x24,0x41,0x81,0x10, 
…… 
…… 
…… 

之后利用命令: 
OUTI(0X40); 
for (i=0;i<64;i++) 
{ OUTD(neu[i]);} 
将这个数组中的数据赋值到1602内部CGRAM中。完成后,再设置数据在1602上的
显示地址,根据图1的结构,设置好位置,如在第一行第一个字符和第二个字符以及第二行
第一个字符和第二个字符处显示: 
Command_out (0x80+0x00); 
for (i=0;i<4;i++) 
{ data_out (i); } 
Command_out(0x80+0x40); 
for (i=4;i<8;i++) 
{ data_out(i); } 
command_out()的作用是设置显示的起始地址,data_out()的作用是将hanzi[]数组中的16
进制数据发送出去,每一个16进制数字对应之5*7点阵上的每一行内容。第1行第1个字
符需要8个16进制数据,第1行第2个字符处也需要816进制数据。还有第2行的两个字
符点阵,这样显示一个汉字需要32个16进制数据。
下面列举一个实例(proteus仿真通过)
//***************************************************************************************
//硬件连接:1602VDD接5V,VO接地,BL1接5V,BL2接地,8根数据线接P0口,RS RW E分别接P2.0、P2.1、P.4口
//***************************************************************************************
#include <reg52.h>
#include <string.h>
#define Busy 0x80 //用于检测LCM状态字中的Busy标识
#define LCM_Data P0
sbit LCM_RS=P2^0;             //寄存器选择
sbit LCM_RW=P2^1;          //读/写控制
sbit LCM_E=P2^4;             //读/写使能
int i,j;
//自定义字符列表
//=====================================================================================
unsigned char character0[8] = {0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02},   //年
    character1[8] = {0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11}, //月
    character2[8] = {0x0f,0x09,0x09,0x09,0x0f,0x09,0x09,0x0f}, //日
    characterN[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //日
//==========================================================
//=========================================================
//延时程序
//=======================================================
void Delay5Ms(void)
{
unsigned long int TempCyc = 5552;
while(TempCyc--);
}
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
};
}
//===================================================================
//读写子程序
//=================================================================
//读数据
unsigned char ReadDataLCM(void)
{
LCM_RS = 1; 
LCM_RW = 1;
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1;
return(LCM_Data);
}
//读状态
unsigned char ReadStatusLCM(void)
{
LCM_Data = 0xFF; 
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1;
//while (LCM_Data & Busy); //检测忙信号
return(LCM_Data);
}
//写数据
void WriteDataLCM(unsigned char WDLCM)
{
ReadStatusLCM(); //检测忙
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 1; 
LCM_E = 0; //若晶振速度太高可以在这后加小的延时
for(i=0;i<100;i++);//延时
LCM_E = 1;
}
//写指令
void WriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC为0时忽略忙检测
{
if (BuysC) ReadStatusLCM(); //根据需要检测忙
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0; 
LCM_E = 1;
LCM_E = 0;
for(i=0;i<100;i++);
LCM_E = 1; 
}
//===========================================================
//初始化子程序
//===================================================================
void LCMInit(void) //LCM初始化
{
LCM_Data = 0;
WriteCommandLCM(0x38,0); // 三次显示模式设置,不检测忙信号
Delay5Ms(); 
WriteCommandLCM(0x38,0);
Delay5Ms(); 
WriteCommandLCM(0x38,0);
Delay5Ms(); 
WriteCommandLCM(0x38,1); // 显示模式设置,开始要求每次检测忙信号
Delay5Ms();
WriteCommandLCM(0x08,1); // 关闭显示
Delay5Ms();
WriteCommandLCM(0x01,1); // 清屏
Delay5Ms();
WriteCommandLCM(0x06,1); // 显示光标移动设置
Delay5Ms();
WriteCommandLCM(0x0c,1); // 显示开及光标设置
Delay5Ms();
}

//================================================================
//按指定位置显示一个字符
//===================================================================
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
X |= 0x80; //算出指令码
WriteCommandLCM(X, 0); //这里不检测忙信号,发送地址码
WriteDataLCM(DData);
}
//=================================================================
//按指定位置显示一串字符
//void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
//说明: x(0-15):x参数 y(0-1):y参数 DData(字符串):要显示的内容(英文、数字、符号)
//========================================================================

void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength,j;
ListLength = strlen(DData);
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
      if (X <= 0xF) //X坐标应小于0xF
        { 
      for(j=0;j<ListLength;j++)
        {
             DisplayOneChar(X, Y, DData[j]); //显示单个字符
             X++;
            }
        }
}
//==========================================================
//显示自定义字符
//void mychar(char xx,char yy,unsigned char *character,unsigned char saveto)
//说明:xx(0-15):为x参数.yy(0-1):y参数.character:要显示的字符的列表地址,在程序前面有定义
//saveto(1-7)为字符保存的RAM,每屏最多显示7个自定义字符
//(0x00-0x0h是自定义字符)
//=============================================================
void mychar(char xx,char yy,unsigned char *character,unsigned char saveto)
{
unsigned char add = (saveto<<3) | 0x40;
unsigned char t;      //临时变量,每一行的值
/*
t=*(character+0);
WriteCommandLCM(add, 0);     //第1行
WriteDataLCM(t);
t=*(character+1);
WriteCommandLCM(add+1, 0);     //第2行
WriteDataLCM(t);
t=*(character+2);
WriteCommandLCM(add+2, 0);     //第3行
WriteDataLCM(t);
t=*(character+3);
WriteCommandLCM(add+3, 0);     //第4行
WriteDataLCM(t);
t=*(character+4);
WriteCommandLCM(add+4, 0);     //第5行
WriteDataLCM(t);
t=*(character+5);
WriteCommandLCM(add+5, 0);     //第6行
WriteDataLCM(t);
t=*(character+6);
WriteCommandLCM(add+6, 0);     //第7行
WriteDataLCM(t);
t=*(character+7);
WriteCommandLCM(add+7, 0);     //第8行
WriteDataLCM(t);
*/
for(i = 0;i<8;i++)
{
   WriteCommandLCM(add+i, 0); 
WriteDataLCM(*(character+i));
}
DisplayOneChar(xx,yy,saveto);    //显示字符
}
//===============================================================
//主函数
//=========================================================
main()
{
Delay400Ms();
    LCMInit();
Delay400Ms();      //1602初始化
while(1)
{
DisplayListChar(0,0,"This is ListChar");
DisplayListChar(0,1,"!");
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
DisplayListChar(0,0,"This is OneChar:");
DisplayOneChar(0,1,0x4f);
DisplayOneChar(1,1,0x6e);
DisplayOneChar(2,1,0x65);
DisplayOneChar(3,1,0x21);
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
DisplayListChar(0,0,"This is MyChar:");
mychar(0,1, character0,1);
mychar(1,1, character1,2);
mychar(2,1, character2,3);
for(j=0;j<30;j++)for(i=0;i<30000;i++);
WriteCommandLCM(0x01,1); //清屏
Delay5Ms();
}
}
(本文引自www.mcujl.com/article.asp?conID=427)


---------------------------------------------------------------------------------------------------
[打印文章] [关闭本页] [返回顶部]
本网站部分资料转自网上,如有侵权请来信告明,我们会尽快删除  | 网站地图
Copyright @ 2007-2010 深圳单片机交流网.版权所有
网站创办者:詹长亮,周发辉,李林盛
网站支持:zcl843@163.com QQ:380476830 13723787271