#include<reg52.h>
#include<intrins.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit E=P1^2;
sbit PSB =P3^6;
#define uchar unsigned char
#define uint unsigned int
#define lcd_data P2
uchar table[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,};
void delay(uchar x)//短延時(shí)
{
for(x;x>0;x--)
_nop_(); //執(zhí)行一個(gè)機(jī)器周期
}
void delay1(uchar x)//1ms延時(shí)
{
uchar y;
for(x;x>0;x--)
for(y=110;y>0;y--);
}
unsigned char read_busy(void)
{
//uchar i;
rs = 0;
rw = 1; //rs=0和rw=1的時(shí)候,表示讀忙標(biāo)志(BF)及地址指針計(jì)數(shù)器(AC)的狀態(tài)
E = 1; //E=1配合 讀狀態(tài)或數(shù)據(jù),指令
while (lcd_data & 0x80); //忙標(biāo)志在 DB7 位 所以將讀到的數(shù)據(jù)與0x80 相與,取出 DB7,檢測(cè)其是否為1.
E = 0; // 釋放E
}
void write_data(uchar x)//寫數(shù)據(jù)
{
read_busy(); //操作液晶的時(shí)候要檢測(cè)忙信號(hào)
rs=1;
rw=0; //rs=1,rw=0,表示寫數(shù)據(jù)到數(shù)據(jù)暫存器DR中
lcd_data=x;
E = 1;
delay(2);
E = 0; //寫數(shù)據(jù)或者指令是在E由高電平到低電平的下降沿. 既是:1--0 的過程.
}
void write_com(uchar x)//寫指令
{
read_busy();
rs=0;
rw=0; //表示寫指令
lcd_data=x;
E = 1;
delay(2);
E = 0; //同上
}
void init()
{
write_com(0x36);//功能設(shè)定,表示DL=1,RE=0 既是選擇8位數(shù)據(jù)流,基本操作指令.
write_com(0x01); //清除顯示
write_com(0x06); //
write_com(0x0c);
}
void clear(void)
{
write_com(0x01); //顯示清屏
write_com(0x34); // 顯示光標(biāo)移動(dòng)設(shè)置
write_com(0x30); // 顯示開及光標(biāo)設(shè)置
}
void main()
{
char i;
delay1(400);
init();
delay1(5);
while(1)
{
//clear();
delay(1);
//write_com(0x36);
write_com(0x34);//關(guān)閉繪圖顯示
write_com(0x80);//發(fā)送水平地址碼
write_com(0x80);//發(fā)送垂直地址碼,
for(i=0;i<16;i++)
write_data(table);
// write_com(0x30);
write_com(0x80);
write_com(0x81);
for(i=16;i<32;i++)
write_data(table);
write_com(0x36);//打開顯示
delay1(400);
delay1(400);
}
}
我寫這個(gè)總是亂碼,不知道錯(cuò)在哪里,望高手們,不吝賜教啊!
歡迎光臨 (http://www.torrancerestoration.com/bbs/) | Powered by Discuz! X3.1 |