新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 自制51單片機常用頭文件(st7920并行方式)

        自制51單片機常用頭文件(st7920并行方式)

        作者: 時間:2016-11-10 來源:網絡 收藏
        /*--------------------------------------------------------------------------

        ST7920.H

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

        The user function is C51.
        Copyright (c) 1988-2004 Keil Elektronik GmbH sum zhaojun
        All rights reserved.
        --------------------------------------------------------------------------*/
        // 并行方式
        #ifndef __ST7920_H__
        #define __ST7920_H__

        #define uchar unsigned char
        #define uint unsigned int

        sbit LCD_RS = P3^2; // 指令/數據選擇
        sbit LCD_RW = P3^3; // 并行讀寫選擇信號
        sbit LCD_EN = P3^4; // 并行使能信號
        sbit LCD_PSB = P3^5; // 并/串選擇端口:H - 并行;L - 串行
        sbit LCD_RES = P3^6;// LCD復位,LCD模塊自帶復位電路。可不接

        #define LCD_DATA P1 // MCU P1<------>LCM
        /*****************************************************
        函 數 名:void Delay_LCD(void)
        功 能:5ms延時
        入口參數:無
        返 回 值:無
        ****************************************************/
        void Delay_LCD(uint t)
        {
        uint i,j;

        for (i=0; i{
        for (j=0; j<10; j++)
        {
        ;
        }
        }
        }
        /*****************************************************
        函 數 名:void WriteCommandLCM()
        功 能:向LCM中寫入指令
        入口參數:WCLCM
        返 回 值:無
        ****************************************************/
        void WriteCommandLCM(uchar WCLCM)
        {
        LCD_RS = 0; // 選擇寫入指令
        LCD_RW = 0; // 寫入
        LCD_EN = 1; // 使能

        LCD_DATA = WCLCM; // 寫入指令
        LCD_EN = 0;
        Delay_LCD(5);
        }
        /*****************************************************
        函 數 名:void WriteDataLCM()
        功 能:向LCM1602中寫入數據
        說 明:將形參WDLCM中的數據寫入LCM中
        入口參數:WDLCM
        返 回 值:無
        *****************************************************/
        void WriteDataLCM(uchar WDLCM)
        {
        LCD_RS = 1; // 選擇寫入數據
        LCD_RW = 0; // 寫入
        LCD_EN = 1; //

        LCD_DATA = WDLCM; // 寫入數據
        LCD_EN = 0;
        Delay_LCD(5);
        }
        /*****************************************************
        函 數 名:void LCMInit()
        功 能:初始化LCM
        說 明:LCM在工作前先要對顯示屏初始化,否則模塊無法正常工作
        入口參數:無
        返 回 值:無
        *****************************************************/
        void LCMInit(void)
        {
        Delay_LCD(20);
        LCD_PSB = 1; // 選擇并行傳輸方式
        Delay_LCD(20);

        LCD_RES = 0; // 復位,可不要
        Delay_LCD(1);
        LCD_RES = 1;

        WriteCommandLCM(0x30); // 選擇基本指令集
        Delay_LCD(10);
        WriteCommandLCM(0x0c); // 開顯示(無游標、反白)
        Delay_LCD(10);
        WriteCommandLCM(0x01); // 清顯示并設地址指針為00H
        Delay_LCD(50);
        }
        /*****************************************************
        函 數 名:void DisplayListChar()
        功 能:向指點的地址寫入字符串
        入口參數:x-橫坐標,y-縱坐標,s-字符串
        返 回 值:無
        *****************************************************/
        void DisplayListChar(uchar x, uchar y, uchar code *s)
        {
        uchar add; // 顯示地址

        switch (y) // 顯示地址計數
        {
        case 0: add = x + 0x80; break; // 第一行的地址
        case 1: add = x + 0x90; break; // 第二行的地址
        case 2: add = x + 0x88; break; // 第三行的地址
        case 3: add = x + 0x98; break; // 第四行的地址
        default: break;
        }

        WriteCommandLCM(0x30); // 基本指令
        WriteCommandLCM(add); // 寫入地址

        while (*s > 0) // 寫入字符串
        {
        WriteDataLCM(*s);
        s++;
        Delay_LCD(50);
        }
        }

        /*****************************************************
        函 數 名:void DisplayPicture()
        功 能:寫入一幅128x64的圖片
        入口參數:img - 圖片數組名
        返 回 值:無
        *****************************************************/
        //顯示圖片(圖片為公司名稱)
        void DisplayPicture(uchar code *img)
        {
        uint j = 0;
        uchar x,y;
        // -------------- 上半屏 ----------------
        for (y=0; y<32; y++) // 垂直坐標32位
        {
        for (x=0; x<8; x++) // 水平坐標16字節
        {
        WriteCommandLCM(0x36); // 擴展指令
        WriteCommandLCM(y+0x80); // 先寫入垂直坐標
        WriteCommandLCM(x+0x80); // 再寫入水平坐標

        WriteCommandLCM(0x30); // 基本指令
        WriteDataLCM(img[j++]); // 連續寫入16位數據
        WriteDataLCM(img[j++]);
        }
        }
        // --------------- 下半屏 ---------------
        j = 512; // 下半屏起始數據

        for (y=0; y<32; y++) // 垂直坐標32位
        {
        for (x=0; x<8; x++) // 水平坐標16字節
        {
        WriteCommandLCM(0x36); // 擴展指令
        WriteCommandLCM(y+0x80); // 先寫入垂直坐標
        WriteCommandLCM(x+0x88); // 再寫入水平坐標

        WriteCommandLCM(0x30); // 基本指令
        WriteDataLCM(img[j++]); // 連續寫入16位數據
        WriteDataLCM(img[j++]);
        }
        }
        }

        #endif



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 乐业县| 乌鲁木齐县| 全州县| 赤峰市| 腾冲县| 新野县| 鹤庆县| 肇源县| 高州市| 从江县| 隆林| 日照市| 蒙阴县| 临澧县| 耒阳市| 怀集县| 息烽县| 万宁市| 太康县| 德兴市| 丰镇市| 嘉峪关市| 隆子县| 伊川县| 金溪县| 三江| 宁乡县| 承德县| 三门峡市| 科技| 安仁县| 咸阳市| 南皮县| 漳平市| 东光县| 永清县| 绍兴县| 金溪县| 民县| 万年县| 北流市|