专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

LCD1602液晶显示(51单片机c语言)

作者:佚名   来源:本站原创   点击数:  更新时间:2014年04月10日   【字体:


效果图:
电路图:

#include<reg52.h> 
#include<intrins.h>  //包含_nop_()函数定义的头文件
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^5;    //寄存器选择位
sbit RW=P2^6;    //读写选择位
sbit E=P2^7;     //使能信号位
//sbit BF=P0^7;    //忙碌标志位
const uchar string1[ ]={"    mazirong     "};
const uchar string2[ ]={"TEL:13297961386  "};
/*****************************************************
函数功能:判断液晶模块的忙碌状态
返回值:result。result=1,忙碌;result=0,不忙
***************************************************/
/*uchar BusyTest()
{
    bit result;
 RS=0;       //根据规定,RS为低电平,RW为高电平时,可以读状态
    RW=1;
    E=1;        //E=1,才允许读写
    _nop_();   //空操作
    _nop_();
    _nop_();
    _nop_();   //空操作四个机器周期,给硬件反应时间
    result=BF;  //将忙碌标志电平赋给result
 E=0;
    return result;
}*/
//*********************延时
void delay(uchar z)
{
 uchar i,j;
 for(i=z;i>0;i--);
  for(j=110;j>0;j--);
}
//********************写指令
void WriteInstruction(uchar dictate)

// while(BusyTest()==1);
 RS=0;                  //根据规定,RS和R/W同时为低电平时,可以写入指令
 RW=0; 
 E=0;                   //E置低电平(写指令时,E为高脉冲)
                             // 就是让E从0到1发生正跳变,所以应先置"0"
 P0=dictate;            //将数据送入P0口,即写入指令或地址
 delay(1);
 /*_nop_();
   _nop_();
   _nop_();
  _ nop_();*/
  E=1;                   //E置高电平
  _nop_();
  _nop_();
  _nop_();
  _nop_();               //空操作四个机器周期,给硬件反应时间
   E=0;                  //当E由高电平跳变成低电平时,液晶模块开始执行命令
}
//*********************写数据
void WriteData(uchar dat)
{
// while(BusyTest()==1);  
 RS=1;   //RS为高电平,RW为低电平时,可以写入数据
 RW=0;
 E=0;            //(写指令时,E为高脉冲)
                    // 就是让E从0到1发生正跳变,所以应先置"0"
 P0=dat;         //将数据送入P0口,即将数据写入液晶模块
 delay(1);
 /*_nop_();
   _nop_();
   _nop_();
  _ nop_();*/
  E=1;          //E置高电平
  _nop_();
  _nop_();
  _nop_();
  _nop_();      //空操作四个机器周期,给硬件反应时间
  E=0;          //当E由高电平跳变成低电平时,液晶模块开始执行命令
}
//*******************初始化
void Init(void)
{
 WriteInstruction(0x38);  //显示模式设置:16×2显示,5×7点阵,8位数据接口
 WriteInstruction(0x0c);  //显示模式设置:显示开,无光标
 WriteInstruction(0x06);  //显示模式设置:光标右移,字符不移
 WriteInstruction(0x01);  //清屏幕指令,将以前的显示内容清除
}
//*******************主函数
void main(void)         
{
 uchar a;
   Init();      
   while(1)
   {
  a=0;
  WriteInstruction(0x80);  // 设置显示位置为第一行的第1个字
    while(string1[a] != '\0')   //'\0'是数组结束标志
  {     
   WriteData(string1[a]);
   a++;
  }
 
  a=0;
  WriteInstruction(0x80+0x40); // 设置显示位置为第二行的第1个字
  while(string2[a] != '\0') //'\0'是数组结束标志
  {     
   WriteData(string2[a]);
   a++;
  }    
 }
}

 

关闭窗口