#include <reg51.h>
#include <stdio.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop() _nop_()
#define lcdbus P1
uchar code tab[];
sbit SCK=P3^0; //SCLK
sbit SID=P3^1; //SID
sbit CS=P3^2; //CS
/*******************
delay1ms
*****************************/
void delay1ms(uint delay_xms)
{
uint i,j,m;
for(i=0;i<delay_xms;i++)
{
for(j=0;j<40;j++)
{
for(m=0;m<125;m++)
{
nop();
nop();
nop();
nop();
}}}}
void delay50us(uchar count50us)
{
uchar data k ;
_nop_();
for(k=0;k<2*count50us;k++)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
//串口发送一个字节
void SendByte(unsigned char Dbyte)
{
unsigned char i ;
for(i=0 ;i< 8 ;i++)
{
SCK = 0 ;
Dbyte=Dbyte<< 1 ; //左移一位
SID = CY ; //移出的位给SID
SCK = 1 ;
SCK = 0 ;
}
}
//串口接收一个字节
//仅在读取数据的时候用到
//而读出的数据是只能读出4bit的
unsigned char ReceiveByte(void)
{
unsigned char i,temp1,temp2 ;
temp1=temp2=0 ;
for(i=0 ;i< 8 ;i++)
{
temp1=temp1<< 1 ;
SCK = 0 ;
SCK = 1 ;
SCK = 0 ;
if(SID) temp1++ ;
}
for(i=0 ;i< 8 ;i++)
{
temp2=temp2<< 1 ;
SCK = 0 ;
SCK = 1 ;
SCK = 0 ;
if(SID) temp2++ ;
}
return ((0xf0&temp1)+(0x0f&temp2)) ;
}
void CheckBusy( void )
{
do SendByte(0xfc) ; //11111,RW(1),RS(0),0
while(0x80&ReceiveByte()) ; //BF(.7)=1 Busy
}
void Wcommand( unsigned char Cbyte )
{
CS = 1 ;
CheckBusy() ;
SendByte(0xf8) ; //11111,RW(0),RS(0),0
SendByte(0xf0&Cbyte) ; //高四位
SendByte(0xf0&Cbyte<< 4) ;//低四位(先执行< ;< ;)
CS = 0 ;
}
void WData( unsigned char Dbyte )
{
CS = 1 ;
CheckBusy() ;
SendByte(0xfa) ; //11111,RW(0),RS(1),0
SendByte(0xf0&Dbyte) ; //高四位
SendByte(0xf0&Dbyte<< 4) ;//低四位(先执行< ;< ;)
CS = 0 ;
}
unsigned char ReadData( void )
{
CheckBusy() ;
SendByte(0xfe) ; //11111,RW(1),RS(1),0
return ReceiveByte() ;
}
/******************
清除指令
********************/
void clear(uchar m)
{
uchar i,j;
Wcommand(0x02);
Wcommand(0x34);
Wcommand(0x36); // ;RE=1 扩展指令选择 G=1 开图形显示
for(j=0;j<32;j++)
{
Wcommand(j|0x80); //Y方向的首地址
Wcommand(0x80); //X方向的首地址
for(i=0;i<20;i++)
{
WData(m);
}
}
delay1ms(1);
Wcommand(0x34);
Wcommand(0x30);
}
/*******************
初始化
********************/
void initlcd(void)
{
Wcommand(0x30);// ;0 0 1 DL X RE X X DL=1, RE=0 基本指令RE=1扩充指令
delay50us(100);
Wcommand(0x0C);//;0 0 0 0 1 D C B ;开显示,游标,游标位置
delay50us(1);
Wcommand(0x01);// ;清显示
delay50us(1);
Wcommand(0x06);// ;0 0 0 1 S/C R/L X X
delay50us(1);
}
/********************************
直接写入汉字
***********************************/
void hanzi(uchar x,uchar m,uchar *s)
{
uchar i;
Wcommand(x|0x80); //X 方向的首地址
for(i=0;i<m;i++,s++)
{
WData(*s);}
}
/***********************************
直接写入字母
************************************/
void hz(uchar x,uchar m,uchar *s)
{
uchar i;
Wcommand(x|0x80); //X 方向的首地址
for(i=0;i<m;i++,s++ )
{
WData(*s);}
}
/***************************************************
写入一副160x32图片
**************************************************/
void map(unsigned char *puts,uchar f)
{
unsigned int x=0 ;
unsigned char i,j ;
Wcommand(0x34) ; //8Bit扩充指令集,即使是36H也要写两次
Wcommand(0x36) ; //绘图ON,基本指令集里面36H不能开绘图
for(i=0 ;i< 32 ;i++) //160x32
{
Wcommand(0x80|i) ; //行位置
Wcommand(0x80) ; //列位置
for(j=0 ;j<20 ;j++)
{ //列位置每行自动增加
if(f==1)
{WData(puts[x]) ;}
else
{WData(~puts[x]) ;}
x++ ;
}
}
}
/**********
主函数
***************/
void main()
{
initlcd();
Wcommand(0x01);
hanzi(0,20,"杭州清达光电有限公司");
hanzi(16,20,"产品型号: *HG160322*");
while(1);
}