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

📄 main.c

📁 普通19264图形液晶模块的单片机驱动程序
💻 C
字号:
#include <at89x52.h>
#include <intrins.h>

sbit CS1 =P1^3; //1片选
sbit CS2 =P1^5; //2片选
sbit CS3 =P1^6; //3片选
sbit RS  =P1^0; //数据指令
sbit RW  =P1^1; //读写
sbit E   =P1^2; //使能
sbit RST =P1^5;//复位


#define DATA P2
#define uchar unsigned char
#define uint unsigned int

/*----------------------------------------------------------------------------
Delay some time//延时
------------------------------------------------------------------------------*/
void delay10ms(unsigned char x)
{
	unsigned char i,j,k;
	for(i=0;i<x;i++)
	for(j=0;j<10;j++)
	for(k=0;k<120;k++);
}
/*----------------------------------------------------------------------------
Send the instruction to the First KS0108 window   
------------------------------------------------------------------------------
--*/
void OutFI(uchar i)
{ 
	unsigned char data_sta;
	do{ 
		E=0;RW=1;RS=0;CS1=0;_nop_();
		E=1;_nop_();
		data_sta=DATA;
		E=0;RW=0;CS1=1;E=1;
	}while(data_sta&0x80);
	
	E=0;RW=0;RS=0;CS1=0;_nop_();
	E=1;_nop_();
	DATA=i;
	_nop_();E=0;RW=1;CS1=1;E=1; 
}

/*----------------------------------------------------------------------------
Send the instruction to the Second KS0108 window.
------------------------------------------------------------------------------
--*/
void OutSI(uchar i)
{ 
unsigned char data_sta;
	do{ 
		E=0;RW=1;RS=0;CS2=0;_nop_();
		E=1;_nop_();
		data_sta=DATA;
		E=0;RW=0;CS2=1;E=1;
	}while(data_sta&0x80);
	
	E=0;RW=0;RS=0;CS2=0;_nop_();
	E=1;_nop_();
	DATA=i;
	_nop_();E=0;RW=1;CS2=1;E=1; 
}
/*----------------------------------------------------------------------------
Send the instruction to the Third KS0108 window
------------------------------------------------------------------------------
--*/
void OutTI(uchar i)
{ 
	unsigned char data_sta;
	do{ 
		E=0;RW=1;RS=0;CS3=0;_nop_();
		E=1;_nop_();
		data_sta=DATA;
		E=0;RW=0;CS3=1;E=1;
	}while(data_sta&0x80);

	E=0;RW=0;RS=0;CS3=0;_nop_();
	E=1;_nop_();
	DATA=i;
	_nop_();E=0;RW=1;CS3=1;E=1; 
}

/*----------------------------------------------------------------------------
Send the data to the First ks0108 Window
------------------------------------------------------------------------------
--*/
void OutFD(uchar i)
{ 
	unsigned char data_sta;
	do{ 
		E=0;RW=1;RS=0;CS1=0;_nop_();
		E=1;_nop_();
		data_sta=DATA;
		E=0;RW=0;CS1=1;E=1;
	}while(data_sta&0x80);
	
	E=0;RW=0;RS=1;CS1=0;_nop_();
	E=1;_nop_();
	DATA=i;
	_nop_();E=0;RW=1;CS1=1;E=1; 
}
/*----------------------------------------------------------------------------
Send the data to the Second ks0108 Window
------------------------------------------------------------------------------
--*/
void OutSD(uchar i)
{ 
	unsigned char data_sta;
	do{ 
		E=0;RW=1;RS=0;CS2=0;_nop_();
		E=1;_nop_();
		data_sta=DATA;
		E=0;RW=0;CS2=1;E=1;
	}while(data_sta&0x80);

	E=0;RW=0;RS=1;CS2=0;_nop_();
	E=1;_nop_();
	DATA=i;
	_nop_();E=0;RW=1;CS2=1;E=1; 
}
/*----------------------------------------------------------------------------
Send the data to the Third ks0108 Window
------------------------------------------------------------------------------
--*/
void OutTD(uchar i)
{ 
	unsigned char data_sta;
	do{ 
		E=0;RW=1;RS=0;CS3=0;_nop_();
		E=1;_nop_();
		data_sta=DATA;
		E=0;RW=0;CS3=1;E=1;
	}while(data_sta&0x80);
	
	E=0;RW=0;RS=1;CS3=0;_nop_();
	E=1;_nop_();
	DATA=i;
	_nop_();E=0;RW=1;CS3=1;E=1; 
}

