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

📄 12864lcd.c

📁 51单片机12864带汉字驱动
💻 C
字号:
#include "12864LCD.h"

#define LBYTE(x)	(unsigned char)x
#define HBYTE(x)	(unsigned)(x >> 8)

void delay(unsigned time)
{
	while ( time-- );
}//

void LongDelay(unsigned char time)
{
	while(time--)
		delay(60000);
}//

unsigned char ReadState_LCD(void)
{
	unsigned char  i = 0;
	i = COMM_PORT;	
	return i;
}

bit IsBusy_LCD(void)
{
	unsigned char i = 0;	
	i = COMM_PORT;
	return (i & 0x03) != 3;
}

void WriteCommand_LCD(unsigned char cCom)
{					 
	while (IsBusy_LCD());
	COMM_PORT = cCom;
}

unsigned char ReadData_LCD(void)
{
	unsigned char i;
	while (IsBusy_LCD());
	i = DATA_PORT;
	return i;	
}

void WriteData_LCD(unsigned char cData)
{
	while (IsBusy_LCD());
	DATA_PORT = cData;
}

void SendCommand_LCD(unsigned char cCom, unsigned char cData1, unsigned char cData2)
{
	WriteData_LCD(cData1);
	WriteData_LCD(cData2);
	WriteCommand_LCD(cCom);
}

void Init_LCD(void)
{
	WriteCommand_LCD(0x90);		  //Display  Mode
	SendCommand_LCD(0x24, 0x00, 0x00);		
	SendCommand_LCD(0x40, dTextHomeAdd_L, dTextHomeAdd_H);		    // Control Word set
	SendCommand_LCD(0x41, 0x20, 0x00);							   // Control Word set
	SendCommand_LCD(0x42, dGraphHomeAdd_L, dGraphHomeAdd_H);	  // Control Word set
	SendCommand_LCD(0x43, 0x20, 0x00);				       	     // Control Word set
	WriteCommand_LCD(0x80);									    //Mode set
	SetScrMode_LCD(1, 1, 0);
	Clrscr_LCD();
	SetPos_LCD(0x00, 0x00);
}

void SetScrMode_LCD(unsigned char Graph, unsigned char Text, unsigned char Cur)
{
	WriteCommand_LCD(0x90 + (Graph << 3) + (Text << 2) + (Cur << 1) + 1);			  // Control Word set
}

void ClrScrText_LCD(unsigned char cbSel)
{
	unsigned i = 1000;
	unsigned char cbOffset_L = cbSel ? dSecTextAdd_L : dTextHomeAdd_L;
	unsigned char cbOffset_H = cbSel ? dSecTextAdd_H : dTextHomeAdd_H;

	SendCommand_LCD(0x24, cbOffset_L, cbOffset_H);				  //定义寄存器变量
	while (i--)
	{
		WriteData_LCD(0x00);
		WriteCommand_LCD(0xc0);
	}
	SendCommand_LCD(0x24, cbOffset_L, cbOffset_H);			       //定义寄存器变量
	SendCommand_LCD(0x21, 0, 0);								  //定义寄存器变量
}

void SetPos_LCD(unsigned char x, unsigned char y)
{
	unsigned m = y * 32;
	unsigned nOffset = dTextHomeAdd + m + x;

	SendCommand_LCD(0x24, LBYTE(nOffset), HBYTE(nOffset));		  //定义寄存器变量
}

void PutStrXY_LCD(unsigned char* str, unsigned char x, unsigned char y)
{
	SetPos_LCD(x, y);
	PutStr_LCD(str);
}

void PutStr_LCD(unsigned char* str)
{
	while (*str)
	{
		WriteData_LCD( (*str++) - 32 );
		WriteCommand_LCD(0xc0);					 //Data read Write

	}
}

void PutNumXY_LCD(unsigned Num, unsigned char Len, unsigned char x, unsigned char y)
{
	register unsigned char i;

	SetPos_LCD(x + Len - 1, y);

	for (i = 0; i < Len; i++) 
	{
		WriteData_LCD(Num % 10 + 16);
		WriteCommand_LCD(0xc2);					   //Data read Write
		Num /= 10;
	}
}

void SetCurXY_LCD(unsigned char x, unsigned char y)
{
	SendCommand_LCD(0x21, x, y);				  //定义寄存器变量
}

void SetCurLine_LCD(unsigned char Line)
{
	WriteCommand_LCD(0x9f + Line);				   //Display Mode

}

