簡單的12864顯示程序


  • 2864是128*64點陣液晶模塊的點陣數簡稱。我使用的內部自帶漢字庫的一款液晶作為風速顯示
  • 好了廢話不多說,接下來就貼上我的程序
  • #//頭信息
    #include <reg51.h>
    #include <math.h>
    #include <INTRINS.H>
    
    //定義數據類型
    #define  uchar unsigned char
    #define  uint unsigned int
    #define  xchar unsigned char code  
    
    #define      DataPort P1
    #define     DELAYMS 80
    
    //端口定義
    sbit RS_Port    =    P2^7;
    sbit RW_Port    =    P2^6;
    sbit E_Port      = P2^5;
    sbit PSB_Port    =    P2^4;
    
    
    //輸入顯示的內容   # 每行顯示七個字 要改內內容直接在下邊該就行了 
    // 但是需要留意的是,每行必須七個字 否則亂碼  后期我會修改
    xchar CorpInf[]=
    {
         "風速風速風速風"
        "風速風速風速風"
        "風速風速風速風"
        "風速風速風速風"
    };
    
    //短延時函數
    #pragma disable         
    void delay(uchar uc_dly)
    {
        while (uc_dly--);
    }
    
    //長延時函數,以秒每單位
    #pragma disable      
    void delays() 
    {
        uchar uc_dly,uc_dly1,uc_dly2;
    
        uc_dly =DELAYMS;
        
        while (uc_dly --)
        {
            for (uc_dly1=0;uc_dly1<50;uc_dly1++)
                for (uc_dly2=0;uc_dly2<50;uc_dly2++);
        };
    }
    
    
    //寫命令
    #pragma disable  
    void wr_cmd(uchar cmd)  
    {
        E_Port = 0;
        _nop_();
        _nop_();
    
        RS_Port=0;
        _nop_();
        _nop_();
    
        RW_Port=0;
        _nop_();
        _nop_();
    
        E_Port=1;
        _nop_();
        _nop_();
    
        DataPort=cmd;
        _nop_();
        _nop_();
    
        E_Port=0;
        _nop_();
        _nop_();
    
        delay(5);
    }
    
    //寫數據
    #pragma disable
    void wr_dat(uchar dat) 
    {    
        E_Port = 0;
        _nop_();
        _nop_();
     RS_Port=1;
        _nop_();
        _nop_();
        
        RW_Port=0;
        _nop_();
        _nop_();
    
        E_Port=1;
        _nop_();
        _nop_();
            
        DataPort=dat;
        _nop_();
        _nop_();
    
        E_Port=0;
        _nop_();
        _nop_();
    
        delay(5);
    }
    
    //顯示初始化
    #pragma disable
    void Init(void)   
    {
        wr_cmd(0x30);     //DL=1:8-BIT interface
        wr_cmd(0x30);     //RE=0:basic instruction
        wr_cmd(0x06);     //Entire display shift right by 1
        wr_cmd(0x08);     //Display OFF,Cursor OFF,Cursor position blink OFF
    
        wr_cmd(0x34);     //DL=1:8-BIT interface
        wr_cmd(0x34);     //RE=0:Extend instruction
        wr_cmd(0x03);
    }
    
    
    //寫入數據的行數以及數據的分布
    #pragma disable
    void DisGBStr(xchar *CorpInf)
    {
        uchar uc_GBCnt;
    
        wr_cmd(0x30);     //DL=1:8-BIT interface
        wr_cmd(0x30);     //RE=0:basic instruction
        wr_cmd(0x0C);     //Display OFF,Cursor OFF,Cursor position blink OFF
    
        wr_cmd(0x80);
        for (uc_GBCnt=0;uc_GBCnt<16;uc_GBCnt++)
        {
            wr_dat(CorpInf[2 * uc_GBCnt]);
            wr_dat(CorpInf[2 * uc_GBCnt + 1]);
        };
    
        wr_cmd(0x90);
        for (uc_GBCnt=0;uc_GBCnt<16;uc_GBCnt++)
        {
            wr_dat(CorpInf[2 * uc_GBCnt + 32]);
            wr_dat(CorpInf[2 * uc_GBCnt + 33]);
        };
    
        delays();
    }
    
    #pragma disable
    void CRAM_OFF()
    {
        wr_cmd(0x30);     //DL=1:8-BIT interface
        wr_cmd(0x30);     //RE=0:basic instruction
        wr_cmd(0x08);    //Display ON,Cursor OFF,Cursor position blink OFF
        wr_cmd(0x01);     //CLEAR
        delay(250);
    }
    
    void main()
    {
        EA=1; //Interurupt Enabled
    
        IT0 = 1;//INT0 Low Level Trigger
    
        EX0 = 1;//INT0 Enabled
    
        PSB_Port =1;
        _nop_();
        delay(250);
    
        //ST7920 Init
        Init();
    
        while (1)
        {
            CRAM_OFF();
            DisGBStr(CorpInf);
        }
    }

     

  • 其中需要注意的是在數組中顯示的是可以變化的 如果不想寫這么多字的化 也可以修改后邊的for循環來是顯示的字變少,但是如果不這樣操作的話,就會使顯示亂碼
    xchar CorpInf[]=
    {
         "風速風速風速風"
        "風速風速風速風"
        "風速風速風速風"
        "風速風速風速風"
    };

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM