⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ks0108.c

📁 单片机版俄罗斯方块游戏
💻 C
字号:
/*--------------------------------------------------------------KS0108 LCD DIRVER-*/
/*----------------------------------------------write by codekey, codekey@126.com-*/
/*-------------------------------------------------------------------------2008-3-*/
/*---------------------------------------------欢迎使用和修改,但需保留原作者信息-*/
/*--------------------------------------------------------------------------------*/
#include <reg52.h>
#include <intrins.h>
#include <math.h>
#include "KS0108.H"

UINT8 ReadStatus(UINT8 sel)
{
	UINT8 status;
	CS = sel;
	DI = 0;	// ┒
	RW = 1;	// ┛读状态指令
	E  = 1;	// 在E高电平期间,数据被读出
	_nop_();
	status = DB;
	E=0;
	_nop_();
	return status;
}

void WaitForUse(UINT8 sel)
{
	UINT8 status;
	CS=sel;
	do
	{
		DB =0xFF;
		DI = 0;	// ┒
		RW = 1;	// ┛读状态指令
		E  = 1;	// 在E高电平期间,数据被读出	
		_nop_();
		status = DB;
		E=0;
		_nop_();
	}
	while((status&0x80)==0x80);
}

void SetDisplay(UINT8 sel, UINT8 cmd)
{
	WaitForUse(sel);
	E = 0;
	CS = sel;	// 片选
	DI = 0;		// ┒
	RW = 0;		// ┛
	DB = cmd;
	E  = 1;  	// ┒
	_nop_();
	E  = 0;     // ┛在下降沿数据被写入
}

void SetDispOnOff(UINT8 sel,UINT8 on)
{
	if(on){ 	
		SetDisplay(sel,0x3F);
	}
	else{
		SetDisplay(sel,0x3E);
	}
}


void SetDispFirstRow(UINT8 sel, UINT8 row)
{
	SetDisplay(sel, row|0xC0);
}

void SetDispCurPage(UINT8 sel, UINT8 page)
{
	SetDisplay(sel, (page&0x07)|0xB8);
}

void SetDispCurCol(UINT8 sel, UINT8 col)
{
	SetDisplay(sel, (col&0x3F)|0x40);
}

void WriteData(UINT8 sel,UINT8 wdata)
{
	WaitForUse(sel);
	CS = sel;	// 片选
	DI = 1;		// ┒
	RW = 0;		// ┛读状态指令
	E  = 0;   	
	DB = wdata;
	E  = 1;  	// ┒
	_nop_();	// 
	E  = 0;     // ┛在下降沿数据被写入
	_nop_();
}

UINT8 ReadData(UINT8 sel)
{
	UINT8 rdata;
	WaitForUse(sel);
	CS = sel;	// 片选	 
	DI = 1;		// ┒
	RW = 1;		// ┛读状态指令
	DB = 0xFF; 
	E  = 1;   	// 在E高电平期间,数据被读出
 	_nop_();
	rdata = DB;
	E  = 0;		// 归位
	 _nop_();
	return rdata;
}
void DrawAssistFun(UINT8 sel, UINT8 tcp, UINT8 ty1, UINT8 ty2, UINT8 dat, BOOL br, BOOL type) 
{
	UINT8 rdata=0;
	SetDispCurPage(sel,tcp);
	if(!br)
		SetDispCurCol(sel,ty1%64);
	while(ty1<=ty2)
	{
		if(br) // 根据br来确定是否读已经存在的数据
		{		 // 这样可以节省读取时间(比较耗时)
			SetDispCurCol(sel,ty1%64);
			rdata=ReadData(sel);
			rdata=ReadData(sel);
			SetDispCurCol(sel,ty1%64);
		}
		if(type)
			WriteData(sel,dat|rdata);
		else
			WriteData(sel,dat&rdata);	
		ty1++;
	}
}

/* 画矩形:[(y1,r1),(y2,r2)], 其中y是列,r是行*/
void DrawRect(UINT8 y1, UINT8 r1, UINT8 y2, UINT8 r2, BOOL type) // type为0表示清除
{	
	UINT8 bp=r1/8; // 开始的page
	UINT8 ep=r2/8; // 结束的page
	UINT8 cp=bp+1; // 当前page
	UINT8 hd,ed,sel; 

	if( y1>y2 || r1>r2 || y2>127 || r2>63 )
		return;	

	while(cp<ep)  // 先填充中间的page
	{	
		hd = (0==type)? 0: 0xFF;
		if( y2<64 || y1>63 )
		{
			sel=y2/64;  // y2>=y1
			DrawAssistFun(sel,cp,y1,y2,hd,0,type);	
		}
		else
		{	
			DrawAssistFun(0,cp,y1,63,hd,0,type);
			DrawAssistFun(1,cp,0,y2%64,hd,0,type);
		}
		cp+=1;
	}	
	
	// 中间填充完毕,开始处理两头
	if( bp==ep )
	{			
		hd= pow(2,r1%8)-1;
		ed= 0xff-pow(2,r2%8+1)+1;
		hd = 0xff & (~hd)& (~ed);		
		sel=y1/64;
		hd = (0==type)? (~hd): hd;
		
		if( y2<64 || y1>63 ){
			DrawAssistFun(sel,bp,y1,y2,hd,1,type);
		}
		else{
			DrawAssistFun(0,bp,y1,63,hd,1,type);
			DrawAssistFun(1,bp,0,y2%64,hd,1,type);
		}
	}
	else
	{
		hd = (pow(2,r1%8)-1);  // 头, 		
		ed = pow(2,r2%8+1)-1;  // 尾
		if(0==type){ 
			ed = ~ed; // hd本应该取反,故不变
		}
		else{ // 填充
			hd = ~hd;  // ed 不变
		}
		if( y2<64 || y1>63 )
		{
			sel=y2/64;  // y2>=y1
			DrawAssistFun(sel,bp,y1,y2,hd,1,type);
			DrawAssistFun(sel,ep,y1,y2,ed,1,type);
		}
		else
		{
			DrawAssistFun(0,bp,y1,63,hd,1,type);
			DrawAssistFun(0,ep,y1,63,ed,1,type);
			DrawAssistFun(1,bp,0,y2%64,hd,1,type);
			DrawAssistFun(1,ep,0,y2%64,ed,1,type);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -