|
|
/*
**********************************************************************
* 頭文件包含
**********************************************************************
*/
#include "lcd12864.h"
/*
**********************************************************************
* 本地全局變量
**********************************************************************
*/
sbit gLcd12864_E = P2^7; // LCD12864控制總線的使能信號
sbit gLcd12864_RW = P2^6; // LCD12864控制總線的讀寫選擇信號,串行數(shù)據(jù)口
sbit gLcd12864_RS = P2^5; // LCD12864控制總線的數(shù)據(jù)/命令選擇信號
//sbit gLcd12864_PSB= P1^0; // PSB=1為并口,PSB=0為串口
/*
**********************************************************************
* 內(nèi)部函數(shù)原型聲明
**********************************************************************
*/
static void delay_1ms(u16 x);
//static void delay500ms(void);
/*********************************************************************
* 函 數(shù) 名 : delay_1ms
* 函數(shù)功能 : 延時
* 參數(shù)列表 : x——設(shè)定延時時間
* 函數(shù)輸出 : 無
*********************************************************************/
static void delay_1ms(u16 x)
{
u16 i,j;
for(j=0;j<x;j++)
for(i=0;i<110;i++);
}
/*********************************************************************
* 函 數(shù) 名 : delay500ms
* 函數(shù)功能 : 單片機(jī)小精靈V1.3生成的延時500ms的精確延時函數(shù)
* 參數(shù)列表 : 無
* 函數(shù)輸出 : 無
*********************************************************************/
static void delay500ms(void) //誤差 0us
{
unsigned char a,b,c;
for(c=23;c>0;c--)
for(b=152;b>0;b--)
for(a=70;a>0;a--);
}
/*********************************************************************
* 函 數(shù) 名 : sendbyte
* 函數(shù)功能 : 讀一個字節(jié)
* 參數(shù)列表 : zdata
* 函數(shù)輸出 : 無
*********************************************************************/
static void sendbyte(u8 zdata)
{
u8 i;
for(i=0; i<8; i++)
{
if((zdata << i) & 0x80) //一個字節(jié)與上10000000(0x80),如果字節(jié)最高位為1則數(shù)據(jù)線發(fā)1,反之發(fā)0
{
gLcd12864_RW = 1;
}
else
{
gLcd12864_RW = 0;
}
gLcd12864_E = 0;
_nop_(); //一個正脈沖(可要可不要)
gLcd12864_E = 1;
}
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864WriteCmd
* 函數(shù)功能 : 按照LCD12864低層時序向LCD內(nèi)部寫入8位命令字
* 參數(shù)列表 : cmd - 待寫入的8位命令字
* 函數(shù)輸出 : 無
*********************************************************************/
static void Lcd12864WriteCmd(u8 cmd)
{
gLcd12864_RS=1;
gLcd12864_E=0;
sendbyte(0xf8); //第一個字節(jié)11111000——0xf8-寫指令
sendbyte(cmd & 0xf0);
sendbyte((cmd << 4) & 0xf0);
gLcd12864_RS=0; // 禁止LCD
delay_1ms(1);
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864WriteData
* 函數(shù)功能 : 按照LCD12864低層時序向LCD內(nèi)部寫入8位數(shù)據(jù)
* 參數(shù)列表 : dat - 待寫入的8位命令字
* 函數(shù)輸出 : 無
*********************************************************************/
static void Lcd12864WriteData(u8 dat)
{
gLcd12864_RS=1;
gLcd12864_E=0;
sendbyte(0xfa); //第一個字節(jié)11111000——0xf8-寫指令
sendbyte(dat & 0xf0);
sendbyte((dat << 4) & 0xf0);
gLcd12864_RS=0; // 禁止LCD
delay_1ms(1);
}
/************* 上面是底層時序函數(shù),下面是高層時序函數(shù) ***************/
/*********************************************************************
* 函 數(shù) 名 : Lcd12864Init
* 函數(shù)功能 : 按照LCD12864低層時序進(jìn)行初始化序列
* 參數(shù)列表 : 無
* 函數(shù)輸出 : 無
*********************************************************************/
void Lcd12864Init(void)
{
//gLcd12864_PSB = 1; // 選定8位并行模式
// 發(fā)送初始化序列
Lcd12864WriteCmd(0x30); // 0x30為基本指令集
Lcd12864WriteCmd(0x02);
Lcd12864WriteCmd(0x0c); // 整體顯示、游標(biāo)關(guān)閉
Lcd12864WriteCmd(0x01); // 0x01為清屏指令
Lcd12864WriteCmd(0x80); // 地址自動加1
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864ShowStr
* 函數(shù)功能 : 從坐標(biāo)(x, y)開始顯示字符串str,注意這個函數(shù)不能跨行
* 顯示,因為顯存地址是不連續(xù)的。
* 參數(shù)列表 : x - 橫向坐標(biāo),范圍是0-7
* y - 縱向坐標(biāo),范圍是0-3
* pStr - 指向待顯示的字符串的指針
* 函數(shù)輸出 : 無
*********************************************************************/
void Lcd12864ShowStr(u8 x, u8 y, u8 *pStr) //顯示字符串
{
switch (y)
{
case 0:
x |= 0x80; break; // 第一行
case 1:
x |= 0x90; break; // 第二行
case 2:
x |= 0x88; break; // 第三行
case 3:
x |= 0x98; break; // 第四行
default:
break;
}
Lcd12864WriteCmd(x); // 發(fā)送地址碼
while (*pStr != '\0') // 若到達(dá)字串尾則退出
{
Lcd12864WriteData(*pStr); //
pStr++;
delay_1ms(5);
}
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864sleep
* 函數(shù)功能 : 使液晶睡眠
* 參數(shù)列表 :
* 函數(shù)輸出 : 無
*********************************************************************/
void Lcd12864sleep(u8 x)
{
if(x==0)
{ Lcd12864WriteCmd(0x08);} //進(jìn)入睡眠
if(x==1)
{ Lcd12864WriteCmd(0x0c);} //退出睡眠
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864ClearScreen
* 函數(shù)功能 : 清屏,即清除屏幕整個顯示內(nèi)容
* 參數(shù)列表 : 無
* 函數(shù)輸出 : 無
*********************************************************************/
void Lcd12864ClearScreen(void)
{
// 發(fā)送初始化序列
Lcd12864WriteCmd(0x01); // 0x01為清屏指令
// Lcd12864WriteCmd(0x34); // 0x34為擴(kuò)充指令集
// Lcd12864WriteCmd(0x30); // 0x30為基本指令集
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864FlashScreen3
* 函數(shù)功能 : 屏幕閃爍3次
* 參數(shù)列表 : 無
* 函數(shù)輸出 : 無
*********************************************************************/
void Lcd12864FlashScreen3(void)
{
u8 time = 0;
for (time=0; time<3; time++)
{
Lcd12864WriteCmd(0x08);
delay500ms();
Lcd12864WriteCmd(0x0c);
delay500ms();
}
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864ShowImage
* 函數(shù)功能 : 顯示圖片
* 參數(shù)列表 : *pData
* 函數(shù)輸出 : 無
*********************************************************************/
void Lcd12864ShowImage(u8 *pData)
{
u8 x = 0, y = 0, i = 0;
for(i=0;i<9;i=i+8)
for(y=0;y<32;y++)
{
for(x=0;x<8;x++)
{
Lcd12864WriteCmd(0x36);
Lcd12864WriteCmd(0x80+y);
Lcd12864WriteCmd(0x80+x+i);
Lcd12864WriteData(*pData++);
Lcd12864WriteData(*pData++);
Lcd12864WriteCmd(0x30);
}
}
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864tiao
* 函數(shù)功能 : 顯示圖片
* 參數(shù)列表 : *pData
* 函數(shù)輸出 : 無
*********************************************************************/
void Lcd12864tiao()
{
u8 x = 0, y = 0, i = 0;
for(i=0;i<9;i=i+8)
for(y=0;y<32;y++)
{
for(x=0;x<8;x++)
{
Lcd12864WriteCmd(0x36);
Lcd12864WriteCmd(0x80+y);
Lcd12864WriteCmd(0x80+x+i);
Lcd12864WriteData(0x3c);
Lcd12864WriteData(0x3c);
Lcd12864WriteCmd(0x30);
}
}
}
/*********************************************************************
* 函 數(shù) 名 : Lcd12864heng()
* 函數(shù)功能 : 顯示圖片
* 參數(shù)列表 : *pData
* 函數(shù)輸出 : 無
*********************************************************************/
void Lcd12864heng()
{
u8 x = 0, y = 0, i = 0,k;
for(i=0;i<9;i=i+8)
for(y=0;y<32;y++)
{
if((y%8)==1)k=0xff;
else k=0;
for(x=0;x<8;x++)
{
Lcd12864WriteCmd(0x36);
Lcd12864WriteCmd(0x80+y);
Lcd12864WriteCmd(0x80+x+i);
Lcd12864WriteData(k);
Lcd12864WriteData(k);
Lcd12864WriteCmd(0x30);
}
}
}
|
|