/*
*********************************************************************************************************
*                            Set digital contrast for TG322430FFEGB-T1 LCD module
*						(c) Copyright 2006, John Leung, eMed Technologies Ltd. Hong Kong
*											All Rights Reserved
*
* File name		: TGcontrast.c
* Programmer 	: John Leung, eMed Technologies Ltd. Hong Kong
* Web presence  : www.emed-technologies.com
*
* Remarks     	: Hardware specific, consult TG322430FFEGB-T1 data sheet
* Date		  	: 6th Jan 2006
* MCU		  	: Microchip 16F88 @ 4MHz internal osc, LCD module = TG322430FFEGB-T1 LCD module
*********************************************************************************************************
*/

#define TG_Contrast		185 					// good starting point 185
#define TG_VDIN			RA0
#define TG_VDIN_TRIS	TRISA0
#define TG_VCLK			RA7
#define TG_VCLK_TRIS	TRISA7
#define TG_VSTRB		RA6
#define TG_VSTRB_TRIS	TRISA6

/*
*********************************************************************************************************
*												Prototypes
*********************************************************************************************************
*/
void	TGSetContrast(unsigned char contrast);		// Set LCD contrast by arg. constrast


/*
*********************************************************************************************************
* Description : This function shifts contrast value to S-8330 electric volume generator
* Arguments   : (unsigned char) contrast
*               Example (acutal measurements) :	contrast = 180, Vout=20.45
*						 						contrast = 191, Vout=20.97
*												contrast = 200, Vout=21.40
* Returns     : None.
*********************************************************************************************************
*/
void TGSetContrast(unsigned char contrast)
{
	unsigned char i, data;

	TG_VDIN_TRIS	= 0;		//set VDIN, VCLK, and VSTRB pins all output
	TG_VCLK_TRIS	= 0;
	TG_VSTRB_TRIS	= 0;

	TG_VSTRB = 0;	TG_VCLK = 0;	TG_VDIN = 0;
	DelayUs(4);

	//reset S8330 electric volume
	TG_VSTRB = 1;	TG_VCLK	= 1;
	DelayUs(8);
	TG_VSTRB = 0;	TG_VCLK	= 0;
	DelayUs(8);
	
	// contrast data shift-in, MSB first

	for (i=0;i<8;i++)
		{
			data = contrast&0x80;
			TG_VDIN=(BOOLEAN)(data==0x80)? 1:0;
			asm("nop");
			TG_VCLK = 1;
			DelayUs(4);
			TG_VCLK = 0;
			contrast<<=1;
		}

	//Reset and hold the data shift-in
	TG_VSTRB = 1;
	DelayUs(8);
	TG_VSTRB = 0;

	//set both VDIN and VCLK pins low for easier power management
	TG_VDIN	 = 0;
	TG_VCLK  = 0;
}
