專注電子技術學習與研究
當前位置:單片機教程網(wǎng) >> MCU設計實例 >> 瀏覽文章

LCD1602液晶顯示(51單片機c語言)

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2014年04月10日   【字體:


效果圖:
電路圖:

#include<reg52.h> 
#include<intrins.h>  //包含_nop_()函數(shù)定義的頭文件
#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  "};
/*****************************************************
函數(shù)功能:判斷液晶模塊的忙碌狀態(tài)
返回值:result。result=1,忙碌;result=0,不忙
***************************************************/
/*uchar BusyTest()
{
    bit result;
 RS=0;       //根據(jù)規(guī)定,RS為低電平,RW為高電平時,可以讀狀態(tài)
    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;                  //根據(jù)規(guī)定,RS和R/W同時為低電平時,可以寫入指令
 RW=0; 
 E=0;                   //E置低電平(寫指令時,E為高脈沖)
                             // 就是讓E從0到1發(fā)生正跳變,所以應先置"0"
 P0=dictate;            //將數(shù)據(jù)送入P0口,即寫入指令或地址
 delay(1);
 /*_nop_();
   _nop_();
   _nop_();
  _ nop_();*/
  E=1;                   //E置高電平
  _nop_();
  _nop_();
  _nop_();
  _nop_();               //空操作四個機器周期,給硬件反應時間
   E=0;                  //當E由高電平跳變成低電平時,液晶模塊開始執(zhí)行命令
}
//*********************寫數(shù)據(jù)
void WriteData(uchar dat)
{
// while(BusyTest()==1);  
 RS=1;   //RS為高電平,RW為低電平時,可以寫入數(shù)據(jù)
 RW=0;
 E=0;            //(寫指令時,E為高脈沖)
                    // 就是讓E從0到1發(fā)生正跳變,所以應先置"0"
 P0=dat;         //將數(shù)據(jù)送入P0口,即將數(shù)據(jù)寫入液晶模塊
 delay(1);
 /*_nop_();
   _nop_();
   _nop_();
  _ nop_();*/
  E=1;          //E置高電平
  _nop_();
  _nop_();
  _nop_();
  _nop_();      //空操作四個機器周期,給硬件反應時間
  E=0;          //當E由高電平跳變成低電平時,液晶模塊開始執(zhí)行命令
}
//*******************初始化
void Init(void)
{
 WriteInstruction(0x38);  //顯示模式設置:16×2顯示,5×7點陣,8位數(shù)據(jù)接口
 WriteInstruction(0x0c);  //顯示模式設置:顯示開,無光標
 WriteInstruction(0x06);  //顯示模式設置:光標右移,字符不移
 WriteInstruction(0x01);  //清屏幕指令,將以前的顯示內容清除
}
//*******************主函數(shù)
void main(void)         
{
 uchar a;
   Init();      
   while(1)
   {
  a=0;
  WriteInstruction(0x80);  // 設置顯示位置為第一行的第1個字
    while(string1[a] != '\0')   //'\0'是數(shù)組結束標志
  {     
   WriteData(string1[a]);
   a++;
  }
 
  a=0;
  WriteInstruction(0x80+0x40); // 設置顯示位置為第二行的第1個字
  while(string2[a] != '\0') //'\0'是數(shù)組結束標志
  {     
   WriteData(string2[a]);
   a++;
  }    
 }
}

 

關閉窗口