找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3094|回复: 0
打印 上一主题 下一主题
收起左侧

LCD1602显示汉字 年月日

[复制链接]
跳转到指定楼层
楼主
ID:114039 发表于 2016-4-13 19:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//***************************************************************************************
//硬件连接: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 = P3^7;             //寄存器选择
sbit LCM_RW = P3^6;          //读/写控制
sbit LCM_E = P3^5;             //读/写使能
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--);
}
//========================================第5页========================================
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);
}
//写数据
//========================================第6页========================================
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); // 关闭显示
//========================================第7页========================================
        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
//========================================第8页========================================
    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行
        //========================================第9页========================================
        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);
                //========================================第10页========================================
                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();
        }
}



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表