找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3022|回復: 1
收起左側

單片機資料 字符液晶1062

[復制鏈接]
ID:79207 發(fā)表于 2015-5-7 18:56 | 顯示全部樓層 |閱讀模式
#include <reg51.h>
#include<intrins.h>

#define        Uchar unsigned char
#define Uint unsigned int

// LCM1602A端口地址定義
char xdata Lcd1602CmdPort _at_ 0x7cff;                //E=1 RS=0 RW=0
char xdata Lcd1602WdataPort _at_ 0x7eff;            //E =1 RS=1 RW=0
char xdata Lcd1602StatusPort _at_ 0x7dff;            //CS=1 RS=0 RW=1

#define Busy        0x80                                                 // 忙判別位

code char exampl[]="For an example. - By xiaoqi\n";

void Delay400Ms(void);
void Delay5Ms(void);
void LcdWriteData( char dataW );
void LcdWriteCommand( Uchar CMD,Uchar AttribC );
void LcdReset( void );
void Display( Uchar dd );
void DispOneChar(Uchar x,Uchar y,Uchar Wdata);
void ePutstr(Uchar x,Uchar y, Uchar code *ptr);

//主程序
void main(void)
{
        Uchar temp;
       
        Delay400Ms();                                //啟動時必須的延時,等待lcm進入工作狀態(tài)

        LcdReset();                                    //這也是必需的....初始化
        temp = 32;
        ePutstr(0,0,exampl);                    //上面一行顯示一個預定字符串

        Delay400Ms();                            //保留顯示內容
        Delay400Ms();
        Delay400Ms();
        Delay400Ms();
        Delay400Ms();
        Delay400Ms();
        Delay400Ms();
        Delay400Ms();

        while(1)
        {
                temp &= 0x7f;                            //只顯示ASCII字符

                if (temp<32)temp=32;           //屏蔽控制字符,不予顯示
                Display( temp++ );
               
                Delay400Ms();
        }
}

//顯示字符串
void ePutstr(Uchar x,Uchar y, Uchar code *ptr)
{
Uchar i,l=0;
        while (ptr[l] >31){l++;};
        for (i=0;i<l;i++) {
                DispOneChar(x++,y,ptr[i]);
                if ( x == 16 )
{
                        x = 0; y ^= 1;
                }
        }
}

// 演示一行連續(xù)字符串,配合上位程序演示移動字串
void Display( Uchar dd )
{

Uchar i;
       
        for (i=0;i<16;i++)
{

                DispOneChar(i,1,dd++);
               
                dd &= 0x7f;
                if (dd<32) dd=32;
        }
}

// 顯示光標定位
void LocateXY( char posx,char posy)
{

Uchar temp;

        temp = posx & 0xf;
        posy &= 0x1;
        if ( posy )temp |= 0x40;
        temp |= 0x80;
        LcdWriteCommand(temp,0);
}

// 按指定位置顯示數(shù)出一個字符
void DispOneChar(Uchar x,Uchar y,Uchar Wdata)
{
        LocateXY( x, y );                                     // 定位顯示地址
        LcdWriteData( Wdata );                             // 寫字符
}

// 初始化程序, 必須按照產(chǎn)品資料介紹的初始化過程進行
void LcdReset( void ) {

           LcdWriteCommand( 0x38, 0);                        // 顯示模式設置(不檢測忙信號)
                Delay5Ms();
        LcdWriteCommand( 0x38, 0);                        // 共三次
                Delay5Ms();
        LcdWriteCommand( 0x38, 0);
                Delay5Ms();

        LcdWriteCommand( 0x38, 1);                        // 顯示模式設置(以后均檢測忙信號)
    LcdWriteCommand( 0x08, 1);                        // 顯示關閉
           LcdWriteCommand( 0x01, 1);                        // 顯示清屏
        LcdWriteCommand( 0x06, 1);                        // 顯示光標移動設置
        LcdWriteCommand( 0x0c, 1);                        // 顯示開及光標設置
}

// 寫控制字符子程序: E=1 RS=0 RW=0

void LcdWriteCommand( Uchar CMD,Uchar AttribC ) {

        if (AttribC) while( Lcd1602StatusPort & Busy );                // 檢測忙信號?
        Lcd1602CmdPort = CMD;
}
       
// 當前位置寫字符子程序: E =1 RS=1 RW=0
void LcdWriteData( char dataW ) {

        while( Lcd1602StatusPort & Busy );                                // 檢測忙信號
        Lcd1602WdataPort = dataW;
}

// 短延時
void Delay5Ms(void)
{
        Uint i = 5552;
        while(i--);
}

//長延時
void Delay400Ms(void)
{
        Uchar i = 5;
        Uint j;
        while(i--)
        {
                j=7269;
                while(j--);
        };
}


回復

使用道具 舉報

ID:79207 發(fā)表于 2015-5-7 18:57 | 顯示全部樓層
覺得有用就進來瞧瞧
回復

使用道具 舉報

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

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

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

快速回復 返回頂部 返回列表