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

📄 measure.c

📁 此程序为twell8806驱动程序
💻 C
📖 第 1 页 / 共 3 页
字号:
	WORD	Back, Front;

	//------- Display the Result ---------------------

	#ifdef DEBUG_PC0
	dPuts  ("\r\n-------------------------------------------");
	dPrintf("\r\n   Resolution    = %d x %d (%s)", PCMDATA[PcMode].HAN, PCMDATA[PcMode].VAN, PcModeStr[PcMode]);
	dPrintf("\r\n   IVF(Refresh)  = (%d)", IVF);
	dPrintf("\r\n   IHF           = %08lx (%ld)", IHF, IHF);
	dPrintf("\r\n   IHPN(PLLdiv)  = %04x(%d) %02x", GetCoarse(), GetCoarse(), (WORD)GetPhaseCurrent() );
	dPrintf("\r\n   VPN           = %04x(%d)", GetVPN(), GetVPN() );
	dPrintf("\r\n   PPF           = %ld / 2 = %ld", GetPPF(), GetPPF()/2 );
	dPrintf("\r\n   IPF           = %ld", IHF * GetCoarse() );
	#endif

	//----- calculate H Pos Range
	Back  = GetHactiveStart() - GetHSYNCPulse();
	Front = GetCoarse() - PCMDATA[PcMode].HAN - GetHactiveStart();

	#ifdef DEBUG_PC0
	dPrintf("\r\nH  ~~~|_%d_|~%d~", (WORD)GetHSYNCPulse(), (WORD)Back );
	dPrintf(":+++ %d +++:~%d~|_%d_|~~~ ",  (WORD)PCMDATA[PcMode].HAN, (WORD)Front, (WORD)GetHSYNCPulse() );
	#endif

	if( (Back>50) && (Front>50) ) {
		Back = 50;
		Front = 50;
	}
	else if( Back+Front>=100 ) {
		if( Back>Front )
			Back = 100 - Front;
		else if( Front>Back ) 
			Front = 100 - Back;
	}

	HPosCurr = (BYTE)Back;
	HPosMax  = (BYTE)(Back + Front);

	//----- calculate V Pos Range
	Back  = GetVactiveStart() - GetVSYNCPulse();
	Front = GetVPN() - PCMDATA[PcMode].VAN - GetVactiveStart();

	#ifdef DEBUG_PC0
	dPrintf("\r\nV  ~~~|_%d_|~%d~", (WORD)GetVSYNCPulse(), Back );
	dPrintf(":+++ %d +++:~%d~|_%d_|~~~ ",  PCMDATA[PcMode].VAN, Front, (WORD)GetVSYNCPulse() );
	dPuts  ("\r\n-------------------------------------------");
	#endif

	VPosCurr = Back-4;

	if( Front==0 )
		VPosMax = VPosCurr+1;
	else
		VPosMax = VPosCurr + Front;

}
/*===========================================================================*/
/*						  Real Measurement routine				             */
/*===========================================================================*/
BYTE DoMeasurement(void)
{
	WORD coarse;

	//------------------------ Clock Tuning -------------------------------

	coarse = AutoTuneClock();			// plldiv
	if( coarse == 0 ) {
		ePuts("\r\nToo big IPF");
		MeasureAndWait(3);
		return FALSE;
	}

	//-------------------------- Fine Tuning ------------------------------

	AutoTunePhase();
	
	//-------------------------- Get Active Region ------------------------
	if( !GetActiveRegion() ) {

		GetPCDataEE(PcMode);

		MeasureAndWait(3);
		return FALSE;
	}

	//-------------------------- Display Result ---------------------------
	DisplayResultAndSetActiveRange();

	return TRUE;
}

/////////////////////////////////////////////////////////////////////
//#ifdef SUPPORT_DTV
void RGBModeFieldDetect(BYTE flag)
{
	BYTE val;

	val = ReadTW88(0x42);
	if( flag ) val = val | 0x80;
	else       val = val & 0x7f;
	WriteTW88( 0x42, val);
}
//#endif


#include "data\DTV_PC.txt"