void ClrScrGraph_LCD(unsigned char cbSel)
{
	unsigned i = 0x1000;
	unsigned char cbOffset_L = cbSel ? dSecGraphAdd_L : dGraphHomeAdd_L;
	unsigned char cbOffset_H = cbSel ? dSecGraphAdd_H : dGraphHomeAdd_H;
	
	SendCommand_LCD(0x24, cbOffset_L, cbOffset_H);			//定义寄存器变量
	while (i--)
	{
		WriteData_LCD(0x00);
		WriteCommand_LCD(0xc0);							      //Data read Write
	}
	SendCommand_LCD(0x24, cbOffset_L, cbOffset_H);			 	//定义寄存器变量
}

void PutPixel_LCD(unsigned char x, unsigned char y, bit Bit)
{
	unsigned uGraphOffset = (y << 7) + x;
	unsigned uTextOffset = (uGraphOffset >> 3) + dGraphHomeAdd;
	unsigned char cBitOffset = 7 - uGraphOffset % 8;

	while (IsBusy_LCD());	
	DATA_PORT = (unsigned char)(uTextOffset & 0x00ff);
	while (IsBusy_LCD());	
	DATA_PORT = (unsigned char)(uTextOffset >> 8);
	while (IsBusy_LCD());	
	COMM_PORT = 0x24;
	while (IsBusy_LCD());	
	COMM_PORT = (Bit ? 0xf8 : 0xf0) + cBitOffset;		  // Bit Set/Reset

}

void LineY_LCD(unsigned char x, unsigned char y, register unsigned char l, bit Bit)
{
	while (l--)
		PutPixel_LCD(x, y + l, Bit);
}

void LineX_LCD(unsigned char x, unsigned char y, register unsigned char l, bit Bit)
{
	while (l--)
		PutPixel_LCD(x + l, y, Bit);
}

void SetPointer_LCD(unsigned uAdd)
{
	while (IsBusy_LCD());	
	DATA_PORT = (unsigned char)(uAdd & 0x00ff);
	while (IsBusy_LCD());	
	DATA_PORT = (unsigned char)(uAdd >> 8);
	while (IsBusy_LCD());	
	COMM_PORT = 0x24;								  	//定义寄存器变量
}


void SetMem_LCD(unsigned uAdd, unsigned char cData)
{
	SendCommand_LCD(0x24, (unsigned char)(uAdd & 0x00ff), (unsigned char)(uAdd >> 8));	   	//定义寄存器变量

	WriteData_LCD(cData);														  	//定义寄存器变量
	WriteCommand_LCD(0xc4);							      //Data read Write
}

void PutPicture_LCD(unsigned char x,unsigned char y,unsigned char *p,unsigned char px,unsigned char py)
{
	unsigned char i;
	unsigned char j;
	SetPointer_LCD(dGraphHomeAdd+y*32+x);
	WriteCommand_LCD(0xb0);
	for(j=0;j<py;j++)
	{
		for(i=0;i<px/8;i++)
		{
			while(!(ReadState_LCD()&0x08));
       	    WriteData_LCD(*p++);
		}
	}
	while(!(ReadState_LCD()&0x08));
    WriteCommand_LCD(0xb2);
}


void SetCurMem_LCD(unsigned char cData)
{
	WriteData_LCD(cData);
	WriteCommand_LCD(0xc0);
}


unsigned char ReadMem_LCD(unsigned uAdd)
{
	SendCommand_LCD(0x24, (unsigned char)(uAdd & 0x00ff), (unsigned char)(uAdd >> 8));	   	//定义寄存器变量

	WriteCommand_LCD(0xc5);
	return ReadData_LCD();
}	


void ClearMem_LCD(void)
{
	unsigned i;
	
	SetPos_LCD(0x00, 0x00);	
	for (i = 0x0000; i < 0x8000; i++)
	{
		WriteData_LCD(0x00);
		WriteCommand_LCD(0xc0);
	}
}

void Clrscr_LCD(void)
{
	ClrScrText_LCD(0);
	ClrScrGraph_LCD(0);
}
void PutHz16(char * str,int x,int y)
{
	int i,j;
	bit a;
	char s;
	for(i = 0; i < 32; i+=2)
	{	
		s = str[i];	
		for(j = 0 ; j < 8 ; j++)
		{
			s<<=1;
			a = CY;
			PutPixel_LCD(x+j,y+i,a);
		}
		s = str[i+1];	
		for(j = 8 ; j < 16 ; j++)
		{
			s<<=1;
			a = CY;
			PutPixel_LCD(x+j,y+i,a);
		}	
	}
}

⌨️ 快捷键说明

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