#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit SCL=P2^1;
sbit SDA=P2^0;
uchar code table[]="study up";
//延時(shí)函數(shù)
void delayus()
{
;;;
}
void delayms(uchar x)
{
uchar j;
while(x--)
for(j=110;j>0;j--);
}
//開始信號(hào)
void iic_start(void)
{
SDA = 1;
delayus();
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL = 1;
delayus();
SDA=0;
delayus();
}
//停止信號(hào)
void iic_stop(void)
{
SDA = 0;
delayus();
SCL = 1;
delayus();
SDA = 1;
delayus();
}
//初始化
void iicinit(void)
{
SDA = 1;
delayus();
SCL=1;
delayus();
}
//應(yīng)答
void respons(void)
{
uchar i=0;
SCL = 1;
delayus();
while((SDA=1)&&(i<255))
i++;
SCL = 0;
delayus();
}
//發(fā)送一個(gè)字節(jié)
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
SCL = 0;
delayus();
SDA=CY;
delayus();
SCL = 1;
delayus();
}
SCL=0;
delayus();
SDA=1;
delayus();
}
//寫地址,數(shù)據(jù)
void write_lcd(uchar contr)
{
iic_start();
write_byte(0x4e);
respons();
write_byte(contr); //延時(shí)時(shí)間太長,背光特別閃
respons();
iic_stop();
}
void write_com(uchar temp)
{
uchar temp1;
temp1=temp<<4;
write_lcd(temp&0xf0|0x08);
write_lcd(temp&0xf0|0x0c); //先寫地址
delayms(5);
write_lcd(temp&0xf0&0xfb);
write_lcd(temp1&0xf0|0x08);
write_lcd(temp1&0xf0|0x0c);
delayms(5);
write_lcd(temp1&0xf0&0xfb);
//iic_stop();
}
void write_data(uchar temp)
{
uchar temp1;
temp1=temp<<4;
write_lcd(temp&0xf0|0x09);
write_lcd(temp&0xf0|0x0d);
delayms(5);
write_lcd(temp&0xf0&0xfb);
write_lcd(temp1&0xf0|0x09);
write_lcd(temp1&0xf0|0x0d);
delayms(5);
write_lcd(temp&0x00&0xfb);
}
void lcd_init()
{
write_com(0x28); //四位數(shù)據(jù)控制
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void main()
{
iicinit();
lcd_init();
while(1)
{
uchar i=0;
write_com(0x80);
write_data('1');
write_data('2');
write_data('3');
write_com(0xc0);
while(table[i]!='\0')
{
write_data(table[i]);
i++;
}
}
}
個(gè)人覺得最難的部分是寫數(shù)據(jù)與寫命令子函數(shù)中或和與的邏輯關(guān)系,程序?qū)懥艘簧衔,但是光找錯(cuò)誤就找了一天多,真心不容易啊,希望大家以后注意!!
|