找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

帖子
查看: 2145|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

LCD1602基本程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:197714 發(fā)表于 2018-4-27 16:24 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#ifndef __LCD_H_
#define __LCD_H_
/**********************************
當(dāng)使用的是4位數(shù)據(jù)傳輸?shù)臅r(shí)候定義,
使用8位取消這個(gè)定義
**********************************/
//#define LCD1602_4PINS

/**********************************
包含頭文件
**********************************/
#include<reg52.h>

//---重定義關(guān)鍵詞---//
#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint
#define uint unsigned int
#endif

/**********************************
PIN口定義
**********************************/
#define LCD1602_DATAPINS P0
sbit LCD1602_E=P2^7;
sbit LCD1602_RW=P2^5;
sbit LCD1602_RS=P2^6;

/**********************************
函數(shù)聲明
**********************************/
/*在51單片機(jī)12MHZ時(shí)鐘下的延時(shí)函數(shù)*/
void Lcd1602_Delay1ms(uint c);   //誤差 0us
/*LCD1602寫(xiě)入8位命令子函數(shù)*/
void LcdWriteCom(uchar com);
/*LCD1602寫(xiě)入8位數(shù)據(jù)子函數(shù)*/       
void LcdWriteData(uchar dat)        ;
/*LCD1602初始化子程序*/               
void LcdInit();                                                  

#endif







#include "lcd.h"

/*******************************************************************************
* 函 數(shù) 名         : Lcd1602_Delay1ms
* 函數(shù)功能                   : 延時(shí)函數(shù),延時(shí)1ms
* 輸    入         : c
* 輸    出         : 無(wú)
* 說(shuō)    名         : 該函數(shù)是在12MHZ晶振下,12分頻單片機(jī)的延時(shí)。
*******************************************************************************/

void Lcd1602_Delay1ms(uint c)   //誤差 0us
{
    uchar a,b;
        for (; c>0; c--)
        {
                 for (b=199;b>0;b--)
                 {
                          for(a=1;a>0;a--);
                 }      
        }
           
}

/*******************************************************************************
* 函 數(shù) 名         : LcdWriteCom
* 函數(shù)功能                   : 向LCD寫(xiě)入一個(gè)字節(jié)的命令
* 輸    入         : com
* 輸    出         : 無(wú)
*******************************************************************************/
#ifndef         LCD1602_4PINS         //當(dāng)沒(méi)有定義這個(gè)LCD1602_4PINS時(shí)
void LcdWriteCom(uchar com)          //寫(xiě)入命令
{
        LCD1602_E = 0;     //使能
        LCD1602_RS = 0;           //選擇發(fā)送命令
        LCD1602_RW = 0;           //選擇寫(xiě)入
       
        LCD1602_DATAPINS = com;     //放入命令
        Lcd1602_Delay1ms(1);                //等待數(shù)據(jù)穩(wěn)定

        LCD1602_E = 1;                  //寫(xiě)入時(shí)序
        Lcd1602_Delay1ms(5);          //保持時(shí)間
        LCD1602_E = 0;
}
#else
void LcdWriteCom(uchar com)          //寫(xiě)入命令
{
        LCD1602_E = 0;         //使能清零
        LCD1602_RS = 0;         //選擇寫(xiě)入命令
        LCD1602_RW = 0;         //選擇寫(xiě)入

        LCD1602_DATAPINS = com;        //由于4位的接線(xiàn)是接到P0口的高四位,所以傳送高四位不用改
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;         //寫(xiě)入時(shí)序
        Lcd1602_Delay1ms(5);
        LCD1602_E = 0;

        LCD1602_DATAPINS = com << 4; //發(fā)送低四位
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;         //寫(xiě)入時(shí)序
        Lcd1602_Delay1ms(5);
        LCD1602_E = 0;
}
#endif
/*******************************************************************************
* 函 數(shù) 名         : LcdWriteData
* 函數(shù)功能                   : 向LCD寫(xiě)入一個(gè)字節(jié)的數(shù)據(jù)
* 輸    入         : dat
* 輸    出         : 無(wú)
*******************************************************************************/                  
#ifndef         LCD1602_4PINS                  
void LcdWriteData(uchar dat)                        //寫(xiě)入數(shù)據(jù)
{
        LCD1602_E = 0;        //使能清零
        LCD1602_RS = 1;        //選擇輸入數(shù)據(jù)
        LCD1602_RW = 0;        //選擇寫(xiě)入

        LCD1602_DATAPINS = dat; //寫(xiě)入數(shù)據(jù)
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;   //寫(xiě)入時(shí)序
        Lcd1602_Delay1ms(5);   //保持時(shí)間
        LCD1602_E = 0;
}
#else
void LcdWriteData(uchar dat)                        //寫(xiě)入數(shù)據(jù)
{
        LCD1602_E = 0;          //使能清零
        LCD1602_RS = 1;          //選擇寫(xiě)入數(shù)據(jù)
        LCD1602_RW = 0;          //選擇寫(xiě)入

        LCD1602_DATAPINS = dat;        //由于4位的接線(xiàn)是接到P0口的高四位,所以傳送高四位不用改
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;          //寫(xiě)入時(shí)序
        Lcd1602_Delay1ms(5);
        LCD1602_E = 0;

        LCD1602_DATAPINS = dat << 4; //寫(xiě)入低四位
        Lcd1602_Delay1ms(1);

        LCD1602_E = 1;          //寫(xiě)入時(shí)序
        Lcd1602_Delay1ms(5);
        LCD1602_E = 0;
}
#endif
/*******************************************************************************
* 函 數(shù) 名       : LcdInit()
* 函數(shù)功能                 : 初始化LCD屏
* 輸    入       : 無(wú)
* 輸    出       : 無(wú)
*******************************************************************************/                  
#ifndef                LCD1602_4PINS
void LcdInit()                                                  //LCD初始化子程序        8位數(shù)據(jù)
{
        LcdWriteCom(0x38);  //開(kāi)顯示
        LcdWriteCom(0x0c);  //開(kāi)顯示不顯示光標(biāo)
        LcdWriteCom(0x06);  //寫(xiě)一個(gè)指針加1
        LcdWriteCom(0x01);  //清屏
        LcdWriteCom(0x80);  //設(shè)置數(shù)據(jù)指針起點(diǎn)
}
#else
void LcdInit()                                                  //LCD初始化子程序         4位數(shù)據(jù)
{
        LcdWriteCom(0x32);         //將8位總線(xiàn)轉(zhuǎn)為4位總線(xiàn)
        LcdWriteCom(0x28);         //在四位線(xiàn)下的初始化
        LcdWriteCom(0x0c);  //開(kāi)顯示不顯示光標(biāo)
        LcdWriteCom(0x06);  //寫(xiě)一個(gè)指針加1
        LcdWriteCom(0x01);  //清屏
        LcdWriteCom(0x80);  //設(shè)置數(shù)據(jù)指針起點(diǎn)
}
#endif


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表