|
|
0.png (110.95 KB, 下載次數(shù): 38)
下載附件
2019-4-18 00:09 上傳
sbit LCD_RS = P2^5; //片選信號(hào)輸入
sbit LCD_RW = P2^6; //串行數(shù)據(jù)輸入
sbit LCD_E = P2^7; //串行時(shí)鐘輸入
sbit D0=P0^0;
void delay_nms(uint t)
{
uint i;
uchar j;
for(i=0;i<t;i++)
for(j=0;j<110;j++);
}
/*********************************************************
*函數(shù)功能:向lcd12864串行輸入一個(gè)字節(jié)
**********************************************************/
void send_byte(uchar Dbyte)
{
uchar i;
for(i=0;i<8;i++)
{
LCD_E = 0;
Dbyte = Dbyte<<1;
LCD_RW = CY;
LCD_E = 1;
_nop_();
LCD_E = 0;
}
}
/*********************************************************
*函數(shù)功能:接收l(shuí)cd12864輸出的一個(gè)有效字節(jié)
**********************************************************/
uchar recive_byte()
{
uchar temp1,temp2,i;
bit flag;
temp1 = 0;
temp2 = 0;
for(i=0;i<8;i++)//接收有效字節(jié)的高四位
{
temp1 = temp1<<1;
LCD_E = 0;
LCD_E = 1;
_nop_();
LCD_E = 0;
flag = LCD_RW;
if(flag==1)
temp1 = temp1&0x01;
}
for(i=0;i<8;i++)//接收有效字節(jié)的低四位
{
temp2 = temp2<<1;
LCD_E = 0;
LCD_E= 1;
_nop_();
LCD_E = 0;
flag = LCD_RW;
if(flag==1)
temp2 = temp2&0x01;
}
return ((temp1&0xf0)|(temp2&0x0f));//返回接收到的有效字節(jié)
}
/*********************************************************
*函數(shù)功能:檢測(cè)忙標(biāo)志位
**********************************************************/
void lcd12864_checkbusy()
{
do
{
send_byte(0xfc);//1111 1【1 RW】【0 RS】0
}while(recive_byte()&0x80==0x80); //BF(.7)=1 Busy
}
/*********************************************************
*函數(shù)功能:向12864寫(xiě)命令函數(shù)
**********************************************************/
void lcd12864_write_com(uchar com)
{
LCD_RS = 1;
lcd12864_checkbusy();//檢測(cè)忙信號(hào)
send_byte(0xf8);//1111 1[0 rw][0 rs]0 寫(xiě)命令起始指令
send_byte(com&0xf0);
send_byte((com<<4)&0xf0);
LCD_RS = 0;
}
/*********************************************************
*函數(shù)功能:向12864寫(xiě)數(shù)據(jù)函數(shù)
**********************************************************/
void lcd12864_write_dat(uchar dat)
{
LCD_RS = 1;
lcd12864_checkbusy();//檢測(cè)忙信號(hào)
send_byte(0xfa);//1111 1[0 rw][1 rs]0 寫(xiě)數(shù)據(jù)起始指令
send_byte(dat&0xf0);
send_byte((dat<<4)&0xf0);
LCD_RS = 0;
}
/*********************************************************
*函數(shù)功能:設(shè)置光標(biāo)位置
**********************************************************/
void lcd12864_set_cursor(uchar x,uchar y)
{
uchar addr;
switch(x)//確定行
{
case 0x00:addr = 0x80;break;//第一行
case 0x01:addr = 0x90;break;//第二行
case 0x02:addr = 0x88;break;//第三行
case 0x03:addr = 0x98;break;//第四行
default:break;
}
addr += y; //確定列
lcd12864_write_com(addr); //設(shè)置光標(biāo)
}
/*********************************************************
*函數(shù)功能:lcd12864清顯示(顯示漢字、字符模式下的清屏)
**********************************************************/
void lcd12864_clear0(void)
{
lcd12864_write_com(0x01);
delay_nms(10);
}
/*********************************************************
*函數(shù)功能:lcd12864清顯示(繪圖模式下的清屏)
**********************************************************/
void lcd12864_clear1(void)
{
uchar i, j, k ;
lcd12864_write_com(0x34);//功能設(shè)置:擴(kuò)充指令集動(dòng)作
lcd12864_write_com(0x36);//開(kāi)繪圖顯示
for( i = 0 ; i < 2 ; i++ )//分上下兩屏寫(xiě)
{
for( j = 0 ; j < 32 ; j++ )
{
lcd12864_write_com( 0x80 + j ) ;//寫(xiě)Y坐標(biāo)(行坐標(biāo))
if( i == 0 ) //寫(xiě)X坐標(biāo)(列坐標(biāo))
lcd12864_write_com( 0x80 ) ;
else
lcd12864_write_com( 0x88 ) ;
for( k = 0 ; k < 16 ; k++ ) //寫(xiě)一整行數(shù)據(jù)
lcd12864_write_dat(0x00) ;
}
}
lcd12864_write_com( 0x30 ) ;//功能設(shè)置:恢復(fù)為基本指令集
// 關(guān)繪圖顯示
}
/*********************************************************
*函數(shù)功能:初始化函數(shù)
**********************************************************/
void lcd12864_init()
{
delay_nms(50);
//功能設(shè)定
lcd12864_write_com(0x30); //設(shè)置為8位并行口,基本指令集
delay_nms(1);
lcd12864_write_com(0x30); //再次設(shè)置為8位并行口,基本指令集
delay_nms(1);
lcd12864_write_com(0x0c); //顯示狀態(tài)設(shè)置:整體顯示ON 游標(biāo)OFF
delay_nms(10);
lcd12864_write_com(0x01); //清除顯示,并且設(shè)定地址指針為00H
delay_nms(10);
lcd12864_write_com(0x06); //設(shè)置為游標(biāo)右移,DDRAM位地址加1,畫(huà)面不移動(dòng)
}
/*********************************************************
*函數(shù)功能:在指定位置 顯示一個(gè)字符
**********************************************************/
void lcd12864_write_char(uchar x,uchar y,uchar chr)
{
lcd12864_set_cursor(x,y);
lcd12864_write_dat(chr);
}
/*********************************************************
*函數(shù)功能:在指定位置 顯示一個(gè)字符
**********************************************************/
void lcd12864_write_str(uchar x,uchar y,uchar *string)
{
uchar *str;
uchar i=x;
uchar j=y;
str = string;
lcd12864_set_cursor(x,y);
while(*str!='\0')
{
lcd12864_write_dat(*str);
j++;
if(j>0x0f)
{
i++;
j=0;
if(i>4)
i=0;
lcd12864_set_cursor(i,j);
}
str++;
}
}
//********************************************************
//畫(huà)滿屏圖片
//參數(shù):dat為填充的數(shù)據(jù)
//********************************************************
void draw_full_picture (uchar *dat)
{
uchar i;
uchar j;
uchar k;
uchar bGDRAMAddrX = 0x80; //GDRAM水平地址
uchar bGDRAMAddrY = 0x80; //GDRAM垂直地址
for(i = 0; i < 2; i++)
{
for(j = 0; j < 32; j++)
{
for(k = 0; k < 8; k++)
{
lcd12864_write_com(0x34); //設(shè)置為8位MPU接口,擴(kuò)充指令集
lcd12864_write_com(bGDRAMAddrY+j); //垂直地址Y
lcd12864_write_com(bGDRAMAddrX+k); //水平地址X
lcd12864_write_dat(*dat++);
lcd12864_write_dat(*dat++);
}
}
bGDRAMAddrX = 0x88; //寫(xiě)下半屏幕
}
lcd12864_write_com(0x36); //打開(kāi)繪圖模式
lcd12864_write_com(0x30); //恢復(fù)基本指令集,關(guān)閉繪圖模式
}
|
|