/**********************************************************************
*** 功能:12864LCD顯示 顯示閃動字體,各位同學(xué)可以自己修改。
*** 實驗內(nèi)容:0x81 0x90 0x88 0x98 分別是LCD四行的行首地址,我們操作LCD現(xiàn)實的字符就是分別放在這里面的
*** 開發(fā)板連接方法: 接上有字庫LCD12864 注意調(diào)節(jié)對比度,LCD12864工作電流較大,請用外接電源供電。
****************************************************************************************************/
#include<pic.h>
__CONFIG(0xff32);
//芯片配置字,看門狗關(guān),上電延時開,掉電檢測關(guān),低壓編程關(guān),加密,4M晶體HS振蕩
#define rs RA5 //定義LCD的數(shù)據(jù)/命令控制口
#define rw RA4 //定義LCD的讀/寫控制口
#define e RA3 //定義LCD的使能口
#define psb RA2 //定義通信方式控制口
#define rst RA1 //定義復(fù)位口
#define nop() asm("nop") //定義空操作函數(shù)
const unsigned char TAB1A[ ] = {"中科技電子網(wǎng)"};
const unsigned char TAB1B[ ] = {***.*********.***"};
const unsigned char TAB1C[ ] = {"0755-110"};
const unsigned char TAB1D[ ] = {"HD-34 PIC 開發(fā)板"};
unsigned int lcd_x; //定義LCD頁地址寄存器
unsigned int lcd_y; //定義LCD列地址寄存器
bit busy; //定義LCD忙標志位
void init(); //申明I/O口設(shè)置函數(shù)
void lcd_init(); //申明LCD初始化函數(shù)
void clear_p(); //申明清屏函數(shù)
void writelcd(const unsigned char *pt); //聲明LCD寫字符串函數(shù)
void wr_zb(); //申明有關(guān)顯示設(shè)置函數(shù)
void flash(); //申明設(shè)置LCD顯示閃爍函數(shù)
void send_d(unsigned char x); //申明送一字節(jié)數(shù)據(jù)顯示函數(shù)
void send_i(unsigned char x); //申明送一字節(jié)控制命令函數(shù)
void chk_busy(); //申明檢測LCD是否工作繁忙函數(shù)
void delay(); //申明延時函數(shù)1,供各命令之間的延時和決定顯示快慢
void delay1(); //申明延時函數(shù)2,用以決定顯示閃爍快慢
/****************************************************************************
* 名 稱:main()
* 功 能:主函數(shù)
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void main()
{
init(); //調(diào)用I/O口設(shè)置函數(shù)
while (1)
{
lcd_init(); //調(diào)用LCD初始化函數(shù)
clear_p(); //調(diào)用清屏函數(shù)
send_i(0x81); //第1行
writelcd(TAB1A);
send_i(0x90); //第2行
writelcd(TAB1B);
send_i(0x88); //第3行
writelcd(TAB1C);
send_i(0x98); //第4行
writelcd(TAB1D);
delay(); //延長顯示一段時間
flash(); //調(diào)用顯示閃爍函數(shù)
clear_p(); //調(diào)用清屏函數(shù)
}
}
/****************************************************************************
* 名 稱:init()
* 功 能:I/O口初始化
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void init()
{
ADCON1 = 0X07; //設(shè)置A口為普通I/O口
TRISA = 0X00; //設(shè)置A口為輸出
TRISD = 0X00; //設(shè)置D口為輸出
PORTA = 0X00000001;
}
/****************************************************************************
* 名 稱:lcd_init()
* 功 能:LCD初始化
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void lcd_init()
{
rst = 0; //復(fù)位LCD
delay(); //保證復(fù)位所需要的時間
rst = 1; //恢復(fù)LCD正常工作
nop();
psb = 1; //設(shè)置LCD為8位并口通信
send_i(0x30); //基本指令操作
send_i(0x01); //清除顯示
send_i(0x06); //指定在寫入或讀取時,光標的移動方向
send_i(0x0c); //開顯示,關(guān)光標,不閃爍
}
/****************************************************************************
* 名 稱:wr_zb()
* 功 能:顯示設(shè)置
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void wr_zb()
{
send_i(lcd_y);
send_i(lcd_x);
}
/****************************************************************************
* 名 稱:flash()
* 功 能:顯示閃爍
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void flash()
{
send_i(0x08); //關(guān)顯示
delay1(); //延長一定時間
send_i(0x0c); //開顯示
delay1();delay1(); //延長關(guān)顯示兩倍的時間
send_i(0x08); //關(guān)顯示
delay1();
send_i(0x0c); //開顯示
delay1();delay1();
send_i(0x08); //關(guān)顯示
delay1();
send_i(0x0c); //開顯示
delay1();delay1();
}
/****************************************************************************
* 名 稱:clear_p()
* 功 能:清屏
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void clear_p()
{
send_i(0x1); //清除所有顯示
send_i(0x34); //擴展指令操作
send_i(0x30); //基本指令操作
}
/****************************************************************************
* 名 稱:send_d()
* 功 能:寫一個字節(jié)數(shù)據(jù)到LCD
* 入口參數(shù):unsigned char x 字符
* 出口參數(shù):
* 說 明:
****************************************************************************/
void send_d(unsigned char x)
{
chk_busy(); //檢測LCD是否工作繁忙
rs = 1; //設(shè)置該字節(jié)數(shù)據(jù)是顯示數(shù)據(jù)
rw = 0; //設(shè)置該次操作為寫
PORTD = x; //送數(shù)據(jù)口PORTD
e = 1; //使能
nop();
nop();
nop();
e = 0; //禁止
}
/****************************************************************************
* 名 稱:send_i()
* 功 能:寫一個字節(jié)命令到LCD
* 入口參數(shù):unsigned char x 字符
* 出口參數(shù):
* 說 明:
****************************************************************************/
void send_i(unsigned char x)
{
chk_busy(); //檢測LCD是否工作繁忙
rs = 0; //設(shè)置該字節(jié)數(shù)據(jù)為控制命令
rw = 0; //設(shè)置此次操作為寫
PORTD = x; //送數(shù)據(jù)口PORTD
e = 1; //使能
nop();
nop();
nop();
e = 0; //禁止
}
/****************************************************************************
* 名 稱:chk_busy()
* 功 能:檢測LCD是否工作繁忙
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void chk_busy()
{
busy = 1; //先置位繁忙標志位
TRISD = 0XFF; //更改通信為輸入
rs = 0; //設(shè)置該字節(jié)數(shù)據(jù)為命令代碼
rw = 1; //設(shè)置此次操作為讀
while (busy)
{
nop();nop();nop();
e = 1; //使能
nop();nop();nop();
if (!RD7) busy = 0; //檢測LCD是否工作繁忙
nop();nop();nop();
e = 0; //禁止
}
e = 0; //禁止
TRISD = 0X00; //恢復(fù)通信為輸出
}
/****************************************************************************
* 名 稱:delay()
* 功 能:延時
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void delay()
{
int i;
for (i = 0;i < 5000;i++);
}
/****************************************************************************
* 名 稱:delayl()
* 功 能:延時
* 入口參數(shù):
* 出口參數(shù):
* 說 明:
****************************************************************************/
void delay1()
{
int i;
for (i = 0;i < 10;i++)
{
delay(); //調(diào)用延時函數(shù)
}
}
/****************************************************************************
* 名 稱: writelcd()
* 功 能: 在LCD上顯示字符串
* 入口參數(shù):const unsigned char *pt 字符串的首地址
* 出口參數(shù):
* 說 明:
****************************************************************************/
void writelcd(const unsigned char *pt)
{
while(*pt) //檢測是否達到了字符串最后
send_d(*pt++); //發(fā)送數(shù)據(jù)給lcd
}