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

📄 adc_9883.c

📁 此程序为twell8806驱动程序
💻 C
字号:
#include "Config.h"

#if defined( SUPPORT_PC ) || defined ( SUPPORT_DTV )

#include "reg.h"
#include "typedefs.h"
#include "i2c.h"
#include "adc.h"
#include "printf.h"
#include "main.h"
#include "tw88.h"
#include "pc_eep.h"
#include "measure.h"

#define PLLDIVM		1
#define PLLDIVL		2
#define PHASE		4

//=============================================================================
//                             DVI Functions
//=============================================================================
#ifdef SUPPORT_DVI
BYTE IsDVIOn(void)
{
	#ifdef DEBUG_PC
	//Printf("\n Detect DVI: %d(SCDT)",(WORD)P0_6);
	#endif
	return (BYTE)P0_6;
}
#endif

void ADCPowerDown(bit flag)
{
	BYTE val;

	val = ReadADC(0x0f);
	if( flag ) WriteADC(0x0f, val | 0x02);
	else       WriteADC(0x0f, val & 0xfd);

	#ifdef DEBUG
	dPrintf("\r\n---- ADC Power = %bd", (BYTE)flag);
	#endif
}

void SelectADCmux(BYTE sel)
{
	if( sel==PC ) P0_7 = 0;		// Select PC
	else  P0_7 = 1;		// Select DTV
}
//=============================================================================
//                             ADC Functions
//=============================================================================
CODE BYTE DTV_YPbPr_AD9883_init[] = 
{
	ADCI2CAddress, 15-6,

	0x05, 0x40,	// clamp place
	0x06, 0x40,	// clamp duration
	0x07, 0x88,	// Hsync pulse width

	0x0e, 0x5b, //0x49);	// Sync control
	0x0f, 0x4e,	// External Coast
	0x10, 0xa5,	// SOG Threshold
	0x11, 0x20,	// Sync Separator Threshold
	0x12, 0x20,	// Pre-Coast
	0x13, 0x20,	// Post-Coast
	0xff, 0xff
};

CODE BYTE RGB_AD9883_init[] = 
{
	ADCI2CAddress, 12-6+1,

	0x0f, 0x6e,	// Sync Coast			/// initial trigger ???

	0x05, 0x0a,	//0x08,	// clamp place	/// HHY 1.61
	0x06, 0x10,	// clamp duration
	0x07, 0x31,	// Hsync pulse width

	0x0e, 0x40,	// Sync control
	0x0f, 0x4e,	// Sync Coast			/// HHY 1.61 
	0x10, 0xb8,	// SOG Threshold
	0xff, 0xff
};

void SetADCMode(BYTE mode)
{
	#ifdef DEBUG_PC
	dPuts("\r\n SetADCMode");
	#endif

	if( mode==DTV ) {	// DTV-YPbPr
		#ifdef DEBUG_PC
		dPuts(" -- AD9883 for YPbPr");
		#endif
		I2CDeviceInitialize( DTV_YPbPr_AD9883_init );
	}
	else {			// RGB
		#ifdef DEBUG_PC
		dPuts(" -- AD9883 for RGB");
		#endif
		I2CDeviceInitialize( RGB_AD9883_init );
	}

	///WriteADC(0x15, 0x00);	// Set AD9888(15) - Mode
}

WORD GetCoarse(void)
{
	WORD buf;

	buf = ReadADC(PLLDIVM) << 4;
	buf |= (ReadADC(PLLDIVL) >> 4 );

	return buf;
}

void SetCoarse(WORD i)
{
	#ifdef DEBUG_PC
	dPrintf("\r\nSet Coarse->%04x(%d) ", i, i);
	#endif

	WriteADC(PLLDIVM, (BYTE)(i >> 4));
	WriteADC(PLLDIVL, (BYTE)(i << 4));
}

void SetPhase(BYTE j)
{
	#ifdef DEBUG_PC
	dPrintf("\r\nSet Phase->%04x ", (WORD)j);
	#endif

	WriteADC(PHASE, j);
}

BYTE GetPhaseCurrent(void)
{
	return ReadADC(PHASE) & 0xf8;
}


