新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 12864實際應用的驅動程序

        12864實際應用的驅動程序

        作者: 時間:2016-11-26 來源:網絡 收藏
        一下程序在使用時,緊緊結合每句后面的注釋進行修改,比如端口的銜接問題

        這是一個12864結合ST168進行方位顯示的程序:

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

        使用技巧:只需從中復制有關12864的驅動程序,修改相關參數即可

        #include

        #define uchar unsigned char

        #define uint unsigned int //宏定義

        sbit SID = P2^1; //串行數據RW端口

        sbit SCLK = P2^2; //串行同步時鐘 E

        sbit cs=P2^0; //為寫rs端口

        sbit psb=P2^3;

        uchar code display1[]={"123"},

        //定義一個要顯示的數據串,只當是12864中的字庫,必須要雙引號,特定意義

        display8[]={"456"},

        display2[]={"789"},

        display9[]={"458"},

        display3[]={"654"},

        display7[]={"785!"},

        display4[]={"4254"},

        display10[]={"4587"},

        display5[]={"1010"},

        display11[]={"10201"},

        display6[]={"15420"},

        display12[]={"1201245"},

        displayrt[]={"前42452!!"},

        display13[]={"4524返回"};

        char dir=0,dil=0,zongjil[]={"5204 次"},

        zongjir[]={"右4520"};

        void delayms(uint t) //約延時n(ms)

        { uint i;

        while(t--)

        { for(i=0;i<125;i++); }

        }

        void SendByte(uchar Dbyte) //必須的子函數,寫指令和寫數據都需要調用

        {

        uchar i; for(i=0;i<8;i++)

        { SCLK = 0;

        Dbyte=Dbyte<<1;

        //左移一位

        SID = CY; //移出的位給SID,CY為Dbyte的最好位,下降沿將數據傳入

        SCLK = 1;

        SCLK = 0; }

        }

        uchar ReceiveByte(void) //檢查忙碌的時候調用

        { uchar i,temp1,temp2;

        temp1 = 0;

        temp2 = 0;

        for(i=0;i<8;i++)

        { temp1=temp1<<1;//傳出來的是從高位到低位,從SID中傳出來

        SCLK = 0;

        SCLK = 1;

        SCLK = 0;//下降沿傳出數據

        if(SID)

        temp1++;

        }

        for(i=0;i<8;i++)

        {

        temp2=temp2<<1;

        SCLK = 0;

        SCLK = 1;

        SCLK = 0;

        if(SID) temp2++;//低位先出來

        }

        return ((0xf0&temp1)+(0x0f&temp2));//高八位和低八位

        }

        void CheckBusy( void )

        {

        do SendByte(0xfc); //11111,RW(1),RS(0),0

        while(0x80&ReceiveByte());

        //BF(.7)=1 Busy

        }

        void Lcd_WriteCmd(uchar Cbyte )

        { CheckBusy();

        SendByte(0xf8); //11111,RW(0)A(低表示從單片機到LCD,RS(0)B(低表示指令),0C固定我0;

        SendByte(0xf0&Cbyte); //高四位

        SendByte(0xf0&(Cbyte<<4));

        //低四位(先執行<<)

        }

        void Lcd_WriteData(uchar Dbyte )

        { CheckBusy();

        SendByte(0xfa);

        //11111,RW(0),RS(1),0

        SendByte(0xf0&Dbyte);

        //高四位

        SendByte(0xf0&Dbyte<<4);//低四位(先執行<<)

        }

        void Lcd_Init(void)

        { delayms(50);

        Lcd_WriteCmd(0x30);

        //選擇基本指令集

        delayms(1);

        Lcd_WriteCmd(0x30);

        //選擇8bit數據流

        delayms(1);

        Lcd_WriteCmd(0x0c);

        //開顯示(無游標、不反白)

        delayms(1);

        Lcd_WriteCmd(0x01); //清除顯示,并且設定地址指針為00H

        delayms(20); }

        void LCD12864_Write_Str(unsigned char x,unsigned char y,char *buff)//尋找地址

        { unsigned char addr,i=0;

        if(x==1)addr=0x80;

        if(x==2)addr=0x90;

        if(x==3)addr=0x88;

        if(x==4)addr=0x98;

        addr=addr+y;

        Lcd_WriteCmd(0x30); //基本指令功能

        write_com(0x30);//發出尋地址指令 //

        delay_ms(10);

        Lcd_WriteCmd(addr);

        //基本指令功能.

        write_com(addr);//尋找地址

        while(buff[i]!=)

        { Lcd_WriteData(buff[i]);

        //write_data(buff[i]); i++; } }

        void main()

        { uchar shuju,num[]={"0123456789"},a=1,b=1;

        P1=0xff;

        cs=1;

        psb=0;

        Lcd_Init(); // LCD初始化 ,讓LCD處于白屏狀態.

        delayms(10);

        while(1)

        { Lcd_Init();

        shuju=P1;

        //shuju&=0xf8;

        if((shuju==0xf7|shuju==0xef)&&a==1)

        dil++;

        if((shuju==0xfd|shuju==0xfb)&&b==1)

        dir++;

        zongjil[8]=num[dil/10];

        zongjil[9]=num[dil];

        zongjir[8]=num[dir/10];

        zongjir[9]=num[dir];

        switch(shuju) { case 0xf7: { LCD12864_Write_Str(2,0,display8);//左

        LCD12864_Write_Str(1,0,display1);

        LCD12864_Write_Str(3,0,zongjil);

        a++;

        b=0;

        break; }

        case 0xef: { LCD12864_Write_Str(2,0,display9);//左

        LCD12864_Write_Str(1,0,display2);

        LCD12864_Write_Str(3,0,zongjil);

        a++;

        b=0;

        break;

        }

        case 0xfe:

        { LCD12864_Write_Str(2,0,display7); LCD12864_Write_Str(1,0,display3); break; } //(行,列,字符)

        case 0xfd: { LCD12864_Write_Str(2,0,display10);//右

        LCD12864_Write_Str(1,0,display4);

        LCD12864_Write_Str(3,0,zongjir);

        b++;

        a=0;

        break;

        }

        case 0xfb:

        { LCD12864_Write_Str(2,0,display11);//右

        LCD12864_Write_Str(1,0,display5);

        LCD12864_Write_Str(3,0,zongjir);

        b++; a=0;

        break; }

        case 0xe0: { LCD12864_Write_Str(2,0,display13);

        LCD12864_Write_Str(1,0,displayrt); break; }

        default: LCD12864_Write_Str(2,0,display12);

        LCD12864_Write_Str(1,0,display6); break; }

        delayms(500); } }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 藁城市| 八宿县| 万载县| 马边| 工布江达县| 泉州市| 出国| 漯河市| 承德县| 芜湖县| 南木林县| 原平市| 博兴县| 通海县| 兴义市| 玉林市| 梧州市| 宜昌市| 文登市| 敖汉旗| 贡山| 大新县| 民勤县| 江安县| 原阳县| 南和县| 神木县| 青岛市| 阿鲁科尔沁旗| 德州市| 泰州市| 渝北区| 台州市| 德令哈市| 濉溪县| 万宁市| 潼关县| 吴桥县| 文成县| 崇仁县| 滕州市|