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

📄 lcd_auto.c

📁 keil c51平台,此代码可用于学习TFT LCD TV 搜台等,(本人自己修改)
💻 C
📖 第 1 页 / 共 5 页
字号:
#define __AUTO__

#include "reg52.h"

#include "MAIN_DEF.h"
#include "ACCESS.H"
#include "LCD_MAIN.H"
#include "CONFIG.H"
#include "LCD_FUNC.H"
#include "LCD_AUTO.H"

void Wait_Finish(void)
{
    unsigned char   Wait_Time_Cnt, IVS_Event;

	RTDSetByte(STATUS0_01, 0x00);  // Clear status  
    RTDSetByte(STATUS1_1F, 0x00);  // Clear status

    
    Wait_Time_Cnt   = 120;//60;           // Auto-Phase timeout 60ms
    IVS_Event       = 50;//25;           // IVS timeout 25ms 
    do
    {  
         Delay_Xms(2);
     

#if(AS_NON_FRAMESYNC == 0)

         RTDRead(STATUS0_01, 1, N_INC);  // Get status        

        if (Data[0] & 0x63)
        {
            bLIGHT_PWR  = LIGHT_OFF;

            RTDCodeW(FreeV);

            Data[0] = ERROR_INPUT;
            RTDSetByte(STATUS0_01, 0x00);  // Clear status  
            return;
        }
#endif

#if(AS_NON_FRAMESYNC == 0 || AS_DV_TOTAL == 0)
        RTDRead(STATUS1_1F, 1, N_INC);  // Get status


        if ((Data[0] & (EVENT_UNDERFLOW | EVENT_OVERFLOW)) || (0 == --IVS_Event))
        {
            bLIGHT_PWR  = LIGHT_OFF;

            RTDCodeW(FreeV);

            Data[0] = ERROR_INPUT;
			RTDSetByte(STATUS1_1F,0x00); //Event happened, write once to clear the status
            return;
        }
        else if (Data[0] & (EVENT_IVS | EVENT_IEN_START))
        {
            IVS_Event   = 25;       // IVS timeout 25ms 
			RTDSetByte(STATUS1_1F,0x00); //Event happened, write once to clear the status
        }
#endif
        RTDRead(AUTO_ADJ_CTRL_7F, 1, N_INC);
    }
    while ((Data[0] & 0x01) && (--Wait_Time_Cnt));
    
    RTDRead(STATUS0_01, 1, N_INC);  // Get status


	if(Data[0])
		RTDSetByte(STATUS0_01,0x00); //Event happened, write once to clear the status


    // Return non-zero value in Data[0] if :
    // 1. IVS or IHS changed
    // 2. Buffer underflow or overflow
    // 3. Auto-Phase Tracking timeout
    Data[0] = (Data[0] & 0x63) ? ERROR_INPUT : (0 == Wait_Time_Cnt) ? ERROR_TIMEOUT : ERROR_SUCCEED;
}

