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

📄 lcd.c

📁 12864串口液晶,包含两个文件:lcd.c与lcd.h. 调用 void printLCD(uint8 x,uint8 y,uint8 *fmt, ... ),即可将显示的字符串发送到液晶屏显示
💻 C
字号:
/*===========================================================================
Filename: CP_LCD.C
Version : 2005-1-21
Note    : WGM-12864COG-41 V3.0驱动程序 
Hardware: XTAL=7.158MHz(3.579MHz倍频)
============================================================================*/

/*============================================================================
;	WGM-12864COG-41 VER2.0 serial LCD dirver program
;	author zhaofei,modification date 04-3-23 
;	copyright ningbo land communicatins equipment co.,ltd,2004-03
=============================================================================*/
#define LCD_GLOBAL
#include <stdarg.h>
#include  "..\inc\Dotlib.h"
#include  "..\inc\config.h"

					////0123456789012345
uint8	code EmptyTable[] = "                ";
void Delay1us(uint8 n)
{
	while(n--)
	{
		_nop_();
	}
}
/*================================================================================
;	func	data serial out
;	intern function
;	call	no
;	called  InitialLCD()   InitialLCD1() ClearLCD()  putHZ()  PutASCII() 
;	modification author zhaofei    modification date 04-03-11 
=================================================================================*/
void lcd_wr(uint8 ComDatFlag,uint8 OutData)
{
	uint8 j;
	if(ComDatFlag)
	{
        LCDA0 = 1;
	}
	else
	{
		LCDA0 = 0;
	}
	
	
	for(j=0;j<8;j++)
	{
        LCDSCL = 0;
		//Delay1us(1);
		_nop_();

		if((OutData&0x80)==0x80)
            LCDSDA = 1;
		else
			LCDSDA = 0;
		//Delay1us(1);
		_nop_();
		OutData<<=1;
        LCDSCL = 1;
		//Delay1us(2);
		_nop_();
		_nop_();
	}
} 

//void OpenLcdBackLight(void)
//{
//    //Clr_Pin_X(PIN1,LEDON);	//低电平点亮背光
//    LEDON = 1; 
//    LcdBackLightOpenTime = LCD_BLIGHT_OPEN_TIME;
//}
//
//void CloseLcdBackLight(void)
//{
//    //Set_Pin_X(PIN1,LEDON);
//    LEDON = 0;
//}

void init_LCD(void)
{	
    
    LCDCS = 1;	
//    LCDRST = 1;	
	LCDA0 = 0;	
    LCDSCL = 1;	  
	LCDSDA = 0;
    OpenLcdBackLight();
    //Clr_Pin_X(PIN1,LEDON);
//    LcdBackLightOpenTime = 0xffff;
 
	Delay1us(5);
//	LCDRST = 0;
//	Delay1ms(10);
//    LCDRST = 1;
//	Delay1ms(10);
	
	LCDCS = 0;
//	Delay10ms(1);
    lcd_wr(LCD_COMMAND,0xA2);		//置液晶显示驱动偏压比D0=0=1/9                
    lcd_wr(LCD_COMMAND,0xA0);		//显示缓冲区地址与段输出对应关系D0=0=正常 =1=按Y轴反向//a0//a1
    lcd_wr(LCD_COMMAND,0xC8);		//置公共端输出扫描方向D3=0=正常 =1=按X轴反向   //c0//c
                        
    lcd_wr(LCD_COMMAND,0x2F);		//打开供电电源                  
    Delay1ms(5); 

    lcd_wr(LCD_COMMAND,0x25);// v5:Rb/Ra,20-27粗调,越小越淡               
    Delay1ms(5);
    
	lcd_wr(LCD_COMMAND,0x81);		//BIT2-BIT0=V5电压调节,选择内部电阻比例//25//23      
	ReadBytesFMEEPROM(LCDCONTRAST_Adds,1);
  	lcd_wr(LCD_COMMAND,EEPROMDataBuf[0]);	//uLCDContrast);		//V5=1,微调,越小越淡0-3f   0x30              
  	Delay1ms(10); 
 
    lcd_wr(LCD_COMMAND,0xA4);		//D0=0=正常显示/D0=1=全亮              
    lcd_wr(LCD_COMMAND,0xA6);		//正显
    lcd_wr(LCD_COMMAND,0xAF);		//液晶显示开/关D0=1=开    
                  
    lcd_wr(LCD_COMMAND,0x60);		//GO 33 LINE         
    Delay1ms(5); 	      
    LCDCS = 1;
   
    Delay1ms(5); 

}