BYTE SetADCandInputRegisterByVGAMode(BYTE mode)
{
	//#ifdef SUPPORT_DTV

	CODE_P BYTE *reg=0;
	BYTE fielddetect=0, ret;

	SetCoarse( PCMDATA[mode].CLOCK );
//	ret = SetVCORange(IHF * PCMDATA[mode].CLOCK);	
	ret = SetVCORange(PCMDATA[mode].IPF * 100000L);	

	if( GetInputSelection()==DTV ) {
		switch (mode) {
		case EE_YPbPr_480I:		reg = DTV_YPbPr_480I_Reg;		break;
		case EE_YPbPr_576I:		reg = DTV_YPbPr_576I_Reg;		break;
		case EE_YPbPr_480P:		reg = DTV_YPbPr_480P_Reg;		break;
		case EE_YPbPr_576P:		reg = DTV_YPbPr_576P_Reg;		break;
		case EE_YPbPr_720P:		reg = DTV_YPbPr_720P_Reg;		break;
		case EE_YPbPr_720P50:	reg = DTV_YPbPr_720P50_Reg;		break;
		case EE_YPbPr_1080I:	reg = DTV_YPbPr_1080I_Reg;		break;
		case EE_YPbPr_1080I50A:	reg = DTV_YPbPr_1080I50A_Reg;	break;
		default:				break;
		}
	}
	else {
		switch (mode) {
		case EE_RGB_480I:		
		case EE_RGB_576I:		
		case EE_RGB_1080I:		
		case EE_RGB_1080I50A:
								reg = DTV_RGB_480I_Reg;	fielddetect = 1;	break;
		case EE_RGB_576P:
		case EE_RGB_480P:
								reg = DTV_RGB_480P_Reg; fielddetect = 0;	break;
		case EE_RGB_720P50:		
		case EE_RGB_720P:
								reg = DTV_RGB_720P_Reg; fielddetect = 0;	break;
		default:				reg = PC_RGB_AD9883_Reg; fielddetect = 0;	break;
		}
	}

	RGBModeFieldDetect(fielddetect);
	if( reg ) I2CDeviceInitialize( reg );

	return ret;

	/*#else // not SUPPORT_DTV

	BYTE ret;

	if(GetInputSelection()==PC) WriteADC(0xc1, 0x30);
	SetCoarse( PCMDATA[mode].CLOCK );
	if(GetInputSelection()==PC) WriteADC(0xc1, 0x20);

	ret = SetVCORange(IHF * PCMDATA[mode].CLOCK);	

	I2CDeviceInitialize( PC_RGB_AD9883_Reg );

	return ret;

	#endif // SUPPORT_DTV
*/

}
/*===========================================================================*/
/*                                                                           */
/*===========================================================================*/