//--------------------Measure Vertical Position---------------------//
// Return Message => ERROR_SUCCESS   : Success                      //
//                   ERROR_INPUT     : 1. IVS or IHS changed        //
//                                     2. underflow or overflow     //
//                   ERROR_TIMEOUT   : Measure Time_Out             //
//                   ERROR_NOTACTIVE : No Avtive Image              //
//------------------------------------------------------------------//
unsigned char Measure_PositionV(unsigned char NM_V)
{
    unsigned int    usLBound, usRBound;

    RTDRead(MEAS_HI_51, 0x02, Y_INC);
    Data[2] = Data[1] & 0x0f;
    Data[3] = Data[0];

    usRBound    = usADC_Clock + (unsigned int)stMUD.CLOCK - 128;
    usLBound    = (unsigned long)usRBound * ((unsigned int *)Data)[1] / usStdHS;

    // Original formula : 
    // usRBound    = usRBound - 2 - (PROGRAM_HDELAY - MEASURE_HDEALY) - (stMUD.H_POSITION - ucH_Min_Margin);
    // usLBound    = usLBound + 20 - (PROGRAM_HDELAY - MEASURE_HDEALY) - (stMUD.H_POSITION - ucH_Min_Margin);

    usRBound    = usRBound - 2 + MEASURE_HDEALY - PROGRAM_HDELAY + ucH_Min_Margin - stMUD.H_POSITION;
    usLBound    = usLBound + 20 + ucH_Min_Margin + MEASURE_HDEALY;
    usLBound    = usLBound > ((unsigned int)stMUD.H_POSITION + PROGRAM_HDELAY) ? (usLBound - PROGRAM_HDELAY - stMUD.H_POSITION) : 1;

    NM_V        = NM_V & 0xfc;

    Data[0]     = 6;
    Data[1]     = Y_INC;
    Data[2]     = H_BND_STA_L_75;
    Data[3]     = (unsigned char)usLBound;
    Data[4]     = (unsigned char)usRBound;
    Data[5]     = ((unsigned char)(usLBound >> 4) & 0x70) | ((unsigned char)(usRBound >> 8) & 0x0f);    
    Data[6]     = 8;
    Data[7]     = Y_INC;
    Data[8]     = MARGIN_R_7B;
    Data[9]     = NM_V;
    Data[10]    = NM_V | PIXEL_1;
    Data[11]    = NM_V;
    Data[12]    = 0x00;
    Data[13]    = 0x01;
    Data[14]    = 0;
    RTDWrite(Data);
    
    Wait_Finish();
    
    if (ERROR_SUCCEED != Data[0])   return Data[0];
	
    RTDRead(VER_START_80, 4, Y_INC);

    // Translate byte order : RTD little indian -> 8051 big indian
    Data[6] = Data[1] & 0x0f;
    Data[7] = Data[0];
    Data[8] = Data[3] & 0x0f;
    Data[9] = Data[2];

    // V Start/End should subtract 1
    usVer_Start     = ((unsigned int *)Data)[3] ? ((unsigned int *)Data)[3] - 1 : 0;
    usVer_End       = ((unsigned int *)Data)[4] ? ((unsigned int *)Data)[4] - 1 : 0;

    // Check all black
    if (0x0000 == usVer_End)    return  ERROR_NOTACTIVE;

/*
    // Issac 2002/10/15
    // To prevent from noise induced by VSYNC
    if (usVer_End > (usVer_Start + usIPV_ACT_LEN - 1))
    {
        usVer_End   = usVer_Start + usIPV_ACT_LEN - 1;

        ((unsigned int *)Data)[4]   = usVer_End;
    }
*/
	if ((9 - PROGRAM_VDELAY) > usVer_Start)
	{
	    ((unsigned int *)Data)[3]	= 9 - PROGRAM_VDELAY;
	}
	else
	{
        // To prevent from noise induced by VSYNC
        if (usVer_End > (usVer_Start + usIPV_ACT_LEN - 1))
        {
            usVer_End   = usVer_Start + usIPV_ACT_LEN - 1;

            ((unsigned int *)Data)[4]   = usVer_End;
        }
	}

    // Update auto-tracking window vertical range
    Data[0] = 6;
    Data[1] = Y_INC;
    Data[2] = V_BND_STA_L_78;
    Data[3] = Data[7];    
    Data[4] = Data[9];
    Data[5] = (Data[6] << 4) + Data[8];
    Data[6] = 0;
    RTDWrite(Data);

    return ERROR_SUCCEED;
}