void SetLCDContrast( uchar n)		//设置对比度
{
	
	LCDCS = 1;	
//    LCDRST = 1;	

	Delay1us(5);
//	LCDRST = 0;
//	Delay1ms(10);
//    LCDRST = 1;
//	Delay1ms(10);
	LCDCS = 0;
	Delay1ms(10);
    lcd_wr(LCD_COMMAND,0x81);		//BIT2-BIT0=V5电压调节,选择内部电阻比例//25//23      
    if(n>0x3f)	n = uLCDContrast;		
    lcd_wr(LCD_COMMAND,n);		//V5=1,微调,越小越淡0-3f   0x30              
    Delay1ms(5); 
    LCDCS = 1;
    Delay1ms(10); 
}


void putASCII( int8 chASC, uint8 reserve )
{
	/*该函数用来显示一个ASCII码字符*/
	uint8 i;
	uint16 ASC_index, MAXLEN;
	
	if ( chASC == '\n' )
	{
		chCursorSiteNow_X = 0;
		chCursorSiteNow_Y += 16;
		if ( chCursorSiteNow_Y > Y_SIZE - 1 )
		{
			chCursorSiteNow_Y = 0;
		}
		return;
	}
	
	if ( chCursorSiteNow_X + 8 > X_SIZE )     //如果剩下的位置不够显示一个ASCII码字符,则换行
	{
		chCursorSiteNow_X = 0;
		chCursorSiteNow_Y += 16;
		if ( chCursorSiteNow_Y > Y_SIZE - 1 )
		{
			chCursorSiteNow_Y = 0;
		}
	}
	
	MAXLEN = sizeof( ASCIITAB ) / sizeof( lattice8_16 );
	for ( ASC_index = 0; ASC_index < MAXLEN; ++ASC_index )
	{
		if ( ASCIITAB[ ASC_index ].index[0] == chASC )
			break;
	}
	
	if ( ASC_index < MAXLEN )
	{
        LCDCS = 0;
	
		set_address( chCursorSiteNow_X, chCursorSiteNow_Y );
	
		if ( reserve )
		{
			for ( i = 0; i < 8; i++ )
			{
				lcd_wr( LCD_DATA, ASCIITAB[ ASC_index ].TABLE[ i ]^0xff );
			}
		}
		else
		{
			for ( i = 0; i < 8; i++ )
			{
				lcd_wr( LCD_DATA, ASCIITAB[ ASC_index ].TABLE[ i ] );
			}
		}	
	
		set_address( chCursorSiteNow_X, chCursorSiteNow_Y + 8 );
	
		if ( reserve )
		{
			for ( i = 0; i < 8; i++, chCursorSiteNow_X++ )
			{
				lcd_wr( LCD_DATA, ASCIITAB[ ASC_index ].TABLE[ i + 8 ]^0xff );
			}
		}
		else
		{
			for ( i = 0; i < 8; i++, chCursorSiteNow_X++ )
			{
				lcd_wr( LCD_DATA, ASCIITAB[ ASC_index ].TABLE[ i + 8 ] );
			}
		}
		
        LCDCS = 1;
	}
}