BYTE SetVCORange(DWORD _IPF)
{
	BYTE VCO_CURR, oldv, chged=0;
	BYTE val;
	
	val = _IPF / 1000000L;
	
	if     ( val < 15 )		VCO_CURR = 0x01;	// 00 001
	else if( val < 34 )		VCO_CURR = 0x06;	// 00 110
	else if( val < 45 )		VCO_CURR = 0x0c;	// 01 100
	else if( val < 63 )		VCO_CURR = 0x0d;	// 01 101
	else if( val < 70 )		VCO_CURR = 0x0e;	// 10 101
	else if( val < 80 )		VCO_CURR = 0x14;	// 10 100
	else if( val <100 )		VCO_CURR = 0x16;	// 10 101
	else if( val <110 )		VCO_CURR = 0x16;	// 10 110
	else					VCO_CURR = 0x1d;	// 11 110
	
	oldv = ReadADC(0x03) & 0xf8;	// curr VCO_CURR value  
	val = VCO_CURR << 3;
	if( oldv != val ) {
		chged = 1;
		WriteADC(0x03, val);		//
		delay(1);					// time to stabilize
	}

	#ifdef DEBUG_PC
	dPrintf("\r\nSetVCO=%02x, changed=%d", (WORD)val, (WORD)chged );
	#endif

	return chged;
}

#ifdef SUPPORT_DTV
///////////////////////////////////////////////////////////////////////////////
//
//			Auto Color Adjust for DTV	Rev 2.02
//
///////////////////////////////////////////////////////////////////////////////

//===== 75% color bar =====
#define BLACK_LEVEL		0
#define WHITE_LEVEL		164
#define RED_LEVEL		212
#define BLUE_LEVEL		212


/*
//===== 100% color bar =====
#define BLACK_LEVEL		16
#define WHITE_LEVEL		235//164
#define RED_LEVEL		128//240//212
#define BLUE_LEVEL		128//240//212
*/

struct {
	WORD h;	BYTE cnt, idx, aidx, level;
} 
CODE doffgain[6] = 
{
	{  70, 4, 0x59, 0x0c, BLACK_LEVEL },	// offset Y
	{  70, 4, 0x58, 0x0d, 128         },	// offset Pb
	{  70, 4, 0x57, 0x0b, 128         },	// offset Pr
	{ 130, 4, 0x59, 0x09, WHITE_LEVEL },	// gain   Y
	{ 740, 4, 0x58, 0x0a, BLUE_LEVEL  },	// gain   Pb
	{ 630, 4, 0x57, 0x08, RED_LEVEL   }		// gain   Pr
};

BYTE GetAverageValue(BYTE c)
{
	BYTE i, min, max;
	WORD h, val;
	
	val = 0;
	for(i=0; i<doffgain[c].cnt; i++) {
		h = doffgain[c].h + i*10;
		SetMeasureWindowH(h, h+10);
		MeasureAndWait(3);
		WriteTW88(0x5b, 0x30);				// read Min
		min = ReadTW88(doffgain[c].idx);
		WriteTW88(0x5b, 0x40);				// read Max
		max = ReadTW88(doffgain[c].idx);
		val = val + min + max;
	}
	val = (val+i) / (i*2);

	return val;
}


void AdjustOffsetForDTV(BYTE c)
{
	BYTE	level, offset;

	#ifdef DEBUG_PC
	dPrintf("\r\n==== DTV OFFSET color(%d) ====", (WORD)c);
	#endif

	for( offset=0x40; offset<0xa0; offset+=2 ) {
		WriteADC(doffgain[c].aidx, offset);
		level = GetAverageValue(c);

		#ifdef DEBUG_PC
		dPrintf("\r\n   Offset=%02x   level=%3d ", (WORD)offset, (WORD)level );
		//dPrintf("   57=%02bx  58=%02bx  59=%02bx", ReadTW88(0x57), ReadTW88(0x58), ReadTW88(0x59) );
		#endif

		if( level <= doffgain[c].level ) break;
		if( level > doffgain[c].level + 5 ) offset += 4;
	}
}

void AdjustGainForDTV(BYTE c)
{
	BYTE	level, gain;

	#ifdef DEBUG_PC
	dPrintf("\r\n==== DTV GAIN color(%d) ====", (WORD)c);
	#endif

	for( gain=0xa0; gain>0x60; gain-- ) {
		WriteADC(doffgain[c].aidx, gain);
		level = GetAverageValue(c);

		#ifdef DEBUG_PC
		dPrintf("\r\n   Gain=%02x   level=%3d ", (WORD)gain, (WORD)level );
		#endif

		if( level >= doffgain[c].level ) break;
		if( level < doffgain[c].level - 5 ) gain -= 4;
	}
}