//--------------------Measure Horizontal Position-------------------//
// Return Message => ERROR_SUCCESS   : Success                      //
//                   ERROR_INPUT     : 1. IVS or IHS changed        //
//                                     2. underflow or overflow     //
//                   ERROR_TIMEOUT   : Measure Time_Out             //
//                   ERROR_NOTACTIVE : No Avtive Image              //
//------------------------------------------------------------------//
unsigned char Measure_PositionH(unsigned char NM_H)
{
    unsigned int    usLBound, usRBound;

    RTDRead(MEAS_HI_51, 0x02, Y_INC);
    Data[2] = Data[1] & 0x0f;
    Data[3] = Data[0];

    usRBound    = usADC_Clock + (unsigned int)stMUD.CLOCK - 128;
    usLBound    = (unsigned long)usRBound * ((unsigned int *)Data)[1] / usStdHS;

    usRBound    = usRBound - 2 + MEASURE_HDEALY - PROGRAM_HDELAY + ucH_Min_Margin - stMUD.H_POSITION;

    usLBound    = usLBound + 20 + ucH_Min_Margin + MEASURE_HDEALY;
    usLBound    = usLBound > ((unsigned int)stMUD.H_POSITION + PROGRAM_HDELAY) ? (usLBound - PROGRAM_HDELAY - stMUD.H_POSITION) : 1;

    NM_H        = NM_H & 0xfc;

    Data[0]     = 6;
    Data[1]     = Y_INC;
    Data[2]     = H_BND_STA_L_75;
    Data[3]     = (unsigned char)usLBound;
    Data[4]     = (unsigned char)usRBound;
    Data[5]     = ((unsigned char)(usLBound >> 4) & 0x70) | ((unsigned char)(usRBound >> 8) & 0x0f);    
    Data[6]     = 8;
    Data[7]     = Y_INC;
    Data[8]     = MARGIN_R_7B;
    Data[9]     = NM_H;
    Data[10]    = NM_H;
    Data[11]    = NM_H;
    Data[12]    = 0x00;
    Data[13]    = 0x01;
    Data[14]    = 0;
    RTDWrite(Data);

    Wait_Finish();

    if (ERROR_SUCCEED != Data[0])   return Data[0];

    RTDRead(HOR_START_84, 4, Y_INC);

    // Translate byte order : RTD little indian -> 8051 big indian
    Data[4] = Data[3] & 0x0f;
    Data[5] = Data[2];
    Data[2] = Data[1] & 0x0f;
    Data[3] = Data[0];

    if (0x07FF <= ((unsigned int *)Data)[1])     return  ERROR_NOTACTIVE;

    RTDRead(VGIP_CTRL_04, 1, N_INC);

    //if (0x14 == (Data[0] & 0x1c))
	if (0x08 == (Data[0] & 0x0c))
    {
        ((unsigned int *)Data)[1]   += 0x03;
        ((unsigned int *)Data)[2]   += 0x03;
    }

/*  
    usH_Start   = MEAS_H_STA_OFFSET < ((unsigned int *)Data)[1] ? ((unsigned int *)Data)[1] - MEAS_H_STA_OFFSET : 0x0000;
    usH_End     = MEAS_H_END_OFFSET < ((unsigned int *)Data)[2] ? ((unsigned int *)Data)[2] - MEAS_H_END_OFFSET : 0x0fff;

    if (0x0000 != usH_Start)    usH_Start   = usH_Start + stMUD.H_POSITION - 128;
    if (0x0fff != usH_End)      usH_End     = usH_End + stMUD.H_POSITION - 128;
*/        

    usH_Start   = (((unsigned int *)Data)[1] + stMUD.H_POSITION) >= (128 + MEAS_H_STA_OFFSET)
                ? (((unsigned int *)Data)[1] + stMUD.H_POSITION) - (128 + MEAS_H_STA_OFFSET) : 0x0000;
    usH_End     = (((unsigned int *)Data)[2] + stMUD.H_POSITION) >= (128 + MEAS_H_END_OFFSET)
                ? (((unsigned int *)Data)[2] + stMUD.H_POSITION) - (128 + MEAS_H_END_OFFSET) : 0x0fff;

    return ERROR_SUCCEED;
}

//---------------Measure Vertical & Horizontal Position-------------//
// Return Message => ERROR_SUCCESS   : Success                      //
//                   ERROR_INPUT     : 1. IVS or IHS changed        //
//                                     2. underflow or overflow     //
//                   ERROR_TIMEOUT   : Measure Time_Out             //
//                   ERROR_NOTACTIVE : No Avtive Image              //
//------------------------------------------------------------------//
unsigned char Measure_PositionN(unsigned char NM)
{
    unsigned char Result;
    
    Result  = Measure_PositionV(NM);

    if (ERROR_SUCCEED == Result)    
    {
        Result  = Measure_PositionH(NM);
    }
        
    return Result;
}

