標(biāo)題: LCD1602在51單片機中的初級顯示程序 [打印本頁]

作者: 啊哈小明1234    時間: 2017-5-20 00:59
標(biāo)題: LCD1602在51單片機中的初級顯示程序
#include <reg52.h>

/*=================================================
*自定義數(shù)據(jù)類型
=================================================*/
typedef unsigned char uchar;
typedef unsigned int uint;

#define LCD1602_DB P0        //LCD1602數(shù)據(jù)總線

sbit LCD1602_RS = P3^5;         //RS端
sbit LCD1602_RW = P3^6;         //RW端
sbit LCD1602_EN = P3^4;         //EN端
sbit DU = P2^6;//
sbit WE = P2^7;//數(shù)碼管位選段選用于關(guān)閉數(shù)碼管顯示

/*=================================================
*函數(shù)名稱:Read_Busy
*函數(shù)功能:判斷1602液晶忙,并等待
=================================================*/
void Read_Busy()
{
        uchar busy;
        LCD1602_DB = 0xff;//復(fù)位數(shù)據(jù)總線
        LCD1602_RS = 0;          //拉低RS
        LCD1602_RW = 1;          //拉高RW讀
        do
        {
                LCD1602_EN = 1;//使能EN
                busy = LCD1602_DB;//讀回數(shù)據(jù)
                LCD1602_EN = 0;         //拉低使能以便于下一次產(chǎn)生上升沿
        }while(busy & 0x80); //判斷狀態(tài)字BIT7位是否為1,為1則表示忙,程序等待
}
/*=================================================
*函數(shù)名稱:LCD1602_Write_Cmd
*函數(shù)功能:寫LCD1602命令
*調(diào)用:Read_Busy();
*輸入:cmd:要寫的命令
=================================================*/
void LCD1602_Write_Cmd(uchar cmd)
{
        Read_Busy();         //判斷忙,忙則等待
        LCD1602_RS = 0;
        LCD1602_RW = 0;        //拉低RS、RW操作時序情況1602課件下中文使用說明基本操作時序章節(jié)
        LCD1602_DB = cmd;//寫入命令
        LCD1602_EN = 1;         //拉高使能端 數(shù)據(jù)被傳輸?shù)絃CD1602內(nèi)
        LCD1602_EN = 0;         //拉低使能以便于下一次產(chǎn)生上升沿
}
/*=================================================
*函數(shù)名稱:LCD1602_Write_Dat
*函數(shù)功能:寫LCD1602數(shù)據(jù)
*調(diào)用:Read_Busy();
*輸入:dat:需要寫入的數(shù)據(jù)
=================================================*/
void LCD1602_Write_Dat(uchar dat)
{
        Read_Busy();
        LCD1602_RS = 1;
        LCD1602_RW = 0;
        LCD1602_DB = dat;
        LCD1602_EN = 1;
        LCD1602_EN = 0;
}
/*=================================================
*函數(shù)名稱:LCD1602_Dis_OneChar
*函數(shù)功能:在指定位置顯示 *一個* 字符
*調(diào)用:LCD1602_Write_Cmd(); LCD1602_Write_Dat();       
*輸入:x:要顯示的橫坐標(biāo)取值0-40,y:要顯示的行坐標(biāo)取值0-1(0為第一行,1為第二行)
                dat:需要顯示的數(shù)據(jù)以ASCLL形式顯示
=================================================*/
void LCD1602_Dis_OneChar(uchar x, uchar y,uchar dat)
{
        if(y)        x |= 0x40;//第二行時
        x |= 0x80;
        LCD1602_Write_Cmd(x);
        LCD1602_Write_Dat(dat);               
}
/*=================================================
*函數(shù)名稱:LCD1602_Dis_Str
*函數(shù)功能:在指定位置顯示字符串
*調(diào)用:LCD1602_Write_Cmd(); LCD1602_Write_Dat();
*輸入:x:要顯示的橫坐標(biāo)取值0-40,y:要顯示的行坐標(biāo)取值0-1(0為第一行,1為第二行)
                *str:需要顯示的字符串
=================================================*/
void LCD1602_Dis_Str(uchar x, uchar y, uchar *str)
{
        if(y) x |= 0x40;
        x |= 0x80;
        LCD1602_Write_Cmd(x);
        while(*str != '\0')
        {
                LCD1602_Write_Dat(*str++);
        }
}
/*=================================================
*函數(shù)名稱:Init_LCD1602
*函數(shù)功能:1602初始化
*調(diào)用:        LCD1602_Write_Cmd();
=================================================*/
void Init_LCD1602()
{
        LCD1602_Write_Cmd(0x38); //        設(shè)置16*2顯示,5*7點陣,8位數(shù)據(jù)接口
        LCD1602_Write_Cmd(0x0c); //開顯示
        LCD1602_Write_Cmd(0x06); //讀寫一字節(jié)后地址指針加1
        LCD1602_Write_Cmd(0x01); //清除顯示
}

void main()
{
        uchar TestStr[] = {"Welcome!"};//字符串?dāng)?shù)組
        DU = 0;
        WE = 0;//關(guān)閉數(shù)碼管顯示
        Init_LCD1602();//1602初始化
        LCD1602_Dis_Str(0, 0, &TestStr[0]);        //顯示字符串
        LCD1602_Dis_OneChar(10, 1, 0xff); //(1,10)后顯示一個黑方格
        while(1);
}







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1