// IDE:KEIL C51或Silicon Laboratories IDE//// 液晶点阵显示---- SG19264-3A,控制器KS0108B
//// By Ryongwin.B// Jun.2006.//#include "c8051F000.h"//#include "intrins.h"//sbit di = P2^0 //命令寄存器和数据RAM之间的选择sbit rs = P2^0; //命令寄存器和数据RAM之间的选择sbit rw = P2^1; //读写控制线sbit en = P2^2; //使能控制线
#define LCD_DATA_PORT P3 //定义数据端口#define TIMEOUT 20000 //KS0108B忙超时#define NOP2() {_nop_();_nop_();}#define NOP4() {_nop_();_nop_(); _nop_();_nop_();}#define NOP5() {_nop_();_nop_(); _nop_();_nop_(); _nop_();}#define NOP6() {_nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_();}
//KS0108B 命令宏定义#define TURNONOFF 0x3E //0011111x显示开关#define SETX 0xb8 //10111xxx设置页地址#define SETY 0x40 //01xxxxxx设置列地址#define SETZ 0xC0 //11xxxxxx设置显示起始行//函数原型:void Wait(unsigned int t)//功能:延时等待。//参数:t,等待时间。//返回值:无//说明:同Delay()函数。void Wait(unsigned char t){ if(t) while(--t);}//函数原型:void IsKS0070Busy(void)//功能:检测KS0070B忙标志。//参数:无。//返回值:无。//说明:void IsKS0108Busy(void)//reentrant{ unsigned int timeout; unsigned char dat; timeout=0; LCD_DATA_PORT=0xFF;//设置51MCU端口为输入 rs=0; // rw=1; do { en=1; NOP4(); //Wait(T); dat=LCD_DATA_PORT; dat&=0x90; //4位复位位,7位忙标志 en=0; NOP6(); //Wait(T); }while((dat!=0)&&(timeout++<TIMEOUT)); en=0; NOP6(); //Wait(T);}//函数原型:void WriteByteToKs0108b(unsigned char dat)//功能:写显示数据//参数:dat:显示数据//返回值://说明:void WriteByteToKs0108b(unsigned char dat){ IsKS0108Busy();
rs=1; rw=0; LCD_DATA_PORT=dat; en=1; NOP6(); //Wait(T); en=0; NOP6(); //Wait(T);}//函数原型:unsigned char ReadByteFromKs0108b(void)//功能:读显示数据。//参数:无。//返回值:显示数据。//说明:unsigned char ReadByteFromKs0108b(void){ unsigned char dat; IsKS0108Busy(); LCD_DATA_PORT=0xFF;//设置51MCU端口为输入 rs=1; rw=1;