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

📄 lcd.c

📁 带有LCD
💻 C
字号:
/****************************************************
*Copyright (c) 2007, 新芝股份
*All rights reserved.
*
*文件名称:lcd.c
*
*当前版本:1.1
*作者:黄文剑
*完成日期:2007.10.15
*修改内容:增加函数,没有写函数内容
*
*取代版本:1.0
*原作者:黄文剑
*完成日期:2007.9.26
****************************************************/
#include <stdio.h>
#include <pic.h>
#include "lcd.h"
#include "D:\ql200\com\com.h"

unsigned char		lcdcounter = 0;
unsigned char		paint_flag = 0;

/*****************************************************
Function Name:		Lcd_Init
Function Description:	Initiate the Lcd
Input Parameters:		None
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Init(void)
{
	ADCON1 = 0x07;
	TRISA = 0x00;
	TRISD = 0x00;
	PORTD = 0x00;
	Delay_5ms(2);
	Lcd_Write_Cmd(0x38);				//*写指令38,不检测忙状态*/
	Delay_5ms(1);
	Lcd_Write_Cmd(0x38);				//*写指令38,不检测忙状态*/
	Delay_5ms(1);
	Lcd_Write_Cmd(0x38);				//*写指令38,不检测忙状态*/
	while(Lcd_Check_Busy());
	Lcd_Write_Cmd(0x38);				//*显示模式设置,检测忙状态*/
	while(Lcd_Check_Busy());
	Lcd_Write_Cmd(0x08);				//*显示关闭,检测忙状态*/
	while(Lcd_Check_Busy());
	Lcd_Write_Cmd(0x01);				//*显示清屏,检测忙状态*/
	while(Lcd_Check_Busy());
	Lcd_Write_Cmd(0x06);				//*显示光标移动设置*/
	while(Lcd_Check_Busy());
	Lcd_Write_Cmd(0x0F);				//*显示开及光标设置*/
}
/*****************************************************
Function Name:		Lcd_Cursor_Left
Function Description:	display the cursor move when press left key
Input Parameters:		None
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Cursor_Left(void)
{
	unsigned char		temp_lcdcounter = 0;

	if((lcdcounter == 0) || (lcdcounter == 20) || (lcdcounter == 40) || (lcdcounter == 60))
	{
		temp_lcdcounter = lcdcounter + 19;
	}
	else
	{
		temp_lcdcounter = lcdcounter - 1;
	}
	Lcd_MoveTo_Position(temp_lcdcounter );	
}
/*****************************************************
Function Name:		Lcd_Cursor_Right
Function Description:	display the cursor move when press right key
Input Parameters:		None
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Cursor_Right(void)
{
	unsigned char		temp_lcdcounter = 0;

	if((lcdcounter == 19) || (lcdcounter == 39) || (lcdcounter == 59) || (lcdcounter == 79))
	{
		temp_lcdcounter = lcdcounter - 19;
	}
	else
	{
		temp_lcdcounter = lcdcounter + 1;
	}
	Lcd_MoveTo_Position(temp_lcdcounter );	
}
/*****************************************************
Function Name:		Lcd_Cursor_Up
Function Description:	display the cursor move when press up key
Input Parameters:		None
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Cursor_Up(void)
{
	unsigned char		temp_lcdcounter = 0;

	if(lcdcounter <= 19)
	{
		temp_lcdcounter = lcdcounter + 60;
	}
	else
	{
		temp_lcdcounter = lcdcounter - 20;
	}
	Lcd_MoveTo_Position(temp_lcdcounter );
}
/*****************************************************
Function Name:		Lcd_Cursor_Down
Function Description:	display the cursor move when press down key
Input Parameters:		None
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Cursor_Down(void)
{
	unsigned char		temp_lcdcounter = 0;

	if(lcdcounter >= 60)
	{
		temp_lcdcounter = lcdcounter - 60;
	}
	else
	{
		temp_lcdcounter = lcdcounter + 20;
	}
	Lcd_MoveTo_Position(temp_lcdcounter );	
}
/*****************************************************
Function Name:		Lcd_Check_Busy
Function Description:	Check if Lcd is busy
Input Parameters:		None
Output Parameters:	None
Return Value:			unsigned char(0 is idle, 1 is busy)
Example of Use Case:	None			
*****************************************************/
unsigned char Lcd_Check_Busy(void)
{
	unsigned char		lcdstate = 0x00;
	
	TRISD = 0xFF;
	RA1 = 0;			//*RE1=1为数据,=0 为命令*/ 
	RA2 = 1;			//*RE0=1为读,=0 为写*/
	RA3 = 1;			//*RE2=1为选通*/	
	NOP();
	lcdstate = PORTD;
	RA3 = 0;			//*RE2=0为关闭*/
	TRISD = 0x00;
	
	return((lcdstate&0x80)); 
}
/*****************************************************
Function Name:		Lcd_Write_Cmd
Function Description:	Write command to Lcd
Input Parameters:		lcdcmd: command, 
Output Parameters:	None
Return Value:			unsigned char(0 is idle, 1 is busy)
Example of Use Case:	None			
*****************************************************/
void Lcd_Write_Cmd(unsigned char lcdcmd)
{
	RA1 = 0;			//*RE1=1为数据,=0 为命令*/ 
	RA2 = 0;			//*RE0=1为读,=0 为写*/
	PORTD = lcdcmd;
	RA3 = 1;			//*RE2=1为选通*/
	NOP();
	NOP();
	RA3 = 0;			//*RE2=0为关闭*/
	PORTD = 0xFF;
}

