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

📄 lcd.c

📁 Lcd 1602 module c file
💻 C
字号:
/*
**********************************************************************************************
* File:		Lcd.c
* Contents:
*           The Lcd 1602 module c file
*           use GPIO LCD_CE and LCD_RS, LCD_RW,LCD_DATA to control  communicate with MSC1602
*
* Copyright (c) 2006 Fameg, Inc. All rights reserved
***********************************************************************************************
*/

#include "common.h"
#include  <intrins.h>

#include <string.h>


#define IsLCDBusy    (ReadStatus()&bmBusy)

/*
RS  =0     c/s
RS  =1     data

RW  =0     write
RW  =1     read
*/

/*
*****************************************************************************************************
* Prototype:  BYTE ReadStatus(void)
* Des:        read status form LCD
* Parameter:  No
* Return:     The LCD status
*****************************************************************************************************
*/
static BYTE ReadStatus(void)
{
	BYTE tmp;
	LCD_RS=0;
	LCD_RW=1;

	_nop_();
	_nop_();

	LCD_CE=1;             //Tsu>40ns
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	tmp= LCD_DATA;        //Td<120ns
	/*
	tmp=0;
	tmp|=P05<<7;
	tmp|=P06<<6;
	
	*/
	_nop_();
	_nop_();
	_nop_();
	_nop_();

	LCD_CE=0;          //Tw>220ns
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();

	return tmp ;           //Tc-Tw>280ns
}

/*
*****************************************************************************************************
* Prototype:  BYTE ReadData(void)
* Des:        read data form LCD
* Parameter:  No
* Return:     The LCD data
*****************************************************************************************************
*/
/*static BYTE ReadData(void)
{
	BYTE tmp;

	LCD_RS=1;
	LCD_RW=1;

	_nop_();
	_nop_();

	LCD_CE=1;             //Tsu>40ns
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	tmp= LCD_DATA;        //Td<120ns
	_nop_();
	_nop_();
	_nop_();

	_nop_();

	LCD_CE=0;          //Tw>220ns
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();

	return tmp ;           //Tc-Tw>280ns
}

/*
*****************************************************************************************************
* Prototype:  void WriteCmd(BYTE cmd)
* Des:        write command to LCD
* Parameter:  The LCD command
* Return:     No
*****************************************************************************************************
*/
static void WriteCmd(BYTE cmd)
{
	LCD_DATA=cmd;
	/*
	P05=(tmp>>7)&0x01;
	P06=(tmp>>6)&0x01;
	
	*/
	LCD_RS=0;
	LCD_RW=0;

	_nop_();
	_nop_();
	LCD_CE=1;             //Tsu>40ns
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	LCD_CE=0;          //Tw>220ns
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();

	return ;           //Tc-Tw>280ns
}

/*
*****************************************************************************************************
* Prototype:  void WriteData(BYTE dat)
* Des:        write data to LCD
* Parameter:  The LCD data
* Return:     No
*****************************************************************************************************
*/
static void WriteData(BYTE dat)
{
	LCD_DATA=dat;
	LCD_RS=1;
	LCD_RW=0;

	_nop_();
	_nop_();
	LCD_CE=1;             //Tsu>40ns
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	LCD_CE=0;          //Tw>220ns
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();

	return ;           //Tc-Tw>280ns
}

/*
*****************************************************************************************************
* Prototype:  void  LCD_SetPosition(BYTE pos)
* Des:        set logical  address
* Parameter:  "pos" is the LCD logical  address
* Return:     No
*****************************************************************************************************
*/
#define LCD_LINE_chars  16

static void  LCD_SetPosition(BYTE pos)
{
	if(pos>=16)
	{
		WriteCmd(CmdDDRAMaddr+ (pos-LCD_LINE_chars)+0x40);
	}
	else
	{
		WriteCmd(CmdDDRAMaddr+pos);
	}
	return ;
}


static BYTE screen[LCD_LINE_chars*2];