void putHZ( uint8 *str, uint8 reserve )
{
	/*该函数用来显示一个汉字*/
	uint8   iHZ_GB2312[ 2 ]; 
	uint16  hz_index, MAXLEN;
	uint8  i;
	
	MAXLEN = sizeof( HZTAB ) / sizeof( lattice16_16 );
	iHZ_GB2312[ 0 ] = *str;
	str ++;
	iHZ_GB2312[ 1 ] = *str;
	
	for ( hz_index = 0; hz_index < MAXLEN; ++hz_index )
	{
		if ( HZTAB[ hz_index ].index[ 0 ] == iHZ_GB2312[ 0 ] 
		  && HZTAB[ hz_index ].index[ 1 ] == iHZ_GB2312[ 1 ] )
			break;
	}
	if ( hz_index < MAXLEN )
	{
		if ( chCursorSiteNow_X + 16 > X_SIZE )     //如果剩下的位置不够显示一个汉字,则换行
		{
			chCursorSiteNow_X = 0;
			chCursorSiteNow_Y += 16;
			if ( chCursorSiteNow_Y > Y_SIZE - 1 )
			{
				chCursorSiteNow_Y = 0;
			}
		}
		
        LCDCS = 0;
    	
		set_address( chCursorSiteNow_X, chCursorSiteNow_Y );
		
		if ( reserve )
		{
			for ( i = 0; i < 16; i++ )
			{			
				lcd_wr( LCD_DATA, HZTAB[ hz_index ].TABLE[ i ]^0xff );
			}
		}
		else
		{
			for ( i = 0; i < 16; i++ )
			{			
				lcd_wr( LCD_DATA, HZTAB[ hz_index ].TABLE[ i ] );
			}
		}
		
		set_address( chCursorSiteNow_X, chCursorSiteNow_Y + 8 );
		
		if ( reserve )
		{
			for (i = 0; i < 16; i++, chCursorSiteNow_X++)
			{
				lcd_wr( LCD_DATA, HZTAB[ hz_index ].TABLE[ i + 16 ]^0xff );
			}
		}
		else
		{
			for (i = 0; i < 16; i++, chCursorSiteNow_X++)
			{
				lcd_wr( LCD_DATA, HZTAB[ hz_index ].TABLE[ i + 16 ] );
			}
		}		
        LCDCS = 1;
	}
}

//void printLCD(uint8 x,uint8 y,const uint8 *fmt, ... )
void printLCD(uint8 x,uint8 y,uint8 *fmt, ... )
{
	va_list args;
	va_start (args, fmt);

    setxy(x,y);
	while(*fmt)
	{
	    if(*fmt == '\0' || ( (*fmt == '\\') && (*++fmt == '0') ) )
	    {
	        break;
	    }
	    
		if ( *fmt != '%' || *++fmt == '%' )
		{
			if ( ( uint8)*fmt < 0x7F )
				putASCII( *fmt++, 0 );
			else
			{
				putHZ( fmt++, 0 );
				fmt++;	 //2007-1-5 14:49
			}
			continue;
		}
		else
		{
			char c;
			switch ( c = *fmt++ )
			{
				case 'c' :
					putASCII( va_arg ( args, char ), 0 );
					break;
				case 'd' :
					putASCII( va_arg ( args, char ) + '0', 0 );
					break;
				case 'h' :
					//draw_bmp( va_arg ( args, char ) );
					break;
				default  :
					break;
			}
		}

	}

 	va_end( args );
}



//void reprintLCD( uint8 x,uint8 y,const uint8 *fmt, ... )
void reprintLCD( uint8 x,uint8 y,uint8 *fmt, ... )
{
	va_list args;
	va_start ( args, fmt );
    
    setxy(x,y);
	while( *fmt )
	{
		if( *fmt == '\0' || ( (*fmt == '\\') && (*++fmt == '0') ) )
	    {
	        break;
	    }
		if ( *fmt != '%' || *++fmt == '%' )
		{
			if ( ( uint8)*fmt < 0x7F )
				
				putASCII( *fmt++, 1 );
			else
			{
				putHZ( fmt++, 1 );
				fmt++;	 //2007-1-5 14:49
			}
			continue;
		}
		else
		{
			char c;
			switch ( c = *fmt++ )
			{
				case 'c' :
					putASCII( va_arg ( args, char ), 1 );
					break;
				case 'd' :
					putASCII( va_arg ( args, char ) + '0', 1 );
					break;
				default  :
					break;
			}
		}

	}

 	va_end( args );
}

