此為變態(tài)法,操作有待提高
程序代碼如下:
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code line1[] = {"welcome!"};
uchar p[]= //自定義字模數(shù)組
{
0x00,0x0f,0x04,0x0f,0x05,0x07,0x00,0x00,
0x0f,0x12,0x1f,0x15,0x1f,0x15,0x1f,0x15, //確
0x04,0x02,0x0e,0x02,0x02,0x03,0x02,0x00,
0x04,0x04,0x04,0x04,0x04,0x0a,0x11,0x00, //認(rèn)
0x00,0x0f,0x09,0x06,0x06,0x06,0x09,0x00,
0x1f,0x0a,0x0e,0x0e,0x0a,0x1f,0x02,0x02 //取
};
sbit E=P2^7;
sbit RW=P2^5;
sbit RS=P2^6;
busying ()
{
bit result;
RS = 0;
RW = 1;
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (bit)(P0&0x80);
E = 0;
return result;
}
writeorder (uchar cmd)
{
while (busying());
RS = 0;
RW = 0;
E = 0;
_nop_();
_nop_();
P0 = cmd;
_nop_();
_nop_();
_nop_();
_nop_();
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
E = 0;
}
writedata (uchar dat)
{
while (busying());
RS = 1;
RW = 0;
E = 0;
_nop_();
_nop_();
P0 = dat;
_nop_();
_nop_();
_nop_();
_nop_();
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
E = 0;
}
delay(uchar i)
{
while(i--)
{
uchar j=250;
while(j--)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
initial ()
{
delay (150);
writeorder (0x38); //工作方式:8位、2行、5x7
delay(5);
writeorder (0x06); //輸入方式:光標(biāo)右移、屏幕不動
delay(5);
writeorder (0x0c); //顯示狀態(tài):顯示開、有光標(biāo)、不閃爍
delay(5);
writeorder (0x01); //清屏
delay(5);
}
newbyte (void) //設(shè)置自定義字符
{
uchar i,j,k=0;
uchar temp=0x40;
for(j=0;j<6;j++)
{
for(i=0;i<8;i++)
{
writeorder(temp+i);
writedata(p[k]);
k++;
}
temp += 8;
}
}
main (void)
{
uchar i=0;
initial ();
newbyte ();
writeorder (0x85); //設(shè)置第一行顯示位置
writedata (0x00);
writedata (0x01);
writedata (0x02);
writedata (0x03);
writedata (0x04);
writedata (0x05);
writeorder (0xc4); //設(shè)置第二行顯示位置
while(line1[i] != '\0')
{ // 顯示字符"welcome!"
writedata(line1[i]);
i++;
}
while (1);
}