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

📄 glcd.c.bak

📁 PIC KS0108 圖型液晶驅動C源碼
💻 BAK
字号:
//==============================================================================
// DEVICE       = PICC-18
// VERSION      = 1.0
// DATE         = 11.08.2007
// LAST CHANGE  = -
// Developer    = Keith Yuen
// =============================================================================
// Description:  KS0108 GLCD Driver
// =============================================================================

#include <pic18.h>
#include "glcd.h"

#ifndef GLCD_4MHZ
	#ifndef GLCD_8MHZ
		#include "delay.h"
	#endif
#endif


/****************************************************************
* PRIVATE VARIABLES
****************************************************************/
unsigned char GLCD_line;
unsigned char GLCD_pos;

const unsigned char * GLCD_charPosPtr;
typedef const unsigned char * (*GLCD_GetCharDataPtr)(unsigned char ch);


/****************************************************************
* PRIVATE FUNCTIONS, LOW LEVEL 
****************************************************************/
#ifdef GLCD_4MHZ
	#define GLCD_Delay1Us() asm("nop")
#elif defined(GLCD_8MHZ)
	#define GLCD_Delay1Us() asm("nop");asm("nop")
#else
	#define GLCD_Delay1Us() DelayUs(1)
#endif

/*=====================================================
*Send one enable strobe ___---___
=====================================================*/
//At least 2us hold time
void GLCD_Strobe(void)
{
	GLCD_Delay1Us();
	GLCD_PIN_EN = GLCD_ENABLE;
	GLCD_Delay1Us();
	GLCD_Delay1Us();
	GLCD_PIN_EN = GLCD_DISABLE;
}

/*=====================================================
*Select one chip 0:Left Chip, 1:Right Chip
=====================================================*/
void GLCD_ChipSelect(unsigned char x)
{
	GLCD_PIN_CS &= ~GLCD_CS_BOTH_MASK;
	if(x==0)
		GLCD_PIN_CS |= GLCD_CS_A_MASK;
	else
		GLCD_PIN_CS |= GLCD_CS_B_MASK;
	GLCD_Delay1Us();
}

/*=====================================================
*Select one chip 0:Left Chip, 1:Right Chip
=====================================================*/
#define GLCD_ChipSelectBoth()	\
			GLCD_PIN_CS |= GLCD_CS_BOTH_MASK;	\
			GLCD_Delay1Us();

/*=====================================================
*Set page 0-8 , y position is divided in to 8 sections
=====================================================*/
#define GLCD_SetPage(page)	\
			GLCD_line = page;	\
			GLCD_WriteCommand(0b10111000 + page)

/*=====================================================
* Set x position
=====================================================*/
#define GLCD_SetAddress(address)	\
			GLCD_WriteCommand(0b01000000 + address)


/*=====================================================
*Check if the LCD is busy
=====================================================*/
unsigned char GLCD_GetBusyFlag(void)
{
	unsigned char cBusy = GLCD_NOT_BUSY;
	GLCD_PIN_RS = GLCD_CMD;
	GLCD_PIN_RW = GLCD_READ;

	//Make port input
	GLCD_PIN_DATA_TRIS = 0xFF;
	GLCD_Strobe();
	GLCD_Strobe();
	//Read state
	cBusy = (GLCD_PIN_DATA >= 0x80) ? GLCD_BUSY: GLCD_NOT_BUSY;
	//Make port output
	GLCD_PIN_DATA_TRIS = 0x00;

	//Restore to write mode
	GLCD_PIN_RW = GLCD_WRITE;

	return cBusy;
}

/*=====================================================
*Read data from LCD
=====================================================*/
unsigned char GLCD_ReadData(void)
{
	unsigned char cData;
	
	GLCD_PIN_RW = GLCD_READ;
	GLCD_PIN_RS = GLCD_DATA;
	//Make port input
	GLCD_PIN_DATA_TRIS = 0xFF;
	GLCD_Strobe();
	GLCD_Strobe();
	//Read data
	cData = GLCD_PIN_DATA;
	//Restore port output
	GLCD_PIN_DATA_TRIS = 0x00;
	GLCD_PIN_RW = GLCD_WRITE;

	//Count position
	GLCD_pos++;

	//Select right chip
	if(GLCD_pos == GLCD_HALF_WIDTH)
	{
		GLCD_ChipSelect(1);
		GLCD_SetPos(GLCD_line, GLCD_HALF_WIDTH);
	}

#ifdef GLCD_INVERT
	return ~cData;
#else
	return cData;
#endif

}

/*=====================================================
*Write command to LCD
=====================================================*/

void GLCD_WriteCommand(unsigned char cCmd)
{
	GLCD_PIN_RS = GLCD_CMD;
	GLCD_PIN_DATA = cCmd;
	GLCD_Strobe();
}





/****************************************************************
* PUBLIC FUNCTIONS, LOW LEVEL
****************************************************************/

/*=====================================================
*Init the LCD
=====================================================*/
void GLCD_Init(void)
{
	int x,y;
	//UN-RESET
	GLCD_PIN_RST = GLCD_NOT_RST;
	GLCD_Delay1Us();GLCD_Delay1Us();
	GLCD_Delay1Us();GLCD_Delay1Us();

	GLCD_SetDisplayOn(GLCD_DISPLAY_ON);
	GLCD_SetStartLine(0);
	
	//Clear the screen
	GLCD_Clear();

}

void GLCD_SetFontSize(unsigned char sizeIndex)
{
	
}

/*=====================================================
*Clear the LCD
=====================================================*/
void GLCD_Clear(void)
{
	unsigned char x,y;
	for(y=0; y<8; y++)
	{
		GLCD_SetPos(y, 0);
		for(x=0; x<128; x++)
			GLCD_WriteData(0x00);
	}	
}

/*=====================================================
*Turn on/off the LCD
=====================================================*/
void GLCD_SetDisplayOn(unsigned char onOff)
{
	GLCD_ChipSelectBoth();
	GLCD_WriteCommand(0b00111110 + onOff);
}

/*=====================================================
*Set LCD startline
=====================================================*/
void GLCD_SetStartLine(unsigned char startLine)
{
	GLCD_ChipSelectBoth();
	GLCD_WriteCommand(0b11000000 + startLine);
}

/*=====================================================
*Set the position
=====================================================*/
void GLCD_SetPos(unsigned char line, unsigned char pos)
{
	GLCD_pos = pos;
	if(pos < GLCD_HALF_WIDTH)
	{
		GLCD_ChipSelect(0);
		GLCD_SetAddress(pos);
		GLCD_SetPage(line);
	}
	else
	{
		GLCD_ChipSelect(1);
		GLCD_SetAddress(pos - GLCD_HALF_WIDTH);
		GLCD_SetPage(line);
	}
}

/*=====================================================
*Write data to LCD
=====================================================*/
void GLCD_WriteData(unsigned char cData)
{
	//Send data
	GLCD_PIN_RS = GLCD_DATA;
#ifdef GLCD_INVERT
	GLCD_PIN_DATA = ~cData;
#else
	GLCD_PIN_DATA = cData;
#endif
	GLCD_Strobe();

	//Count position
	GLCD_pos++;

	//Select right chip
	if(GLCD_pos == GLCD_HALF_WIDTH)
	{
		GLCD_ChipSelect(1);
		GLCD_SetPos(GLCD_line, GLCD_HALF_WIDTH);
	}
}



/****************************************************************
* PUBLIC FUNCTIONS, HIGH LEVEL
****************************************************************/

/*=====================================================
*Combine data into LCD (OR the data)
=====================================================*/
void GLCD_WriteCombineData(unsigned char cData)
{
	unsigned char line, pos, data;
	line = GLCD_line;
	pos  = GLCD_pos;
	
	data = GLCD_ReadData();
	
	GLCD_SetPos(line, pos);
	GLCD_WriteData(data | cData);
}

/*=====================================================
*Write a character on the current position
=====================================================*/
void GLCD_WriteChar(unsigned char ch)
{
	unsigned char i;
	const unsigned char * chTmp;
	chTmp = GLCD_GetCharData(ch);
	
	for(i=0; i<GLCD_FONT_WIDTH-1; i++)
		GLCD_WriteData(chTmp[i]);
	//ADD SPACE
	GLCD_WriteData(0x00);
}

/*=====================================================
*Write a character on the current position
=====================================================*/
void GLCD_WritePackedChar(unsigned char ch)
{
	unsigned char i;
	const unsigned char * chTmp;
	chTmp = GLCD_GetCharData(ch);
	
	for(i=0; i<GLCD_FONT_WIDTH-1; i++)
		GLCD_WriteData(chTmp[i]);

}