/*======================== 反显一行子程序 ======================*/
//void LcdReDisp(uint8 x1,uint8 x2,uint8 y1,uint8 y2)
//{
//    uint8 i,j;
//    
//    LCDCS = 0;
//    for(j=y1;j<=y2;j++)
//	{
//	    set_address( x1, j );
//		for(i=x1;i<=x2;i++)
//		{
//		    lcd_wr( LCD_DATA, 0xff );
//		}
//	}
//	LCDCS = 1;
//}

void draw_irr(uint8 x,uint8 y,uchar irr, bool reserve) //2006-11-27 18:43 chxd
{
	uchar i;
	uchar lcdDispDat;
	
	if ( irr > 31 ) irr = 0;
	irr = (irr)/6*4+11;
	
	
	//LCDCS_ON();
	LCDCS = 0;
    	
	set_address( x, y );
	
	for ( i = 0; i < 32; i++ )
	{
		lcdDispDat = (irr0_bmp[i]);
		
		if ( i >= 11 && i <= irr )
		{ 
			lcdDispDat |= (irr_bmp[i]);
		}
		
		if ( reserve )
		{
			lcd_wr( LCD_DATA, lcdDispDat^0xff );
		}
		else
		{
			lcd_wr( LCD_DATA, lcdDispDat );
		}
	}
	
	set_address( x, y + 8 );
	
	for (i = 0; i < 32; i++, chCursorSiteNow_X++)
	{
		
		lcdDispDat = irr0_bmp[i+32];
		
		if ( i >= 11 && i <= irr )
		{ 
			lcdDispDat |= irr_bmp[i+32];
		}
		
		if ( reserve )
		{
			lcd_wr( LCD_DATA, lcdDispDat^0xff );
		}
		else
		{
			lcd_wr( LCD_DATA, lcdDispDat );
		}
	}
	//LCDCS_OFF(); 
	LCDCS = 1;
}
void draw_spk_bmp(uint8 x,uint8 y,uint8 num)	//显示声音音量,0~3,
{
	uint8 i,n;
    LCDCS = 0;
	set_address( x, y );
	if(num == 5)
		n = 64;
	if(num == 4)
		n = 50;
	if(num == 3)
		n = 39;
	if(num == 2)
		n = 28;
	if(num == 1)
		n = 19;
	if(num == 0)
		n = 11;
	for ( i = 0; i < n; i++ )
	{
		lcd_wr( LCD_DATA, spk_voice[ i ] );
	}
	
	set_address( x, y + 8 );
	
	for (i = 0; i < n; i++)
	{
		lcd_wr( LCD_DATA, spk_voice[ i + 64 ] );
	}
	
	LCDCS = 1;
}

#if 0
void draw_bmp( uint8 x, uint8 y, uint8 num )
{
	uint8 i;
	
    LCDCS = 0;
    	
	set_address( x, y );
	
	for ( i = 0; i < 32; i++ )
	{
		if ( num == 2 )
			lcd_wr( LCD_DATA, delete_bmp[ i ] );
		if ( num == 1 )
			lcd_wr( LCD_DATA, cancel_bmp[ i ] );
		if ( num == 0 )
			lcd_wr( LCD_DATA, ok_bmp[ i ] );
	}
	
	set_address( x, y + 8 );
	
	for (i = 0; i < 32; i++)
	{
		if ( num == 2 )
			lcd_wr( LCD_DATA, delete_bmp[ i + 32 ] );
		if ( num == 1 )
			lcd_wr( LCD_DATA, cancel_bmp[ i + 32 ] );
		if ( num == 0 )
			lcd_wr( LCD_DATA, ok_bmp[ i + 32 ] );
	}
	
	LCDCS = 1;
}
#endif