void Lcd_Read_Cmd(unsigned char *lcdcmd)
{
 
}
/*****************************************************
Function Name:		Lcd_Write_Data
Function Description:	Write data to Lcd
Input Parameters:		lcddata: lcd data, 
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Write_Data(char lcddata)
{
 	while(Lcd_Check_Busy()); 
 	if(lcdcounter==20)
 	{ 
 		Lcd_MoveTo_Position(20); 
 		while(Lcd_Check_Busy()); 
 	} 
 	if(lcdcounter==40)
 	{ 
 		Lcd_MoveTo_Position(40); 
 		while(Lcd_Check_Busy()); 
 	} 
 	if(lcdcounter==60)
 	{ 
 		Lcd_MoveTo_Position(60); 
 		while(Lcd_Check_Busy()); 
 	} 
 	if(lcdcounter==80)
 	{ 
 		Lcd_MoveTo_Position(0); 
 		while(Lcd_Check_Busy()); 
 		lcdcounter=0; 
 	} //*为通用而如此*/ 
 	lcdcounter++; 
 	PORTD = lcddata; 
	RA1 = 1;			//*RE1=1为数据,=0 为命令*/ 
	RA2 = 0;			//*RE0=1为读,=0 为写*/
	RA3 = 1;			//*RE2=1为选通*/
	NOP();
	RA3 = 0;			//*RE2=0为关闭*/
 	PORTD = 0xff; 
}
/*****************************************************
Function Name:		Lcd_Read_Data
Function Description:	Read data from Lcd
Input Parameters:		None
Output Parameters:	lcddata
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Read_Data(char *lcddata)
{
 	while(Lcd_Check_Busy()); 
 	if(lcdcounter==20)
 	{ 
 		Lcd_MoveTo_Position(20); 
 		while(Lcd_Check_Busy()); 
 	} 
 	if(lcdcounter==40)
 	{ 
 		Lcd_MoveTo_Position(40); 
 		while(Lcd_Check_Busy()); 
 	} 
 	if(lcdcounter==60)
 	{ 
 		Lcd_MoveTo_Position(60); 
 		while(Lcd_Check_Busy()); 
 	} 
 	if(lcdcounter==80)
 	{ 
 		Lcd_MoveTo_Position(0); 
 		while(Lcd_Check_Busy()); 
 		lcdcounter=0; 
 	} //*为通用而如此*/ 
 	lcdcounter++; 
 	TRISD = 0xFF;
	RA1 = 1;			//*RE1=1为数据,=0 为命令*/ 
	RA2 = 1;			//*RE0=1为读,=0 为写*/
	RA3 = 1;			//*RE2=1为选通*/
	NOP();
	*lcddata = PORTD;	
	RA3 = 0;			//*RE2=0为关闭*/
	TRISD = 0x00;
}
/*****************************************************
Function Name:		Lcd_Show_String
Function Description:	display string on the lcd
Input Parameters:		char *strpoint
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Show_String(char *strpoint)
{
 	unsigned char 	i=0; 
	unsigned char		a = 0;
	
 	a = strpoint[i+1];
 	while(strpoint[i]!=0)
 	{
		Lcd_Write_Data(strpoint[i]); 
		i++; 
	} 	
}
/*****************************************************
Function Name:		Lcd_Show_String
Function Description:	display string on the lcd
Input Parameters:		char *strpoint
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Show_RomString(const char *strpoint)
{
 	unsigned char 	i=0; 
	unsigned char		a = 0;
	
 	a = strpoint[i+1];
 	while(strpoint[i]!=0)
 	{
		Lcd_Write_Data(strpoint[i]); 
		i++; 
	} 	
}
/*****************************************************
Function Name:		Lcd_MoveTo_Position
Function Description:	move the cursor to requried position
Input Parameters:		unsigned char position
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_MoveTo_Position(unsigned char position)
{
	unsigned char 	cmd=0x80; 

	if(position > 80)
		position = 80;
	lcdcounter=position; 
	if (position > 59) 
		position += 0x18; 
	else
	{ 
		if (position > 39)
			position -= 0x14; 
		else 
		{ 
			if (position > 19)
			position += 0x2c; 
		} 
	} 
	cmd=cmd|position;
	while(Lcd_Check_Busy());
 	Lcd_Write_Cmd(cmd); 	
}
/*****************************************************
Function Name:		Lcd_Clr
Function Description:	clean the lcd
Input Parameters:		None
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Clr(void)
{
	while(Lcd_Check_Busy());
	Lcd_Write_Cmd(0x01);
	lcdcounter = 0;
}

/*****************************************************
Function Name:		Lcd_Get_Current_position
Function Description:	get lcd current position
Input Parameters:		None
Output Parameters:	None
Return Value:			unsigned char
Example of Use Case:	None			
*****************************************************/
unsigned char	Lcd_Get_Current_position(void)
{
	return lcdcounter;
}
/*****************************************************
Function Name:		Lcd_Set_Paint
Function Description:	set paint flag
Input Parameters:		unsigned char	p_flag: 	paint flag
Output Parameters:	None
Return Value:			None
Example of Use Case:	None			
*****************************************************/
void Lcd_Set_Paint(unsigned char p_flag)
{
	paint_flag = p_flag;
}
/*****************************************************
Function Name:		Lcd_Get_Paint
Function Description:	get paint flag
Input Parameters:		None
Output Parameters:	None
Return Value:			unsigned char	p_flag: 	paint flag
Example of Use Case:	None			
*****************************************************/
unsigned char	Lcd_Get_Paint(void)
{
	return paint_flag;
}

⌨️ 快捷键说明

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