📄 lcd_auto.c
字号:
if (0x7b == ucDetect)
stMUD.CLOCK = ((unsigned long)0x38000 < ulMaxVal) ? count : stop;
else
stMUD.CLOCK = ((unsigned long)0x48000 < ulMaxVal) ? count : stop;
Set_Clock();
Set_H_Position();
Set_Clock();
}
#endif
#else
unsigned char Result;
unsigned char count, delta, stop,start;
unsigned long ulSum,ulCompare;
ulCompare = 0;
ulSum = 0;
///////////////////////////////
// Measure (V) Start & End //
///////////////////////////////
Result = Measure_PositionV(NM);
if (ERROR_SUCCEED != (Result & 0x80)) return Result;
NM = NM + 0x10; // See Min_Noise_Margin(). Horizontal Measure Result is the same when applying (NM + 0x10)
count = 10;
do
{
///////////////////////////////
// Measure (H) Start & End //
///////////////////////////////
Result = Measure_PositionH(NM);
if (ERROR_SUCCEED != (Result & 0x80)) return Result;
usH_End = usH_End + 1 - usH_Start;
// H_Active Delta
if (usH_End < usIPH_ACT_WID)
delta = (usIPH_ACT_WID - usH_End > 0x00ff) ? 0xff : (unsigned char)(usIPH_ACT_WID - usH_End);
else
delta = (usH_End - usIPH_ACT_WID > 0x00ff) ? 0xff : (unsigned char)(usH_End - usIPH_ACT_WID);
//if (0xc0 < delta) // The difference is too large to fine-tune.
if((usIPH_ACT_WID/3) < delta) //modified 2003/02/25
{
return (usH_End < usIPH_ACT_WID) ? ERROR_TOO_SMALL : ERROR_TOO_BIG;
}
if (1 >= delta) break; // 1023,1024,1025,1026,1027
delta = delta + (delta >> 2);//& 0xfe; // 4n number
#if(ALIGN_LEFT == CLOCK_ALIGN)
// Adjust Clock
if (usH_End < usIPH_ACT_WID) // delta < 0, Measure < Active
{
if ((178 - stMUD.CLOCK) < delta) return ERROR_TOO_SMALL;
stMUD.CLOCK += delta;
Set_Clock();
Set_H_Position();
}
else // delta >= 0, Measure >= Active
{
if ((stMUD.CLOCK - 78) < delta) return ERROR_TOO_BIG;
stMUD.CLOCK -= delta;
Set_H_Position();
Set_Clock();
}
#else
// Adjust Clock
if (usH_End < usIPH_ACT_WID) // delta < 0, Measure < Active
{
if ((228 - stMUD.CLOCK) < delta) return ERROR_TOO_SMALL;
stMUD.CLOCK += delta;
Set_Clock();
Set_H_Position();
}
else // delta >= 0, Measure >= Active
{
if ((stMUD.CLOCK - 28) < delta) return ERROR_TOO_BIG;
stMUD.CLOCK -= delta;
Set_H_Position();
Set_Clock();
}
#endif
}
while (--count);
if (0 == count) return ERROR_TIMEOUT;
stop = 0;
while (1)
{
count = 0x10; // Phase 4 ~ 28 step 4 (4,8,12,16,20,24,28)
delta = 0xff;
while (1)
{
Set_Phase(count);
// Measure usH_Start & usH_End
Result = Measure_PositionH(NM);
if (ERROR_SUCCEED != (Result & 0x80))
{
if (ERROR_NOTACTIVE == Result)
{
// Input pattern is black/white vertical lines.
if (0x70 == count)
{
Set_Phase(stMUD.PHASE); // Restore phase
break;
}
else
{
count += 0x20;
continue;
}
}
Set_Phase(stMUD.PHASE); // Restore phase
return Result;
}
usH_End = usH_End + 1 - usH_Start;
Result = (usH_End < usIPH_ACT_WID)
? 0x80 - (unsigned char)(usIPH_ACT_WID - usH_End)
: 0x80 + (unsigned char)(usH_End - usIPH_ACT_WID);
if (Result < delta)
{
delta = Result; // Save the smallest width
}
if (0x70 == count)
{
Set_Phase(stMUD.PHASE); // Restore phase
break;
}
count += 0x10;
}
if (0x81 < delta)
{
stMUD.CLOCK -= 1;
Set_H_Position();
Set_Clock();
stop = 1;
}
else if (0x80 > delta)
{
if (stop && (0x7f == delta)) break;
stMUD.CLOCK += 1;
Set_Clock();
Set_H_Position();
if (stop) break;
}
else
break;
}
count = stMUD.PHASE; // Record Current Phase
start = stMUD.CLOCK ;
if(FindColor() != ERROR_SUCCEED) return ERROR_ABORT;
// Set threshold
RTDSetByte(DIFF_THRED_7E, 0x30);
ulSum = GetMaxSum(1); //judge if pulse information large enough
ulCompare = GetMaxSum(0);
if((ulSum > 460000) || ((ulSum < 460000) && (ulCompare > 2000000)) )
{
ulCompare = 0;
// ulSum = 0;//GetMaxSum(0);
//////////////////////////////////////////////
if(0x80 < (start - 2) || 0x80 > start)
{
stMUD.CLOCK = 0x80;
Set_H_Position();
Set_Clock();
ulSum = GetMaxSum(0);
if(ulCompare < ulSum)
{
ulCompare = ulSum;
Result = stMUD.CLOCK;
}
stMUD.CLOCK = start + 1;
}
else
{
stMUD.CLOCK = start;
Set_H_Position();
Set_Clock();
ulSum = GetMaxSum(0);
}
////////////////////////////////////////////////
while(1)
{
if(ulCompare < ulSum)
{
ulCompare = ulSum;
Result = stMUD.CLOCK;
}
if(stMUD.CLOCK == start - 2)
break;
stMUD.CLOCK -= 1;
Set_Clock();
Set_H_Position();
ulSum = GetMaxSum(0);
}
stMUD.CLOCK = Result;
stMUD.PHASE = count;
Set_Clock();
Set_H_Position();
Set_Phase(stMUD.PHASE);
}
#endif
return (28 > stMUD.CLOCK) ? ERROR_TOO_BIG : (228 < stMUD.CLOCK) ? ERROR_TOO_SMALL : ERROR_SUCCEED;
}
//------------------------------------------------------------------//
// Auto Position //
//------------------------------------------------------------------//
unsigned char Auto_Position(void)
{
unsigned char Result, Curr_PosH, Curr_PosV;
bAutoInProgress = 1;
Curr_PosH = stMUD.H_POSITION; // Save current stMUD.H_POSITION
Curr_PosV = stMUD.V_POSITION; // Save current stMUD.V_POSITION
if (ucV_Max_Margin < stMUD.V_POSITION)
{
stMUD.V_POSITION = ucV_Max_Margin;
Set_V_Position();
}
RTDCodeW(ADC_DEFAULT);
///////////////////////////////
// Measure NOISE_MARGIN //
///////////////////////////////
Result = Min_Noise_Margin();
if (ERROR_SUCCEED == (Result & 0x80))
{
///////////////////////////////
// Adjust (H/V)Position //
///////////////////////////////
Result = Auto_Position_Do(Data[0]); // Noise margin returned by Min_Noise_Margin() is saved in Data[0];
}
if (ERROR_SUCCEED == (Result & 0x80))
{
Save_MUD(ucMode_Curr);
}
else
{
stMUD.H_POSITION = Curr_PosH;
stMUD.V_POSITION = Curr_PosV;
Set_H_Position();
Set_V_Position();
}
// Restore ADC Gain/Offset
SetADC_GainOffset();
bAutoInProgress = 0;
return Result;
}
//------------------------------------------------------------------//
// Return Message => ERROR_SUCCESS : Success //
// ERROR_SUCCESS_1 : Vertical Start > Max //
// ERROR_SUCCESS_2 : Vertical Start < Min //
// ERROR_SUCCESS_4 : Vertical Start/End Fail //
// ERROR_SUCCESS_8 : Horizontal Start > Max //
// ERROR_SUCCESS_16: Horizontal Start < Min //
// ERROR_SUCCESS_32: Horizontal Start/End Fail //
// ERROR_INPUT : 1. IVS or IHS changed //
// 2. underflow or overflow //
// ERROR_TIMEOUT : Measure Time_Out //
// ERROR_NOTACTIVE : No Avtive Image //
//------------------------------------------------------------------//
unsigned char Auto_Position_Do(unsigned char NM)
{
unsigned char Result;//,ucTemp,ucTemp1;
// unsigned int usTemp;
/*
///////////////////////////////////////////////////////////
stMUD.H_POSITION = 128;
if(128 >= stMUD.H_POSITION)
{
ucTemp = 128 - stMUD.H_POSITION;
if(stMUD.CLOCK > 128)
ucTemp1 = 128;
else
ucTemp1 = stMUD.CLOCK;
ucH_Min_Margin = 128 - ucTemp - (64 - (ucTemp1 >> 1));
usIPH_ACT_STA = usIPH_ACT_STA + 50 - ucTemp - (64 - (ucTemp1 >> 1));
usTemp = usIPH_ACT_STA;
Set_H_Position();
}
///////////////////////////////////////////////////////////////////
*/
Result = Measure_PositionN(NM);
if (ERROR_SUCCEED != (Result & 0x80)) return Result;
Result = ERROR_SUCCEED;
/////////////////////////////////
// Calculate Vertical Position //
/////////////////////////////////
NM = 1;
while (1)
{
if ((usIPV_ACT_STA + ucV_Max_Margin - 128) >= usVer_Start)
{
if ((usIPV_ACT_STA + ucV_Min_Margin - 128) <= usVer_Start)
{
stMUD.V_POSITION = (usVer_Start + 128) - usIPV_ACT_STA;
Set_V_Position();
break; // Success
}
else
Result |= ERROR_SUCCESS_2;
}
else
Result |= ERROR_SUCCESS_1;
// If we can't align upper bound, we try to align lower bound.
if (NM && usVer_End > usIPV_ACT_LEN)
{
usVer_Start = usVer_End - usIPV_ACT_LEN + 1;
NM = 0;
}
else
{
Result |= ERROR_SUCCESS_4;
break;
}
}
///////////////////////////////////
// Calculate Horizontal Position //
///////////////////////////////////
NM = 1;
while (1)
{
#if(ALIGN_LEFT == CLOCK_ALIGN)
if ((usIPH_ACT_STA + (stMUD.CLOCK >> 2) + ucH_Max_Margin - 32 - 128) >= usH_Start)
{
//if(ucH_Min_Margin == (128 - 10))
if(ucH_Min_Margin <= 128 )
((unsigned int*)Data)[0] = usH_Start + 128 - stMUD.H_POSITION + 50;//((unsigned int*)Data)[0] = usH_Start + ucTemp + 100;//((unsigned int*)Data)[0] = usH_Start + 10;
else
((unsigned int*)Data)[0] = usH_Start;// + (stMUD.H_POSITION - 128) + 10;
//if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Min_Margin - 64 - 128) <= (usH_Start + 50))
if ((usIPH_ACT_STA + (stMUD.CLOCK >> 2) + ucH_Min_Margin - 32 - 128) <= ((unsigned int*)Data)[0])
{
//usH_Start is the actual distance from Hsync to active image
stMUD.H_POSITION = usH_Start + 128 + 32 - usIPH_ACT_STA - (stMUD.CLOCK >> 2);
Set_H_Position();
break; // Success
}
else
Result |= ERROR_SUCCESS_16;
}
else
Result |= ERROR_SUCCESS_8;
#else
if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Max_Margin - 64 - 128) >= usH_Start)
{
//if(ucH_Min_Margin == (128 - 10))
if(ucH_Min_Margin <= 128 )
((unsigned int*)Data)[0] = usH_Start + 128 - stMUD.H_POSITION + 100;//((unsigned int*)Data)[0] = usH_Start + ucTemp + 100;//((unsigned int*)Data)[0] = usH_Start + 10;
else
((unsigned int*)Data)[0] = usH_Start;// + (stMUD.H_POSITION - 128) + 10;
//if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Min_Margin - 64 - 128) <= (usH_Start + 50))
if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Min_Margin - 64 - 128) <= ((unsigned int*)Data)[0])
{
//usH_Start is the actual distance from Hsync to active image
stMUD.H_POSITION = usH_Start + 128 + 64 - usIPH_ACT_STA - (stMUD.CLOCK >> 1);
Set_H_Position();
break; // Success
}
else
Result |= ERROR_SUCCESS_16;
}
else
Result |= ERROR_SUCCESS_8;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -