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

📄 lcd_auto.c

📁 keil c51平台,此代码可用于学习TFT LCD TV 搜台等,(本人自己修改)
💻 C
📖 第 1 页 / 共 5 页
字号:
            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();
        }   
    }
    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 > 4000000)) )
	{
		
		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;
    
    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 ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Max_Margin - 64 - 128) >= usH_Start)
        {
            if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Min_Margin - 64 - 128) <= usH_Start)
            {
                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;

        // If we can't align upper bound, we try to align lower bound.
        if (NM && usH_End > usIPH_ACT_WID)
        {
            usH_Start   = usH_End - usIPH_ACT_WID + 1;
            NM          = 0;
        }
        else
        {
            Result  |= ERROR_SUCCESS_32;
            break;
        }
    }

    return Result;
}

unsigned char Min_Noise_Margin(void)
{
    unsigned char   Result, Noise;
    unsigned int    Curr_StartH, Curr_EndH;

    Result  = Measure_PositionV(VERTICAL_MARGIN);
    if (ERROR_SUCCEED != (Result & 0x80))   return Result;

    if (0 == usVer_Start)
    {
        Result  = Measure_PositionV(VERTICAL_MARGIN + 0x20);
        if (ERROR_SUCCEED != (Result & 0x80))   return Result;
    }

    Noise   = 0x00;
    Result  = Measure_PositionH(Noise);
    if (ERROR_SUCCEED != (Result & 0x80))   return Result;
    
    Curr_StartH = usH_Start;    // Save H start position at noise margin = 0
    Curr_EndH   = usH_End;      // Save H end position at noise margin = 0

    do
    {
        Noise   = Noise + 0x10;
        Result  = Measure_PositionH(Noise);

        if (ERROR_SUCCEED != (Result & 0x80))   return Result;
    
        if (Curr_StartH >= usH_Start)
        {
            Curr_StartH = usH_Start;
        }
        else if (0x08 < (usH_Start - Curr_StartH))
        {
            break;  // A large gap of H start position is found.
        }
    }
    while (0x90 > Noise);

    if (0x80 < Noise)   return ERROR_NOISE_TOO_BIG;      

    while (1)
    {   
        Curr_StartH = usH_Start;
        Curr_EndH   = usH_End;

        Result  = Measure_PositionH(Noise + 0x28);
        
        if (ERROR_SUCCEED != (Result & 0x80))   return Result;

        if ((Curr_EndH - Curr_StartH) == (usH_End - usH_Start) || (Curr_EndH - Curr_StartH) >= (usH_End - usH_Start + 3))
        {
            break;  // We got noise margin with stable horizontal start/end position.
        }
        
        if (0xa0 <= Noise)
        {
            break;  // No stable horizontal start/end position are found.
        }

        Noise   = Noise + 0x10;        
        Result  = Measure_PositionH(Noise);

        if (ERROR_SUCCEED != (Result & 0x80))   return Result;
    };
    
    Data[0] = Noise + 0x10;

    return ERROR_SUCCEED;
}

unsigned char Auto_Phase(void)
{
    unsigned char   Result, Curr_PosV;

    bAutoInProgress = 1;

    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();
    }

    // Set ADC to default
//    RTDCodeW(ADC_DEFAULT);

    ///////////////////////////////
    //   Measure  NOISE_MARGIN   //
    ///////////////////////////////
    Result  = Min_Noise_Margin();

    if (ERROR_SUCCEED == (Result & 0x80))
    {   
        Result      = Auto_Phase_Do(Data[0]);   // Noise margin returned by Min_Noise_Margin() is saved in Data[0];
    }

    if (ERROR_SUCCEED != (Result & 0x80))
    {
        // Restore Phase
        Set_Phase(stMUD.PHASE);
    }
    else
    {
        Save_MUD(ucMode_Curr);
    }

    // Restore ADC Gain/Offset
    SetADC_GainOffset();

    // Restore vertical position
    if (Curr_PosV != stMUD.V_POSITION)
    {
        stMUD.V_POSITION    = Curr_PosV;
        Set_V_Position();
    }

    bAutoInProgress = 0;

    return Result;
}


unsigned char Auto_Phase_Do(unsigned char NM)

⌨️ 快捷键说明

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