/*=====================================================
*Write a string to the specified line and position
=====================================================*/
void GLCD_WriteString(const unsigned char *strPtr, unsigned char line, unsigned char startPos)
{	
	//Set position
	GLCD_SetPos(line, startPos);

	//Write characters
	while(strPtr[0])
	{		
		GLCD_WriteChar(strPtr[0]);
		strPtr++;
	}
}

/*=====================================================
* Write a string with alignment
=====================================================*/
void GLCD_WriteAlignString(const unsigned char *strPtr, unsigned char line, unsigned char align)
{
	const char *charPtr;
	unsigned char startIndex;
	unsigned char cnt = 0;
	unsigned char i;
	unsigned char endIndex;
	
	charPtr = strPtr;

	//Count string length
	while(*(charPtr+cnt))
	{
		cnt++;
	}

	//Calculate start position
	switch(align)
	{
		case GLCD_ALIGN_LEFT:
			startIndex = 0;
			break;
		case GLCD_ALIGN_RIGHT:
			startIndex = GLCD_NO_OF_CHARS - cnt;
			break;
		case GLCD_ALIGN_CENTER:
			startIndex = (GLCD_NO_OF_CHARS - cnt) >> 1;
			break;
		default:
			break;
	}
	
	//CLEAR Rightest char
	endIndex = startIndex + cnt;
	
	//DISPLAY
	GLCD_SetPos(line, 0);
	
	for(i=0; i<startIndex; i++)
		GLCD_WriteChar(' ');
	GLCD_WriteString(charPtr, line, GLCD_GetCharPos(startIndex));
	for(i=endIndex; i<GLCD_NO_OF_CHARS; i++)
		GLCD_WriteChar(' ');
	
}


/****************************************************************
* PUBLIC FUNCTIONS, SPECIAL FUNCTION
****************************************************************/

/*=====================================================
* Write a unsigned char in 3 digit decimal representation, on the current position
=====================================================*/
void GLCD_WriteDec2(unsigned char num)
{
	unsigned int numTmp;
	
	numTmp = num;
	numTmp = numTmp % 100 / 10; 
	GLCD_WriteChar(numTmp + 0x30);
	
	numTmp = num;
	numTmp = numTmp % 10;
	GLCD_WriteChar(numTmp + 0x30);	
}

/*=====================================================
* Write a unsigned char in 3 digit decimal representation, on the current position
=====================================================*/
void GLCD_WriteDec3(unsigned int num)
{
	unsigned int numTmp;
	
	numTmp = num / 100;
	GLCD_WriteChar(numTmp + 0x30);
	
	numTmp = num % 100 / 10; 
	GLCD_WriteChar(numTmp + 0x30);
	
	numTmp = num % 10;
	GLCD_WriteChar(numTmp + 0x30);	
}

/*=====================================================
* Write a 4-bits unsigned char in 1 digit hex representation, on the current position
=====================================================*/
void GLCD_Write4Bits(unsigned char num)
{
	
 	if (num <= 9)
		GLCD_WriteChar(num + 0x30);
	else
	{
		switch(num)
		{	
			case 10: GLCD_WriteChar('A');	break;
			case 11: GLCD_WriteChar('B');	break;
			case 12: GLCD_WriteChar('C');	break; 	
			case 13: GLCD_WriteChar('D');	break;
			case 14: GLCD_WriteChar('E');	break; 	
			case 15: GLCD_WriteChar('F');	break;
			default: GLCD_WriteChar('?');	break;
		}
	}
}

/*=====================================================
* Write a 8-bits unsigned char in 2 digit hex representation, on the current position
=====================================================*/
void GLCD_WriteHex(unsigned char num)
{
	unsigned char cc;

	cc = (num &  0xF0) >> 4;
	GLCD_Write4Bits(cc);
	cc = (num & 0x0F);
	GLCD_Write4Bits(cc);

}

/*=====================================================
* Write a dot on the x,y position
=====================================================*/
void GLCD_WriteDot(unsigned char x, unsigned char y)
{
	unsigned char page;
	page = y / 8;
	y = y % 8; 
	GLCD_SetPos(page, x);
	GLCD_WriteData(0x01<<y);
}

/*=====================================================
* Write a dot on the x,y position (combine)
=====================================================*/
void GLCD_WriteDotCombine(unsigned char x, unsigned char y)
{
	unsigned char page;
	unsigned char data;
	page = y / 8;
	y = y % 8; 
	GLCD_SetPos(page, x);
	data = GLCD_ReadData();
	GLCD_SetPos(page, x);
	GLCD_WriteData(data | (0x01<<y));
}

⌨️ 快捷键说明

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