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

📄 ks0108.c

📁 LCD12864的嵌入式C程序 很有参考价值!
💻 C
字号:
#include <regx52.h>
#include <intrins.h>

sbit 	RS = P2^6;	/*"H" is send or read the data;"L" is the instruction*/
sbit	RW = P3^6;
sbit	E  = P2^5;	/*Operation enable*/
sbit 	CS1 = P2^4;
sbit 	CS2 = P2^3;
sbit 	CS3 = P2^2;


#define DATA  P0        
#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<15;j++)
			for(k=0;k<120;k++);
}
/*----------------------------------------------------------------------------
Send the instruction to the First KS0108 window
------------------------------------------------------------------------------
--*/
void OutFI(uchar i)
{ 
	unsigned char data_sta;
	EA=0;
  	do{ 
  		E=0;RW=1;RS=0;CS1=0;_nop_();
		E=1;_nop_();_nop_();_nop_();
		data_sta=DATA;
		_nop_();_nop_();
		E=0;RW=0;CS1=1;
  	}while(data_sta&0x80);

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

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

		E=0;RW=0;CS2=1;
  	}while(data_sta&0x80);

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

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

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

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

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

    E=0;RW=0;RS=1;CS3=0;_nop_();
    DATA=i;
    E=1;_nop_();_nop_();_nop_();
	E=0;RW=1;CS3=1;
	EA=1;
}
/*------------------------------------------------------------------------------
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 large Chinese ideograph width;bmp is the table's name 
--------------------------------------------------------------------------------
-*/
void DrawBmp_large_char(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]);

			OutTI((x-128)|0x40);			/*Display the down screen*/
			OutTI((layer|0xb8)+2);	
			i++;							/*Display zimo table address add 1*/
			OutTD(bmp[i]);

			OutTI((x-128)|0x40);			/*Display the down screen*/
			OutTI((layer|0xb8)+3);	
			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]);

			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+2);	
			i++;
			OutSD(bmp[i]);

			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+3);	
			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]);

			OutFI(x|0x40);
			OutFI((layer|0xb8)+2);	
			i++;							
			OutFD(bmp[i]);

			OutFI(x|0x40);
			OutFI((layer|0xb8)+3);	
			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_large_(uchar x_add,uchar layer,uchar width)
{ 
	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(0);						/*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(0);

			OutTI((x-128)|0x40);			/*Display the down screen*/
			OutTI((layer|0xb8)+2);	
			i++;							/*Display zimo table address add 1*/
			OutTD(0);

			OutTI((x-128)|0x40);			/*Display the down screen*/
			OutTI((layer|0xb8)+3);	
			i++;							/*Display zimo table address add 1*/
			OutTD(0);
 		}
      	else if (x>63) 						/*The Second ks0108*/                                  
      	{
			OutSI((x-64)|0x40);
			OutSI(layer|0xb8);
	  		OutSD(0);
		
			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+1);	
			i++;
			OutSD(0);

			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+2);	
			i++;
			OutSD(0);

			OutSI((x-64)|0x40);
			OutSI((layer|0xb8)+3);	
			i++;
			OutSD(0);
		}

 		else 								/*The First ks0108*/
  	  	{
			OutFI(x|0x40);
			OutFI(layer|0xb8);
			OutFD(0);

			OutFI(x|0x40);
			OutFI((layer|0xb8)+1);	
			i++;							
			OutFD(0);

			OutFI(x|0x40);
			OutFI((layer|0xb8)+2);	
			i++;							
			OutFD(0);

			OutFI(x|0x40);
			OutFI((layer|0xb8)+3);	
			i++;							
			OutFD(0);

	  	}
        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(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_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);
 		}
	}
}



⌨️ 快捷键说明

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