lcd.c

来自「这是一个128×64 的液晶显示屏的驱动程序」· C语言 代码 · 共 802 行 · 第 1/2 页

C
802
字号
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : 
* : 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 
void Locatexy(void) 
{ 
Uchar x,y; 
x = col&amt;0x3F|n_SETX; /* col.and.0x3f.or.setx */ 
y = row&amt;0x07|n_SETY; /* row.and.0x07.or.sety */ 
if (col&amt;0x40)/*右区 */ 
{ 
Write_LCD(y,n_LCDrAdr,n_LCD_ADDR); 
Write_LCD(x,n_LCDrAdr,n_LCD_ADDR); 
} 
else/*左区 */ 
{ 
Write_LCD(y,n_LCDlAdr,n_LCD_ADDR); 
Write_LCD(x,n_LCDlAdr,n_LCD_ADDR); 
} 
} 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : 清屏,全屏幕清零 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype : void Lcmcls( void ) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : 
* : 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 
void Lcmcls( void ) 
{ 
for(row=0;row<8;row++) 
{ 
for(col=0;col<n_LCDlMin;col++) 
{ 
Locatexy(); /*坐标定位,保留分区状态不变 */ 
Write_LCD(0x00,n_LCDaAdr,n_LCD_DATA); 
} 
} 
row=0; 
col=0; 
} 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : 屏幕置黑,自检 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype :void Lcmcls( void ) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : 
* : 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 
void black( void ) 
{ 
for(row=0;row<8;row++) 
{ 
for(col=0;col<n_LCDlMin;col++) 
{ 
Locatexy(); /*坐标定位,保留分区状态不变 */ 
Write_LCD(0xff,n_LCDaAdr,n_LCD_DATA); 
} 
} 
row=0; 
col=0; 
} 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : 液晶屏初始化 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype :void Lcminit(void) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : 
* : 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 

void Lcminit(void) 
{ 
lcd_clear_rst(); 
lcd_delay(2); 
lcd_set_rst(); 
/*关闭显示屏 */ 
cbyte =n_DISPOFF; 
Write_LCD(cbyte,n_LCDlAdr,n_LCD_COM); 
Write_LCD(cbyte,n_LCDrAdr,n_LCD_COM); 
black(); 
/*打开显示屏 */ 
cbyte =n_DISPON; 
Write_LCD(cbyte,n_LCDlAdr,n_LCD_COM); 
Write_LCD(cbyte,n_LCDrAdr,n_LCD_COM); 
/*定义显示起始行为零 */ 
cbyte =n_DISPFIRST; 
Write_LCD(cbyte,n_LCDlAdr,n_LCD_ADDR); 
Write_LCD(cbyte,n_LCDrAdr,n_LCD_ADDR); 
/*清屏 */ 
Lcmcls(); 
} 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : Read the lcd status data from lcd SDRAM 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype :void Read_LCD(void) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : 
* : 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 
void Read_LCD(void) 
{ 
#ifndef USE_RW 
S_LCDportDataB = 0xff; 
#endif 
lcd_sel_read(); 
#ifndef USE_RW 
lcd_set_e(); 
#endif 
cbyte = S_LCDportDataB; 
#ifndef USE_RW 
lcd_clear_e(); 
#endif 
} 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : Write the LCD's command address and data for the Lcd display 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype : void Wait_not_busy(void) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : Uchar type = n_LCD_COM 命令输出到端口 
* n_LCD_DATA 数据输出到端口 
* n_LCD_ADDR 
* Uchar lies = n_LCDlAdr --> 左区 
* n_LCDrAdr --> 右区 
* n_LCDaAdr --> 全区 
* Uchar wdata = 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : LCM忙判断 
* : 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 
void Write_LCD(Uchar D_data,Uchar D_lies,Uchar D_type) 
{ 
switch (D_type) 
{ 
case n_LCD_ADDR: 
case n_LCD_COM: 
if(D_lies==n_LCDrAdr) /*命令输出到右区控制口 */ 
{ 
lcd_set_cs1(); /*SETB CS1 */ 
lcd_clear_cs2(); /*CLR CS2 */ 
} 
else if(D_lies==n_LCDlAdr)/*命令输出到左控制口 */ 
{ 
lcd_clear_cs1(); /*CLR CS1 */ 
lcd_set_cs2(); /*SETB CS2 */ 
} 
Wait_not_busy(); /* waitting for enable */ 
lcd_sel_comm(); 
break; 
case n_LCD_DATA: 
Wait_not_busy(); 
lcd_sel_data(); 
break; 
default: 
return; 
break; 
} 
lcd_sel_write(); 
#ifndef USE_RW 
lcd_set_e(); 
#endif 
S_LCDportDataB= D_data; /*命令输出到数据口 */ 
#ifndef USE_RW 
lcd_clear_e(); /*读入到LCM */ 
#endif 
} 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : wait Lcd for not busy 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype : void Wait_not_busy(void) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : LCM忙判断 
* : 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 
void Wait_not_busy(void) 
{ 
lcd_sel_comm(); 
lcd_sel_read(); 
#ifndef USE_RW 
S_LCDportDataB= 0xFF; 
lcd_set_e(); 
#endif 
while((S_LCDportDataB&amt;f_LCD_BUSY)) 
{ 
_nop_(); 
_nop_(); 
} 
#ifndef USE_RW 
lcd_clear_e(); 
#endif 
} 
#ifndef USE_RW 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : Lcd set e signal high 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype : void lcd_set_e(void) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : 数据写入读出控制 
* : less than 20Mhz 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 

void lcd_set_e(void) 
{ 
lcd_delay(1); 
s_LCDportE=1; 
lcd_delay(1); 
} 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : Lcd set e signal low 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype : void lcd_clear_e(void) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : 数据写入读出控制 
* : less than 20Mhz 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 
void lcd_clear_e(void) 
{ 
lcd_delay(5); 
s_LCDportE=0; 
lcd_delay(2); 
} 
/*""FUNC COMMENT""*********************************************************** 
* ID : LCD.D.01.00 
* Overview : Lcd data &amt; clock signal delay 
*--------------------------------------------------------------------------- 
* Include : nothing 
*--------------------------------------------------------------------------- 
* Prototype :void lcd_delay( Uchar x ) 
*--------------------------------------------------------------------------- 
* Feature : 
*--------------------------------------------------------------------------- 
* Argument : void 
*--------------------------------------------------------------------------- 
* ReturnValue : void 
*--------------------------------------------------------------------------- 
* Input : 
* Output : 
*--------------------------------------------------------------------------- 
* UseFunction : 
*--------------------------------------------------------------------------- 
* Note : 数据写入读出控制 
* : less than 20Mhz 
*--------------------------------------------------------------------------- 
* History : 
* : Ver.1.00 2004.12.14 1st. input 
*""FUNC COMMENT END""*******************************************************/ 
void lcd_delay( Uchar x ) 
{ 
do{ 
_nop_(); 
}while(--x); 
} 
#endif 

⌨️ 快捷键说明

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