/*----------------------------------------------------------------------------
Read the data to the First ks0108 Window
------------------------------------------------------------------------------
--*/
/*unsigned char RD_data_F()
{ 
unsigned char data_sta;
do{ 
E=0;RW=1;RS=0;CS1=0;_nop_();
E=1;_nop_();
data_sta=DATA;
E=0;RW=0;CS1=1;E=1;
}while(data_sta&0x80);

E=0;RW=1;RS=1;CS1=0;_nop_();
E=1;_nop_();_nop_();_nop_();
data_sta=DATA;
_nop_();E=0;RW=0;CS1=1;E=1; 

return (data_sta);
}
/*----------------------------------------------------------------------------
Read the data to the Second ks0108 Window
------------------------------------------------------------------------------
--*/
/*unsigned char RD_data_S()
{ 
unsigned char data_sta;
do{ 
E=0;RW=1;RS=0;CS2=0;_nop_();
E=1;_nop_();_nop_();_nop_();
data_sta=DATA;
E=0;RW=0;CS2=1;E=1;
}while(data_sta&0x80);

E=0;RW=1;RS=1;CS2=0;_nop_();
E=1;_nop_();
data_sta=DATA;
_nop_();E=0;CS2=1;E=1; 

return (data_sta);
}
/*----------------------------------------------------------------------------
Read the data to the Third ks0108 Window
------------------------------------------------------------------------------
--*/
/*unsigned char RD_data_T()
{ 
unsigned char data_sta;
do{ 
E=0;RW=1;RS=0;CS3=0;_nop_();
E=1;_nop_();_nop_();_nop_();
data_sta=DATA;
E=0;RW=0;CS3=1;E=1;
}while(data_sta&0x80);

E=0;RW=1;RS=1;CS3=0;_nop_();
E=1;_nop_();
data_sta=DATA;
_nop_();E=0;CS3=1;E=1; 

return(data_sta);
}
/*------------------------------------------------------------------------------
Initialize the LCD,The system reset,invoke First time
--------------------------------------------------------------------------------
-*/
void InitLCD()
{
	OutFI(0x40); /*Set the Y address 0 */
	OutSI(0x40);
	OutTI(0x40);
	OutFI(0xb8); /*Set the Page 0 */
	OutSI(0xb8);
	OutTI(0xb8); 
	OutFI(0xc0); /*Set the display start line 0 */
	OutSI(0xc0);
	OutTI(0xc0); 
	OutFI(0x3f); /*Display ON */
	OutSI(0x3f);
	OutTI(0x3f); 
}
/*------------------------------------------------------------------------------
Clear the three ks0108's display ram. 
Clrscr screen
--------------------------------------------------------------------------------
-*/
void Clrscreen()
{
	unsigned char i,j;
	for(i=0;i<8;i++)
	{
		OutFI(0xb8|i); 
		for(j=0;j<64;j++)
		{
			OutFI(j|0x40);
			OutFD(0);
		}
	}
	for(i=0;i<8;i++)
	{
		OutSI(0xb8|i); 
		for(j=0;j<64;j++)
		{
			OutSI(j|0x40);
			OutSD(0);
		}
	}
	
	for(i=0;i<8;i++)
	{
		OutTI(0xb8|i); 
		for(j=0;j<64;j++)
		{
			OutTI(j|0x40);
			OutTD(0);
		}
	}
}

/*------------------------------------------------------------------------------
--
Display Chinese ideograph;x_add is the x address;It has 4 layers,0,1,2,3;
Width is the Chinese ideograph width;bmp is the table's name 
--------------------------------------------------------------------------------
-*/
void DrawBmp(uchar x_add,uchar layer,uchar width,uchar *bmp)
{ 
	unsigned char x;
	unsigned int i=0;
	
	for(x=x_add;x<x_add+width;x++)
	{
		if (x>127) /*The Third KS0108*/
		{
			OutTI((x-128)|0x40); /*Set the x address*/
			OutTI(layer|0xb8); /*Set the y address*/
			OutTD(bmp[i]); /*Send thd data to LCD ram*/
			
			OutTI((x-128)|0x40); /*Display the down screen*/
			OutTI((layer|0xb8)+1); 
			i++; /*Display zimo table address add 1*/
			OutTD(bmp[i]);
		}
		else if (x>63) /*The Second ks0108*/ 
		{
			OutSI((x-64)|0x40);
			OutSI(layer|0xb8);
			OutSD(bmp[i]);
			
			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+1); 
			i++;
			OutSD(bmp[i]);
		}
		
		else /*The First ks0108*/
		{
			OutFI(x|0x40);
			OutFI(layer|0xb8);
			OutFD(bmp[i]);
			
			OutFI(x|0x40);
			OutFI((layer|0xb8)+1); 
			i++; 
			OutFD(bmp[i]);
		
		}
		i++;
		
	}
}

