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

📄 lcd12864_ks0108.c

📁 液晶128*64(KS0108)并行驱动,有详细的注释,对初学者有用
💻 C
字号:
/*LCD 12864 Control Instruction:KS0108
--------------------------------------------------------------------
|instruction    	RS  R/W  DB7  DB6  DB5  DB4  DB3  DB2  DB1  DB0 |        	
--------------------------------------------------------------------
|display on/off  	0    0    0    0    1    1    1    1    1   0/1	|	  开/关显示:RS=R/W=0, 3E/3F
--------------------------------------------------------------------
|SET ADDRESS     	0    0    0    1     Y ADDRESS(0~63) 			|
--------------------------------------------------------------------
|SET PAGE         	0    0    1    0    1    1    1     (0~7)		|
--------------------------------------------------------------------
|DISPLAY STATE LINE	0    0    1    1    (0~63)						|
--------------------------------------------------------------------
|STATUS READ		0    1   Busy  0   on/off reset 0   0   0    0  |
|busy=0:ready,busy=1:in operation.  on/off=0:display on,on/off=1:display off.
|reset=0:normal,reset=1:reset
 -------------------------------------------------------------------
|write display data      1    0   (write data)						|
--------------------------------------------------------------------
|read  display data      1    1    (read data)						|
--------------------------------------------------------------------
|12864液晶由左右两屏组成,每屏由8x8点阵组成,由CS1、CS2选择,共8页(0~7),每页8行(0~7)
---------------------------------------------------------------------------------------*/
#include <REG52.H>
#include <intrins.h>
#include <lcd12864.h>
#include "tunerval.h"
uchar  data number=0;
sbit   L_RS=P3^4;
sbit   L_RW=P3^5;
sbit   L_E=P3^3;
sbit   L_CS2=P3^6;
sbit   L_CS1=P3^7;
void busy(void)
	{	
			L_RS=0;
			L_RW=1;
			P0=0xff;//read state;
  			L_E=1;
  			P0=P0&0x80;
			L_E=0;
	} 
void LCD_C_D(bit flag,uchar data1)          /*flag=1 CONTROL flag=0 data*/
{	    
	uchar i;
	busy();
    L_RS = !flag;    /*flag=0时,将数据D0_D7送入指令寄存器执行	*/
	L_E = 0;    
	L_RW = 0; /* 写数据*/  
	P0 = data1;
    _nop_(); _nop_();_nop_();
    L_E = 1;
    _nop_(); _nop_();_nop_();
    L_E = 0; /* L_E从高到底即下降沿锁定数据	*/
    L_RW = 1;/*读出数据*/ 
	L_RS = 1;/*将数据D0_D7送入指令寄存器执行*/
    for ( i = 0; i < 10 ;i++) ;
}
void dis_16x16(uchar x_add,uchar y_add,uchar *position)
 /*dis_16x16(x,y,z):12864液晶能表示16x16大小的汉字共4行(0~3),8列(0~7)
    其中x表示汉字显示的行位置(例X=1表示第二行汉字行),
    y表示列位置(例y=1表示第二列汉字列)
    12864液晶共8页(0~7),两页为一16x16汉字行*/
{
    uchar i,j,w;
	  busy();
    j = x_add*2+0xb8;/*设置需显示的字的页地址位置,B8=10111000:设置为第零页地址,*/
    w = (y_add < 4 ? y_add : y_add-4)*16+0x40;/*  ?:条件运算符号(逻辑表达式?表达式1:表达式2),
						                         要求有三个运算对象,首先计算逻辑表达式,其值为
					                             真时将表达式1的值作为整个逻辑表达式的值,
						                         为假时将表达式2的值作为整个逻辑表达式的值*/
                                                +16为从二个汉字位置处显示,字体为16x16大小*/
    L_CS1 =!(y_add < 4);/*小于4字时选CS1半屏,一屏最多显示4个16x16的汉字*/
    L_CS2 =(y_add < 4);
    LCD_C_D(1,j);/*设置需显示的字的页地址位置*/
    LCD_C_D(1,w);/*设置需显示的字的列地址位置*/
    for (i = 0; i < 32;i++)
    {
        if (i == 16)/*数据为8位,每行128位,汉字字体为16x16,需32字节,需两页才能显示一个字,
        	         必须向两页中对应列送数据,i=16就换页显示*/
        {
            LCD_C_D(1,j+1);/*设置需显示的字的页地址位置,即i=16时换页显示*/
            LCD_C_D(1,w);  /*设置需显示的字的列地址位置,列位置不变*/
        }
        LCD_C_D(0,*position++);/*在指定位置显示字,*position=P0=D0~D7 */
    }
    L_CS1 = L_CS2 = 0;
}

void dis_any_dot(uchar x_add,uchar y_add,uchar dot,uchar reverse)
{
    uchar i,j,tem,tem1,tem2;
	busy();
    if(reverse==1){tem=~dot;}else tem=dot;//反白显示
    if(x_add==0){tem1=0x01|tem;}//第一页要显示上边框	
	else if(x_add==7){tem1=0x80|tem;}//最后一页要显示下边框
	else tem1=tem;
    if(y_add==0|y_add==127){tem2=0xff;}else tem2=tem1;//第一列要显示左边框,最后一列要显示右边框
	i = x_add+0xb8;
    j = (y_add < 64 ? y_add : y_add-64)+0x40;
    L_CS1 =!(y_add < 64);
    L_CS2 =(y_add < 64);
    LCD_C_D(1,i);/*设置需显示的字的页地址位置*/
    LCD_C_D(1,j);/*设置需显示的字的列地址位置*/
    LCD_C_D(0,tem2);/*在指定位置显示字*/
    L_CS1 = L_CS2 = 0;
}
void dis_6x8(uchar page,uchar row,uchar *character,uchar reverse)//显示一行128列
{	
	uchar i;
	for(i=0;i<6;i++){dis_any_dot(page,row+i,*character++,reverse);};
}
void dis_12x12(uchar page,uchar row,uchar *character,uchar reverse)	//page:在第几页显示;row:在第几行显示; reverse=1:反白显示
{	
	uchar i;
	for(i=0;i<24;i++)
		{
			if(i<12){dis_any_dot(page,row+i,*character++,reverse);}
			else dis_any_dot(page+1,row+i-12,*character++,reverse);
		};
}
void show_lcd_borad(void)
{    
	uchar i;
	for(i=0;i<128;i++)
		{
			dis_any_dot(0,i,0x01,0);
			dis_any_dot(7,i,0x80,0);
		};
	for(i=0;i<8;i++)
	{
		dis_any_dot(i,0,0xff,0);
		dis_any_dot(i,127,0xff,0);
		}
}

void CLRLCD(uchar number)
{
    uchar i,j;
	busy();
    L_CS1 = L_CS2 = 1;
    for (i = 0xb8; i < 0xc0;i++)/*选择所有页*/
    {
        LCD_C_D(1,i);
        LCD_C_D(1,0X40);
        for (j = 0; j < 0x40; j++)
        LCD_C_D(0,number);
    }
    L_CS1 = L_CS2 = 0;
}
void lcd_init(void)
{	delay(100);       
    LCD_C_D(1,0X3E);    /*L_RS=0,3E=111110:关显示*/
    LCD_C_D(1,0XC0);    /*C0=11000000:从0行开始显示*/
    LCD_C_D(1,0X3F);    /*开显示*/
    CLRLCD(0);
	}


⌨️ 快捷键说明

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