/*====================================================================================
;	func	clear lcd
;	extern function
;	call	no
;	called  main()
;	extern function
=====================================================================================*/
void ClearLcd(void)
{
	uint8 i,pagenum,pagedata;
	LCDCS = 0;
	Delay1us(10);
	for(pagenum=0;pagenum<9;pagenum++)
	{
		pagedata = pagenum + 0xB0;
		lcd_wr(LCD_COMMAND,pagedata);
		lcd_wr(LCD_COMMAND,0x60);	 
		lcd_wr(LCD_COMMAND,0x10);  
		lcd_wr(LCD_COMMAND,0x00);	

		for(i=0;i<132;i++)
		{
			lcd_wr(LCD_DATA,0x00);
		}
	}
    LCDCS = 1;
	
	chCursorSiteNow_X = 0;
	chCursorSiteNow_Y = 0;
}
void ClearLcdLine(uint8 Oy)
{
	printLCD(0,Oy,EmptyTable);
}
//void ClearLcdLine123(void)
//{
//	ClearLcdLine(0);
//	ClearLcdLine(16);
//	ClearLcdLine(32);
//}
void setxy(unsigned char x, unsigned char y)
{
	chCursorSiteNow_X = x;
	chCursorSiteNow_Y = y;
}

void set_address(unsigned char x, unsigned char y)
{
	lcd_wr(LCD_COMMAND, 0xb0 | y/8);			//设置Page
	lcd_wr(LCD_COMMAND, 0x60);					//设置扫描开始的列号
	lcd_wr(LCD_COMMAND, 0x10 | (x / 0x10));		//设置列的高地址
	lcd_wr(LCD_COMMAND, (x % 0x10) + 1);	    //设置列的低地址
}


////在第0行第no个字符开始显示Length个字符
//void LCDDispLine0(uint8 no,uint8 *str,uint8 Length)
//{	uint8 i;
//	setxy(no*8,0);
//	for(i=0;i<Length;i++)putASCII(*str++,1);
//}				 
//	
////在第1行第no个字符开始显示Length个字符		  
//void LCDDispLine1(uint8 no,uint8 *str,uint8 Length)
//{	uint8 i; 
//	if(WarnTimer)return;								//在显示告警信息期间,不能显示其他信息
//	setxy(no*8,16);
//	for(i=0;i<Length;i++)putASCII(*str++,0);
//}				   
//
////在第2行第no个字符开始显示Length个字符
//void LCDDispLine2(uint8 no,uint8 *str,uint8 Length)
//{	uint8 i; 					   
//	if(WarnTimer)return;								//在显示告警信息期间,不能显示其他信息
//	setxy(no*8,32);
//	for(i=0;i<Length;i++)putASCII(*str++,0);
//}				
//
////在第3行第no个字符开始显示Length个字符
//void LCDDispLine3(uint8 no,uint8 *str,uint8 Length)
//{	uint8 i; 
//	setxy(no*8,48);
//	for(i=0;i<Length;i++)putASCII(*str++,1);
//}		

 //在第2行和第3行显示告警信息1S
//void DispWarnInfo(uint8 *pL2, uint8 *pL3)
//{
//	 LCDDispLine1(0,EmptyTable,16);			//先清行	
//	 LCDDispLine2(0,EmptyTable,16);			//先清行	 
//	 WarnTimer=0;
//	 LCDDispLine1(0,pL2,16);							//显示信息内容	
//	 LCDDispLine2(0,pL3,16);							//显示信息内容 
//	 WarnTimer=WARNINFOTIME;							//设置显示警告信息保护时间
//}

 

 //在第2行和第3行显示告警信息短时间
//void DispWarnInfoShort(uint8 *pL2, uint8 *pL3)
//{
// 	
//	 LCDDispLine1(0,EmptyTable,16);			//先清行	
//	 LCDDispLine2(0,EmptyTable,16);			//先清行
//	 WarnTimer=0;
//	 LCDDispLine1(0,pL2,16);							//显示信息内容	
//	 LCDDispLine2(0,pL3,16);							//显示信息内容 
//	 WarnTimer=2;							//设置显示警告信息保护时间
//}

⌨️ 快捷键说明

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