/*------------------------------------------------------------------------------
--
Display Chinese ideograph;x_add is the x address;It has 4 layers,0,1,2,3;
Width is the Chinese ideograph width;bmp is the table's name. 
--------------------------------------------------------------------------------
-*/
void DrawBmp_f(uchar x_add,uchar layer,uchar width,uchar *bmp)
{ 
	unsigned char x;
	unsigned int i=0;
	
	for(x=x_add;x<x_add+width;x++)
	{
		if (x>127)
		{
			OutTI((x-128)|0x40);
			OutTI(layer|0xb8);
			OutTD(~bmp[i]);
			
			OutTI((x-128)|0x40);
			OutTI((layer|0xb8)+1); 
			i++;
			OutTD(~bmp[i]);
		}
		else if (x>63) 
		{
			OutSI((x-64)|0x40);
			OutSI(layer|0xb8);
			OutSD(~bmp[i]);
			
			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+1); 
			i++;
			OutSD(~bmp[i]);
		}
		
		else 
		{
			OutFI(x|0x40);
			OutFI(layer|0xb8);
			OutFD(~bmp[i]);
			
			OutFI(x|0x40);
			OutFI((layer|0xb8)+1); 
			i++;
			OutFD(~bmp[i]);
		}
		i++;
	
	}
}

/*------------------------------------------------------------------------------
--
Display indention; x_add is the x address;It has 4 layers,0,1,2,3;
Width is the Chinese ideograph width
--------------------------------------------------------------------------------
-*/
void _DrawBmp_(uchar x_add,uchar layer,uchar width)
{ 
	unsigned char x;
	
	for(x=x_add;x<x_add+width;x++)
	{
		if (x>127)
		{
			OutTI((x-128)|0x40);
			OutTI(layer|0xb8);
			OutTD(0);
			
			OutTI((x-128)|0x40);
			OutTI((layer|0xb8)+1); 
			OutTD(0);
		
		}
		else if (x>63) 
		{
			OutSI((x-64)|0x40);
			OutSI(layer|0xb8);
			OutSD(0);
			
			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+1);
			OutSD(0);
		}
		
		else 
		{
			OutFI(x|0x40);
			OutFI(layer|0xb8);
			OutFD(0);
			
			OutFI(x|0x40);
			OutFI((layer|0xb8)+1); 
			OutFD(0);
		}
	}
}

/*------------------------------------------------------------------------------
--
Display indention; x_add is the x address;It has 4 layers,0,1,2,3;
Width is the Chinese ideograph width
--------------------------------------------------------------------------------
-*/
void _DrawBmp_black(uchar x_add,uchar layer,uchar width)
{ 
	unsigned char x;
	
	for(x=x_add;x<x_add+width;x++)
	{
		if (x>127)
		{
			OutTI((x-128)|0x40);
			OutTI(layer|0xb8);
			OutTD(0xff);
			
			OutTI((x-128)|0x40);
			OutTI((layer|0xb8)+1); 
			OutTD(0xff);
			
		}
		else if (x>63) 
		{
			OutSI((x-64)|0x40);
			OutSI(layer|0xb8);
			OutSD(0xff);
			
			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+1);
			OutSD(0xff);
		}
		
		else 
		{
			OutFI(x|0x40);
			OutFI(layer|0xb8);
			OutFD(0xff);
			
			OutFI(x|0x40);
			OutFI((layer|0xb8)+1); 
			OutFD(0xff);
		}
	}
}
/*------------------------------------------------------------------------------
--
Display the line
display_data_graph is the display graph data
--------------------------------------------------------------------------------
-*/
void _DrawBmp_line(uchar x_add,uchar layer,uchar width,uchar display_data_graph)
{ 
	unsigned char x;
	unsigned int i=0;
	
	for(x=x_add;x<x_add+width;x++)
	{
		if (x>127)
		{
			OutTI((x-128)|0x40);
			OutTI(layer|0xb8); 
			OutTD(display_data_graph);
		}
		else if (x>63) 
		{
			OutSI((x-64)|0x40);
			OutSI(layer|0xb8); 
			OutSD(display_data_graph);
		}
		
		else 
		{
			OutFI(x|0x40);
			OutFI(layer|0xb8); 
			OutFD(display_data_graph);
		}
		i++;
		
	}
}