BYTE DecideDTVInputSource(void)
{
	BYTE mode;

	mode = DetectDTVInputSource();
	return mode;

}
/*===========================================================================*/
/*                                                                           */
/*===========================================================================*/
BYTE DetectAndSetForVGAInput(BYTE eflag)		// 0: measure 	 Range4Coarse=0
												// 1: check EEP  Range4Coarse=0
												// 2: measure    Range4Coarse=1
												// 3: restore EEP
{
	bit VCORangeChged;
	BYTE  i;
	WORD vpn, vpn1, ivf;

	if( DebugLevel<3 && (eflag==1 || eflag==3) ) 	LCDPowerOFF();//PanelMute(1); 

	//---------- Set Panel constant reg.------------------

	WriteTW88( 0x5c, (ReadTW88(0x5c) & 0x0f) | 0x30 );	// Noise Mask: 3
	WriteTW88( 0x5d, (ReadTW88(0x5d) & 0x0f) | 0x30 );	// active detect threshold: 3

	AutoPHPCalEnable();
		//	Pause("9");

	WriteTW88( 0xbd, 8 );				// Vdelay=8
	WriteTW88( 0xb8, 6 );				// Vsync Pulse Width

//	WriteTW88( 0xb3, 0x10 );			// Hsync Pulse Width
//	WriteTW88( 0xb4, 0x10 );			// H Back Porch

	//------- Assume the PC_XGA input, 60Hz Refresh -----------------
	SetMeasureWindowH(1, 0x1ff);		// default window
	SetMeasureWindowV(1, 0x0ff);		//

	vpn = GetHpnVpn(1);					// VPN, IVF 
	ivf = IVF;

	//------- Estimate IPF and Set VCO Range (ADC) ---------------

	i=0;
	do {
		if( GetInputSelection()==PC ) 
			PcMode = DecideVGAInputSource(vpn, IVF);
		else 
			PcMode = DecideDTVInputSource();

		if( PcMode==EE_PC_UNKNOWN ) { 
			if( vpn<200 ) vpn = 250;	// HHY 1.62 if coarse value is too small, ADC is malfunctioned.
			VCORangeChged = SetVCORange( IHF * (vpn/3)*4 );	
			SetCoarse(vpn/3*4);						// HHY 1.43
		}
		else if( PcMode==EE_PC_NO_SIGNAL ) {
			#ifdef DEBUG_PC
			ePuts("\r\n-----> Sync Loss");
			#endif
			if( GetInputSelection()==PC ) goto Failure;
		}
		else {

			if( eflag==1 || eflag==3 ) {	// HHY 2.01
				VCORangeChged = SetADCandInputRegisterByVGAMode(PcMode);
			
				#ifdef DEBUG_PC
				ePrintf("\r\n   ---->>> Set Initial value by detected mode[%d]", (WORD)PcMode);
				#endif
			}
		}

		vpn1 = GetHpnVpn(1);						// VPN, IVF ***

		if( vpn1==0 ) {
			#ifdef DEBUG_PC
			dPrintf("\r\n   ---> Error at GetHpnVpn line:%d", __LINE__);
			#endif
			goto Failure;
		}

		// Check SYNC Loss	// HHY 1.47
		if( PcMode == EE_PC_UNKNOWN ) {
			StartNewDetect();
			if( (ReadTW88(0xd0) & 0x06) )  {
				PcMode = EE_PC_NO_SIGNAL;
				#ifdef DEBUG_PC
				ePuts("\r\n-----> Sync Loss");
				#endif
				if( GetInputSelection()==PC ) goto Failure;
			}
		}

		vpn = vpn1;
		ivf = IVF;

		i++;
	} while( ( (PcMode==EE_PC_UNKNOWN) || VCORangeChged) && i<3 );	// by getting valid VPN

	if( PcMode==EE_PC_UNKNOWN ) goto Failure;
	if( PcMode==EE_PC_NO_SIGNAL) goto Failure;

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

	ChangeInternPLL(PCMDATA[PcMode].PPF * 100000L);	// Set PPF (Internal PLL)

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

	#ifdef DEBUG_PC
	dPrintf("\r\n eflag = %d PcMode = %d(%s)", (WORD)eflag, (WORD)PcMode, PcModeStr[PcMode] );
	#endif

	//==================================
	Measure_VPN_IHF(&LastVPN, &LastIHF);

	ivf = (LastIHF + LastVPN/2) / LastVPN;

	// Check if PcMode same as before
	i = DecideVGAInputSource(LastVPN, ivf);
	#ifdef DEBUG_PC
	ePrintf("\r\n  --> Detect PcMode again:%d[%s]", (WORD)i, PcModeStr[i] );
	#endif
	if( PcMode != i )  {
		#ifdef DEBUG_PC
		Printf("  --- New PcMode=%d  i=%d", (WORD)PcMode, (WORD)i);
		#endif
		LastVPN = 0;
		LastIHF = 0;
		PcMode = EE_PC_UNKNOWN;
		return FALSE;
	}
	//==================================

	if( (eflag==3) || (eflag==1) ) {

		//==================
		GetPCDataEE(PcMode);

		goto Success;

	}
	else {
		switch( eflag ) {
		case 0: Range4Coarse = 0; break;
		case 1: Range4Coarse = 0; break;
		case 2: Range4Coarse = 1; break;
		}

//		SetADCGainOffset(0x80, 0xd0);
//Abnormal_Test:

		//--------------------
		i = DoMeasurement(); 			// *** Coarse, Fine, Active
		//--------------------
//		i = 1;

		if( i ) goto Success;
		else	goto Failure;
	}

Success:
	//==============================================

	if( (ReadTW88(0xd0) & 0x06) )  {
		#ifdef DEBUG_PC
		ePuts("\r\n---> Finish, but Sync Loss");
		#endif

		LastVPN = 0;
		LastIHF = 0;
		PcMode = EE_PC_NO_SIGNAL;
		return FALSE;
	}
	else {
		ClearPCInfo();
		if( eflag==0 || eflag==2 ) SavePCDataEE(PcMode);

		
		return TRUE;
	}

Failure:

	WriteDecoder( ACNTL, ReadDecoder(ACNTL) | 0x80 );		// SW reset, because sometimes unstable
	PcMode = EE_PC_UNKNOWN;

	if( (ReadTW88(0xd0) & 0x06) ) {
		PcMode = EE_PC_NO_SIGNAL;

		#ifdef DEBUG_PC
		ePuts("\r\n-----> Out Of Range, but Sync Loss");
		#endif

		LastVPN = 0;
		LastIHF = 0;
		return FALSE;
	}
	else {

		GetHpnVpn(0);

		if( !Measure_VPN_IHF(&LastVPN, &LastIHF) ) {
			LastVPN = 0;
			LastIHF = 0;

			return FALSE;
		}
		IVF = (LastIHF + LastVPN/2) / LastVPN;

		if( PcMode != DecideVGAInputSource(LastVPN, IVF) )  {
			LastVPN = 0;
			LastIHF = 0;
			PcMode = EE_PC_UNKNOWN;
			return FALSE;
		}

		DisplayPCInfo(OutOfRangeStr[GetOSDLang()]);

	}
	return FALSE;
}
/*****************************************************************************/
/*				  PC Function Main - called by main periodically             */
/*****************************************************************************/
void CheckPCinput(void)
{
	BYTE b0_val, /*b1_val,*/ ret;
	static bit inf=1;
	WORD  vpn=0;
	DWORD ihf=0;
	static BYTE cnt=0;
	CODE_P BYTE *Str;
	
	//===================== HSYNC/VSYNC loss ========================

	b0_val = ReadTW88(0xd0);
	//b1_val = ReadTW88(0xb1);

	if( b0_val & 0x06 ) {

		for(ret=0; ret<3; ret++) {
			#ifdef DEBUG_PC0
			ePrintf("\r\nSync Loss cnt=%d", (WORD)ret);
			#endif
			WriteTW88( 0xd0, 0xff);	// clear status
			WriteTW88( 0xd1, 0xff);	// clear status
			b0_val = ReadTW88(0xd0);
			//b1_val = ReadTW88(0xb1);
			if( b0_val & 0x06 ) 
				delay(1);
			else return;
		}

		//InputSource = NOINPUT;
		WriteTW88( 0xd0, 0xff);			// clear status
		WriteTW88( 0xd1, 0xff);			// clear status

		LastVPN = 0;
		LastIHF = 0;

		if( inf ) {						// ** First detect Sync loss
				//LCDPowerOFF();
			inf = 0;
			tic_pc = 0;
		}
		else if( tic_pc>=30 && tic_pc<=100 ) {		// ** After 1 sec

			//WriteTW88( 0xbe, 0x00 );	// Auto Cal. Off
			//WriteTW88(0xd5, 0x34);	// Manual Power OFF 

			Clear_bypass();
			//------------------
			PcMode = EE_PC_NO_SIGNAL;

			if( GetInputSelection()==PC )
			Str = CheckPCCableStr[GetOSDLang()];
			else
			Str = CheckDTVCableStr[GetOSDLang()];
			DisplayPCInfo(Str);

			tic_pc = 101;
			//------------------
			LCDPowerON(1);
		}
		else if( tic_pc>=500 && tic_pc<=600 ) {		// ** After 5.0 sec
			//LCDPowerOFF();
			//PanelMute(0);
			ClearPCInfo();
			DPMSmodeOFF();
			
			//PowerOff();
			
			tic_pc = 0xffff;
		}
		return;
	}
	else inf = 1;

	//============== Measurement VPN, IHF =====================================

	Measure_VPN_IHF(&vpn, &ihf);

	//============== In DTV mode, check  only VPN =============================

	if( IsDTVInput() ) {

		if( gap(LastVPN, vpn) > 5+10 ) {
			cnt++;
			if( cnt>=10 ) cnt = 0;
			else return;
		}
		else {
			cnt = 0;
			return;
		}
	}

	//============== In PC mode, check VPN, IVF ===============================

	else {
		if( (gap(LastVPN, vpn) > 5+10) || (gap(LastIHF, ihf) > 1000) ) 
		{
			#ifdef DEBUG_PC
			ePrintf("\r\n  ===> Input Changed (%d -> %d)", LastVPN, vpn);
			ePrintf("  (%ld -> %ld)", LastIHF, ihf);
			#endif
		}
		else return;
	}
	
	/*#else	// not SUPPORT_DTV

	if( (gap(LastVPN, vpn) > 5+10) || (gap(LastIHF, ihf) > 1000) ) 
	{
		#ifdef DEBUG_PC
		ePrintf("\r\n  ===> Input Changed (%d -> %d)", LastVPN, vpn);
		ePrintf("  (%ld -> %ld)", LastIHF, ihf);
		ePrintf(" cnt=%d", (WORD)cnt);
		#endif

		cnt++;
		if( cnt>=2 ) {
			#ifdef DEBUG_PC
			dPuts("\r\n Really Changed !!!");
			#endif
			cnt = 0;
		}
		else return;
	
	}
	else {
		cnt = 0;
		return;
	}
	
	#endif	// SUPPORT_DTV
	*/
	//============== New Measurement ==========================================
	
	//LCDPowerOFF();		// PanelMute(1);
	//delay(5);
	DPMSmodeON();
	
	tic_pc = 0;
	//-------------------------------
	ret = DetectAndSetForVGAInput(1);
	//-------------------------------
	WriteTW88( 0x06, 0x80 ); // Software Reset


	if( ret ) {
		ClearPCInfo();
		if( PcMode!=EE_DOS ) {
			LCDPowerON(0);
		}

		else {
			for(ret=0; ret<10; ret++) {
				Measure_VPN_IHF(&vpn, &ihf);
				if( (gap(LastVPN, vpn) > 5+10) || (gap(LastIHF, ihf) > 1000) ) 
				{
					#ifdef DEBUG_PC
					ePrintf("\r\n  ===> Input Changed (%d -> %d)", LastVPN, vpn);
					ePrintf("  (%ld -> %ld)", LastIHF, ihf);
					ePrintf(" cnt=%d", (WORD)cnt);
					#endif
					
					cnt = 0;
					return;
				}
			}
		}

		LCDPowerON(0);
	}
	else if( PcMode==EE_PC_UNKNOWN ) 
		LCDPowerON(1);		// HHY 7/26/05 Panel Mute to hide garbage display

	#ifdef DEBUG_PC
	Printf("\r\n--- Detect Mode:%d  Time: %d.%02d sec", (WORD)PcMode, (WORD)tic_pc/100, (WORD)tic_pc%100);
	Printf("  Power=%d",  GetLCDPowerState() );
	#endif

	if( PcMode != EE_PC_NO_SIGNAL ) {	// HHY 1.63 protect display garbage data in transient state
		
		SetLCDPowerState(0x34);		// Auto Power Set
	}

	StartNewDetect();

	InitOSDMenu();  // Standby OSD Menu

	if( PcMode<EE_PC_MAX ) DisplayInput();
}

CODE_P BYTE *GetPCInputSourceName(void)
{
	BYTE *ptr;

	if( PcMode<EE_PC_MAX ) ptr = PcModeStr[PcMode];
	else                   ptr = (CODE_P BYTE *)"";

	#ifdef DEBUG
	dPrintf("\r\n++(GetPCInputSourceName)=<%s>", ptr);
	#endif

	return ptr;
}

BYTE AutoAdjust(void)
{
	BYTE ret=0;

	ClearOSDInfo();
	DisplayAutoAdjust();
	ret = DetectAndSetForVGAInput(0);	
	ClearAutoAdjust();
	LCDPowerON(0);	//HHY 1.63

	return ret;
}						
#endif











⌨️ 快捷键说明

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