void AutoColorAdjustForDTV(BYTE flag)
{
	BYTE i, buf;

	SaveADCGainOffsetForDTVEE(0);			// save default values
	GetADCGainOffsetForDTVEE();
	if( flag==0 ) return;
	
	buf = ReadTW88(0x44);
	WriteTW88(0x44, 0xed);			// change input mode to RGB to measure raw YPbPr data
	SetMeasureWindowV(100, 120);

//////////////////////////
/*
	for(i=0; i<100; i++) {
		SetMeasureWindowH(i*10, (i+1)*10);
		MeasureAndWait(3);
		WriteTW88(0x5b, 0x30);
		Printf("\r\n%2bd : %3bu %3bu %3bu", i, ReadTW88(0x57), ReadTW88(0x58), ReadTW88(0x59) );
	}
*/
//////////////////////////

	for(i=0; i<3; i++) AdjustOffsetForDTV(i);
	for(i=3; i<6; i++) AdjustGainForDTV(i);

	WriteTW88(0x44, buf);					// change input mode to RGB to measure raw YPbPr data

	SaveADCGainOffsetForDTVEE(1);			// save current values
}
#endif // SUPPORT_DTV

///////////////////////////////////////////////////////////////////////////////
//
//			Auto Color Adjust for PC	Rev 1.61
//
///////////////////////////////////////////////////////////////////////////////
#define MIN_RGB		0x02
#define MAX_RGB		0xfc

struct {
	BYTE idx, aidx, level;
} 
CODE poffgain[6] = 
{
	{ 0x59, 0x0b, MIN_RGB },	// offset R
	{ 0x58, 0x0c, MIN_RGB },	// offset G
	{ 0x57, 0x0d, MIN_RGB },	// offset B
	{ 0x59, 0x08, MAX_RGB },	// gain   R
	{ 0x58, 0x09, MAX_RGB },	// gain   G
	{ 0x57, 0x0a, MAX_RGB }		// gain   B
};
/*
void SetADCGainOffset(BYTE gain, BYTE offset)
{
	BYTE i;

	#ifdef SUPPORT_DTV
	if( IsDTVInput() ) return;
	#endif

	for(i=0; i<3; i++) WriteADC(poffgain[i].aidx, offset);
	for(   ; i<6; i++) WriteADC(poffgain[i].aidx, gain);
}
*/
void AdjustOffset(BYTE c)
{
	BYTE	min, offset;

	#ifdef DEBUG_PC
	dPrintf("\r\n==== OFFSET color(%d) ====", (WORD)c);
	#endif

	for( offset=0x50; offset<0xa0; offset+=2 ) {
		WriteADC(poffgain[c].aidx, offset);

		MeasureAndWait(3);
		WriteTW88(0x5b, 0x30);
		min = ReadTW88(poffgain[c].idx);

		#ifdef DEBUG_PC
		dPrintf("\r\n   Offset=%02x   MIN=%3d ", (WORD)offset, (WORD)min );
		#endif

		if( min <= poffgain[c].level ) break;
		if( min > poffgain[c].level + 5 ) offset+=4;
	}
}

void AdjustGain(BYTE c)
{
	BYTE	max, gain;

	#ifdef DEBUG_PC
	dPrintf("\r\n==== GAIN color(%d) ====", (WORD)c);
	#endif

	for( gain=0xa0; gain>0x40; gain-- ) {
		WriteADC(poffgain[c].aidx, gain);

		MeasureAndWait(3);
		WriteTW88(0x5b, 0x40);
		max = ReadTW88(poffgain[c].idx);

		#ifdef DEBUG_PC
		dPrintf("\r\n   Gain  =%02x   MAX=%3d", (WORD)gain, (WORD)max );
		#endif

		if( max >= poffgain[c].level ) break;
		if( max < poffgain[c].level - 10 ) gain -= 4;
	}
}

void AutoColorAdjust(BYTE flag)
{
	#ifdef SUPPORT_DTV
	if( IsDTVInput() && flag ) return;
	#endif

	SaveADCGainOffsetEE(0);		// save default values
	GetADCGainOffsetEE();
	if( flag==0 ) return;		// just default setting

	SetMeasureWindowH( GetHactiveStart(), GetHactiveEnd() );
	SetMeasureWindowV( GetVactiveStart(), GetVactiveStart() + GetVactiveLen() );

	AdjustOffset(0);
	AdjustOffset(1);
	AdjustOffset(2);

	AdjustGain(3);
	AdjustGain(4);
	AdjustGain(5);

	SaveADCGainOffsetEE(1);		// save current values
}

#endif	// SUPPORT_PC


⌨️ 快捷键说明

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