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

📄 lcd.c

📁 lcd
💻 C
字号:
#include "oaks_sfr.h"	
#include "lcd.h"

int DispStr(_far char *str,int len,int line)
{    
	int i;
	char z;
	while(BUSY == lcdbusycheck());
	
	switch(line)     
	{
  	case 1:                                              //在第一行显示
		{
			lcdwrite1command ( 0x80 );                   //第一行地址
			for(i=0;i<len;i++)
			{ 
				while(BUSY == lcdbusycheck());	       /*LCD Busy check*/
				z=*(str+i);                           
				if((z>=21)&&(z<=122))                  //判断其ASCⅡ码值,如为数字或字母
					lcdwrite1data(z);			/*Output the data to 1st line*/
				else lcdwrite1data(' ');               //否则将其显示为空格
			}	
		}
		break;
	case 2:
		{
			lcdwrite1command ( 0xc0 );                 //在第二行显示
			for(i=0;i<len;i++)
			{
				while(BUSY == lcdbusycheck());		/*LCD Busy check*/
				z=*(str+i);
				if((z>=21)&&(z<=122))
					lcdwrite1data(z);			/*Output the data to 2nd line*/
				else lcdwrite1data(' ');
			} 
		}
		break;
	}
}


void DispInt(unsigned int i)
{
	int j,len,b;
	char z;
	char disp[16]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};    //预置空格
    b=i;
	for(len=1;;len++)                    //判断数字长度
	   {
		if(b/10==0) break;               //若为各位数则跳出                                                                           
		b=b/10;                          //否则则去个位
	}
	
    for(j=0;j<len;j++)                   //根据长度进行处理
	{
		disp[15-j]=i%10+48;              //右端显示;每一个数+48后ASCⅡ码值则能正确显示
	    i=i/10;
	}
    DispStr(disp,16,2);                  //调用上述显示程序

}

void LcdInit(void)
{
	p4 = 0x00;						/*Port2(LCD)Initialize*/
	pd4 = PORTOUT;						/*Set Port4 Direction Register to Output mode*/

	wait3();				/*Wating 15ms*/
	lcdwriteinit( 0x03 );			/*Settng LCD Function*/
	wait2();				/*Wating 4.1ms*/
	lcdwriteinit( 0x03 );			/*Settng LCD Function*/
	wait1();				/*Waiting 0.1ms*/
	lcdwriteinit( 0x03 );			/*Settng LCD Function*/
	wait1();
	lcdwriteinit( 0x02 );			/*Set LCD data to 4 bits length*/
	wait1();
	lcdwrite1command(0x28);			/*Set 4bit*2lines 5 * 7dots*/
	wait1();

	/* From here,  each 4bits * 2 times transrating is available.
	Now, start to trans the command after busy check.*/
	
	while(BUSY == lcdbusycheck())		/*LCD busy check*/
	;
	lcdwrite1command(0x08);			/*Display Off*/

	while(BUSY == lcdbusycheck())		/*LCD busy check*/
	;
	lcdwrite1command(0x01);			/*Display clear*/

	while(BUSY == lcdbusycheck())		/*LCD busy check*/
	;
	lcdwrite1command(0x06);			/*Entry mode increment*/

	while(BUSY == lcdbusycheck())		/*LCD busy check*/
	;
	lcdwrite1command(0x0c);			/*Display Off, Cursol Off*/

}

/*----------------------------------------------------------*/
/* Function Name:lcdwriteinit()				*/
/* Function:	 LCD Initialization Command setting     */
/*----------------------------------------------------------*/

void lcdwriteinit( unsigned char command )
{

	p4 = 0x00;			/*Put default value to P4 RW-0(W-specifyed) RS-0(Command-specifyed)E-0 */
	pd4 = PORTOUT;			/*Set P4 to Output mode*/

	command &= 0x0f;		/*Put Control Data to P4(from argument RW-0(W-specifyed) RS-0(Command-specifyed)E-0 */
	p4 = command;			/*Put Command Data to P4*/
	LCDE = HIGH;			/*E-1*/
#pragma ASM				/*From here, descrived by Assembler language*/
	NOP				/*NOP for Time control */
	NOP
	NOP
	NOP
#pragma ENDASM				/*Assembler Description End*/
	LCDE = LOW;			/*E-0*/
}

/*----------------------------------------------------------*/
/* Function Name:lcdwrite1command()			*/
/* Function:      LCD Command Output                    */
/*----------------------------------------------------------*/

void lcdwrite1command( unsigned char command )
{
	unsigned char outcommand;

	p4 = 0x00;			/*Put default value to P4 RW-0(W-specifyed) RS-0(Command-specifyed)E-0*/
	pd4 = PORTOUT;			/*Set P4 to Output mode*/

	outcommand = command>>4;	/*Shift upper 4 bits to lower 4 bits*/
	outcommand &= 0x0f;		/*Mask upper 4 bits*/
	p4 = outcommand;		/*Put command data to upper 4 bits RW-0(W-specifyed) RS-0(Command-specifyed)E-0*/

	LCDE = HIGH;			/*E-1*/
#pragma ASM				/*From here, descrived by Assembler language*/
	NOP				/*NOP for Time control*/
	NOP
	NOP
	NOP
#pragma ENDASM				/*Assembler Description End*/
	LCDE = LOW;			/*E-0*/
	;
	outcommand = command&0x0f;	/*Mask Upper 4 bits of Command data*/
	p4 = outcommand;		/*Put Lower 4 bits of Command data to P4*/

	LCDE = HIGH;			/*E-1*/
#pragma ASM				/*From here, descrived by Assembler language*/
	NOP				/*NOP for Time control*/
	NOP
	NOP
	NOP
#pragma ENDASM				/*Assembler Description End*/
	LCDE = LOW;			/*E-0*/

}