/*------------------------------------------------------------------------------
--
Display the column
display_data_graph is the display graph data
--------------------------------------------------------------------------------
-*/
void _DrawBmp_column(uchar x,uchar width)
{ 
	unsigned int i=0;
	
	if (x>127)
	{
		for(i=0;i<width;i++)
		{
			OutTI((x-128)|0x40);
			OutTI(i|0xb8); 
			OutTD(0xff);
		}
	}
	
	else if (x>63) 
	{
		for(i=0;i<width;i++)
		{
			OutSI((x-64)|0x40);
			OutSI(i|0xb8); 
			OutSD(0xff);
		}
	}
	
	else 
	{
		for(i=0;i<width;i++)
		{
			OutFI(x|0x40);
			OutFI(i|0xb8); 
			OutFD(0xff);
		}
	}
}


  /************************************定义中文字库***************************************/
uchar code Hzk[]={
0x00,0xF8,0x08,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x08,0x08,0xFC,0x08,0x00,	/*中*/
0x00,0x03,0x01,0x01,0x01,0x01,0x01,0xFF,0x01,0x01,0x01,0x01,0x01,0x03,0x00,0x00,
0x00,0xFE,0x02,0x0A,0x8A,0x8A,0x8A,0xFA,0x8A,0xCA,0x8E,0x0A,0x02,0xFF,0x02,0x00,    /*国*/
0x00,0xFF,0x40,0x50,0x50,0x50,0x50,0x5F,0x50,0x52,0x54,0x50,0x40,0xFF,0x00,0x00,
0x24,0x24,0xA4,0xFE,0xA3,0x22,0x10,0x88,0x8C,0x57,0xE4,0x24,0x14,0x0C,0x00,0x00,	/*移*/
0x04,0x02,0x01,0xFF,0x00,0x83,0x80,0x88,0x44,0x46,0x29,0x31,0x11,0x0D,0x03,0x00,
0x20,0x24,0x24,0xE4,0x26,0x34,0x20,0x10,0x10,0xFF,0x10,0x10,0x10,0xF8,0x10,0x00,	/*动*/
0x08,0x1C,0x0B,0x08,0x08,0x8A,0x4C,0x30,0x0C,0x03,0x40,0x80,0x40,0x3F,0x00,0x00,
0x40,0x42,0x44,0xCC,0x00,0x00,0xF1,0x91,0x95,0xF9,0x95,0x93,0xF9,0x10,0x00,0x00,	/*通*/
0x00,0x40,0x20,0x1F,0x20,0x40,0xBF,0x84,0x84,0xBF,0x94,0xA4,0x9F,0xC0,0x40,0x00,
0x80,0x40,0x20,0xF8,0x07,0x24,0x24,0x24,0x25,0x26,0x24,0x24,0xB4,0x26,0x04,0x00,	/*信*/
0x00,0x00,0x00,0xFF,0x00,0x01,0xFD,0x45,0x45,0x45,0x45,0x45,0xFD,0x01,0x00,0x00,
0x20,0x10,0x08,0xFC,0x57,0x54,0x54,0xFD,0x56,0x54,0x54,0x56,0x04,0x00,0x00,0x00,	/*集*/
0x04,0x44,0x44,0x27,0x15,0x0D,0x05,0xFF,0x05,0x0D,0x15,0x25,0x65,0x24,0x04,0x00,
0x00,0xFE,0x22,0x22,0x22,0x22,0xA2,0x62,0xFE,0x22,0x22,0x32,0x22,0xFF,0x02,0x00,	/*团*/
0x00,0xFF,0x48,0x44,0x42,0x41,0x50,0x60,0x5F,0x40,0x40,0x40,0x40,0xFF,0x00,0x00,
0x00,0x00,0x80,0x40,0x30,0x0C,0x00,0xC0,0x06,0x18,0x20,0x40,0x80,0x80,0x80,0x00,	/*公*/
0x01,0x01,0x00,0x30,0x28,0x24,0x23,0x20,0x20,0x28,0x30,0x60,0x00,0x01,0x00,0x00,
0x10,0x10,0x92,0x92,0x92,0x92,0x92,0x92,0xD2,0x9A,0x12,0x02,0xFF,0x02,0x00,0x00,	/*司*/
0x00,0x00,0x3F,0x10,0x10,0x10,0x10,0x10,0x3F,0x00,0x40,0x80,0x7F,0x00,0x00,0x00
};

void Main( void )
{
 delay10ms(1); //等待复位
 InitLCD();
 Clrscreen();
 while(1)
 {
  DrawBmp(1,1,16,Hzk);
  delay10ms(40);
  DrawBmp_f(0,2,16,Hzk);
  delay10ms(40); 
 }
}

⌨️ 快捷键说明

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