#if (MENU_TYPE == MENU_REL)
//------------------------------------------------------------------//
//                           Auto Clock                             //
//------------------------------------------------------------------//
unsigned char Auto_Clock(void)
{
    unsigned char   Result, Curr_PosH, Curr_PosV, Curr_Clock, Curr_Phase;   
    
    bAutoInProgress = 1;
    
    Curr_PosH   = stMUD.H_POSITION;     // Save current stMUD.H_POSITION
    Curr_PosV   = stMUD.V_POSITION;     // Save current stMUD.V_POSITION
    Curr_Clock  = stMUD.CLOCK;          // Save current stMUD.CLOCK 
    Curr_Phase  = stMUD.PHASE;          // Save current stMUD.PHASE

    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();   // Data[0] : Noise Margin
    
    if (ERROR_SUCCEED == (Result & 0x80))
    {
        Result  = Data[0];

        stMUD.CLOCK &= 0xfc;    // stMUD.CLOCK must be times of 4

        if (stMUD.CLOCK != Curr_Clock || 28 > stMUD.CLOCK || 228 < stMUD.CLOCK)
        {
            Set_Clock();
        }
        
        ///////////////////////////////
        //       Adjust Clock        //
        ///////////////////////////////
        Result  = Auto_Clock_Do(Result);
        
        if (ERROR_SUCCEED != (Result & 0x80))
        {
            if (stMUD.CLOCK != Curr_Clock)
            {   
                // Fail to find out suitable clock. Restore original clock and H position.
                stMUD.CLOCK         = Curr_Clock;
                stMUD.H_POSITION    = Curr_PosH;

                Set_Clock();
                Set_H_Position();
            }
        }
        else
        {
            if (stMUD.CLOCK != Curr_Clock)
            {
                stMUD.H_POSITION    = usH_Start + 128 + 64 - usIPH_ACT_STA - (stMUD.CLOCK >> 1);

                if (ucH_Max_Margin < stMUD.H_POSITION)
                    stMUD.H_POSITION    = ucH_Max_Margin;
                else if (ucH_Min_Margin > stMUD.H_POSITION)
                    stMUD.H_POSITION    = ucH_Min_Margin;
				
                Set_H_Position();

                Save_MUD(ucMode_Curr);
            }
        }
    }

    // Restore ADC Gain/Offset
    SetADC_GainOffset();
    
    // Restore ADC phase 
    stMUD.PHASE = Curr_Phase;
    Set_Phase(stMUD.PHASE);

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

    bAutoInProgress = 0;

    return Result;
}
#endif

void Read_Auto_Info(unsigned char index)
{
     if(index == 0) return;

	 RTDRead(AUTO_PHASE0_88, 4, Y_INC);

      Data[index << 2] = Data[3];
      Data[(index << 2) + 1] = Data[2];
      Data[(index << 2) + 2] = Data[1];
      Data[(index << 2) + 3] = Data[0];

}

unsigned char FindColor()
{
unsigned long ulTemp0;
unsigned char ucDetect,ucResult,ucPhase;

RTDSetByte(DIFF_THRED_7E, 0x28);

    ulTemp0     = 0;
    ucDetect    = 0x77;
    do
    {
        ucResult    = COLORS_BLUE;
        ucPhase     = COLORS_BLUE;
        do
        {
            RTDSetByte(MARGIN_B_7D, ucPhase);
            RTDSetByte(AUTO_ADJ_CTRL_7F, ucDetect);

            Wait_Finish();
            if (ERROR_SUCCEED != Data[0])   return Data[0];

            Read_Auto_Info(1);

            if (ulTemp0 < ((unsigned long *)Data)[1])
            {
                ulTemp0     = ((unsigned long *)Data)[1];
                ucResult    = ucPhase;

                if (0x8000 < ulTemp0)   break;
            }

            if (COLORS_BLUE == ucPhase)
                ucPhase = COLORS_GREEN;
            else if (COLORS_GREEN == ucPhase)
                ucPhase = COLORS_RED;
            else
                break;
        }
        while (1);

        if (0 != ulTemp0 || 0x7b != ucDetect)   break;

        ucDetect    = 0x77;
    }
    while (1);

    return ERROR_SUCCEED;
}
unsigned long GetMaxSum(unsigned char select)
{
   unsigned char ucPhase,ucDetect;
   unsigned long ulTemp0;
   ucPhase = 0; //0,8,16,24

⌨️ 快捷键说明

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