新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 【C51】源碼 5 -- LCD 1602 顯示字符

        【C51】源碼 5 -- LCD 1602 顯示字符

        作者: 時間:2016-11-17 來源:網絡 收藏
        LCD 1602,正式說法叫 LCM 1602(包括了一些外圍電路),一般用來顯示簡單的字符,也可以通過自定義的方式“造字”。

        剛學會基本的字符顯示,僅僅是字符顯示就大量應用了各種指令格式,姑且在這個階段寫個程序,總結一下:

        本文引用地址:http://www.104case.com/article/201611/315361.htm

        程序功能:在 LCD 正中央顯示字符:“Hello World”、“By Fate”

        注:LCD 1602 的使用:http://gaebolg.blog.163.com/blog/static/198269068201231665524137/

        附上源碼:(初出茅廬,難免有寫的不好的地方,僅作備份之用,歡迎指點,噴子退散……)

        /*------------------------------------------------------------------------------------
        LCD 1602 顯示字符,在正中央顯示“Hello World”、“By Fate”
        -------------------------------------------------------------------------------------*/

        #include
        #include // 各種函數,這里使用了 空函數 _nop_()

        sbitRS = P2^4;
        sbitRW = P2^5;
        sbitEN = P2^6;

        #defineDataPort P0

        voidInit_LCD(void);// 初始化 LCD 顯示

        voidLCD_Write_Com(unsigned charc);// 向 LCD 寫指令
        voidLCD_Write_Data(unsigned chard);// 向 LCD 寫數據

        bitLCD_Busy(void);// LCD 忙檢測函數,忙 返回 1,閑 返回 0
        voidLCD_Clear(void);// 清屏

        /*-------------------------------------------------------------------------------------
        LCD 參數設定:DL 0 = 數據總線為 4 位 1 = 數據總線為 8 位
            N 0 = 顯示 1 行 1 = 顯示 2 行
           F 0 = 5x7 點陣/每字符1 = 5x10 點陣/每字符
        --------------------------------------------------------------------------------------*/
        voidLCD_Set_Argument(unsigned charDL,unsigned charN,unsigned charF);

        /*-----------------------------------------------------------------------------------------------------------------------------------
        LCD 輸入模式設定:ID 0 = 寫入新數據后光標左移 1 = 寫入新數據后光標右移
           S0 = 寫入新數據后顯示屏不移動1 = 寫入新數據后顯示屏整體右移 1 個字
        ------------------------------------------------------------------------------------------------------------------------------------*/
        voidLCD_Set_InputMode(unsigned charID,unsigned charS);

        /*-----------------------------------------------------------------------
        LCD 顯示設定:D 0 = 顯示功能關 1 = 顯示功能開
            C 0 = 無光標 1 = 有光標
            B 0 = 光標不閃爍 1 = 光標閃爍
        ------------------------------------------------------------------------*/
        voidLCD_Set_DisplayMode(unsigned charD,unsigned charC,unsigned charB);

        /*-----------------------------------------------------------
        在第 row 行,第 col 列的位置,顯示字符 c
        ------------------------------------------------------------*/
        voidLCD_Write_Char(unsigned charrow,unsigned charcol,unsigned charc);

        /*-----------------------------------------------------------
        從第 row 行,第 col 列開始,顯示字符串 s
        ------------------------------------------------------------*/
        voidLCD_Write_String(unsigned charrow,unsigned charcol,unsigned char*s);

        voidDelay(unsigned intt);
        voidDelay_ms(unsigned intt); // 延遲 t ms

        voidmain (void)
        {
        Init_LCD();
        LCD_Write_Char(1, 3, H);
        LCD_Write_Char(1, 4, e);
        LCD_Write_Char(1, 5, l);
        LCD_Write_Char(1, 6, l);
        LCD_Write_Char(1, 7, o);
        LCD_Write_String(1, 9, "World!");
        LCD_Write_String(2, 5, "By Fate!");
        while(1);
        }

        voidInit_LCD(void)
        {
        LCD_Set_Argument(1, 1, 0);// 設定基礎參數:數據總線 8 位、顯示 2 行、5x7 點陣/字符
        LCD_Set_DisplayMode(0, 0, 0);// 設定顯示模式:關顯示、關光標顯示、關光標閃爍
        LCD_Set_InputMode(1, 0);// 設定輸入模式:每輸入一個字符,光標右移、屏幕不動
        LCD_Clear();// 清屏
        LCD_Set_DisplayMode(1, 0, 0);// 開顯示
        }

        voidLCD_Write_Com(unsigned charc)
        {
        while(LCD_Busy());
        RS = 0;
        RW = 0;
        EN = 1;
        DataPort = c;
        _nop_();// 小延時,比 Delay() 小得多,目的:使總線上數據變穩定
        EN = 0;// 寫操作是 EN 下降沿驅動
        }

        voidLCD_Write_Data(unsigned chard)
        {
        while(LCD_Busy());
        RS = 1;
        RW = 0;
        EN = 1;
        DataPort = d;
        _nop_();
        EN = 0;
        }

        bitLCD_Busy(void)
        {
        DataPort = 0xFF;
        RS = 0;
        RW = 1;
        EN = 0;
        _nop_();
        EN = 1;// 讀操作是 EN = 1 驅動
        return(bit)(DataPort & 0x80);// 強制轉換到 bit 時,除非原數據等于 0,bit = 0,否則 bit = 1
        }

        voidLCD_Clear(void)
        {
        LCD_Write_Com(0x01);
        Delay_ms(5);
        }

        voidLCD_Set_Argument(unsigned charDL,unsigned charN,unsigned charF)
        {
        DL <<= 4;// 指令格式決定了要移位,具體看指令格式要求
        N <<= 3;
        F <<= 2;
        LCD_Write_Com(0x20 | DL | N | F);
        Delay_ms(5);
        }

        voidLCD_Set_InputMode(unsigned charID,unsigned charS)
        {
        ID <<= 1;
        LCD_Write_Com(0x04 | ID | S);
        Delay_ms(5);
        }

        voidLCD_Set_DisplayMode(unsigned charD,unsigned charC,unsigned charB)
        {
        D <<= 2;
        C <<= 1;
        LCD_Write_Com(0x08 | D | C | B);
        Delay_ms(5);
        }

        voidLCD_Write_Char(unsigned charrow,unsigned charcol,unsigned charc)
        {
        if(row == 1) LCD_Write_Com(0x80 + col - 0x01); // 由指令格式可知,DB7 = 1,因此要加上 80H
        else if(row == 2) LCD_Write_Com(0xC0 + col - 0x01);
        LCD_Write_Data(c);
        }

        voidLCD_Write_String(unsigned charrow,unsigned charcol,unsigned char*s)
        {
        if(row == 1) LCD_Write_Com(0x80 + col - 0x01);
        else if(row == 2) LCD_Write_Com(0xC0 + col - 0x01);
        while(*s) {
        LCD_Write_Data(*s);
        s++;
        }
        }

        voidDelay(unsigned intt)
        {
        while(t--);
        }

        voidDelay_ms(unsigned intt)// 根據測試,可以相當近似的表示 t ms
        {
        while(t--) {
        Delay(245);
        Delay(245);
        }
        }



        關鍵詞: C51LCD1602顯示字

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 铜陵市| 威信县| 梨树县| 延寿县| 深圳市| 扎鲁特旗| 武川县| 边坝县| 富裕县| 青海省| 盐边县| 平定县| 招远市| 炎陵县| 盐津县| 拉孜县| 赤峰市| 若羌县| 天气| 宁晋县| 新和县| 连山| 宽城| 定陶县| 如皋市| 沧州市| 凤庆县| 湖口县| 堆龙德庆县| 房产| 阳谷县| 札达县| 山阴县| 平利县| 镇江市| 奉新县| 临武县| 江华| 怀安县| 泰顺县| 苏州市|