找回密码
 立即注册

QQ登录

只需一步,快速开始

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

用LCD1602显示xingxiangrong的单片机源程序

[复制链接]
跳转到指定楼层
楼主
ID:167441 发表于 2018-8-9 16:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//功能:用LCD1602显示 xingxiangrong
//编译环境: KEIL UVISION2
//单片机晶振:12M  单片机型号AT89S52
//单片机晶振: 无特殊要求
//作者:兴向荣电子元件店
//日期:2013.07.02
//功能:

#include <reg52.h>
#include <intrins.h>

#define uchar unsigned char
#define uint  unsigned int


//==============LCD1602接口连接方法=====================
/*-----------------------------------------------------
       |DB0-----P0.0 | DB4-----P0.4 | RW-------P2.3    |
       |DB1-----P0.1 | DB5-----P0.5 | RS-------P2.4    |
       |DB2-----P0.2 | DB6-----P0.6 | E--------P2.2    |
       |DB3-----P0.3 | DB7-----P0.7 |
    ---------------------------------------------------*/
//================================================*/              
#define LCM_Data     P0    //LCD1602数据接口
#define Busy         0x80   //用于检测LCM状态字中的Busy标识
sbit    LCM_RS     = P2^6;  //读写控制输入端,LCD1602的第五脚
sbit    LCM_RW     = P2^5;  //寄存器选择输入端,LCD1602的第四脚
sbit    LCM_E      = P2^7;  //使能信号输入端,LCD1602的第6脚


sbit   beek_c  =P2^2;
sbit   lock_c  =P2^3;
//**************函数声明***************************************
void        WriteDataLCM        (uchar WDLCM);//LCD模块写数据
void         WriteCommandLCM        (uchar WCLCM,BuysC); //LCD模块写指令
uchar   ReadStatusLCM(void);//读LCD模块的忙标
void    DisplayOneChar(uchar X,uchar Y,uchar ASCII);//在第X+1行的第Y+1位置显示一个字符
void    LCMInit(void);//LCD初始
void    delayms(uint ms);//1MS基准延时程序
void    DisplayListChar(uchar X,uchar Y,uchar delayms, uchar code *DData);
void   judge_xianshi(void);//显示处理程序



uchar flag1;
//***********************主程序******************************
main()   
{   
   beek_c=1;//关闭蜂鸣器
   lock_c=0;//关闭锁
   LCMInit();
   judge_xianshi();
   while(1);
}

void   judge_xianshi()
{

  DisplayListChar(0,1,0, "xingxiangrong");//在液晶的第一行显示字符"XINGXIANGR"

}

/*====================================================================  
  按指定位置显示一串字符:第 X 行,第 y列
  注意:字符串不能长于16个字符
======================================================================*/
void DisplayListChar(uchar X,uchar Y,uchar ms, uchar code *DData)
{
unsigned char ListLength;

ListLength = 0;

X &= 0x1;
Y &= 0xF; //限制X不能大于15,Y不能大于1
while (DData[ListLength]!='\0') //若到达字串尾则退出
  {
     if (Y <= 0xF) //X坐标应小于0xF
     {
        DisplayOneChar(X, Y, DData[ListLength]); //显示单个字符
        ListLength++;
        Y++;
            delayms(ms);//延时显示字符串
     }
     else
            break;//跳出循环体
  }
}
/*======================================================================
LCM初始化
======================================================================*/
void LCMInit(void)
{
LCM_Data = 0;
WriteCommandLCM(0x38,0); //三次显示模式设置,不检测忙信号
delayms(5);
WriteCommandLCM(0x38,0);
delayms(5);
WriteCommandLCM(0x38,0);
delayms(5);
WriteCommandLCM(0x38,1); //显示模式设置,开始要求每次检测忙信号
WriteCommandLCM(0x08,1); //关闭显示
WriteCommandLCM(0x01,1); //显示清屏
WriteCommandLCM(0x06,1); // 显示光标移动设置
WriteCommandLCM(0x0C,1); // 显示开及光标设置
delayms(100);
}
//==============================LCD1602显示子程序================================================
// 写数据函数: E =高脉冲 RS=1 RW=0
//======================================================================*/
void WriteDataLCM(uchar WDLCM)
{
ReadStatusLCM(); //检测忙
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 0; //若晶振速度太高可以在这后加小的延时
LCM_E = 0; //延时
LCM_E = 1;
}
/*====================================================================
  写指令函数: E=高脉冲 RS=0 RW=0
======================================================================*/
void WriteCommandLCM(uchar WCLCM,BuysC) //BuysC为0时忽略忙检测
{
if (BuysC) ReadStatusLCM(); //根据需要检测忙
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 0;
LCM_E = 0;
LCM_E =1;
}
/*====================================================================
  正常读写操作之前必须检测LCD控制器状态:E=1 RS=0 RW=1;
  DB7: 0 LCD控制器空闲,1 LCD控制器忙。
  读状态
======================================================================*/
uchar ReadStatusLCM(void)
{
LCM_Data = 0xFF;
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
while (LCM_Data & Busy); //检测忙信号  
return(LCM_Data);
}
/*======================================================================
功 能:     在1602 指定位置显示一个字符:第一行位置0~15,第二行16~31
说 明:     第 X 行,第 y 列  注意:字符串不能长于16个字符
======================================================================*/
void DisplayOneChar( uchar X, uchar Y, uchar ASCII)
{
  X &= 0x1;
  Y &= 0xF; //限制Y不能大于15,X不能大于1
  if (X) Y |= 0x40; //当要显示第二行时地址码+0x40;
  Y |= 0x80; // 算出指令码
  WriteCommandLCM(Y, 0); //这里不检测忙信号,发送地址码
  WriteDataLCM(ASCII);
}
/*====================================================================
  设定延时时间:x*1ms
====================================================================*/
void delayms(uint Ms)
{
  uint i,TempCyc;
  for(i=0;i<Ms;i++)
  {
    TempCyc =70;
    while(TempCyc--);
  }
}


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

使用道具 举报

沙发
ID:277550 发表于 2018-8-9 21:13 | 只看该作者
再试试不用添加元件,能不能不添加元件实现2IO驱动(不算RW/RS/E)。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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