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

📄 glcddriver.c

📁 128*64 GLCD Driver for pic 18
💻 C
📖 第 1 页 / 共 2 页
字号:
		TRISBbits.TRISB6=1;	
		TRISBbits.TRISB7=1;
   		GLCD_RW = 1;		//setting for read dispaly data 
    	GLCD_RS = 1;
		GLCD_E = 0;
		Delay();
    	GLCD_E = 1;		//enable cycle to read data
		Delay();
		tempdata = PORTB;				//read input data
		tempdata = PORTB;				//read input data
		tempdata = PORTB;				//read input data
		temp=(unsigned char*)&tempdata;
   		GLCD_E = 0;
		data=*temp;
  		return (data);					// return read data
	}
//----------------------------------------------------------------------------------------------
//Name	:			glcd_init(int1 mode)	
// Purpose:       	Initialize the LCD.
//                	Call before using any other LCD function.
// Inputs:        	OFF - Turns the LCD off
//                	ON  - Turns the LCD on
// Date:          	8/12/2006  
//----------------------------------------------------------------------------------------------


	void glcd_init(unsigned char mode)
	{
	
						 			// Initialze pins
		TRISAbits.TRISA0=0;
 		TRISAbits.TRISA1=0;
 		TRISAbits.TRISA2=0;
 		TRISAbits.TRISA3=0;
 		TRISAbits.TRISA5=0;
 		TRISCbits.TRISC6=0;
		DDRAbits.RA0=0;
		DDRAbits.RA1=0;
		DDRAbits.RA2=0;
		DDRAbits.RA3=0;
		DDRAbits.RA5=0;

		LATA=0xFF;
		PORTA=0xFF;
		TRISBbits.TRISB0=0;	
		TRISBbits.TRISB1=0;	
		TRISBbits.TRISB2=0;	
		TRISBbits.TRISB3=0;	
		TRISBbits.TRISB4=0;	
		TRISBbits.TRISB5=0;	
		TRISBbits.TRISB6=0;	
		TRISBbits.TRISB7=0;				//port d as output
    	GLCD_RS = 1;
    	GLCD_RW = 1;
    	GLCD_E  = 1;
    	GLCD_CS1= 1;
    	GLCD_CS2= 1;
    	GLCD_RST= 1;
		GLCD_RST=1;  
 		GLCD_E  =0;
   		GLCD_CS1=1;
   		GLCD_CS2=0;
  		GLCD_RS=0;                 	// Set for instruction
		GLCD_RW=0; 

		GLCD_RST = 0;				//reset dispaly
    	GLCD_RST =1;			
    	GLCD_CS1 = 1;				//select left side dispaly
  		glcd_WaitIfReset();

		glcd_WriteCommand(GLCD_LEFT,  0xC0);		// setting start line left
		glcd_WriteCommand(GLCD_RIGHT, 0xC0);		// setting start line right	
		glcd_WriteCommand(GLCD_LEFT,  0x40);		//set x address left
		glcd_WriteCommand(GLCD_RIGHT, 0x40);		// setting x address right 
		glcd_WriteCommand(GLCD_LEFT,  0xB8);		// setting y address left 
		glcd_WriteCommand(GLCD_RIGHT, 0xB8);		// setting y address right
  		if(mode)
   		{
			glcd_WriteCommand(GLCD_LEFT,  0x3F); // Turn the display on
			glcd_WriteCommand(GLCD_RIGHT, 0x3F);
   		}
  		else
   		{
     		glcd_WriteCommand(GLCD_LEFT,  0x3E); // Turn the display off
      		glcd_WriteCommand(GLCD_RIGHT, 0x3E);
   		}

 		glcd_fillScreen(OFF);               		 // Clear the display

	}

