找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1872|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

LCD1602顯示實驗

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:319593 發(fā)表于 2018-4-30 21:19 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
/***************************************************************************
標(biāo)題:        LCD1602
實驗板:LY5A-L2A開發(fā)板
作者:        林洋電子-單片機(jī)
客服QQ:52927029
郵箱:        52927029@qq.com
說明:        本程序在液晶LCD1602上顯示兩行字符
******************************************************************************/

//頭文件:
#include <reg52.h>

/********IO引腳定義***********************************************************/
sbit LCD_RS=P1^0;//定義引腳
sbit LCD_RW=P1^1;
sbit LCD_E=P1^2;

/********宏定義***********************************************************/
#define LCD_Data P0
#define Busy    0x80 //用于檢測LCD狀態(tài)字中的Busy標(biāo)識

/********數(shù)據(jù)定義*************************************************************/
unsigned char code uctech[] = {"shi yan"};
unsigned char code net[] = {"sun "};

/********函數(shù)聲明*************************************************************/
void WriteDataLCD(unsigned char WDLCD);                                        //寫數(shù)據(jù)
void WriteCommandLCD(unsigned char WCLCD,BuysC);                //寫命令
unsigned char ReadDataLCD(void);                                                //讀數(shù)據(jù)
unsigned char ReadStatusLCD(void);                                                //讀狀態(tài)
void LCDInit(void);                                                                                //初始化
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);                        //相應(yīng)坐標(biāo)顯示字節(jié)內(nèi)容
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);        //相應(yīng)坐標(biāo)開始顯示一串內(nèi)容
void Delay5Ms(void);                                                                        //延時
void Delay400Ms(void);                                                                        //延時


/***********主函數(shù)開始********************************************************/       
void main(void)
{
        Delay400Ms();         //啟動等待,等LCD講入工作狀態(tài)
        LCDInit();                 //初始化
        Delay5Ms();         //延時片刻(可不要)

        DisplayListChar(0, 0, uctech);
        DisplayListChar(0, 5, net);
        ReadDataLCD();        //測試用句無意義
        while(1);
}

/***********寫數(shù)據(jù)********************************************************/       
void WriteDataLCD(unsigned char WDLCD)
{
        ReadStatusLCD(); //檢測忙
        LCD_Data = WDLCD;
        LCD_RS = 1;
        LCD_RW = 0;
        LCD_E = 0;                 //若晶振速度太高可以在這后加小的延時
        LCD_E = 0;                 //延時
        LCD_E = 1;
}

/***********寫指令********************************************************/       
void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC為0時忽略忙檢測
{
        if (BuysC) ReadStatusLCD(); //根據(jù)需要檢測忙
        LCD_Data = WCLCD;
        LCD_RS = 0;
        LCD_RW = 0;
        LCD_E = 0;
        LCD_E = 0;
        LCD_E = 1;
}

/***********讀數(shù)據(jù)********************************************************/       
unsigned char ReadDataLCD(void)
{
        LCD_RS = 1;
        LCD_RW = 1;
        LCD_E = 0;
        LCD_E = 0;
        LCD_E = 1;
        return(LCD_Data);
}

/***********讀狀態(tài)*******************************************************/       
unsigned char ReadStatusLCD(void)
{
        LCD_Data = 0xFF;
        LCD_RS = 0;
        LCD_RW = 1;
        LCD_E = 0;
        LCD_E = 0;
        LCD_E = 1;
        while (LCD_Data & Busy); //檢測忙信號
        return(LCD_Data);
}

/***********初始化********************************************************/       
void LCDInit(void)
{
        LCD_Data = 0;
        WriteCommandLCD(0x38,0);         //三次模式設(shè)置,不檢測忙信號
        Delay5Ms();
        WriteCommandLCD(0x38,0);
        Delay5Ms();
        WriteCommandLCD(0x38,0);
        Delay5Ms();

        WriteCommandLCD(0x38,1);         //顯示模式設(shè)置,開始要求每次檢測忙信號
        WriteCommandLCD(0x08,1);         //關(guān)閉顯示
        WriteCommandLCD(0x01,1);         //顯示清屏
        WriteCommandLCD(0x06,1);         //顯示光標(biāo)移動設(shè)置
        WriteCommandLCD(0x0C,1);         //顯示開及光標(biāo)設(shè)置
}

/***********按指定位置顯示一個字符*******************************************/       
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
        Y &= 0x1;
        X &= 0xF;                                 //限制X不能大于15,Y不能大于1
        if (Y) X |= 0x40;                 //當(dāng)要顯示第二行時地址碼+0x40;
        X |= 0x80;                         //算出指令碼
        WriteCommandLCD(X, 0); //這里不檢測忙信號,發(fā)送地址碼
        WriteDataLCD(DData);
}

/***********按指定位置顯示一串字符*****************************************/       
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
        unsigned char ListLength;

        ListLength = 0;
        Y &= 0x1;
        X &= 0xF;                                 //限制X不能大于15,Y不能大于1
        while (DData[ListLength]>=0x20){ //若到達(dá)字串尾則退出
                   if (X <= 0xF){                 //X坐標(biāo)應(yīng)小于0xF
                     DisplayOneChar(X, Y, DData[ListLength]); //顯示單個字符
                     ListLength++;
                     X++;
            }
          }
}

/***********短延時********************************************************/       
void Delay5Ms(void)
{
        unsigned int TempCyc = 5552;
        while(TempCyc--);
}

/***********長延時********************************************************/       
void Delay400Ms(void)
{
        unsigned char TempCycA = 5;
        unsigned int TempCycB;
        while(TempCycA--){
                  TempCycB=7269;
                  while(TempCycB--);
        }
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表