BOOL  LCD_SetCurPosition(BYTE pos)
{
	//     BYTE AC;

	if(IsLCDBusy)
	return bmBusy;

	/*        LCD_SetDispMode(CmdCGRAMaddr,0x58);
	Delay(10);
	for(i=0;i<8;i++)
	{
	//while(IsLCDBusy);
	cursor[i]=ReadData();
	Delay(10);
	}*/

	{
		#if 0
		BYTE cursor[10]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff};

		BYTE dat[10];
		BYTE i;

		LCD_SetPosition(pos);
		Delay(10);
		for(i=0;i<9;i++)
		{
			//while(IsLCDBusy);
			dat[i]=ReadData();
			Delay(10);
			//   AC=ReadStatus();
		}
		LCD_SetDispMode(CmdCGRAMaddr,0x0);
		Delay(10);
		for(i=0;i<9;i++)
		{
			//while(IsLCDBusy);
			WriteData(dat[i]|cursor[i]);
			Delay(10);
		}

		LCD_SetPosition(pos);
		Delay(10);
		WriteData(0);
		#elif 0
		if(pos)
		pos=pos-1;
		else
		pos=31;
		#endif
	}

	LCD_SetPosition(pos);
	/*        Delay(10);
	WriteData(screen[pos]);  */

	return true;
}

/*
*****************************************************************************************************
* Prototype:  void LCD_Init (void)
* Des:        Initailize the LCD
* Parameter:  No
* Return:     No
*****************************************************************************************************
*/
void LCD_Init (void)
{
	Delay(200);                                  //>15ms after powerOn
	WriteCmd(CmdInit|bm8bit|bm2line|bm510dot);
	Delay(80);                                 //>4.1ms
	WriteCmd(CmdInit|bm8bit|bm2line|bm510dot);
	Delay(10);                                //>100us
	WriteCmd(CmdInit|bm8bit|bm2line|bm510dot);

	LCD_SetDispMode(CmdInit,bm8bit|bm2line|bm510dot);
	LCD_SetDispMode(CmdDisplayMode,0);
	LCD_ClearScreen();

	LCD_SetDispMode(CmdPositionMode,bmCursorIncrease);
	LCD_SetDispMode(CmdShiftMode,bmRightShift);
}

/*
*****************************************************************************************************
* Prototype:  BOOL LCD_ClearScreen(void)
* Des:        clear all in LCD
* Parameter:  No
* Return:     if the LCD is busy ,return false.
*****************************************************************************************************
*/
BOOL LCD_ClearScreen(void)
{
	if(IsLCDBusy)
	return bmBusy;

	WriteCmd(CmdClear);
	Delay(100);                //for display cursor
	return true;
}

BOOL LCD_ClearLine(BYTE line)
{
	char screenchar[17];

	memset(screenchar,' ',16);
	screenchar[16]='\0';
	return LCD_Display(line==1?0:16,screenchar);
}
/*
*****************************************************************************************************
* Prototype:  BOOL LCD_SetDispMode (BYTE cmd,BYTE bm)
* Des:        set the display mode of Lcd
* Parameter:  "cmd" and "bm" combinate the LCD display mode,
* Return:      if the LCD is busy ,return false.
*****************************************************************************************************
*/
BOOL LCD_SetDispMode (BYTE cmd,BYTE bm)
{
	if(IsLCDBusy)
	return bmBusy;

	WriteCmd(cmd|bm);
	return true;
}

/*
*****************************************************************************************************
* Prototype:  BOOL LCD_Display (BYTE pos,BYTE *str )
* Des:        set logical  address
* Parameter:  "pos" is the LCD logical  address
* Return:      if the LCD is busy ,return false.
*****************************************************************************************************
*/
BOOL LCD_Display (BYTE pos,char *str )
{
	BYTE len=strlen(str);
	BYTE i,j;

	if(IsLCDBusy)
	return bmBusy;

	memcpy(screen+pos,str,len);
	if((pos+len<LCD_LINE_chars+1)||(pos>=LCD_LINE_chars))
	{
		LCD_SetPosition(pos);
		Delay(10);
		for(i=0;i<len;i++)
		{
			//while(IsLCDBusy);
			WriteData(str[i]);
			Delay(10);
		}
		//      AC=ReadStatus();
	}
	else
	{
		LCD_SetPosition(pos);
		j=LCD_LINE_chars-pos;
		for(i=0;i<j;i++)
		{
			//while(IsLCDBusy);
			WriteData(str[i]);
			Delay(10);
		}

		LCD_SetPosition(LCD_LINE_chars);
		for(;i<len;i++)
		{
			//while(IsLCDBusy);
			WriteData(str[i]);
			Delay(10);
		}
	}
	return true;
}

⌨️ 快捷键说明

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