//----------------------------------------------------------------------------------------------
// Name :		glcd_pixel(int8 x, int8 y, int1 color)	
// Purpose:    Turn a pixel on a graphic LCD on or off
// Inputs:     1) x - the x coordinate of the pixel
//             2) y - the y coordinate of the pixel
//             3) color - ON or OFF
// Date :		1/1/2007
//----------------------------------------------------------------------------------------------
	
	void glcd_pixel(unsigned char x, unsigned char y, unsigned char color)
	{
 		char data=0;
   		char side = GLCD_LEFT;  			// Stores which chip to use on the LCD
   		if(x > 63)              			// Check for first or second display area
  		{
      		x = x-64;
      		side = GLCD_RIGHT;
   		}

	 	BitClear(&x,7);									// Clear the MSB. Part of the instruction code
	 	BitSet(&x,6);									// Set bit 6.  part of the instruction code
		glcd_WriteCommand(side, x);                     // Set the horizontal address
		glcd_WriteCommand(side, ((y/8) & 0xBF) | 0xB8); // Set the vertical page address
 		data = glcd_ReadData(side);                     // Need two reads to get data
 		data = glcd_ReadData(side);                  	//  at new address
   		if(color )
      	{
			BitSet(&data,y%8);	// Turn the pixel on
      	}
   		else                          			// or
      	{
			BitClear(&data,y%8);	// turn the pixel off
    		
		}
		glcd_WriteCommand(side, x);      	// Set the horizontal address
		glcd_WriteData(side, data);   		// Write the pixel data

	}
//----------------------------------------------------------------------------------------------
// Name:		glcd_fillScreen(int1 color)
// Purpose:    	Fill the LCD screen with the passed in color
// Inputs:     	ON  - turn all the pixels on
//            	OFF - turn all the pixels off
// Date :		1/1/2007
//----------------------------------------------------------------------------------------------

	
	void glcd_fillScreen(unsigned char color)
	{
   		unsigned char i, j;
  									// Loop through the vertical pages
  		for(i = 0; i < 8; ++i)
   		{
      		glcd_WriteCommand(GLCD_LEFT, 0x40);    // Set horizontal address to 0
      		glcd_WriteCommand(GLCD_RIGHT, 0x40);
      		glcd_WriteCommand(GLCD_LEFT, i | 0xB8);// Set page address
      		glcd_WriteCommand(GLCD_RIGHT, i | 0xB8);
													 // Loop through the horizontal sections
      		for(j = 0; j < 64; ++j)
      		{
         		glcd_WriteData(GLCD_LEFT, 0xFF*color);  // Turn pixels on or off
         		glcd_WriteData(GLCD_RIGHT, 0xFF*color); // Turn pixels on or off
      		}
   		}
	}

//------------------------------------------------------------------------------------
// Name:		BlinkCurser(int x,int y)
// Purpose:    	Blink the cursor at the given x y location
// Inputs:     	x  - x position on the GLCD
//             	y - y position on the GLCD
// Date :		1/1/2007
//------------------------------------------------------------------------------------

unsigned char col=0;

	void BlinkCurser(unsigned int x,unsigned int y)
	{
		char i,j;
		GoTo_XY(x,y,col);
		col=~col;      //  complement the colour flag
		for(i=x;i<x+7;i++)    //show 1 variable position with defined colour
		{
		 	for(j=y;j<y+9;j++)
			{
				glcd_pixel(i,j,~col);  //enable or disable 9x7 area
			}
		}
	}

//------------------------------------------------------------------------------------
// Name :   	GoTo_XY(int x,int y,int1 colour1) 
// Purpose: 	Set the character address possition to the given X,Y(Before PrintDataOnLCD)
// Inputs:     	x  - x position on the GLCD
//             	y - y position on the GLCD
//			  	colour1 - background colour 
// Date :		1/1/2007
//------------------------------------------------------------------------------------

	
	void GoTo_XY(unsigned int x,unsigned int y,unsigned char colour1)
	{
		current_position_X=x;       //load x to current x possition
		current_position_Y=y;		//load y to current y possition
		colour=colour1;				//colour to colour 1
	}


//----------------------------------------------------------------------------------------------------
// Name :   	GoTo_XY(int x,int y,int1 colour1) 
// Purpose: 	Print characters in graphical mode
// Inputs:     	Data - character data to dispaly 
// Date :		1/1/2007
//----------------------------------------------------------------------------------------------------

	
	void PrintCharOnLCD(char data)
	{
		unsigned int x_pos=0,bit_pos=0,y_pos=0; 
		if(data<0x20) {return;}                     // if character less than space
		if(data>0x7f) {return;}						//   if character greater than z
		charsel=data;	

		if((current_position_X+7)>127)              // if end of line in LCD
		{
			{
				current_position_Y+=9;				// next line
				current_position_X=0;				// 
			}
		}
		if(current_position_Y>54)							//
		{
			current_position_Y=0;
			glcd_WriteCommand(GLCD_LEFT,  0x41);		//set x address left
			glcd_WriteCommand(GLCD_RIGHT, 0x41);		// setting x address right 
		}
			charpos=data-0x20;					//to select the array line			
			charpos=charpos * 0x07;				//to select the starting position
			for(x_pos=current_position_X;x_pos<(current_position_X+7);x_pos++)
			{
		 		for(bit_pos=0,y_pos=current_position_Y;bit_pos<9;y_pos++,bit_pos++)
				{
					if(bit_pos<8)
					{
						if(!colour){glcd_pixel(x_pos,y_pos, BitTest(TABLE[charpos],(bit_pos-1)));}		// if the background is white
						else{glcd_pixel(x_pos,y_pos, !BitTest(TABLE[charpos],(bit_pos-1)));}   //   if background is black
					}
					else{glcd_pixel(x_pos,y_pos, colour);}					//fill the 9th line with the colour
				}
				charpos++;                                    //incriment the character position
			}
		current_position_X+=7;								  //incriment the character X position
	}