/*----------------------------------------------------------*/
/* Function Name:lcdwrite1data()			*/
/* Function:LCD Data Output				*/
/*----------------------------------------------------------*/

void	lcdwrite1data( unsigned char data )
{
	unsigned char lcddata;
	p4 = 0x00;			/*Put default value to P4 RW-0(W-specifyed) RS-0(Command-specifyed)E-0*/
	pd4 = PORTOUT;			/*Set P4 to Output mode*/

	lcddata = data>>4;		/*Shift upper 4 bits of LCD data to lower 4 bits*/
	lcddata &= 0x0f;		/*Mask Upper 4 bits of LCD data*/
	p4 = lcddata;			/*Put Upper 4 bits of LCD data to P4*/
	LCDRS = LCD_DATA;		/*RS-1(Data specify)*/
	LCDE = HIGH;			/*E-1*/
#pragma ASM				/*From here, descrived by Assembler language*/
	NOP				/*NOP for Time control*/
	NOP
	NOP
	NOP
#pragma ENDASM				/*Assembler Description End*/
	LCDE = LOW;			/*E-0*/
	
	lcddata =data & 0x0f;		/*Mask Upper 4 bits of LCD data*/
	p4 = lcddata;			/*Put Lower 4 bits of LCD data to P4*/

	LCDRS = LCD_DATA;		/*RS-1(Data specify)*/
	LCDE = HIGH;			/*E-1*/
#pragma ASM				/*From here, descrived by Assembler language*/
	NOP				/*NOP for Time control*/
	NOP
	NOP
	NOP
#pragma ENDASM				/*Assembler Description End*/
	LCDE = LOW;			/*E-0*/
}

/*----------------------------------------------------------*/
/* Function Name:wait1()				*/
/* Function :Wait 0.1ms					 */
/*----------------------------------------------------------*/
void wait1(void){			/* Wait 0.1ms */
#pragma ASM				/*From here, descrived by Assembler language*/
	MOV.W 	#0C8H,A0		/*Put default value to counter*/
LOOP1:
	NOP
	NOP
	NOP
	DEC.W	A0
	JNZ	LOOP1			/*Judge end or not for looping*/
#pragma ENDASM				/*Assembler Description End*/
}

/*----------------------------------------------------------*/
/* Function Name:wait2()				*/
/* Function :Wait 4.1ms					*/
/*----------------------------------------------------------*/

void wait2(void){			/*Wait 4.1ms */
#pragma ASM				/*From here, descrived by Assembler language*/
	MOV.W 	#2007H,A0		/*Put default value to counter*/
LOOP2:
	NOP
	NOP
	NOP
	DEC.W	A0
	JNZ	LOOP2			/*Judge end or not for looping*/
#pragma ENDASM				/*Assembler Description End*/
}

/*----------------------------------------------------------*/
/* Function Name:wait3()				*/
/* Function :Wait 15ms					*/
/*----------------------------------------------------------*/

void wait3(void){			/*Wait 15ms*/
#pragma ASM				/*From here, descrived by Assembler language*/
	MOV.W 	#7530H,A0		/*Put default value to counter*/
LOOP3:
	NOP
	NOP
	NOP
	DEC.W	A0
	JNZ	LOOP3			/*Judge end or not for looping*/
#pragma ENDASM				/*Assembler Description End*/
}

/*----------------------------------------------------------*/
/* Function Name:lcdbusycheck()				*/
/* Function :LCD Busy Check				*/
/*----------------------------------------------------------*/

unsigned char lcdbusycheck( void )
{
	unsigned char command_high,command_low,b_data;

	p4 = 0x00;			/*Put default value to P4 RW-0(W-specifyed) RS-0(Command-specifyed)E-0 */
	pd4 = PORTOUTIN;		/*Set  Upper 4 bits of P4 Direction register to Output mode, Lower 4 bits to Input mode*/

	LCDRW = HIGH;			/*RW-1(R-specifyed)*/

	LCDE = HIGH;			/*E-1*/
#pragma ASM				/*From here, descrived by Assembler language*/
	NOP				/*NOP for Time control*/
	NOP
	NOP
	NOP
#pragma ENDASM				/*Assembler Description End*/
	command_high = p4;		/*Input Upper 4 bits of Command*/
	LCDE = LOW;			/*E-0*/

	command_high <<=4;		/*Shift lower 4 bits to upper 4 bits*/
	command_high &= 0xf0;		/*Mask Lower 4 bits */

	LCDE = HIGH;			/*E-1*/
#pragma ASM				/*From here, descrived by Assembler language*/
	NOP				/*NOP for Time control*/
	NOP
	NOP
	NOP
#pragma ENDASM				/*Assembler Description End*/
	command_low = p4;		/*Input lower 4 bits of Command*/
	LCDE = LOW;			/*E-0*/

	command_low &= 0x0f;		/*Mask upper 4 bits*/
	b_data = command_high|command_low;	/* Pack to two "4bits" to one 8 bits data*/
	b_data &= 0x80;			/*Mask data except 7th bit*/
	if(b_data==0)
		b_data = NOBUSY;	/*If 7th bit is 0 value, return LCD writing enable*/
	else
		b_data = BUSY;		/*If 7th bit is 1 value, return LCD writing busy*/
		
	return(b_data);
}
	





	

⌨️ 快捷键说明

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