找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 4910|回復(fù): 5
收起左側(cè)

4條數(shù)據(jù)線驅(qū)動1602的程序

[復(fù)制鏈接]
ID:113415 發(fā)表于 2016-4-16 11:32 | 顯示全部樓層 |閱讀模式
LCD1602 四線驅(qū)動程序
    只用4條數(shù)據(jù)線就可以驅(qū)動1602液晶屏。見有朋友尋找這種方案,特貼出來。源程序來源于網(wǎng)上,但經(jīng)本人修改驗證并加上注釋。
    其中的RS、RW、E與8線驅(qū)動一樣。此方案可以省出4條單片機的管腳作別的用途,對于管腳緊張的項目,很有價值。
    如果認(rèn)為好,請加分。   
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // - - 接口定義
    # define LCD_DB P2 //
    sbit LCD_RS=P0^7; //
    sbit LCD_RW=P0^6; //
    sbit LCD_E=P0^5; //
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // - - 自定義數(shù)據(jù)類型
    # define uchar unsigned char
    # define uint unsigned int
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // - - 定義子程序函數(shù)
    void LCD_init(void); // - - 初始化LCD1602函數(shù)
    void LCD_write_H4bit_command(uchar dat);
    void LCD_write_L4bit_command(uchar dat);
    void LCD_delay_10us(uint n); // - - 10微秒的延時子程序
    void LCD_delay_50us(uint n); // - - 50微秒的延時子程序
    void LCD_write_4bit_command(uchar command); // - - 向LCD1602寫指令函數(shù)
    void LCD_write_4bit_data(uchar dat); // - - 向LCD1602寫數(shù)據(jù)函數(shù)

/ - - 初始化LCD1602
    void LCD_init(void)
    {
    LCD_delay_50us(20);
    LCD_RS=0; // - - 指令
    LCD_RW=0; // - - 寫入
    LCD_E=0; // - - 使能
    LCD_write_L4bit_command(0x03); // - - 設(shè)置4位格式,2行,5x7
    LCD_delay_50us(2);
    LCD_write_L4bit_command(0x03); // - - 設(shè)置4位格式,2行,5x7
    LCD_delay_50us(10);
    LCD_write_L4bit_command(0x03); // - - 設(shè)置4位格式,2行,5x7
    LCD_delay_50us(2);
    LCD_write_L4bit_command(0x02); // - - 設(shè)置4位格式,2行,5x7
    LCD_delay_50us(2);
    LCD_write_4bit_command(0x28); // - - 設(shè)置4位格式,2行,5x7
    LCD_delay_10us(10);
    LCD_write_4bit_command(0x0c); // - - 整體顯示,關(guān)光標(biāo),不閃爍
    LCD_delay_10us(10);
    LCD_write_4bit_command(0x06); // - - 設(shè)定輸入方式,增量不移位
    LCD_delay_10us(10);
    LCD_write_4bit_command(0x01); // - - 清除屏幕顯示
    LCD_delay_50us(40);
    }
/*
上面初始化程序最重要的是連續(xù)三次寫入0x03,在液晶控制器的技術(shù)手冊中已經(jīng)有說明,特別是在電壓不足的時候,以這樣的方法進行初始化,

容易成功。*/
//*******************************
// - - 向LCD1602寫數(shù)據(jù)或指令
特別注意:
    //LCD_DB:其實就是P2口,且只用了高四位,這樣低四位可用于別的用途;
    //(LCD_DB&0x0F)的結(jié)果是保留P2口現(xiàn)存內(nèi)容的低四位,只有這樣,才不會影響低四位狀態(tài),方便別的應(yīng)用;
    //(dat&0xF0)的結(jié)果是屏蔽dat低四位,保留高四位
    //(LCD_DB&0x0F)|(dat&0xF0)的結(jié)果是將保留的高四位與低四位拼接成8位給P2  
   
void LCD_write_H4bit_command(uchar dat)
    { //調(diào)用本函數(shù)只能將dat的高四位寫入1602
    LCD_delay_10us(10);//延時
    LCD_DB=(LCD_DB&0x0F)|(dat&0xF0);
    LCD_delay_10us(10);
    LCD_E=1; // - - 允許
    LCD_delay_10us(10);
    LCD_E=0;
    }

// 向LCD1602寫低四位指令
    void LCD_write_L4bit_command(uchar dat)
    {//調(diào)用本函數(shù)只能將dat的低四位寫入1602
    dat<<=4;//首先,將dat低四位移動到高四位
    LCD_delay_10us(10);
    LCD_DB=(LCD_DB&0x0F)|(dat&0xF0);
    LCD_delay_10us(10);
    LCD_E=1; // - - 允許
    LCD_delay_10us(10);
    LCD_E=0;
    }
   
// - - 向LCD1602寫指令
    void LCD_write_4bit_command(uchar dat)
    {
    LCD_delay_10us(10);
    LCD_RS=0; // - - 指令
    LCD_RW=0; // - - 寫入
    LCD_write_H4bit_command(dat);//高四位寫入
    LCD_write_L4bit_command(dat);//低四位寫入
    }

    // - - 向LCD1602寫數(shù)據(jù)
    void LCD_write_4bit_data(uchar dat)
    {
    LCD_delay_10us(10);
    LCD_RS=1; // - - 數(shù)據(jù)
    LCD_RW=0; // - - 寫入
    LCD_write_H4bit_command(dat);
    LCD_write_L4bit_command(dat);
    }
void LCD_delay_10us(uint n) // - - 10微秒的延時子程序
    {
    uint i,j;
    for(i=n*10;i>0;i--) // - - 晶振及單片機修改設(shè)置
    for(j=2;j>0;j--);
    }

    void LCD_delay_50us(uint n) // - - 50微秒的延時子程序
    {
    uint i,j;
    for(i=n*10;i>0;i--) // - - 晶振及單片機修改設(shè)置
    for(j=22;j>0;j--);
    }

評分

參與人數(shù) 3黑幣 +100 收起 理由
perfect123 + 15 贊一個!
暗光 + 35
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復(fù)

使用道具 舉報

ID:112693 發(fā)表于 2016-4-18 21:48 | 顯示全部樓層
初學(xué)者,膜拜中..........
回復(fù)

使用道具 舉報

ID:68727 發(fā)表于 2016-4-19 11:21 | 顯示全部樓層
能用嗎我下了好幾個都沒法使用
回復(fù)

使用道具 舉報

ID:68727 發(fā)表于 2016-4-19 11:22 | 顯示全部樓層
樓主用的什么單片機
回復(fù)

使用道具 舉報

ID:113415 發(fā)表于 2016-4-19 14:35 | 顯示全部樓層
我這個程序是自己用過并證明確實管用的,單片機型號是stc12c5410ad,由于買的是那種20腳封裝的,管腳太少,無奈才用4線驅(qū)動1602,省出4條管腳作別的用途。
     這段代碼的核心技術(shù),是將8bit的數(shù)據(jù)或命令分成兩次傳送,第一次傳送高四位,第二次先把低四位移到高四位然后再傳送。特別是,不管是第一次或是第二次傳送,都必須注意:不能影響該IO口低四位的使用,仔細讀讀代碼,就理解是如何處理的了。
回復(fù)

使用道具 舉報

ID:68727 發(fā)表于 2016-4-19 21:26 來自觸屏版 | 顯示全部樓層
baofu 發(fā)表于 2016-4-19 14:35
我這個程序是自己用過并證明確實管用的,單片機型號是stc12c5410ad,由于買的是那種20腳封裝的,管腳太少, ...

也就是1T的單片機使用,89c52Rc系列用不了
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

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