//-------------------------------------------------------------------------------
// Name :   	PrintString()
// Purpose: 	to OFF the GLCD display
// Inputs:     	none
// Date :		1/1/2007
//-------------------------------------------------------------------------------
void PrintStringOnLCD(char *Data)
{
	unsigned int i;
	for(i=0;*Data!='\0';i++)
	{
		if(*Data=='\n')
		{
			if(current_position_Y==54)
			{
				current_position_Y=0;
			}
			else
			current_position_Y+=9;				// next line
			current_position_X=0;
			Data++;
		}
		else
		{
			if(current_position_Y==54&&current_position_X==126)
			{
				current_position_Y=0;
				current_position_X=0;
			}
			else
			{
				PrintCharOnLCD(*Data);
				Data++;
			}
		}
	}
}



//-------------------------------------------------------------------------------
// Name :   	DisplayOff()
// Purpose: 	to OFF the GLCD display
// Inputs:     	none
// Date :		1/1/2007
//-------------------------------------------------------------------------------



		void DisplayOff()
		{
			glcd_WriteCommand(1,0x3e);
			glcd_WriteCommand(0,0x3e);
		}
		
		
		
//-------------------------------------------------------------------------------
// Name :   	DisplayOn()
// Purpose: 	to ON the GLCD display
// Inputs:     	none
// Date :		1/1/2007
//-------------------------------------------------------------------------------


 	void DisplayOn()
	{
		glcd_WriteCommand(1,0x3f);
		glcd_WriteCommand(0,0x3f);
	}
	
	
	
	
	//---------------------------------------------------------------------------

//---------------------------------------------------------------------------


	void PlotOutSideSquare()
	{
		int x,y,i;
		for(i=7;i<121;i++)
		{
			glcd_pixel(i,9,1);
			glcd_pixel(i,55,1);
			glcd_pixel(i,8,1);
			glcd_pixel(i,56,1);
		}
		for(i=9;i<56;i++)
		{
			glcd_pixel(7,i,1);
			glcd_pixel(121,i,1);
			glcd_pixel(6,i,1);
			glcd_pixel(122,i,1);
		}
	}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

void BitSet(unsigned char *Data,unsigned char bitpos)
{
	*Data |= (1<<bitpos);
}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

void BitClear(unsigned char *Data,unsigned int bitpos)
{
	*Data &= ~(1<<bitpos);
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

unsigned char BitTest(unsigned char Data,unsigned int bitpos)
{
	if(((Data & (1<<bitpos)) != 0))
	return 1;
	else
	return 0;
}

//-----------------------------------------------------------------------------
// Name :   	ShowTitle()
// Purpose: 	welcome screen display
// Inputs:     	none
// Date :		1/1/2007
//-----------------------------------------------------------------------------


	void ShowTitle()
	{
		static int it,pos=0,xt=0,yt=0,j;
		DisplayOff();
		for(it=0;it<1024;it++)
		{
			for(j=0;j<8;j++)
			{		
				glcd_pixel(xt,yt, !BitTest(Welcome[it],j));     //dispaly the welcome screen
				yt++;
			}
			xt++;
			yt=pos;
			if(xt==128)
			{
				pos=pos+8;
				xt=0;
			}
		}
 	 DisplayOn();
	}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void Delay()
{
//	Delay10TCYx(100);
	Delay1TCY();
	Delay1TCY();
	Delay1TCY();
	Delay1TCY();
	Delay1TCY();
}

⌨️ 快捷键说明

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