新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 【C51】源碼 5 -- LCD 1602 顯示字符

【C51】源碼 5 -- LCD 1602 顯示字符

作者: 時間:2016-11-17 來源:網(wǎng)絡(luò) 收藏
LCD 1602,正式說法叫 LCM 1602(包括了一些外圍電路),一般用來顯示簡單的字符,也可以通過自定義的方式“造字”。

剛學(xué)會基本的字符顯示,僅僅是字符顯示就大量應(yīng)用了各種指令格式,姑且在這個階段寫個程序,總結(jié)一下:

本文引用地址:http://butianyuan.cn/article/201611/315361.htm

程序功能:在 LCD 正中央顯示字符:“Hello World”、“By Fate”

注:LCD 1602 的使用:http://gaebolg.blog.163.com/blog/static/198269068201231665524137/

附上源碼:(初出茅廬,難免有寫的不好的地方,僅作備份之用,歡迎指點,噴子退散……)

/*------------------------------------------------------------------------------------
LCD 1602 顯示字符,在正中央顯示“Hello World”、“By Fate”
-------------------------------------------------------------------------------------*/

#include
#include // 各種函數(shù),這里使用了 空函數(shù) _nop_()

sbitRS = P2^4;
sbitRW = P2^5;
sbitEN = P2^6;

#defineDataPort P0

voidInit_LCD(void);// 初始化 LCD 顯示

voidLCD_Write_Com(unsigned charc);// 向 LCD 寫指令
voidLCD_Write_Data(unsigned chard);// 向 LCD 寫數(shù)據(jù)

bitLCD_Busy(void);// LCD 忙檢測函數(shù),忙 返回 1,閑 返回 0
voidLCD_Clear(void);// 清屏

/*-------------------------------------------------------------------------------------
LCD 參數(shù)設(shè)定:DL 0 = 數(shù)據(jù)總線為 4 位 1 = 數(shù)據(jù)總線為 8 位
    N 0 = 顯示 1 行 1 = 顯示 2 行
   F 0 = 5x7 點陣/每字符1 = 5x10 點陣/每字符
--------------------------------------------------------------------------------------*/
voidLCD_Set_Argument(unsigned charDL,unsigned charN,unsigned charF);

/*-----------------------------------------------------------------------------------------------------------------------------------
LCD 輸入模式設(shè)定:ID 0 = 寫入新數(shù)據(jù)后光標(biāo)左移 1 = 寫入新數(shù)據(jù)后光標(biāo)右移
   S0 = 寫入新數(shù)據(jù)后顯示屏不移動1 = 寫入新數(shù)據(jù)后顯示屏整體右移 1 個字
------------------------------------------------------------------------------------------------------------------------------------*/
voidLCD_Set_InputMode(unsigned charID,unsigned charS);

/*-----------------------------------------------------------------------
LCD 顯示設(shè)定:D 0 = 顯示功能關(guān) 1 = 顯示功能開
    C 0 = 無光標(biāo) 1 = 有光標(biāo)
    B 0 = 光標(biāo)不閃爍 1 = 光標(biāo)閃爍
------------------------------------------------------------------------*/
voidLCD_Set_DisplayMode(unsigned charD,unsigned charC,unsigned charB);

/*-----------------------------------------------------------
在第 row 行,第 col 列的位置,顯示字符 c
------------------------------------------------------------*/
voidLCD_Write_Char(unsigned charrow,unsigned charcol,unsigned charc);

/*-----------------------------------------------------------
從第 row 行,第 col 列開始,顯示字符串 s
------------------------------------------------------------*/
voidLCD_Write_String(unsigned charrow,unsigned charcol,unsigned char*s);

voidDelay(unsigned intt);
voidDelay_ms(unsigned intt); // 延遲 t ms

voidmain (void)
{
Init_LCD();
LCD_Write_Char(1, 3, H);
LCD_Write_Char(1, 4, e);
LCD_Write_Char(1, 5, l);
LCD_Write_Char(1, 6, l);
LCD_Write_Char(1, 7, o);
LCD_Write_String(1, 9, "World!");
LCD_Write_String(2, 5, "By Fate!");
while(1);
}

voidInit_LCD(void)
{
LCD_Set_Argument(1, 1, 0);// 設(shè)定基礎(chǔ)參數(shù):數(shù)據(jù)總線 8 位、顯示 2 行、5x7 點陣/字符
LCD_Set_DisplayMode(0, 0, 0);// 設(shè)定顯示模式:關(guān)顯示、關(guān)光標(biāo)顯示、關(guān)光標(biāo)閃爍
LCD_Set_InputMode(1, 0);// 設(shè)定輸入模式:每輸入一個字符,光標(biāo)右移、屏幕不動
LCD_Clear();// 清屏
LCD_Set_DisplayMode(1, 0, 0);// 開顯示
}

voidLCD_Write_Com(unsigned charc)
{
while(LCD_Busy());
RS = 0;
RW = 0;
EN = 1;
DataPort = c;
_nop_();// 小延時,比 Delay() 小得多,目的:使總線上數(shù)據(jù)變穩(wěn)定
EN = 0;// 寫操作是 EN 下降沿驅(qū)動
}

voidLCD_Write_Data(unsigned chard)
{
while(LCD_Busy());
RS = 1;
RW = 0;
EN = 1;
DataPort = d;
_nop_();
EN = 0;
}

bitLCD_Busy(void)
{
DataPort = 0xFF;
RS = 0;
RW = 1;
EN = 0;
_nop_();
EN = 1;// 讀操作是 EN = 1 驅(qū)動
return(bit)(DataPort & 0x80);// 強(qiáng)制轉(zhuǎn)換到 bit 時,除非原數(shù)據(jù)等于 0,bit = 0,否則 bit = 1
}

voidLCD_Clear(void)
{
LCD_Write_Com(0x01);
Delay_ms(5);
}

voidLCD_Set_Argument(unsigned charDL,unsigned charN,unsigned charF)
{
DL <<= 4;// 指令格式?jīng)Q定了要移位,具體看指令格式要求
N <<= 3;
F <<= 2;
LCD_Write_Com(0x20 | DL | N | F);
Delay_ms(5);
}

voidLCD_Set_InputMode(unsigned charID,unsigned charS)
{
ID <<= 1;
LCD_Write_Com(0x04 | ID | S);
Delay_ms(5);
}

voidLCD_Set_DisplayMode(unsigned charD,unsigned charC,unsigned charB)
{
D <<= 2;
C <<= 1;
LCD_Write_Com(0x08 | D | C | B);
Delay_ms(5);
}

voidLCD_Write_Char(unsigned charrow,unsigned charcol,unsigned charc)
{
if(row == 1) LCD_Write_Com(0x80 + col - 0x01); // 由指令格式可知,DB7 = 1,因此要加上 80H
else if(row == 2) LCD_Write_Com(0xC0 + col - 0x01);
LCD_Write_Data(c);
}

voidLCD_Write_String(unsigned charrow,unsigned charcol,unsigned char*s)
{
if(row == 1) LCD_Write_Com(0x80 + col - 0x01);
else if(row == 2) LCD_Write_Com(0xC0 + col - 0x01);
while(*s) {
LCD_Write_Data(*s);
s++;
}
}

voidDelay(unsigned intt)
{
while(t--);
}

voidDelay_ms(unsigned intt)// 根據(jù)測試,可以相當(dāng)近似的表示 t ms
{
while(t--) {
Delay(245);
Delay(245);
}
}



關(guān)鍵詞: C51LCD1602顯示字

評論


技術(shù)專區(qū)

關(guān)閉