Lcd_auto.lst
上传用户:xmyjxjd
上传日期:2013-05-04
资源大小:1517k
文件大小:350k
开发平台:

C/C++

  1. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 1   
  2. C51 COMPILER V7.06, COMPILATION OF MODULE LCD_AUTO
  3. OBJECT MODULE PLACED IN .OutputLcd_auto.obj
  4. COMPILER INVOKED BY: C:KeilC51BINC51.EXE CodeLcd_auto.c OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND CODE SYMBOLS PR
  5.                     -INT(.OutputLcd_auto.lst) PREPRINT(.OutputLcd_auto.i) OBJECT(.OutputLcd_auto.obj)
  6. stmt level    source
  7.    1          #define __AUTO__
  8.    2          
  9.    3          #include "reg52.h"
  10.    4          
  11.    5          #include "HeaderMAIN_DEF.h"
  12.    6          #include "HeaderACCESS.H"
  13.    7          #include "HeaderLCD_MAIN.H"
  14.    8          #include "HeaderCONFIG.H"
  15.    9          #include "HeaderLCD_FUNC.H"
  16.   10          #include "HeaderLCD_AUTO.H"
  17.   11          #include "HeaderLCD_OSD.H"
  18.   12          
  19.   13          
  20.   14          void Wait_Finish(void)
  21.   15          {
  22.   16   1          unsigned char   Wait_Time_Cnt, IVS_Event;
  23.   17   1      
  24.   18   1              RTDSetByte(STATUS0_01, 0x00);  // Clear status  
  25.   19   1          RTDSetByte(STATUS1_1F, 0x00);  // Clear status
  26.   20   1      
  27.   21   1          
  28.   22   1          Wait_Time_Cnt   = 60;           // Auto-Phase timeout 60ms
  29.   23   1          IVS_Event       = 25;           // IVS timeout 25ms 
  30.   24   1          do
  31.   25   1          {  
  32.   26   2               Delay_Xms(1);
  33.   27   2           
  34.   28   2      /*
  35.   29   2      #if(AS_NON_FRAMESYNC == 0)
  36.   30   2      
  37.   31   2               RTDRead(STATUS0_01, 1, N_INC);  // Get status        
  38.   32   2      
  39.   33   2              if (Data[0] & 0x63)
  40.   34   2              {
  41.   35   2      
  42.   36   2      #if(MCU_TYPE == MCU_WINBOND)
  43.   37   2              bLIGHT_PWR  = LIGHT_OFF;
  44.   38   2      #else
  45.   39   2              MCU_WriteBacklightPower(LIGHT_OFF);
  46.   40   2      #endif
  47.   41   2      
  48.   42   2                  RTDCodeW(FreeV);
  49.   43   2      
  50.   44   2                  Data[0] = ERROR_INPUT;
  51.   45   2                  RTDSetByte(STATUS0_01, 0x00);  // Clear status  
  52.   46   2      
  53.   47   2                  return;
  54.   48   2                              
  55.   49   2              }
  56.   50   2      #endif
  57.   51   2            //Delay_Xms(2);
  58.   52   2      #if(MCU_TYPE == MCU_MTV512)
  59.   53   2            RTDSetByte(STATUS1_1F, 0x00);  // Clear status
  60.   54   2      #endif
  61. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 2   
  62.   55   2      
  63.   56   2      #if(AS_NON_FRAMESYNC == 0 || AS_DV_TOTAL == 0)
  64.   57   2              RTDRead(STATUS1_1F, 1, N_INC);  // Get status
  65.   58   2              
  66.   59   2      
  67.   60   2              if ((Data[0] & (EVENT_UNDERFLOW | EVENT_OVERFLOW)) || (0 == --IVS_Event))
  68.   61   2              {
  69.   62   2               
  70.   63   2      #if(MCU_TYPE == MCU_WINBOND)
  71.   64   2              bLIGHT_PWR  = LIGHT_OFF;
  72.   65   2      #else
  73.   66   2              MCU_WriteBacklightPower(LIGHT_OFF);
  74.   67   2      #endif
  75.   68   2                  RTDCodeW(FreeV);
  76.   69   2      
  77.   70   2                  Data[0] = ERROR_INPUT;
  78.   71   2                              RTDSetByte(STATUS1_1F,0x00); //Event happened, write once to clear the status
  79.   72   2      
  80.   73   2                  return;
  81.   74   2              }
  82.   75   2              else if (Data[0] & (EVENT_IVS | EVENT_IEN_START))
  83.   76   2              {
  84.   77   2                  IVS_Event   = 25;       // IVS timeout 25ms 
  85.   78   2                              RTDSetByte(STATUS1_1F,0x00); //Event happened, write once to clear the status
  86.   79   2              }
  87.   80   2                      
  88.   81   2      #endif
  89.   82   2      */
  90.   83   2              RTDRead(AUTO_ADJ_CTRL_7F, 1, N_INC);
  91.   84   2          }
  92.   85   1          while ((Data[0] & 0x01) && (--Wait_Time_Cnt));
  93.   86   1          
  94.   87   1          RTDRead(STATUS0_01, 1, N_INC);  // Get status
  95.   88   1      
  96.   89   1      
  97.   90   1              if(Data[0])
  98.   91   1                      RTDSetByte(STATUS0_01,0x00); //Event happened, write once to clear the status
  99.   92   1      
  100.   93   1      
  101.   94   1          // Return non-zero value in Data[0] if :
  102.   95   1          // 1. IVS or IHS changed
  103.   96   1          // 2. Buffer underflow or overflow
  104.   97   1          // 3. Auto-Phase Tracking timeout
  105.   98   1              
  106.   99   1          Data[0] = (Data[0] & 0x63) ? ERROR_INPUT : (0 == Wait_Time_Cnt) ? ERROR_TIMEOUT : ERROR_SUCCEED;
  107.  100   1      }
  108.  101          
  109.  102          #if(HARDWARE_AUTO)
  110.  103          void Wait_For_IVS(void)
  111.  104          {
  112.  105   1         unsigned char t;
  113.  106   1      
  114.  107   1              t = 50;
  115.  108   1      
  116.  109   1              RTDSetByte(0x1f,0x00);
  117.  110   1              do
  118.  111   1              {
  119.  112   2                      RTDRead(0x1f, 1, Y_INC);
  120.  113   2                      Data[0] = Data[0] & EVENT_IVS;
  121.  114   2                      //t--;
  122.  115   2                      Delay_Xms(1);
  123.  116   2              }while((Data[0] == 0) && (t--));
  124. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 3   
  125.  117   1      }
  126.  118          #endif
  127.  119          
  128.  120          //--------------------Measure Vertical Position---------------------//
  129.  121          // Return Message => ERROR_SUCCESS   : Success                      //
  130.  122          //                   ERROR_INPUT     : 1. IVS or IHS changed        //
  131.  123          //                                     2. underflow or overflow     //
  132.  124          //                   ERROR_TIMEOUT   : Measure Time_Out             //
  133.  125          //                   ERROR_NOTACTIVE : No Avtive Image              //
  134.  126          //------------------------------------------------------------------//
  135.  127          unsigned char Measure_PositionV(unsigned char NM_V)
  136.  128          {
  137.  129   1          unsigned int    usLBound, usRBound;
  138.  130   1      
  139.  131   1          RTDRead(MEAS_HI_51, 0x02, Y_INC);
  140.  132   1          Data[2] = Data[1] & 0x0f;
  141.  133   1          Data[3] = Data[0];
  142.  134   1      
  143.  135   1          usRBound    = usADC_Clock + (unsigned int)stMUD.CLOCK - 128;
  144.  136   1          usLBound    = (unsigned long)usRBound * ((unsigned int *)Data)[1] / usStdHS;
  145.  137   1      
  146.  138   1          // Original formula : 
  147.  139   1          // usRBound    = usRBound - 2 - (PROGRAM_HDELAY - MEASURE_HDEALY) - (stMUD.H_POSITION - ucH_Min_Margin
  148.              -);
  149.  140   1          // usLBound    = usLBound + 20 - (PROGRAM_HDELAY - MEASURE_HDEALY) - (stMUD.H_POSITION - ucH_Min_Margi
  150.              -n);
  151.  141   1      
  152.  142   1          usRBound    = usRBound - 2 + MEASURE_HDEALY - PROGRAM_HDELAY + ucH_Min_Margin - stMUD.H_POSITION;
  153.  143   1          usLBound    = usLBound + 20 + ucH_Min_Margin + MEASURE_HDEALY;
  154.  144   1          usLBound    = usLBound > ((unsigned int)stMUD.H_POSITION + PROGRAM_HDELAY) ? (usLBound - PROGRAM_HDELA
  155.              -Y - stMUD.H_POSITION) : 1;
  156.  145   1      
  157.  146   1          NM_V        = NM_V & 0xfc;
  158.  147   1      
  159.  148   1          Data[0]     = 6;
  160.  149   1          Data[1]     = Y_INC;
  161.  150   1          Data[2]     = H_BND_STA_L_75;
  162.  151   1          Data[3]     = (unsigned char)usLBound;
  163.  152   1          Data[4]     = (unsigned char)usRBound;
  164.  153   1          Data[5]     = ((unsigned char)(usLBound >> 4) & 0x70) | ((unsigned char)(usRBound >> 8) & 0x0f);    
  165.  154   1          Data[6]     = 8;
  166.  155   1          Data[7]     = Y_INC;
  167.  156   1          Data[8]     = MARGIN_R_7B;
  168.  157   1          Data[9]     = NM_V;
  169.  158   1          Data[10]    = NM_V | PIXEL_1;
  170.  159   1          Data[11]    = NM_V;
  171.  160   1          Data[12]    = 0x00;
  172.  161   1          Data[13]    = 0x01;
  173.  162   1          Data[14]    = 0;
  174.  163   1          RTDWrite(Data);
  175.  164   1          
  176.  165   1              Wait_Finish();
  177.  166   1      
  178.  167   1      
  179.  168   1          if (ERROR_SUCCEED != Data[0])   return Data[0];
  180.  169   1      
  181.  170   1          RTDRead(VER_START_80, 4, Y_INC);
  182.  171   1      
  183.  172   1          // Translate byte order : RTD little indian -> 8051 big indian
  184.  173   1          Data[6] = Data[1] & 0x0f;
  185.  174   1          Data[7] = Data[0];
  186.  175   1          Data[8] = Data[3] & 0x0f;
  187. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 4   
  188.  176   1          Data[9] = Data[2];
  189.  177   1      
  190.  178   1          // V Start/End should subtract 1
  191.  179   1          usVer_Start     = ((unsigned int *)Data)[3] ? ((unsigned int *)Data)[3] - 1 : 0;
  192.  180   1          usVer_End       = ((unsigned int *)Data)[4] ? ((unsigned int *)Data)[4] - 1 : 0;
  193.  181   1      
  194.  182   1          // Check all black
  195.  183   1          if (0x0000 == usVer_End)    return  ERROR_NOTACTIVE;
  196.  184   1      
  197.  185   1      /*
  198.  186   1          // Issac 2002/10/15
  199.  187   1          // To prevent from noise induced by VSYNC
  200.  188   1          if (usVer_End > (usVer_Start + usIPV_ACT_LEN - 1))
  201.  189   1          {
  202.  190   1              usVer_End   = usVer_Start + usIPV_ACT_LEN - 1;
  203.  191   1      
  204.  192   1              ((unsigned int *)Data)[4]   = usVer_End;
  205.  193   1          }
  206.  194   1      */
  207.  195   1              if ((9 - PROGRAM_VDELAY) > usVer_Start)
  208.  196   1              {
  209.  197   2                  ((unsigned int *)Data)[3]   = 9 - PROGRAM_VDELAY;
  210.  198   2              }
  211.  199   1              else
  212.  200   1              {
  213.  201   2              // To prevent from noise induced by VSYNC
  214.  202   2              if (usVer_End > (usVer_Start + usIPV_ACT_LEN - 1))
  215.  203   2              {
  216.  204   3                  usVer_End   = usVer_Start + usIPV_ACT_LEN - 1;
  217.  205   3      
  218.  206   3                  ((unsigned int *)Data)[4]   = usVer_End;
  219.  207   3              }
  220.  208   2              }
  221.  209   1      
  222.  210   1          // Update auto-tracking window vertical range
  223.  211   1          Data[0] = 6;
  224.  212   1          Data[1] = Y_INC;
  225.  213   1          Data[2] = V_BND_STA_L_78;
  226.  214   1          Data[3] = Data[7];    
  227.  215   1          Data[4] = Data[9];
  228.  216   1          Data[5] = (Data[6] << 4) + Data[8];
  229.  217   1          Data[6] = 0;
  230.  218   1          RTDWrite(Data);
  231.  219   1      
  232.  220   1          return ERROR_SUCCEED;
  233.  221   1      }
  234.  222          
  235.  223          //--------------------Measure Horizontal Position-------------------//
  236.  224          // Return Message => ERROR_SUCCESS   : Success                      //
  237.  225          //                   ERROR_INPUT     : 1. IVS or IHS changed        //
  238.  226          //                                     2. underflow or overflow     //
  239.  227          //                   ERROR_TIMEOUT   : Measure Time_Out             //
  240.  228          //                   ERROR_NOTACTIVE : No Avtive Image              //
  241.  229          //------------------------------------------------------------------//
  242.  230          unsigned char Measure_PositionH(unsigned char NM_H)
  243.  231          {
  244.  232   1          unsigned int    usLBound, usRBound;
  245.  233   1      
  246.  234   1          RTDRead(MEAS_HI_51, 0x02, Y_INC);
  247.  235   1          Data[2] = Data[1] & 0x0f;
  248.  236   1          Data[3] = Data[0];
  249.  237   1      
  250. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 5   
  251.  238   1          usRBound    = usADC_Clock + (unsigned int)stMUD.CLOCK - 128;
  252.  239   1          usLBound    = (unsigned long)usRBound * ((unsigned int *)Data)[1] / usStdHS;
  253.  240   1      
  254.  241   1          usRBound    = usRBound - 2 + MEASURE_HDEALY - PROGRAM_HDELAY + ucH_Min_Margin - stMUD.H_POSITION;
  255.  242   1      
  256.  243   1          usLBound    = usLBound + 20 + ucH_Min_Margin + MEASURE_HDEALY;
  257.  244   1          usLBound    = usLBound > ((unsigned int)stMUD.H_POSITION + PROGRAM_HDELAY) ? (usLBound - PROGRAM_HDELA
  258.              -Y - stMUD.H_POSITION) : 1;
  259.  245   1      
  260.  246   1          NM_H        = NM_H & 0xfc;
  261.  247   1      
  262.  248   1          Data[0]     = 6;
  263.  249   1          Data[1]     = Y_INC;
  264.  250   1          Data[2]     = H_BND_STA_L_75;
  265.  251   1          Data[3]     = (unsigned char)usLBound;
  266.  252   1          Data[4]     = (unsigned char)usRBound;
  267.  253   1          Data[5]     = ((unsigned char)(usLBound >> 4) & 0x70) | ((unsigned char)(usRBound >> 8) & 0x0f);    
  268.  254   1          Data[6]     = 8;
  269.  255   1          Data[7]     = Y_INC;
  270.  256   1          Data[8]     = MARGIN_R_7B;
  271.  257   1          Data[9]     = NM_H;
  272.  258   1          Data[10]    = NM_H;
  273.  259   1          Data[11]    = NM_H;
  274.  260   1          Data[12]    = 0x00;
  275.  261   1          Data[13]    = 0x01;
  276.  262   1          Data[14]    = 0;
  277.  263   1          RTDWrite(Data);
  278.  264   1      
  279.  265   1          Wait_Finish();
  280.  266   1      
  281.  267   1          if (ERROR_SUCCEED != Data[0])   return Data[0];
  282.  268   1      
  283.  269   1          RTDRead(HOR_START_84, 4, Y_INC);
  284.  270   1      
  285.  271   1          // Translate byte order : RTD little indian -> 8051 big indian
  286.  272   1          Data[4] = Data[3] & 0x0f;
  287.  273   1          Data[5] = Data[2];
  288.  274   1          Data[2] = Data[1] & 0x0f;
  289.  275   1          Data[3] = Data[0];
  290.  276   1          
  291.  277   1          if (0x07FF <= ((unsigned int *)Data)[1])     return  ERROR_NOTACTIVE;
  292.  278   1          
  293.  279   1      
  294.  280   1          RTDRead(VGIP_CTRL_04, 1, N_INC);
  295.  281   1      
  296.  282   1          //if (0x14 == (Data[0] & 0x1c))
  297.  283   1              if (0x08 == (Data[0] & 0x0c))
  298.  284   1          {
  299.  285   2              ((unsigned int *)Data)[1]   += 0x03;
  300.  286   2              ((unsigned int *)Data)[2]   += 0x03;
  301.  287   2          }
  302.  288   1      
  303.  289   1      /*  
  304.  290   1          usH_Start   = MEAS_H_STA_OFFSET < ((unsigned int *)Data)[1] ? ((unsigned int *)Data)[1] - MEAS_H_STA_O
  305.              -FFSET : 0x0000;
  306.  291   1          usH_End     = MEAS_H_END_OFFSET < ((unsigned int *)Data)[2] ? ((unsigned int *)Data)[2] - MEAS_H_END_O
  307.              -FFSET : 0x0fff;
  308.  292   1      
  309.  293   1          if (0x0000 != usH_Start)    usH_Start   = usH_Start + stMUD.H_POSITION - 128;
  310.  294   1          if (0x0fff != usH_End)      usH_End     = usH_End + stMUD.H_POSITION - 128;
  311.  295   1      */        
  312.  296   1      
  313. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 6   
  314.  297   1          usH_Start   = (((unsigned int *)Data)[1] + stMUD.H_POSITION) >= (128 + MEAS_H_STA_OFFSET)
  315.  298   1                      ? (((unsigned int *)Data)[1] + stMUD.H_POSITION) - (128 + MEAS_H_STA_OFFSET) : 0x0000;
  316.  299   1          usH_End     = (((unsigned int *)Data)[2] + stMUD.H_POSITION) >= (128 + MEAS_H_END_OFFSET)
  317.  300   1                      ? (((unsigned int *)Data)[2] + stMUD.H_POSITION) - (128 + MEAS_H_END_OFFSET) : 0x0fff;
  318.  301   1      
  319.  302   1          return ERROR_SUCCEED;
  320.  303   1      }
  321.  304          
  322.  305          //---------------Measure Vertical & Horizontal Position-------------//
  323.  306          // Return Message => ERROR_SUCCESS   : Success                      //
  324.  307          //                   ERROR_INPUT     : 1. IVS or IHS changed        //
  325.  308          //                                     2. underflow or overflow     //
  326.  309          //                   ERROR_TIMEOUT   : Measure Time_Out             //
  327.  310          //                   ERROR_NOTACTIVE : No Avtive Image              //
  328.  311          //------------------------------------------------------------------//
  329.  312          unsigned char Measure_PositionN(unsigned char NM)
  330.  313          {
  331.  314   1          unsigned char Result;
  332.  315   1          
  333.  316   1          Result  = Measure_PositionV(NM);
  334.  317   1      
  335.  318   1          if (ERROR_SUCCEED == Result)    
  336.  319   1          {
  337.  320   2              Result  = Measure_PositionH(NM);
  338.  321   2          }
  339.  322   1              
  340.  323   1          return Result;
  341.  324   1      }
  342.  325          
  343.  326          /*
  344.  327          //------------------------------------------------------------------//
  345.  328          //                           Auto Clock                             //
  346.  329          //------------------------------------------------------------------//
  347.  330          unsigned char Auto_Clock(void)
  348.  331          {
  349.  332              unsigned char   Result, Curr_PosH, Curr_PosV, Curr_Clock, Curr_Phase;   
  350.  333              
  351.  334              bAutoInProgress = 1;
  352.  335              
  353.  336              Curr_PosH   = stMUD.H_POSITION;     // Save current stMUD.H_POSITION
  354.  337              Curr_PosV   = stMUD.V_POSITION;     // Save current stMUD.V_POSITION
  355.  338              Curr_Clock  = stMUD.CLOCK;          // Save current stMUD.CLOCK 
  356.  339              Curr_Phase  = stMUD.PHASE;          // Save current stMUD.PHASE
  357.  340          
  358.  341              if (ucV_Max_Margin < stMUD.V_POSITION)
  359.  342              {
  360.  343                  stMUD.V_POSITION    = ucV_Max_Margin;
  361.  344                  Set_V_Position();
  362.  345              }
  363.  346          
  364.  347              RTDCodeW(ADC_DEFAULT);
  365.  348          
  366.  349              ///////////////////////////////
  367.  350              //   Measure  NOISE_MARGIN   //
  368.  351              ///////////////////////////////
  369.  352              Result      = Min_Noise_Margin();   // Data[0] : Noise Margin
  370.  353              
  371.  354              if (ERROR_SUCCEED == (Result & 0x80))
  372.  355              {
  373.  356                  Result  = Data[0];
  374.  357          
  375.  358                  stMUD.CLOCK &= 0xfc;    // stMUD.CLOCK must be times of 4
  376. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 7   
  377.  359          
  378.  360                  if (stMUD.CLOCK != Curr_Clock || 28 > stMUD.CLOCK || 228 < stMUD.CLOCK)
  379.  361                  {
  380.  362                      Set_Clock();
  381.  363                  }
  382.  364                  
  383.  365                  ///////////////////////////////
  384.  366                  //       Adjust Clock        //
  385.  367                  ///////////////////////////////
  386.  368                  Result  = Auto_Clock_Do(Result);
  387.  369                  
  388.  370                  if (ERROR_SUCCEED != (Result & 0x80))
  389.  371                  {
  390.  372                      if (stMUD.CLOCK != Curr_Clock)
  391.  373                      {   
  392.  374                          // Fail to find out suitable clock. Restore original clock and H position.
  393.  375                          stMUD.CLOCK         = Curr_Clock;
  394.  376                          stMUD.H_POSITION    = Curr_PosH;
  395.  377          
  396.  378                          Set_Clock();
  397.  379                          Set_H_Position();
  398.  380                      }
  399.  381                  }
  400.  382                  else
  401.  383                  {
  402.  384                      if (stMUD.CLOCK != Curr_Clock)
  403.  385                      {
  404.  386                          stMUD.H_POSITION    = usH_Start + 128 + 64 - usIPH_ACT_STA - (stMUD.CLOCK >> 1);
  405.  387          
  406.  388                          if (ucH_Max_Margin < stMUD.H_POSITION)
  407.  389                              stMUD.H_POSITION    = ucH_Max_Margin;
  408.  390                          else if (ucH_Min_Margin > stMUD.H_POSITION)
  409.  391                              stMUD.H_POSITION    = ucH_Min_Margin;
  410.  392                                          
  411.  393                          Set_H_Position();
  412.  394          
  413.  395                          Save_MUD(ucMode_Curr);
  414.  396                      }
  415.  397                  }
  416.  398              }
  417.  399          
  418.  400              // Restore ADC Gain/Offset
  419.  401              SetADC_GainOffset();
  420.  402              
  421.  403              // Restore ADC phase 
  422.  404              stMUD.PHASE = Curr_Phase;
  423.  405              Set_Phase(stMUD.PHASE);
  424.  406          
  425.  407              // Restore vertical position
  426.  408              if (Curr_PosV != stMUD.V_POSITION)
  427.  409              {
  428.  410                  stMUD.V_POSITION    = Curr_PosV;
  429.  411                  Set_V_Position();
  430.  412              }
  431.  413          
  432.  414              bAutoInProgress = 0;
  433.  415          
  434.  416              return Result;
  435.  417          }
  436.  418          */
  437.  419          void Read_Auto_Info(unsigned char index)
  438.  420          {
  439. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 8   
  440.  421   1           if(index == 0) return;
  441.  422   1      
  442.  423   1               RTDRead(AUTO_PHASE0_88, 4, Y_INC);
  443.  424   1      
  444.  425   1            Data[index << 2] = Data[3];
  445.  426   1            Data[(index << 2) + 1] = Data[2];
  446.  427   1            Data[(index << 2) + 2] = Data[1];
  447.  428   1            Data[(index << 2) + 3] = Data[0];
  448.  429   1      
  449.  430   1      }
  450.  431          
  451.  432          unsigned char FindColor()
  452.  433          {
  453.  434   1      unsigned long ulTemp0;
  454.  435   1      unsigned char ucDetect,ucResult,ucPhase;
  455.  436   1      
  456.  437   1      RTDSetByte(DIFF_THRED_7E, 0x28);
  457.  438   1      
  458.  439   1          ulTemp0     = 0;
  459.  440   1          ucDetect    = 0x77;
  460.  441   1          do
  461.  442   1          {
  462.  443   2              ucResult    = COLORS_BLUE;
  463.  444   2              ucPhase     = COLORS_BLUE;
  464.  445   2              do
  465.  446   2              {
  466.  447   3                  RTDSetByte(MARGIN_B_7D, ucPhase);
  467.  448   3                  RTDSetByte(AUTO_ADJ_CTRL_7F, ucDetect);
  468.  449   3      
  469.  450   3                  Wait_Finish();
  470.  451   3                  if (ERROR_SUCCEED != Data[0])   return Data[0];
  471.  452   3      
  472.  453   3                  Read_Auto_Info(1);
  473.  454   3      
  474.  455   3                  if (ulTemp0 < ((unsigned long *)Data)[1])
  475.  456   3                  {
  476.  457   4                      ulTemp0     = ((unsigned long *)Data)[1];
  477.  458   4                      ucResult    = ucPhase;
  478.  459   4      
  479.  460   4                      if (0x8000 < ulTemp0)   break;
  480.  461   4                  }
  481.  462   3      
  482.  463   3                  if (COLORS_BLUE == ucPhase)
  483.  464   3                      ucPhase = COLORS_GREEN;
  484.  465   3                  else if (COLORS_GREEN == ucPhase)
  485.  466   3                      ucPhase = COLORS_RED;
  486.  467   3                  else
  487.  468   3                      break;
  488.  469   3              }
  489.  470   2              while (1);
  490.  471   2      
  491.  472   2              if (0 != ulTemp0 || 0x7b != ucDetect)   break;
  492.  473   2      
  493.  474   2              ucDetect    = 0x77;
  494.  475   2          }
  495.  476   1          while (1);
  496.  477   1      
  497.  478   1          return ERROR_SUCCEED;
  498.  479   1      }
  499.  480          unsigned long GetMaxSum(unsigned char select)
  500.  481          {
  501.  482   1         unsigned char ucPhase,ucDetect;
  502. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 9   
  503.  483   1         unsigned long ulTemp0;
  504.  484   1         ucPhase = 0; //0,8,16,24
  505.  485   1         ulTemp0 = 0;
  506.  486   1         Set_Phase(ucPhase);
  507.  487   1         ucDetect    = (select == 0) ? 0x77 : 0x7b;
  508.  488   1         
  509.  489   1      #if(HARDWARE_AUTO)
  510.  490   1      
  511.  491   1         RTDSetByte(HW_AUTO_PHASE_9E,0x07);  //Step 8 auto phase
  512.  492   1         Wait_For_IVS();
  513.  493   1         //Wait_For_Event(EVENT_IVS);
  514.  494   1         RTDSetByte(AUTO_ADJ_CTRL_7F, ucDetect);  //Auto start
  515.  495   1         //Wait_For_Event(EVENT_IVS);
  516.  496   1         Wait_For_IVS();
  517.  497   1         for(ucPhase = 0;ucPhase < 4; ucPhase++)
  518.  498   1         {
  519.  499   2             //Wait_For_Event(EVENT_IVS);
  520.  500   2                 Wait_For_IVS();
  521.  501   2             Read_Auto_Info(1);
  522.  502   2      
  523.  503   2             if(ulTemp0 < ((unsigned long *)Data)[1])
  524.  504   2             {
  525.  505   3               ulTemp0   = ((unsigned long *)Data)[1] & 0xffffff00;
  526.  506   3             }
  527.  507   2         }
  528.  508   1         RTDSetByte(HW_AUTO_PHASE_9E,0x00);  //Switch back to software auto phase
  529.  509   1         Wait_Finish();
  530.  510   1         if(ERROR_SUCCEED != Data[0])     return (Data[0] & 0x000000ff);
  531.  511   1         
  532.  512   1      #else
  533.                  while(1)
  534.                  {
  535.                     RTDSetByte(AUTO_ADJ_CTRL_7F, ucDetect);
  536.                     Wait_Finish();
  537.                     if (ERROR_SUCCEED != Data[0])   return Data[0];
  538.               
  539.                     Read_Auto_Info(1);
  540.               
  541.                          if (ulTemp0 < ((unsigned long *)Data)[1])
  542.                      {
  543.                               ulTemp0     = ((unsigned long *)Data)[1];
  544.                      }
  545.                          ucPhase += 0x20;
  546.                          if(ucPhase > 0x60)
  547.                               break;
  548.               
  549.                          Set_Phase(ucPhase);
  550.                       }
  551.               #endif
  552.  532   1          return ulTemp0;
  553.  533   1      }
  554.  534          //------------------------------------------------------------------//
  555.  535          // Return Message => ERROR_SUCCESS   : Success                      //
  556.  536          //                   ERROR_TOO_SMALL : Measure Result << ACT_WIDTH  //
  557.  537          //                   ERROR_TOO_BIG   : Measure Result >> ACT_WIDTH  //
  558.  538          //                   ERROR_INPUT     : 1. IVS or IHS changed        //
  559.  539          //                                     2. underflow or overflow     //
  560.  540          //                   ERROR_TIMEOUT   : Measure Time_Out             //
  561.  541          //                                     Process Time_Out             //
  562.  542          //                   ERROR_NOTACTIVE : No Avtive Image              //
  563.  543          //------------------------------------------------------------------//
  564.  544          unsigned char Auto_Clock_Do(unsigned char NM)
  565. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 10  
  566.  545          {
  567.  546   1      #if(0)
  568.               
  569.                   unsigned char   ucResult;
  570.                   unsigned char   count, delta, stop;    
  571.                       
  572.                   ///////////////////////////////
  573.                   //  Measure (V) Start & End  //
  574.                   ///////////////////////////////
  575.                   ucResult    = Measure_PositionV(NM);
  576.               
  577.                   if (ERROR_SUCCEED != (ucResult & 0x80))
  578.                   {
  579.                       if (ERROR_NOTACTIVE == ucResult)
  580.                       {
  581.                           if (0x80 < stMUD.CLOCK)
  582.                           {
  583.                               stMUD.CLOCK = 0x80;
  584.                               Set_H_Position();
  585.                               Set_Clock();
  586.                           }
  587.                           else
  588.                           {
  589.                               stMUD.CLOCK = 0x80;
  590.                               Set_Clock();
  591.                               Set_H_Position();
  592.                           }
  593.               
  594.                           ucResult    = Measure_PositionV(NM);
  595.                           
  596.                           if (ERROR_SUCCEED != (ucResult & 0x80))     return ucResult;
  597.                       }
  598.                       else
  599.                           return ucResult;
  600.                   }
  601.               
  602.                   NM      = NM + 0x10;    // See Min_Noise_Margin(). Horizontal Measure Result is the same when applying
  603.              - (NM + 0x10)
  604.               
  605.                   count   = 10;
  606.                   do
  607.                   {
  608.                       ///////////////////////////////
  609.                       //  Measure (H) Start & End  //
  610.                       ///////////////////////////////
  611.                       ucResult    = Measure_PositionH(NM);
  612.               
  613.                       if (ERROR_SUCCEED != (ucResult & 0x80))     return ucResult;
  614.                       
  615.                       usH_End = usH_End + 1 - usH_Start;
  616.                         
  617.                       // H_Active Delta
  618.                       if (usH_End < usIPH_ACT_WID)
  619.                           delta = (usIPH_ACT_WID - usH_End > 0x00ff) ? 0xff : (unsigned char)(usIPH_ACT_WID - usH_End);
  620.                       else 
  621.                           delta = (usH_End - usIPH_ACT_WID > 0x00ff) ? 0xff : (unsigned char)(usH_End - usIPH_ACT_WID);
  622.               
  623.                       if (0xc8 < delta)       // The difference is too large to fine-tune.
  624.                       {
  625.                           if (10 == count)
  626.                           {
  627.                               if (0x80 < stMUD.CLOCK)
  628. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 11  
  629.                               {
  630.                                   stMUD.CLOCK = 0x80;
  631.                                   Set_H_Position();
  632.                                   Set_Clock();
  633.                               }
  634.                               else
  635.                               {
  636.                                   stMUD.CLOCK = 0x80;
  637.                                   Set_Clock();
  638.                                   Set_H_Position();
  639.                               }
  640.               
  641.                               continue;
  642.                           }
  643.                           else
  644.                               return (usH_End < usIPH_ACT_WID) ? ERROR_TOO_SMALL : ERROR_TOO_BIG;
  645.                       }
  646.                       
  647.                       if (2 >= delta)     break;  // 1023,1024,1025,1026,1027
  648.                       
  649.                       delta   = (delta + (delta >> 2) + 2) & 0xfc;  // 4n number
  650.               #if(ALIGN_LEFT == CLOCK_ALIGN)
  651.                       // Adjust Clock
  652.                       if (usH_End < usIPH_ACT_WID)    // delta < 0, Measure < Active
  653.                       {
  654.                           if ((178 - stMUD.CLOCK) < delta)    return ERROR_TOO_SMALL;
  655.                   
  656.                           stMUD.CLOCK  += delta;
  657.                               
  658.                           Set_Clock();
  659.                           Set_H_Position();
  660.                       }
  661.                       else                            // delta >= 0, Measure >= Active
  662.                       {
  663.                           if ((stMUD.CLOCK - 78) < delta)     return ERROR_TOO_BIG;
  664.                   
  665.                           stMUD.CLOCK -= delta;
  666.                               
  667.                           Set_H_Position();
  668.                           Set_Clock();
  669.                       }   
  670.               
  671.               #else
  672.                       // Adjust Clock
  673.                       if (usH_End < usIPH_ACT_WID)    // delta < 0, Measure < Active
  674.                       {
  675.                           if ((228 - stMUD.CLOCK) < delta)    return ERROR_TOO_SMALL;
  676.                   
  677.                           stMUD.CLOCK += delta;
  678.                               
  679.                           Set_Clock();
  680.                           Set_H_Position();
  681.                       }
  682.                       else                            // delta >= 0, Measure >= Active
  683.                       {
  684.                           if ((stMUD.CLOCK - 28) < delta)     return ERROR_TOO_BIG;
  685.                   
  686.                           stMUD.CLOCK -= delta;
  687.                               
  688.                           Set_H_Position();
  689.                           Set_Clock();
  690.                       }   
  691. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 12  
  692.               #endif
  693.                   }
  694.                   while (--count);
  695.               
  696.                   if (0 == count)  return ERROR_TIMEOUT;
  697.               
  698.                   stop    = 0;
  699.               
  700.                   while (1)
  701.                   {
  702.                       count   = 0x10;     // Phase 4 ~ 28 step 8 (4,12,20,28)
  703.                       delta   = 0x00;
  704.               
  705.                       while (1)
  706.                       {
  707.                           Set_Phase(count);
  708.                           
  709.                           // Measure usH_Start & usH_End
  710.                           ucResult    = Measure_PositionH(NM);
  711.               
  712.                           if (ERROR_SUCCEED != (ucResult & 0x80))
  713.                           {
  714.                               if (ERROR_NOTACTIVE == ucResult)
  715.                               {
  716.                                   // Input pattern is black/white vertical lines.
  717.                                   if (0x70 == count)
  718.                                   {
  719.                                       Set_Phase(stMUD.PHASE); // Restore phase
  720.                                       break;
  721.                                   }
  722.                                   else
  723.                                   {
  724.                                       count += 0x20;
  725.                                       continue;
  726.                                   }
  727.                               }
  728.               
  729.                               Set_Phase(stMUD.PHASE); // Restore phase
  730.               
  731.                               return ucResult;
  732.                           }
  733.                           
  734.                           usH_End     = usH_End + 1 - usH_Start;
  735.               
  736.                           ucResult    = (usH_End < usIPH_ACT_WID)
  737.                                   ? 0x80 - (unsigned char)(usIPH_ACT_WID - usH_End)
  738.                                   : 0x80 + (unsigned char)(usH_End - usIPH_ACT_WID);
  739.                           
  740.                           if (ucResult > delta)
  741.                           {
  742.                               delta   = ucResult;     // Save the biggest width
  743.                           }
  744.               
  745.                           if (0x70 == count)
  746.                           {
  747.                               Set_Phase(stMUD.PHASE); // Restore phase
  748.                               break;
  749.                           }
  750.               
  751.                           count += 0x20;
  752.                       }
  753.               
  754. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 13  
  755.                       if (0x82 < delta)
  756.                       {
  757.                           stMUD.CLOCK -= 4;
  758.               
  759.                           Set_H_Position();
  760.                           Set_Clock();
  761.               
  762.                           stop    = 1;
  763.                       }
  764.                       else if (0x80 > delta)
  765.                       {
  766.                           if (stop && (0x7f == delta))    break; 
  767.               
  768.                           stMUD.CLOCK += 4;
  769.               
  770.                           Set_Clock();
  771.                           Set_H_Position();
  772.               
  773.                           delta   += 3;
  774.               
  775.                           if (stop)   break;
  776.                       }
  777.                       else    
  778.                           break;
  779.                   }
  780.               
  781.               #if (MORE_CLOCK)
  782.                   if (0x84 > delta && 0x7c < delta)
  783.                   {
  784.                       unsigned long   ulTemp0, ulMaxVal;
  785.                       unsigned char   ucPhase, ucDetect;
  786.               
  787.                       if (ERROR_SUCCEED != Measure_PositionN(NM))    return ERROR_ABORT;
  788.               
  789.                       // Set auto-tracking window
  790.                       Data[0]     = 6;
  791.                       Data[1]     = Y_INC;
  792.                       Data[2]     = H_BND_STA_L_75;
  793.                       Data[3]     = (unsigned char)(usH_Start + MEAS_H_STA_OFFSET - 2);
  794.                       Data[4]     = (unsigned char)(usH_End + MEAS_H_END_OFFSET + 1);
  795.                       Data[5]     = ((unsigned char)((usH_Start + MEAS_H_STA_OFFSET - 2) >> 4) & 0x70) | ((unsigned char
  796.              -)((usH_End + MEAS_H_END_OFFSET + 1) >> 8) & 0x0f);
  797.                       Data[6]     = 0;
  798.                       RTDWrite(Data);
  799.               
  800.                       RTDSetByte(DIFF_THRED_7E, 0x50);
  801.                       
  802.                       ulTemp0     = 0;
  803.                       ucDetect    = 0x7b;
  804.                       ucResult    = COLORS_GREEN;
  805.                       ucPhase     = COLORS_GREEN;
  806.                       do
  807.                       {
  808.                           RTDSetByte(MARGIN_B_7D, ucPhase);
  809.                           RTDSetByte(AUTO_ADJ_CTRL_7F, ucDetect);
  810.               
  811.                           Wait_Finish();
  812.                           if (ERROR_SUCCEED != Data[0])   return Data[0];
  813.               
  814.                           Read_Auto_Info(1);
  815.               
  816.                           if (ulTemp0 < ((unsigned long *)Data)[1])
  817. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 14  
  818.                           {
  819.                               ulTemp0     = ((unsigned long *)Data)[1];
  820.                               ucResult    = ucPhase;
  821.               
  822.                               if (0x10000 < ulTemp0)   break;
  823.                           }
  824.               
  825.                           if (COLORS_GREEN == ucPhase)
  826.                               ucPhase = COLORS_BLUE;
  827.                           else if (COLORS_BLUE == ucPhase)
  828.                               ucPhase = COLORS_RED;
  829.                           else
  830.                           {
  831.                               if (0x10000 <= ulTemp0 || 0x77 == ucDetect)      break;
  832.               
  833.                               ulTemp0     = 0;
  834.                               ucDetect    = 0x77;
  835.                               ucPhase     = COLORS_GREEN;
  836.                               ucResult    = COLORS_GREEN;
  837.               
  838.                               RTDSetByte(DIFF_THRED_7E, 0x70);
  839.                           }
  840.                       }
  841.                       while (1);
  842.                       
  843.                       // Abort if no suitable color is found
  844.                       if (0x8000 > ulTemp0)
  845.                       {
  846.                           return (28 > stMUD.CLOCK) ? ERROR_TOO_BIG : (228 < stMUD.CLOCK) ? ERROR_TOO_SMALL : ERROR_SUCC
  847.              -EED;
  848.                       }
  849.               
  850.                       // Save 4N clock
  851.                       stop    = stMUD.CLOCK;
  852.               
  853.                       if (0x81 > delta)
  854.                       {
  855.                           stMUD.CLOCK = stMUD.CLOCK + 4;
  856.                           Set_Clock();
  857.                           Set_H_Position();
  858.                       }
  859.               
  860.                       ulMaxVal    = 0;
  861.                       delta       = 4;
  862.                       do
  863.                       {
  864.                               
  865.                           if (ERROR_SUCCEED != Measure_PositionN(NM))    return ERROR_ABORT;
  866.               
  867.                           // Set auto-tracking window
  868.                           Data[0] = 6;
  869.                           Data[1] = Y_INC;
  870.                           Data[2] = H_BND_STA_L_75;
  871.                           Data[3] = (unsigned char)(usH_Start + MEAS_H_STA_OFFSET - 2);
  872.                           Data[4] = (unsigned char)(usH_End + MEAS_H_END_OFFSET + 1);
  873.                           Data[5] = ((unsigned char)((usH_Start + MEAS_H_STA_OFFSET - 2) >> 4) & 0x70) | ((unsigned char
  874.              -)((usH_End + MEAS_H_END_OFFSET + 1) >> 8) & 0x0f);
  875.                           Data[6] = 0;
  876.                           RTDWrite(Data);
  877.                           
  878.                           // Select color for auto-phase tracking
  879.                           RTDSetByte(MARGIN_B_7D, ucResult);
  880. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 15  
  881.               
  882.                           // Set threshold
  883.                           RTDSetByte(DIFF_THRED_7E, (0x7b == ucDetect) ? 0x50 : 0x70);
  884.               
  885.                           ulTemp0 = 0;
  886.                           ucPhase = 0x00;
  887.                           do
  888.                           {
  889.                               Set_Phase(ucPhase);
  890.               
  891.                               RTDSetByte(AUTO_ADJ_CTRL_7F, ucDetect);
  892.               
  893.                               Wait_Finish();
  894.                               if (ERROR_SUCCEED != Data[0])   return Data[0];
  895.               
  896.                                Read_Auto_Info(1);
  897.                               if (ulTemp0 < ((unsigned long *)Data)[1])   ulTemp0 = ((unsigned long *)Data)[1];
  898.               
  899.                               ucPhase += 0x10;
  900.                           }
  901.                           while (0x80 > ucPhase);
  902.               
  903.                           if (ulMaxVal < ulTemp0)
  904.                           {
  905.                               ulMaxVal    = ulTemp0;
  906.                               count       = stMUD.CLOCK;
  907.                           }
  908.               
  909.                           if (0x00 == delta)
  910.                           {
  911.                               if (128 < stMUD.CLOCK)
  912.                               {
  913.                                   // See if default clock is the best
  914.                                   delta       = 0xff;
  915.                                   stMUD.CLOCK = 128;
  916.                                   Set_H_Position();
  917.                                   Set_Clock();
  918.                                   
  919.                                   continue;
  920.                               }
  921.                               else
  922.                                   break;
  923.                           }
  924.                           else if (0xff == delta)
  925.                           {
  926.                               break;
  927.                           }
  928.               
  929.                           delta       -= 1;
  930.                           stMUD.CLOCK -= 1;
  931.                           Set_H_Position();
  932.                           Set_Clock();
  933.                       }
  934.                       while (1); 
  935.               
  936.                       if (0x7b == ucDetect)
  937.                           stMUD.CLOCK = ((unsigned long)0x38000 < ulMaxVal) ? count : stop;
  938.                       else
  939.                           stMUD.CLOCK = ((unsigned long)0x48000 < ulMaxVal) ? count : stop;
  940.               
  941.                       Set_Clock();
  942.                       Set_H_Position();
  943. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 16  
  944.                       Set_Clock();
  945.                   }
  946.               #endif
  947.               
  948.               #else
  949.  918   1          unsigned char   Result;
  950.  919   1          unsigned char   count, delta, stop,start;    
  951.  920   1          unsigned long   ulSum,ulCompare;
  952.  921   1              ulCompare = 0;
  953.  922   1              ulSum = 0;
  954.  923   1              
  955.  924   1          ///////////////////////////////
  956.  925   1          //  Measure (V) Start & End  //
  957.  926   1          ///////////////////////////////
  958.  927   1          Result  = Measure_PositionV(NM);
  959.  928   1      
  960.  929   1          if (ERROR_SUCCEED != (Result & 0x80))   return Result;
  961.  930   1      
  962.  931   1          NM      = NM + 0x10;    // See Min_Noise_Margin(). Horizontal Measure Result is the same when applying
  963.              - (NM + 0x10)
  964.  932   1      
  965.  933   1          count   = 10;
  966.  934   1          do
  967.  935   1          {
  968.  936   2                 
  969.  937   2              ///////////////////////////////
  970.  938   2              //  Measure (H) Start & End  //
  971.  939   2              ///////////////////////////////
  972.  940   2              Result  = Measure_PositionH(NM);
  973.  941   2      
  974.  942   2              if (ERROR_SUCCEED != (Result & 0x80))    return Result;
  975.  943   2              
  976.  944   2              usH_End = usH_End + 1 - usH_Start;
  977.  945   2                
  978.  946   2              // H_Active Delta
  979.  947   2              if (usH_End < usIPH_ACT_WID)
  980.  948   2                  delta = (usIPH_ACT_WID - usH_End > 0x00ff) ? 0xff : (unsigned char)(usIPH_ACT_WID - usH_End);
  981.  949   2              else 
  982.  950   2                  delta = (usH_End - usIPH_ACT_WID > 0x00ff) ? 0xff : (unsigned char)(usH_End - usIPH_ACT_WID);
  983.  951   2      
  984.  952   2              //if (0xc0 < delta)       // The difference is too large to fine-tune.
  985.  953   2              if((usIPH_ACT_WID/3) < delta)  //modified 2003/02/25
  986.  954   2              {
  987.  955   3                  return (usH_End < usIPH_ACT_WID) ? ERROR_TOO_SMALL : ERROR_TOO_BIG;
  988.  956   3              }
  989.  957   2              
  990.  958   2              if (1 >= delta)     break;  // 1023,1024,1025,1026,1027
  991.  959   2              
  992.  960   2              delta   = delta + (delta >> 2);//& 0xfe;  // 4n number
  993.  961   2      
  994.  962   2      #if(ALIGN_LEFT == CLOCK_ALIGN)
  995.  963   2              // Adjust Clock
  996.  964   2              if (usH_End < usIPH_ACT_WID)    // delta < 0, Measure < Active
  997.  965   2              {
  998.  966   3                  if ((178 - stMUD.CLOCK) < delta)    return ERROR_TOO_SMALL;
  999.  967   3          
  1000.  968   3                  stMUD.CLOCK  += delta;
  1001.  969   3                      
  1002.  970   3                  Set_Clock();
  1003.  971   3                  Set_H_Position();
  1004.  972   3              }
  1005.  973   2              else                            // delta >= 0, Measure >= Active
  1006. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 17  
  1007.  974   2              {
  1008.  975   3                  if ((stMUD.CLOCK - 78) < delta)     return ERROR_TOO_BIG;
  1009.  976   3          
  1010.  977   3                  stMUD.CLOCK -= delta;
  1011.  978   3                      
  1012.  979   3                  Set_H_Position();
  1013.  980   3                  Set_Clock();
  1014.  981   3              }   
  1015.  982   2      
  1016.  983   2      #else
  1017.                       // Adjust Clock
  1018.                       if (usH_End < usIPH_ACT_WID)    // delta < 0, Measure < Active
  1019.                       {
  1020.                           if ((228 - stMUD.CLOCK) < delta)    return ERROR_TOO_SMALL;
  1021.                   
  1022.                           stMUD.CLOCK  += delta;
  1023.                               
  1024.                           Set_Clock();
  1025.                           Set_H_Position();
  1026.                       }
  1027.                       else                            // delta >= 0, Measure >= Active
  1028.                       {
  1029.                           if ((stMUD.CLOCK - 28) < delta)     return ERROR_TOO_BIG;
  1030.                   
  1031.                           stMUD.CLOCK -= delta;
  1032.                               
  1033.                           Set_H_Position();
  1034.                           Set_Clock();
  1035.                       }   
  1036.                   
  1037.               #endif
  1038. 1005   2          }
  1039. 1006   1          while (--count);
  1040. 1007   1      
  1041. 1008   1              
  1042. 1009   1      
  1043. 1010   1          if (0 == count)  return ERROR_TIMEOUT;
  1044. 1011   1      
  1045. 1012   1          stop    = 0;
  1046. 1013   1      
  1047. 1014   1          while (1)
  1048. 1015   1          {
  1049. 1016   2              count   = 0x10;     // Phase 4 ~ 28 step 4 (4,8,12,16,20,24,28)
  1050. 1017   2              delta   = 0xff;
  1051. 1018   2      
  1052. 1019   2              while (1)
  1053. 1020   2              {
  1054. 1021   3                        
  1055. 1022   3      
  1056. 1023   3                  Set_Phase(count);
  1057. 1024   3                  
  1058. 1025   3                  // Measure usH_Start & usH_End
  1059. 1026   3                  Result  = Measure_PositionH(NM);
  1060. 1027   3      
  1061. 1028   3                  if (ERROR_SUCCEED != (Result & 0x80))
  1062. 1029   3                  {
  1063. 1030   4                      if (ERROR_NOTACTIVE == Result)
  1064. 1031   4                      {
  1065. 1032   5                          // Input pattern is black/white vertical lines.
  1066. 1033   5                          if (0x70 == count)
  1067. 1034   5                          {
  1068. 1035   6                              Set_Phase(stMUD.PHASE); // Restore phase
  1069. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 18  
  1070. 1036   6                              break;
  1071. 1037   6                          }
  1072. 1038   5                          else
  1073. 1039   5                          {
  1074. 1040   6                              count += 0x20;
  1075. 1041   6                              continue;
  1076. 1042   6                          }
  1077. 1043   5                      }
  1078. 1044   4      
  1079. 1045   4                      Set_Phase(stMUD.PHASE); // Restore phase
  1080. 1046   4      
  1081. 1047   4                      return Result;
  1082. 1048   4                  }
  1083. 1049   3                  
  1084. 1050   3                  usH_End = usH_End + 1 - usH_Start;
  1085. 1051   3      
  1086. 1052   3                  Result  = (usH_End < usIPH_ACT_WID)
  1087. 1053   3                          ? 0x80 - (unsigned char)(usIPH_ACT_WID - usH_End)
  1088. 1054   3                          : 0x80 + (unsigned char)(usH_End - usIPH_ACT_WID);
  1089. 1055   3                  
  1090. 1056   3                  if (Result < delta)
  1091. 1057   3                  {
  1092. 1058   4                      delta   = Result;       // Save the smallest width
  1093. 1059   4                  }
  1094. 1060   3      
  1095. 1061   3                  if (0x70 == count)
  1096. 1062   3                  {
  1097. 1063   4                      Set_Phase(stMUD.PHASE); // Restore phase
  1098. 1064   4                      break;
  1099. 1065   4                  }
  1100. 1066   3      
  1101. 1067   3                  count += 0x10;
  1102. 1068   3              }
  1103. 1069   2              
  1104. 1070   2              if (0x81 < delta)
  1105. 1071   2              {
  1106. 1072   3                  stMUD.CLOCK -= 1;
  1107. 1073   3      
  1108. 1074   3                  Set_H_Position();
  1109. 1075   3                  Set_Clock();
  1110. 1076   3      
  1111. 1077   3                  stop    = 1;
  1112. 1078   3              }
  1113. 1079   2              else if (0x80 > delta)
  1114. 1080   2              {
  1115. 1081   3                  if (stop && (0x7f == delta))    break; 
  1116. 1082   3      
  1117. 1083   3                  stMUD.CLOCK += 1;
  1118. 1084   3      
  1119. 1085   3                  Set_Clock();
  1120. 1086   3                  Set_H_Position();
  1121. 1087   3      
  1122. 1088   3                  if (stop)   break;
  1123. 1089   3              }
  1124. 1090   2              else    
  1125. 1091   2                  break;
  1126. 1092   2          }
  1127. 1093   1          
  1128. 1094   1          count = stMUD.PHASE;  // Record Current Phase
  1129. 1095   1          start = stMUD.CLOCK ;
  1130. 1096   1              
  1131. 1097   1              
  1132. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 19  
  1133. 1098   1              if(FindColor() != ERROR_SUCCEED) return ERROR_ABORT;
  1134. 1099   1      // Set threshold
  1135. 1100   1          RTDSetByte(DIFF_THRED_7E, 0x30);
  1136. 1101   1      
  1137. 1102   1              ulSum = GetMaxSum(1); //judge if pulse information large enough
  1138. 1103   1          ulCompare = GetMaxSum(0);
  1139. 1104   1      
  1140. 1105   1              
  1141. 1106   1      
  1142. 1107   1              if((ulSum > 460000) || ((ulSum < 460000) && (ulCompare > 2000000)) )
  1143. 1108   1              {
  1144. 1109   2                      
  1145. 1110   2                      ulCompare = 0;    
  1146. 1111   2                      //    ulSum = 0;//GetMaxSum(0);
  1147. 1112   2                      //////////////////////////////////////////////
  1148. 1113   2                      if(0x80 < (start - 2) || 0x80 > start)
  1149. 1114   2                      {
  1150. 1115   3                              stMUD.CLOCK = 0x80;
  1151. 1116   3                              Set_H_Position();
  1152. 1117   3                              Set_Clock();
  1153. 1118   3                              ulSum = GetMaxSum(0);
  1154. 1119   3                              
  1155. 1120   3                              if(ulCompare < ulSum)
  1156. 1121   3                              {
  1157. 1122   4                                      ulCompare = ulSum;
  1158. 1123   4                                      Result = stMUD.CLOCK;
  1159. 1124   4                              }
  1160. 1125   3      
  1161. 1126   3                              stMUD.CLOCK = start + 1;
  1162. 1127   3                      }
  1163. 1128   2                      else
  1164. 1129   2                      {
  1165. 1130   3                              stMUD.CLOCK = start;
  1166. 1131   3                              Set_H_Position();
  1167. 1132   3                              Set_Clock();
  1168. 1133   3                              ulSum = GetMaxSum(0);
  1169. 1134   3                      }
  1170. 1135   2                      ////////////////////////////////////////////////
  1171. 1136   2              
  1172. 1137   2      
  1173. 1138   2                      while(1)
  1174. 1139   2                      {
  1175. 1140   3                              if(ulCompare < ulSum)
  1176. 1141   3                              {
  1177. 1142   4                                      ulCompare = ulSum;
  1178. 1143   4                                      Result = stMUD.CLOCK;
  1179. 1144   4                              }
  1180. 1145   3                              if(stMUD.CLOCK == start - 2)
  1181. 1146   3                                      break;    
  1182. 1147   3      
  1183. 1148   3                              stMUD.CLOCK -= 1;
  1184. 1149   3                              Set_Clock();
  1185. 1150   3                              Set_H_Position();
  1186. 1151   3                              ulSum = GetMaxSum(0);        
  1187. 1152   3                      }
  1188. 1153   2      
  1189. 1154   2                      stMUD.CLOCK = Result;
  1190. 1155   2                      stMUD.PHASE = count;
  1191. 1156   2                      Set_Clock();
  1192. 1157   2                      Set_H_Position();
  1193. 1158   2                      Set_Phase(stMUD.PHASE);
  1194. 1159   2          }  
  1195. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 20  
  1196. 1160   1      
  1197. 1161   1      
  1198. 1162   1      
  1199. 1163   1      #endif
  1200. 1164   1          return (28 > stMUD.CLOCK) ? ERROR_TOO_BIG : (228 < stMUD.CLOCK) ? ERROR_TOO_SMALL : ERROR_SUCCEED;
  1201. 1165   1      }
  1202. 1166          
  1203. 1167          //------------------------------------------------------------------//
  1204. 1168          //                          Auto Position                           //
  1205. 1169          //------------------------------------------------------------------//
  1206. 1170          unsigned char Auto_Position(void)
  1207. 1171          {
  1208. 1172   1          unsigned char   Result, Curr_PosH, Curr_PosV;
  1209. 1173   1      
  1210. 1174   1          bAutoInProgress = 1;
  1211. 1175   1      
  1212. 1176   1          Curr_PosH   = stMUD.H_POSITION;     // Save current stMUD.H_POSITION
  1213. 1177   1          Curr_PosV   = stMUD.V_POSITION;     // Save current stMUD.V_POSITION
  1214. 1178   1      
  1215. 1179   1          if (ucV_Max_Margin < stMUD.V_POSITION)
  1216. 1180   1          {
  1217. 1181   2              stMUD.V_POSITION    = ucV_Max_Margin;
  1218. 1182   2              Set_V_Position();
  1219. 1183   2          }
  1220. 1184   1      
  1221. 1185   1          RTDCodeW(ADC_DEFAULT);
  1222. 1186   1      
  1223. 1187   1          ///////////////////////////////
  1224. 1188   1          //   Measure  NOISE_MARGIN   //
  1225. 1189   1          ///////////////////////////////
  1226. 1190   1          Result  = Min_Noise_Margin();
  1227. 1191   1      
  1228. 1192   1          if (ERROR_SUCCEED == (Result & 0x80))
  1229. 1193   1          {   
  1230. 1194   2              ///////////////////////////////
  1231. 1195   2              //    Adjust (H/V)Position   //
  1232. 1196   2              ///////////////////////////////
  1233. 1197   2              Result  = Auto_Position_Do(Data[0]);    // Noise margin returned by Min_Noise_Margin() is saved in
  1234.              - Data[0];
  1235. 1198   2          }
  1236. 1199   1      
  1237. 1200   1          if (ERROR_SUCCEED == (Result & 0x80))
  1238. 1201   1          {
  1239. 1202   2              Save_MUD(ucMode_Curr);
  1240. 1203   2          }
  1241. 1204   1          else
  1242. 1205   1          {
  1243. 1206   2              stMUD.H_POSITION    = Curr_PosH;
  1244. 1207   2              stMUD.V_POSITION    = Curr_PosV;
  1245. 1208   2      
  1246. 1209   2              Set_H_Position();
  1247. 1210   2              Set_V_Position();
  1248. 1211   2          }
  1249. 1212   1          
  1250. 1213   1          // Restore ADC Gain/Offset
  1251. 1214   1          SetADC_GainOffset();
  1252. 1215   1      
  1253. 1216   1          bAutoInProgress = 0;
  1254. 1217   1      
  1255. 1218   1          return Result;
  1256. 1219   1      }
  1257. 1220          
  1258. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 21  
  1259. 1221          //------------------------------------------------------------------//
  1260. 1222          // Return Message => ERROR_SUCCESS   : Success                      //
  1261. 1223          //                   ERROR_SUCCESS_1 : Vertical Start > Max         //
  1262. 1224          //                   ERROR_SUCCESS_2 : Vertical Start < Min         //
  1263. 1225          //                   ERROR_SUCCESS_4 : Vertical Start/End Fail      //
  1264. 1226          //                   ERROR_SUCCESS_8 : Horizontal Start > Max       //
  1265. 1227          //                   ERROR_SUCCESS_16: Horizontal Start < Min       //
  1266. 1228          //                   ERROR_SUCCESS_32: Horizontal Start/End Fail    //
  1267. 1229          //                   ERROR_INPUT     : 1. IVS or IHS changed        //
  1268. 1230          //                                     2. underflow or overflow     //
  1269. 1231          //                   ERROR_TIMEOUT   : Measure Time_Out             //
  1270. 1232          //                   ERROR_NOTACTIVE : No Avtive Image              //
  1271. 1233          //------------------------------------------------------------------//
  1272. 1234          unsigned char Auto_Position_Do(unsigned char NM)
  1273. 1235          {
  1274. 1236   1          unsigned char   Result;//,ucTemp,ucTemp1;
  1275. 1237   1      //      unsigned int    usTemp;
  1276. 1238   1      
  1277. 1239   1      /*
  1278. 1240   1          
  1279. 1241   1      ///////////////////////////////////////////////////////////
  1280. 1242   1              stMUD.H_POSITION = 128;
  1281. 1243   1          
  1282. 1244   1              if(128 >= stMUD.H_POSITION)
  1283. 1245   1          {
  1284. 1246   1          ucTemp = 128 - stMUD.H_POSITION;
  1285. 1247   1          
  1286. 1248   1              if(stMUD.CLOCK > 128)
  1287. 1249   1                 ucTemp1 = 128;
  1288. 1250   1              else
  1289. 1251   1                 ucTemp1 = stMUD.CLOCK;
  1290. 1252   1      
  1291. 1253   1              ucH_Min_Margin = 128 - ucTemp - (64 - (ucTemp1 >> 1));
  1292. 1254   1              usIPH_ACT_STA = usIPH_ACT_STA + 50 - ucTemp - (64 - (ucTemp1 >> 1));
  1293. 1255   1              usTemp = usIPH_ACT_STA;
  1294. 1256   1      
  1295. 1257   1              Set_H_Position();
  1296. 1258   1          }
  1297. 1259   1      ///////////////////////////////////////////////////////////////////    
  1298. 1260   1      */
  1299. 1261   1          Result      = Measure_PositionN(NM);
  1300. 1262   1      
  1301. 1263   1          if (ERROR_SUCCEED != (Result & 0x80))   return Result;
  1302. 1264   1          Result  = ERROR_SUCCEED;
  1303. 1265   1      
  1304. 1266   1          /////////////////////////////////
  1305. 1267   1          // Calculate Vertical Position //
  1306. 1268   1          /////////////////////////////////
  1307. 1269   1          NM  = 1;
  1308. 1270   1          while (1)
  1309. 1271   1          {
  1310. 1272   2              if ((usIPV_ACT_STA + ucV_Max_Margin - 128) >= usVer_Start)
  1311. 1273   2              {
  1312. 1274   3                  if ((usIPV_ACT_STA + ucV_Min_Margin - 128) <= usVer_Start)
  1313. 1275   3                  {
  1314. 1276   4                      stMUD.V_POSITION = (usVer_Start + 128) - usIPV_ACT_STA;
  1315. 1277   4                      Set_V_Position();
  1316. 1278   4      
  1317. 1279   4                      break;  // Success
  1318. 1280   4                  }
  1319. 1281   3                  else
  1320. 1282   3                      Result  |= ERROR_SUCCESS_2;
  1321. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 22  
  1322. 1283   3              }
  1323. 1284   2              else
  1324. 1285   2                  Result  |= ERROR_SUCCESS_1;
  1325. 1286   2      
  1326. 1287   2              // If we can't align upper bound, we try to align lower bound.
  1327. 1288   2              if (NM && usVer_End > usIPV_ACT_LEN)
  1328. 1289   2              {
  1329. 1290   3                  usVer_Start = usVer_End - usIPV_ACT_LEN + 1;
  1330. 1291   3                  NM          = 0;
  1331. 1292   3              }
  1332. 1293   2              else
  1333. 1294   2              {
  1334. 1295   3                  Result  |= ERROR_SUCCESS_4;
  1335. 1296   3                  break;
  1336. 1297   3              }
  1337. 1298   2          }
  1338. 1299   1          
  1339. 1300   1          ///////////////////////////////////
  1340. 1301   1          // Calculate Horizontal Position //
  1341. 1302   1          ///////////////////////////////////
  1342. 1303   1          NM  = 1;
  1343. 1304   1          while (1)
  1344. 1305   1          {
  1345. 1306   2      #if(ALIGN_LEFT == CLOCK_ALIGN)
  1346. 1307   2      
  1347. 1308   2              if ((usIPH_ACT_STA + (stMUD.CLOCK >> 2) + ucH_Max_Margin - 32 - 128) >= usH_Start)
  1348. 1309   2              {
  1349. 1310   3      
  1350. 1311   3                              //if(ucH_Min_Margin  == (128 - 10))
  1351. 1312   3                  if(ucH_Min_Margin  <= 128 )
  1352. 1313   3                                ((unsigned int*)Data)[0] = usH_Start + 128 - stMUD.H_POSITION + 50;//((unsigned int*)Data)[0] = usH_S
  1353.              -tart + ucTemp + 100;//((unsigned int*)Data)[0] = usH_Start + 10;
  1354. 1314   3                              else
  1355. 1315   3                                ((unsigned int*)Data)[0] = usH_Start;// + (stMUD.H_POSITION - 128) + 10;
  1356. 1316   3      
  1357. 1317   3                  //if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Min_Margin - 64 - 128) <= (usH_Start + 50))
  1358. 1318   3                              if ((usIPH_ACT_STA + (stMUD.CLOCK >> 2) + ucH_Min_Margin - 32 - 128) <= ((unsigned int*)Data)[0])
  1359. 1319   3                  {
  1360. 1320   4                      //usH_Start is the actual distance from Hsync to active image
  1361. 1321   4                          stMUD.H_POSITION    = usH_Start + 128 + 32 - usIPH_ACT_STA - (stMUD.CLOCK >> 2);
  1362. 1322   4                                      Set_H_Position();
  1363. 1323   4                      
  1364. 1324   4                      break;  // Success
  1365. 1325   4                  }
  1366. 1326   3                  else
  1367. 1327   3                      Result  |= ERROR_SUCCESS_16;
  1368. 1328   3              }
  1369. 1329   2              else
  1370. 1330   2                  Result  |= ERROR_SUCCESS_8;
  1371. 1331   2      
  1372. 1332   2      
  1373. 1333   2      #else
  1374.                       if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Max_Margin - 64 - 128) >= usH_Start)
  1375.                       {
  1376.               
  1377.                                       //if(ucH_Min_Margin  == (128 - 10))
  1378.                           if(ucH_Min_Margin  <= 128 )
  1379.                                         ((unsigned int*)Data)[0] = usH_Start + 128 - stMUD.H_POSITION + 100;//((unsigned int*)Data)[0] = usH_
  1380.              -Start + ucTemp + 100;//((unsigned int*)Data)[0] = usH_Start + 10;
  1381.                                       else
  1382.                                         ((unsigned int*)Data)[0] = usH_Start;// + (stMUD.H_POSITION - 128) + 10;
  1383.               
  1384. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 23  
  1385.                           //if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Min_Margin - 64 - 128) <= (usH_Start + 50))
  1386.                                       if ((usIPH_ACT_STA + (stMUD.CLOCK >> 1) + ucH_Min_Margin - 64 - 128) <= ((unsigned int*)Data)[0])
  1387.                           {
  1388.                               //usH_Start is the actual distance from Hsync to active image
  1389.                                   stMUD.H_POSITION    = usH_Start + 128 + 64 - usIPH_ACT_STA - (stMUD.CLOCK >> 1);
  1390.                                               Set_H_Position();
  1391.                               
  1392.                               break;  // Success
  1393.                           }
  1394.                           else
  1395.                               Result  |= ERROR_SUCCESS_16;
  1396.                       }
  1397.                       else
  1398.                           Result  |= ERROR_SUCCESS_8;
  1399.               
  1400.               #endif
  1401. 1359   2              // If we can't align upper bound, we try to align lower bound.
  1402. 1360   2              if (NM && usH_End > usIPH_ACT_WID)
  1403. 1361   2              {
  1404. 1362   3                  usH_Start   = usH_End - usIPH_ACT_WID + 1;
  1405. 1363   3                  NM          = 0;
  1406. 1364   3              }
  1407. 1365   2              else
  1408. 1366   2              {
  1409. 1367   3                  Result  |= ERROR_SUCCESS_32;
  1410. 1368   3                  break;
  1411. 1369   3              }
  1412. 1370   2          }
  1413. 1371   1      /*
  1414. 1372   1      //////////////////////////////////////////////////////////////////////////////
  1415. 1373   1          if(ucH_Min_Margin == (128 - ucTemp))
  1416. 1374   1              {
  1417. 1375   1              if(usTemp != usIPH_ACT_STA)
  1418. 1376   1                      {
  1419. 1377   1                         stMUD.H_POSITION = stMUD.H_POSITION - (usTemp - usIPH_ACT_STA);
  1420. 1378   1                         usIPH_ACT_STA = usTemp - 50 + ucTemp + (64 - (ucTemp1 >> 1));
  1421. 1379   1                      }
  1422. 1380   1                      else
  1423. 1381   1                          usIPH_ACT_STA   = usIPH_ACT_STA - 50 + ucTemp + (64 - (ucTemp1 >> 1));
  1424. 1382   1      
  1425. 1383   1                      ucH_Min_Margin  = 128 - 50;   
  1426. 1384   1                      Set_H_Position();
  1427. 1385   1              }
  1428. 1386   1      ////////////////////////////////////////////////////////////////////////////////
  1429. 1387   1      */
  1430. 1388   1          return Result;
  1431. 1389   1      }
  1432. 1390          
  1433. 1391          unsigned char Min_Noise_Margin(void)
  1434. 1392          {
  1435. 1393   1          unsigned char   Result, Noise;
  1436. 1394   1          unsigned int    Curr_StartH, Curr_EndH;
  1437. 1395   1      
  1438. 1396   1          Result  = Measure_PositionV(VERTICAL_MARGIN);
  1439. 1397   1          
  1440. 1398   1          if (ERROR_SUCCEED != (Result & 0x80))   return Result;
  1441. 1399   1                  
  1442. 1400   1          if (0 == usVer_Start)
  1443. 1401   1          {
  1444. 1402   2              Result  = Measure_PositionV(VERTICAL_MARGIN + 0x20);
  1445. 1403   2              if (ERROR_SUCCEED != (Result & 0x80))   return Result;
  1446. 1404   2          }
  1447. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 24  
  1448. 1405   1          
  1449. 1406   1          Noise   = 0x00;
  1450. 1407   1          Result  = Measure_PositionH(Noise);
  1451. 1408   1          if (ERROR_SUCCEED != (Result & 0x80))   return Result;
  1452. 1409   1              
  1453. 1410   1          Curr_StartH = usH_Start;    // Save H start position at noise margin = 0
  1454. 1411   1          Curr_EndH   = usH_End;      // Save H end position at noise margin = 0
  1455. 1412   1      
  1456. 1413   1          do
  1457. 1414   1          {
  1458. 1415   2              Noise   = Noise + 0x10;
  1459. 1416   2              Result  = Measure_PositionH(Noise);
  1460. 1417   2      
  1461. 1418   2              if (ERROR_SUCCEED != (Result & 0x80))   return Result;
  1462. 1419   2          
  1463. 1420   2              if (Curr_StartH >= usH_Start)
  1464. 1421   2              {
  1465. 1422   3                  Curr_StartH = usH_Start;
  1466. 1423   3              }
  1467. 1424   2              else if (0x08 < (usH_Start - Curr_StartH))
  1468. 1425   2              {
  1469. 1426   3                  break;  // A large gap of H start position is found.
  1470. 1427   3              }
  1471. 1428   2          }
  1472. 1429   1          while (0x90 > Noise);
  1473. 1430   1      
  1474. 1431   1          if (0x80 < Noise)   return ERROR_NOISE_TOO_BIG;      
  1475. 1432   1      
  1476. 1433   1          while (1)
  1477. 1434   1          {   
  1478. 1435   2              Curr_StartH = usH_Start;
  1479. 1436   2              Curr_EndH   = usH_End;
  1480. 1437   2      
  1481. 1438   2              Result  = Measure_PositionH(Noise + 0x28);
  1482. 1439   2              
  1483. 1440   2              if (ERROR_SUCCEED != (Result & 0x80))   return Result;
  1484. 1441   2      
  1485. 1442   2              if ((Curr_EndH - Curr_StartH) == (usH_End - usH_Start) || (Curr_EndH - Curr_StartH) >= (usH_End - 
  1486.              -usH_Start + 3))
  1487. 1443   2              {
  1488. 1444   3                  break;  // We got noise margin with stable horizontal start/end position.
  1489. 1445   3              }
  1490. 1446   2              
  1491. 1447   2              if (0xa0 <= Noise)
  1492. 1448   2              {
  1493. 1449   3                  break;  // No stable horizontal start/end position are found.
  1494. 1450   3              }
  1495. 1451   2      
  1496. 1452   2              Noise   = Noise + 0x10;        
  1497. 1453   2              Result  = Measure_PositionH(Noise);
  1498. 1454   2      
  1499. 1455   2              if (ERROR_SUCCEED != (Result & 0x80))   return Result;
  1500. 1456   2          };
  1501. 1457   1      
  1502. 1458   1          Data[0] = Noise + 0x10;
  1503. 1459   1      
  1504. 1460   1          return ERROR_SUCCEED;
  1505. 1461   1      }
  1506. 1462          
  1507. 1463          unsigned char Auto_Phase(void)
  1508. 1464          {
  1509. 1465   1          unsigned char   Result, Curr_PosV;
  1510. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 25  
  1511. 1466   1      
  1512. 1467   1          bAutoInProgress = 1;
  1513. 1468   1      
  1514. 1469   1          Curr_PosV   = stMUD.V_POSITION;     // Save current stMUD.V_POSITION
  1515. 1470   1      
  1516. 1471   1          if (ucV_Max_Margin < stMUD.V_POSITION)
  1517. 1472   1          {
  1518. 1473   2              stMUD.V_POSITION    = ucV_Max_Margin;
  1519. 1474   2              Set_V_Position();
  1520. 1475   2          }
  1521. 1476   1      
  1522. 1477   1          // Set ADC to default
  1523. 1478   1          RTDCodeW(ADC_DEFAULT);
  1524. 1479   1      
  1525. 1480   1          ///////////////////////////////
  1526. 1481   1          //   Measure  NOISE_MARGIN   //
  1527. 1482   1          ///////////////////////////////
  1528. 1483   1          Result  = Min_Noise_Margin();
  1529. 1484   1      
  1530. 1485   1          if (ERROR_SUCCEED == (Result & 0x80))
  1531. 1486   1          {   
  1532. 1487   2              Result      = Auto_Phase_Do(Data[0]);   // Noise margin returned by Min_Noise_Margin() is saved in
  1533.              - Data[0];
  1534. 1488   2          }
  1535. 1489   1      
  1536. 1490   1          if (ERROR_SUCCEED != (Result & 0x80))
  1537. 1491   1          {
  1538. 1492   2              // Restore Phase
  1539. 1493   2              Set_Phase(stMUD.PHASE);
  1540. 1494   2          }
  1541. 1495   1          else
  1542. 1496   1          {
  1543. 1497   2              Save_MUD(ucMode_Curr);
  1544. 1498   2          }
  1545. 1499   1      
  1546. 1500   1          // Restore ADC Gain/Offset
  1547. 1501   1          SetADC_GainOffset();
  1548. 1502   1      
  1549. 1503   1          // Restore vertical position
  1550. 1504   1          if (Curr_PosV != stMUD.V_POSITION)
  1551. 1505   1          {
  1552. 1506   2              stMUD.V_POSITION    = Curr_PosV;
  1553. 1507   2              Set_V_Position();
  1554. 1508   2          }
  1555. 1509   1      
  1556. 1510   1          bAutoInProgress = 0;
  1557. 1511   1      
  1558. 1512   1          return Result;
  1559. 1513   1      }
  1560. 1514          
  1561. 1515          
  1562. 1516          unsigned char Auto_Phase_Do(unsigned char NM)
  1563. 1517          {
  1564. 1518   1          unsigned char idata ucDetect, ucPhase, ucResult;
  1565. 1519   1          unsigned long idata ulTemp0, ulTemp1, ulTemp2;
  1566. 1520   1      
  1567. 1521   1      /*      
  1568. 1522   1          //reduce the bandwidth of ADC to prevent overshoot
  1569. 1523   1          if(ucMode_Curr <= MODE_1280x1024x75HZ)
  1570. 1524   1             RTDSetByte(ADC_REG_TEST_E9, 0x08);
  1571. 1525   1          else if(ucMode_Curr < MODE_1024x0768x70HZ)
  1572. 1526   1             RTDSetByte(ADC_REG_TEST_E9, 0x00);
  1573. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 26  
  1574. 1527   1          else
  1575. 1528   1             RTDSetByte(ADC_REG_TEST_E9, 0x10);
  1576. 1529   1      */
  1577. 1530   1      
  1578. 1531   1          if (ERROR_SUCCEED != Measure_PositionN(NM))    return ERROR_ABORT;
  1579. 1532   1      
  1580. 1533   1          // Set auto-tracking window
  1581. 1534   1          Data[0]     = 6;
  1582. 1535   1          Data[1]     = Y_INC;
  1583. 1536   1          Data[2]     = H_BND_STA_L_75;
  1584. 1537   1          Data[3]     = (unsigned char)(usH_Start + MEAS_H_STA_OFFSET - 2);
  1585. 1538   1          Data[4]     = (unsigned char)(usH_End + MEAS_H_END_OFFSET + 1);
  1586. 1539   1          Data[5]     = ((unsigned char)((usH_Start + MEAS_H_STA_OFFSET - 2) >> 4) & 0x70) | ((unsigned char)((u
  1587.              -sH_End + MEAS_H_END_OFFSET + 1) >> 8) & 0x0f);
  1588. 1540   1          Data[6]     = 0;
  1589. 1541   1          RTDWrite(Data);
  1590. 1542   1      
  1591. 1543   1          RTDSetByte(DIFF_THRED_7E, 0x30);
  1592. 1544   1      
  1593. 1545   1          ulTemp0     = 0;
  1594. 1546   1          ucDetect    = 0x7b;
  1595. 1547   1          do
  1596. 1548   1          {
  1597. 1549   2              ucResult    = COLORS_GREEN;
  1598. 1550   2              ucPhase     = COLORS_GREEN;
  1599. 1551   2              do
  1600. 1552   2              {
  1601. 1553   3                  RTDSetByte(MARGIN_B_7D, ucPhase);
  1602. 1554   3                  RTDSetByte(AUTO_ADJ_CTRL_7F, ucDetect);
  1603. 1555   3      
  1604. 1556   3                  Wait_Finish();
  1605. 1557   3                  if (ERROR_SUCCEED != Data[0])   return Data[0];
  1606. 1558   3      
  1607. 1559   3                  Read_Auto_Info(1);
  1608. 1560   3                  if (ulTemp0 < ((unsigned long *)Data)[1])
  1609. 1561   3                  {
  1610. 1562   4                      ulTemp0     = ((unsigned long *)Data)[1];
  1611. 1563   4                      ucResult    = ucPhase;
  1612. 1564   4      
  1613. 1565   4                      if (0x8000 < ulTemp0)   break;
  1614. 1566   4                  }
  1615. 1567   3      
  1616. 1568   3                  if (COLORS_GREEN == ucPhase)
  1617. 1569   3                      ucPhase = COLORS_BLUE;
  1618. 1570   3                  else if (COLORS_BLUE == ucPhase)
  1619. 1571   3                      ucPhase = COLORS_RED;
  1620. 1572   3                  else
  1621. 1573   3                      break;
  1622. 1574   3              }
  1623. 1575   2              while (1);
  1624. 1576   2      
  1625. 1577   2              if (0 != ulTemp0 || 0x7b != ucDetect)   break;
  1626. 1578   2      
  1627. 1579   2              ucDetect    = 0x77;
  1628. 1580   2          }
  1629. 1581   1          while (1);
  1630. 1582   1      
  1631. 1583   1          // Abort if no suitable color is found
  1632. 1584   1          if (0 == ulTemp0)   return ERROR_NOTACTIVE;
  1633. 1585   1         // NM = COLORS_GREEN;
  1634. 1586   1          // Select color for auto-phase tracking
  1635. 1587   1          RTDSetByte(MARGIN_B_7D, NM | ucResult);
  1636. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 27  
  1637. 1588   1      
  1638. 1589   1      #if(1)
  1639. 1590   1          //NM = 136;
  1640. 1591   1              NM = 100;
  1641. 1592   1      #else
  1642.                   // Find suitable threshold
  1643.                   // We use phase 8 and 24 to find out it
  1644.               
  1645.                   ucPhase = 0x20;
  1646.                   Set_Phase(ucPhase);
  1647.               
  1648.                   ucResult    = 0x28;
  1649.                   NM          = 0x00;
  1650.                   do
  1651.                   {
  1652.                       do
  1653.                       {
  1654.                           ucResult    += 0x20;
  1655.               
  1656.                           RTDSetByte(DIFF_THRED_7E, ucResult);
  1657.                           RTDSetByte(AUTO_ADJ_CTRL_7F, ucDetect);
  1658.               
  1659.                           Wait_Finish();
  1660.                           if (ERROR_SUCCEED != Data[0])   return Data[0];
  1661.               
  1662.                           Read_Auto_Info(1);
  1663.               
  1664.                           if ((unsigned long)0x1000 > ((unsigned long *)Data)[1])
  1665.                           {
  1666.                               ucResult    -= 0x20;
  1667.                               break;
  1668.                           }
  1669.                       }
  1670.                       while (0x88 > ucResult);
  1671.               
  1672.                       if (0x88 == ucResult)
  1673.                       {
  1674.                           NM  = ucResult;
  1675.                           break;
  1676.                       }
  1677.                       else
  1678.                       {
  1679.                           if (0x20 == ucPhase)
  1680.                           {
  1681.                               NM  = ucResult;
  1682.               
  1683.                               ucPhase = 0x60;
  1684.                               Set_Phase(ucPhase);
  1685.                           }
  1686.                           else
  1687.                           {
  1688.                               if (NM < ucResult)      NM  = ucResult;
  1689.                               
  1690.                               break;
  1691.                           }
  1692.                       }
  1693.                   }
  1694.                   while (1);
  1695.               
  1696.                   // Set threshold
  1697.                   if(NM < 136) NM = 136;
  1698.               #endif
  1699. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 28  
  1700. 1650   1      
  1701. 1651   1          RTDSetByte(DIFF_THRED_7E, NM);
  1702. 1652   1      
  1703. 1653   1      #if(1)  //FAST_AUTO method 1
  1704. 1654   1              // Set phase 30
  1705. 1655   1                      Set_Phase(0x78);
  1706. 1656   1                      Delay_Xms(1);
  1707. 1657   1                      
  1708. 1658   1                      RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1709. 1659   1      
  1710. 1660   1                      Wait_Finish();
  1711. 1661   1                      if (ERROR_SUCCEED != Data[0])   return Data[0];
  1712. 1662   1      
  1713. 1663   1                      Read_Auto_Info(1);
  1714. 1664   1            
  1715. 1665   1                      ulTemp1     = ((unsigned long *)Data)[1];
  1716. 1666   1      
  1717. 1667   1      
  1718. 1668   1      #if(HARDWARE_AUTO)
  1719. 1669   1              ulTemp2     = ulTemp1;
  1720. 1670   1                      ulTemp0     = 0;
  1721. 1671   1              RTDSetByte(HW_AUTO_PHASE_9E,0x05);  //Step 2 auto phase
  1722. 1672   1                      //Wait_For_Event(EVENT_IVS);
  1723. 1673   1                      Wait_For_IVS();
  1724. 1674   1                      RTDSetByte(AUTO_ADJ_CTRL_7F,0x77); // Auto start
  1725. 1675   1                      //Wait_For_Event(EVENT_IVS);
  1726. 1676   1                      Wait_For_IVS();
  1727. 1677   1              for(ucDetect = 0;ucDetect < 16; ucDetect++)
  1728. 1678   1              {
  1729. 1679   2                     //Wait_For_Event(EVENT_IVS);
  1730. 1680   2                                 Wait_For_IVS();
  1731. 1681   2                     Read_Auto_Info(3);
  1732. 1682   2                  /*   
  1733. 1683   2                                 Data[0]     = 7;
  1734. 1684   2                     Data[1]     = ADDR_EROM1;
  1735. 1685   2                     Data[2]     = 0x40 + (ucDetect << 2);
  1736. 1686   2                     Data[3]     = (unsigned char)(((unsigned long *)Data)[3] >> 12);
  1737. 1687   2                     Data[4]     = (unsigned char)(((unsigned long *)Data)[3] >> 8);
  1738. 1688   2                     Data[5]     = (unsigned char)(((unsigned long *)Data)[3] >> 4);
  1739. 1689   2                     Data[6]     = 0x00;
  1740. 1690   2                     
  1741. 1691   2                                 I2CWrite(Data);
  1742. 1692   2                                */ 
  1743. 1693   2      
  1744. 1694   2                    if(ulTemp0 < ((unsigned long *)Data)[3])
  1745. 1695   2                    {
  1746. 1696   3                         ulTemp0   = ((unsigned long *)Data)[3] & 0xffffff00; //Store the SOD of phase(n)
  1747. 1697   3                                         ulTemp1   = ulTemp2;                                 //Store the SOD of phase(n-2)
  1748. 1698   3                                         ucResult  = ucDetect << 3;                           //Save the phase
  1749. 1699   3                    }
  1750. 1700   2                    ulTemp2 = ((unsigned long*)Data)[3] & 0xffffff00;
  1751. 1701   2                                
  1752. 1702   2              }
  1753. 1703   1                              Wait_Finish();
  1754. 1704   1                              if (ERROR_SUCCEED != Data[0])   return Data[0];
  1755. 1705   1      
  1756. 1706   1              RTDSetByte(HW_AUTO_PHASE_9E,0x00);  //Switch back to software auto phase
  1757. 1707   1      
  1758. 1708   1      #else
  1759.                               // Set phase 0
  1760.                               Set_Phase(0x00);
  1761.                               Delay_Xms(1);
  1762. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 29  
  1763.                               
  1764.                               RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1765.               
  1766.                               Wait_Finish();
  1767.                               if (ERROR_SUCCEED != Data[0])   return Data[0];
  1768.               
  1769.                               Read_Auto_Info(1);
  1770.                     
  1771.                               ulTemp0     = ((unsigned long *)Data)[1];
  1772.                               ulTemp2     = ulTemp0;
  1773.               
  1774.               
  1775.                               ucResult    = 0x00;
  1776.               
  1777.                               ucPhase     = 0x08;//2 step rought scan
  1778.                               do
  1779.                               {
  1780.                                       Set_Phase(ucPhase);
  1781.                                       Delay_Xms(1);
  1782.               
  1783.                                       RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1784.                                       
  1785.                                       Wait_Finish();
  1786.                                       if (ERROR_SUCCEED != Data[0])   return Data[0];
  1787.               
  1788.                           Read_Auto_Info(3);
  1789.                      
  1790.               //                      ((unsigned long *)Data)[0]  = ulTemp1 + ulTemp2 + ((unsigned long *)Data)[1];
  1791.               
  1792.                                       if (ulTemp0 < ((unsigned long *)Data)[3])
  1793.                                       {
  1794.                                               ulTemp0     = ((unsigned long *)Data)[3];  //Save the SOD of phase(n)
  1795.                                               ulTemp1     = ulTemp2;                     //Save the SOD of phase(n-2)
  1796.                                               ucResult    = ucPhase;
  1797.                                       }
  1798.                                       
  1799.               
  1800.                                       ulTemp2 = ((unsigned long *)Data)[3];
  1801.               
  1802.                                       ucPhase = ucPhase + 0x08;
  1803.                               }
  1804.                               while (0x78 != ucPhase);
  1805.               
  1806.               
  1807.                               if((ucResult == 0x00) && (ulTemp1 > ulTemp2))//Compare the value of phase 28 & phase 30
  1808.                               {
  1809.                                    ucResult = 0x78;  //The maximum equal to phase 30
  1810.                                        ulTemp0 = ulTemp1; //Save the value of phase 30
  1811.                                        ulTemp1 = ulTemp2; //Save the value of phase 28
  1812.                               }
  1813.               #endif
  1814. 1763   1      
  1815. 1764   1              ucPhase = ucResult == 0x00 ? 0x7c : (ucResult - 0x04 );
  1816. 1765   1              ulTemp2 = 0;
  1817. 1766   1              ucDetect = 2;
  1818. 1767   1                      
  1819. 1768   1      
  1820. 1769   1              Set_Phase(ucPhase); //set Phase(n-1)
  1821. 1770   1                      Delay_Xms(1);
  1822. 1771   1                      
  1823. 1772   1                      RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1824. 1773   1      
  1825. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 30  
  1826. 1774   1                      Wait_Finish();
  1827. 1775   1                      if (ERROR_SUCCEED != Data[0])   return Data[0];
  1828. 1776   1              Read_Auto_Info(1);
  1829. 1777   1              ulTemp2 = ((unsigned long*)Data)[1];   //Save the SOD of  phase(n-1)
  1830. 1778   1                      
  1831. 1779   1              ((unsigned long*)Data)[0] 
  1832. 1780   1                      = ulTemp1 + ulTemp2 + ulTemp0 
  1833. 1781   1              -((ulTemp1 > ulTemp2 ? ulTemp1 - ulTemp2 : ulTemp2 - ulTemp1)/2)
  1834. 1782   1                      -((ulTemp2 > ulTemp0 ? ulTemp2 - ulTemp0 : ulTemp0 - ulTemp2)/2);
  1835. 1783   1      
  1836. 1784   1                      ulTemp1 = ((unsigned long*)Data)[0];
  1837. 1785   1                      ucPhase = ucResult; 
  1838. 1786   1              ucResult = ucResult == 0x00 ? 0x7c : ucResult - 0x04;
  1839. 1787   1      
  1840. 1788   1              do //detail scan by one step from phase(n-2) ~ pnase (n+2)
  1841. 1789   1              {
  1842. 1790   2                 ucPhase = (ucPhase == 0x7c) ? 0x00 : ucPhase + 0x04;
  1843. 1791   2                 Set_Phase(ucPhase);
  1844. 1792   2                     Delay_Xms(1);
  1845. 1793   2                      
  1846. 1794   2                         RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1847. 1795   2      
  1848. 1796   2                 Wait_Finish();
  1849. 1797   2                         if (ERROR_SUCCEED != Data[0])   return Data[0];
  1850. 1798   2      
  1851. 1799   2                 Read_Auto_Info(3);
  1852. 1800   2      
  1853. 1801   2                ((unsigned long *)Data)[0]  
  1854. 1802   2                 = ulTemp2 + ulTemp0 + ((unsigned long *)Data)[3]
  1855. 1803   2                 - ((ulTemp0 > ulTemp2 ? ulTemp0 - ulTemp2 : ulTemp2 - ulTemp0) / 2)
  1856. 1804   2                 - ((ulTemp0 > ((unsigned long *)Data)[3] ? ulTemp0 - ((unsigned long *)Data)[3] : ((unsigned lo
  1857.              -ng *)Data)[3] - ulTemp0) / 2);
  1858. 1805   2      
  1859. 1806   2                 if(((unsigned long*)Data)[0] > ulTemp1)
  1860. 1807   2                 {
  1861. 1808   3                     ulTemp1 = ((unsigned long*)Data)[0];
  1862. 1809   3                     ucResult = (ucPhase == 0x00) ? 0x7c : ucPhase - 0x04;
  1863. 1810   3                 }
  1864. 1811   2                 ucDetect -= 1;
  1865. 1812   2                 
  1866. 1813   2                 ulTemp2 = ulTemp0;
  1867. 1814   2                 ulTemp0 = ((unsigned long *)Data)[3];
  1868. 1815   2      
  1869. 1816   2              }while(ucDetect);
  1870. 1817   1      
  1871. 1818   1      
  1872. 1819   1      #else   //FAST AUTO method 2
  1873.                               // Set phase 30
  1874.                               Set_Phase(0x78);
  1875.                               Delay_Xms(1);
  1876.                               
  1877.                               RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1878.               
  1879.                               Wait_Finish();
  1880.                               if (ERROR_SUCCEED != Data[0])   return Data[0];
  1881.               
  1882.                               Read_Auto_Info(1);
  1883.                     
  1884.                               ulTemp2     = ((unsigned long *)Data)[1];
  1885.               
  1886.                               // Set phase 0
  1887.                               //Set_Phase(0x00 | ucDetect);
  1888. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 31  
  1889.                       Set_Phase(0x00);
  1890.                               Delay_Xms(1);
  1891.                               
  1892.                               RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1893.               
  1894.                               Wait_Finish();
  1895.                               if (ERROR_SUCCEED != Data[0])   return Data[0];
  1896.               
  1897.                               Read_Auto_Info(2);
  1898.                      
  1899.               
  1900.                               ulTemp1 = ((unsigned long *)Data)[2];
  1901.               
  1902.                               ulTemp0     = 0;
  1903.                               ucResult    = 0x08;
  1904.                               ucPhase     = 0x08;//2 step rought scan
  1905.                               do
  1906.                               {
  1907.                                       Set_Phase(ucPhase);
  1908.                                       Delay_Xms(1);
  1909.               
  1910.                                       RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1911.                                       
  1912.                                       Wait_Finish();
  1913.                                       if (ERROR_SUCCEED != Data[0])   return Data[0];
  1914.               
  1915.                           Read_Auto_Info(3);
  1916.                      
  1917.               //                      ((unsigned long *)Data)[0]  = ulTemp1 + ulTemp2 + ((unsigned long *)Data)[1];
  1918.               
  1919.                          ((unsigned long *)Data)[0]  
  1920.                                  = ulTemp2 + ulTemp1 + ((unsigned long *)Data)[3]
  1921.                           - ((ulTemp1 > ulTemp2 ? ulTemp1 - ulTemp2 : ulTemp2 - ulTemp1) / 2)
  1922.                           - ((ulTemp1 > ((unsigned long *)Data)[3] ? ulTemp1 - ((unsigned long *)Data)[3] : ((unsigned l
  1923.              -ong *)Data)[3] - ulTemp1) / 2);
  1924.               
  1925.               
  1926.                                       if (ulTemp0 < ((unsigned long *)Data)[0])
  1927.                                       {
  1928.                                               ulTemp0     = ((unsigned long *)Data)[0];
  1929.                                               ucResult    = ucPhase - 0x08;
  1930.                                       }
  1931.               
  1932.                                       ulTemp2 = ulTemp1;
  1933.                                       ulTemp1 = ((unsigned long *)Data)[3];
  1934.               
  1935.                                       ucPhase = ucPhase + 0x08;
  1936.                               }
  1937.                               while (0x78 != ucPhase);
  1938.               
  1939.               
  1940.               //              ((unsigned long *)Data)[0]  = ulTemp1 + ulTemp2 + ((unsigned long *)Data)[3];
  1941.               //              ((unsigned long *)Data)[1]  = ((unsigned long *)Data)[3] + ulTemp1 + ((unsigned long *)Data)[2];
  1942.               
  1943.               
  1944.                      ((unsigned long *)Data)[0]//phase26+phase28+phase30
  1945.                       = ulTemp2 + ulTemp1 + ((unsigned long *)Data)[1]
  1946.                       - ((ulTemp1 > ulTemp2 ? ulTemp1 - ulTemp2 : ulTemp2 - ulTemp1) / 2)
  1947.                       - ((ulTemp1 > ((unsigned long *)Data)[1] ? ulTemp1 - ((unsigned long *)Data)[1] : ((unsigned long 
  1948.              -*)Data)[1] - ulTemp1) / 2);
  1949.                      
  1950.                               
  1951. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 32  
  1952.               
  1953.                      ((unsigned long *)Data)[3]//phase28+phase30+phase0
  1954.                       = ulTemp1 + ((unsigned long *)Data)[1] + ((unsigned long *)Data)[2]
  1955.                       - ((((unsigned long *)Data)[1] > ulTemp1 ? ((unsigned long *)Data)[1] - ulTemp1 : ulTemp1 - ((unsi
  1956.              -gned long *)Data)[1]) / 2)
  1957.                       - ((((unsigned long *)Data)[1] > ((unsigned long *)Data)[2] ? ((unsigned long *)Data)[1] - ((unsig
  1958.              -ned long *)Data)[2] : ((unsigned long *)Data)[2] - ((unsigned long *)Data)[1]) / 2);
  1959.               
  1960.               
  1961.                               if (ulTemp0 < ((unsigned long *)Data)[0])
  1962.                               {
  1963.                                       ulTemp0     = ((unsigned long *)Data)[0];
  1964.                                       ucResult    = 0x70;  //ucResult = phase28
  1965.                               }
  1966.               
  1967.                               if (ulTemp0 < ((unsigned long *)Data)[3])
  1968.                               {
  1969.                                       ulTemp0     = ((unsigned long *)Data)[3];
  1970.                                       ucResult    = 0x78;  //ucResult = phase30
  1971.                               }
  1972.               
  1973.               
  1974.                       ucPhase = ucResult >= 0x08 ? (ucResult - 0x08) : (ucResult + 0x80 - 0x08);
  1975.                       ulTemp0 = 0;
  1976.                       ulTemp1 = 0;
  1977.                       ulTemp2 = 0;
  1978.                       ucDetect = 3;
  1979.               
  1980.                       Set_Phase(ucPhase); //set Phase(n-2)
  1981.                               Delay_Xms(1);
  1982.                               
  1983.                               RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1984.               
  1985.                               Wait_Finish();
  1986.                               if (ERROR_SUCCEED != Data[0])   return Data[0];
  1987.                       Read_Auto_Info(1);
  1988.                       ulTemp1 = ((unsigned long*)Data)[1];
  1989.               
  1990.                       ucPhase = (ucPhase == 0x7c) ? 0x00 : ucPhase + 0x04; //set Phase(n-1)
  1991.                       Set_Phase(ucPhase);
  1992.                               Delay_Xms(1);
  1993.                               
  1994.                               RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  1995.               
  1996.                               Wait_Finish();
  1997.                               if (ERROR_SUCCEED != Data[0])   return Data[0];
  1998.                       Read_Auto_Info(2);
  1999.                       ulTemp2 = ((unsigned long*)Data)[2];
  2000.               
  2001.               
  2002.                       do //detail scan by one step from phase(n-2) ~ pnase (n+2)
  2003.                       {
  2004.                          ucPhase = (ucPhase == 0x7c) ? 0x00 : ucPhase + 0x04;
  2005.                          Set_Phase(ucPhase);
  2006.                              Delay_Xms(1);
  2007.                               
  2008.                                  RTDSetByte(AUTO_ADJ_CTRL_7F, 0x77);
  2009.               
  2010.                          Wait_Finish();
  2011.                                  if (ERROR_SUCCEED != Data[0])   return Data[0];
  2012.               
  2013.                          Read_Auto_Info(3);
  2014. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 33  
  2015.               
  2016.                         ((unsigned long *)Data)[0]  
  2017.                          = ulTemp1 + ulTemp2 + ((unsigned long *)Data)[3]
  2018.                          - ((ulTemp1 > ulTemp2 ? ulTemp1 - ulTemp2 : ulTemp2 - ulTemp1) / 2)
  2019.                          - ((ulTemp2 > ((unsigned long *)Data)[3] ? ulTemp2 - ((unsigned long *)Data)[3] : ((unsigned lo
  2020.              -ng *)Data)[3] - ulTemp2) / 2);
  2021.               
  2022.                          if(((unsigned long*)Data)[0] > ulTemp0)
  2023.                          {
  2024.                              ulTemp0 = ((unsigned long*)Data)[0];
  2025.                              ucResult = (ucPhase == 0x00) ? 0x7c : ucPhase - 0x04;
  2026.                          }
  2027.                          ucDetect -= 1;
  2028.                          
  2029.                          ulTemp1 = ulTemp2;
  2030.                          ulTemp2 = ((unsigned long *)Data)[3];
  2031.               
  2032.                       }while(ucDetect);
  2033.                       
  2034.               //        ucDebug_Value1 = ucResult;
  2035.               #endif        
  2036. 1975   1      
  2037. 1976   1                      stMUD.PHASE = ucResult;
  2038. 1977   1                      Set_Phase(stMUD.PHASE);
  2039. 1978   1              
  2040. 1979   1      /*
  2041. 1980   1          //restore the bandwidth setup of ADC
  2042. 1981   1          if(ucMode_Curr < MODE_1024x0768x70HZ)
  2043. 1982   1              RTDSetByte(ADC_REG_TEST_E9, 0x08);
  2044. 1983   1          else 
  2045. 1984   1              RTDSetByte(ADC_REG_TEST_E9, 0x10);
  2046. 1985   1      */
  2047. 1986   1         
  2048. 1987   1          return ERROR_SUCCEED;
  2049. 1988   1      }
  2050. 1989          
  2051. 1990          unsigned char Auto_Config(void)
  2052. 1991          {
  2053. 1992   1          unsigned char   Result, Noise, Curr_PosH, Curr_PosV, Curr_Clock, Curr_Phase;
  2054. 1993   1      
  2055. 1994   1          unsigned char ucTemp1;
  2056. 1995   1              unsigned int usTemp;
  2057. 1996   1      
  2058. 1997   1      
  2059. 1998   1          bAutoInProgress = 1;
  2060. 1999   1          
  2061. 2000   1          Curr_PosH   = stMUD.H_POSITION;     // Save current stMUD.H_POSITION
  2062. 2001   1          Curr_PosV   = stMUD.V_POSITION;     // Save current stMUD.V_POSITION
  2063. 2002   1          Curr_Clock  = stMUD.CLOCK;          // Save current stMUD.CLOCK
  2064. 2003   1          Curr_Phase  = stMUD.PHASE;          // Save current stMUD.PHASE
  2065. 2004   1      
  2066. 2005   1          if (ucV_Max_Margin < stMUD.V_POSITION)
  2067. 2006   1          {
  2068. 2007   2              stMUD.V_POSITION    = ucV_Max_Margin - 1;
  2069. 2008   2              Set_V_Position();
  2070. 2009   2          }
  2071. 2010   1      
  2072. 2011   1      //   RTDCodeW(ADC_DEFAULT);
  2073. 2012   1      
  2074. 2013   1      /////////original formula////////////////////////////////////////////
  2075. 2014   1          //Set the H Position center(without IHS_Delay)
  2076. 2015   1      /*
  2077. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 34  
  2078. 2016   1              stMUD.H_POSITION = 128; 
  2079. 2017   1          
  2080. 2018   1              if(128 >= stMUD.H_POSITION)
  2081. 2019   1          {
  2082. 2020   1          ucTemp = 128 - stMUD.H_POSITION; 
  2083. 2021   1          
  2084. 2022   1              if(stMUD.CLOCK > 128)
  2085. 2023   1                 ucTemp1 = 128;
  2086. 2024   1              else
  2087. 2025   1                 ucTemp1 = stMUD.CLOCK;
  2088. 2026   1      
  2089. 2027   1          //According to the H_Position adjust the IHS pre-delay
  2090. 2028   1              ucH_Min_Margin = 128 - ucTemp - (64 - (ucTemp1 >> 1));
  2091. 2029   1          //Return the IPH_ACT_STA to the original one first,and compensate the IHS pre-delay
  2092. 2030   1          //The h position must be equal after change the IHS delay and IPH_ACT_STA
  2093. 2031   1              usIPH_ACT_STA = usIPH_ACT_STA + 50 - ucTemp - (64 - (ucTemp1 >> 1));
  2094. 2032   1              usTemp = usIPH_ACT_STA;
  2095. 2033   1      
  2096. 2034   1              Set_H_Position();
  2097. 2035   1          }
  2098. 2036   1              */
  2099. 2037   1      ///////////////////////////////////////////////////////////////////    
  2100. 2038   1      
  2101. 2039   1      ////////////////////////////////////////////////////////////////////
  2102. 2040   1          //Set the H Position center(without IHS_Delay)
  2103. 2041   1      
  2104. 2042   1              stMUD.H_POSITION = 128; 
  2105. 2043   1          
  2106. 2044   1              
  2107. 2045   1          //ucTemp = 128 - stMUD.H_POSITION; 
  2108. 2046   1          
  2109. 2047   1              if(stMUD.CLOCK > 128)
  2110. 2048   1                 ucTemp1 = 128;
  2111. 2049   1              else
  2112. 2050   1                 ucTemp1 = stMUD.CLOCK;
  2113. 2051   1      
  2114. 2052   1          //According to the H_Position adjust the IHS pre-delay
  2115. 2053   1              ucH_Min_Margin = 128 - (64 - (ucTemp1 >> 1));
  2116. 2054   1          //Return the IPH_ACT_STA to the original one first,and compensate the IHS pre-delay
  2117. 2055   1          //The h position must be equal after change the IHS delay and IPH_ACT_STA
  2118. 2056   1          usIPH_ACT_STA   = CAP_WIN[ucMode_Curr][1];
  2119. 2057   1          
  2120. 2058   1          if(ucMode_Curr < MODE_0800x0600x75HZ)
  2121. 2059   1                      Data[0] = 2;
  2122. 2060   1          else if(ucMode_Curr < MODE_1280x1024x75HZ)
  2123. 2061   1                      Data[0] = 5;
  2124. 2062   1          else
  2125. 2063   1                      Data[0] = 3;
  2126. 2064   1      
  2127. 2065   1      //    usIPH_ACT_STA   = usIPH_ACT_STA + Data[0] - PROGRAM_HDELAY;
  2128. 2066   1      
  2129. 2067   1              //usIPH_ACT_STA = usIPH_ACT_STA + 50 - (64 - (ucTemp1 >> 1));
  2130. 2068   1          usIPH_ACT_STA = CAP_WIN[ucMode_Curr][1] + Data[0] - PROGRAM_HDELAY - (64 - (ucTemp1 >> 1));
  2131. 2069   1              
  2132. 2070   1          usTemp = usIPH_ACT_STA;
  2133. 2071   1      
  2134. 2072   1              Set_H_Position();
  2135. 2073   1              
  2136. 2074   1          RTDSetByte(STATUS0_01, 0x00);  // Clear status
  2137. 2075   1          RTDSetByte(STATUS1_1F, 0x00);  // Clear status
  2138. 2076   1      
  2139. 2077   1          
  2140. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 35  
  2141. 2078   1      ///////////////////////////////////////////////////////////////////    
  2142. 2079   1      
  2143. 2080   1          ///////////////////////////////
  2144. 2081   1          //   Measure  NOISE_MARGIN   //
  2145. 2082   1          ///////////////////////////////
  2146. 2083   1      
  2147. 2084   1          Result  = Min_Noise_Margin();   
  2148. 2085   1          Noise   = Data[0];
  2149. 2086   1      
  2150. 2087   1          
  2151. 2088   1      
  2152. 2089   1          
  2153. 2090   1          //--------Auto_Clock-----------
  2154. 2091   1          if (ERROR_SUCCEED == (Result & 0x80))
  2155. 2092   1          {   
  2156. 2093   2              stMUD.CLOCK = (stMUD.CLOCK) & 0xfc; // stMUD.CLOCK must be times of 4
  2157. 2094   2      
  2158. 2095   2              if (stMUD.CLOCK != Curr_Clock)  Set_Clock();
  2159. 2096   2      
  2160. 2097   2              ///////////////////////////////
  2161. 2098   2              //       Adjust Clock        //
  2162. 2099   2              ///////////////////////////////
  2163. 2100   2              Result  = Auto_Clock_Do(Noise);
  2164. 2101   2      
  2165. 2102   2                      
  2166. 2103   2              if (ERROR_SUCCEED != (Result & 0x80))
  2167. 2104   2              {
  2168. 2105   3                  if (stMUD.CLOCK != Curr_Clock)
  2169. 2106   3                  {
  2170. 2107   4                      // Fail to find out suitable clock. Restore original clock and H position.
  2171. 2108   4                      stMUD.H_POSITION    = Curr_PosH;
  2172. 2109   4                      stMUD.CLOCK         = Curr_Clock;
  2173. 2110   4      
  2174. 2111   4                      Set_Clock();
  2175. 2112   4                      Set_H_Position();
  2176. 2113   4                  }
  2177. 2114   3              }
  2178. 2115   2              else
  2179. 2116   2              {
  2180. 2117   3                  stMUD.H_POSITION    = usH_Start + 128 + 64 - usIPH_ACT_STA - (stMUD.CLOCK >> 1);
  2181. 2118   3      
  2182. 2119   3                  if (ucH_Max_Margin < stMUD.H_POSITION)
  2183. 2120   3                      stMUD.H_POSITION    = ucH_Max_Margin;
  2184. 2121   3                  else if (ucH_Min_Margin > stMUD.H_POSITION)
  2185. 2122   3                      stMUD.H_POSITION    = ucH_Min_Margin;
  2186. 2123   3      
  2187. 2124   3                  Set_H_Position();
  2188. 2125   3              }
  2189. 2126   2          }
  2190. 2127   1          
  2191. 2128   1          //---------Auto_Phase-----------
  2192. 2129   1          if (ERROR_SUCCEED == (Result & 0x80))
  2193. 2130   1          {   
  2194. 2131   2              Result      = Auto_Phase_Do(Noise);
  2195. 2132   2      
  2196. 2133   2              if (ERROR_SUCCEED != (Result & 0x80))
  2197. 2134   2              {
  2198. 2135   3                  // Restore Phase
  2199. 2136   3                  stMUD.PHASE = Curr_Phase;
  2200. 2137   3                  Set_Phase(stMUD.PHASE);
  2201. 2138   3      
  2202. 2139   3                  if (ERROR_NOTACTIVE == Result)      Result  = ERROR_SUCCEED;
  2203. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 36  
  2204. 2140   3              }
  2205. 2141   2          }
  2206. 2142   1      
  2207. 2143   1          //---------Auto_Position-----------
  2208. 2144   1          if (ERROR_SUCCEED == (Result & 0x80))    
  2209. 2145   1          {   
  2210. 2146   2              ///////////////////////////////
  2211. 2147   2              //    Adjust (H/V)Position   //
  2212. 2148   2              ///////////////////////////////
  2213. 2149   2              Result  = Auto_Position_Do(Noise);
  2214. 2150   2      
  2215. 2151   2              // Because Auto_Position_Do() never returns bit-7 error, we don't have to check here.
  2216. 2152   2          }
  2217. 2153   1          else
  2218. 2154   1          {
  2219. 2155   2              stMUD.V_POSITION    = Curr_PosV;
  2220. 2156   2              Set_V_Position();
  2221. 2157   2          }
  2222. 2158   1          
  2223. 2159   1      //    if (ERROR_SUCCEED == (Result & 0x80))   Save_MUD(ucMode_Curr);
  2224. 2160   1      
  2225. 2161   1      //////////////////////////////////////////////////////////////////////////////
  2226. 2162   1      /*
  2227. 2163   1          if(ucH_Min_Margin == (128 - ucTemp - (64 - (ucTemp1 >> 1))))
  2228. 2164   1              {
  2229. 2165   1              if(usTemp != usIPH_ACT_STA)
  2230. 2166   1                      {  //if usIPH_ACT_STA has been modified, turn back to original set value
  2231. 2167   1                 //stMUD.H_POSIITON also have to follow up the change of usIPH_ACT_STA
  2232. 2168   1                         stMUD.H_POSITION = stMUD.H_POSITION - (usTemp - usIPH_ACT_STA);
  2233. 2169   1                         usIPH_ACT_STA = usTemp - 50 + ucTemp + (64 - (ucTemp1 >> 1));
  2234. 2170   1                      }
  2235. 2171   1                      else
  2236. 2172   1                          usIPH_ACT_STA   = usIPH_ACT_STA - 50 + ucTemp + (64 - (ucTemp1 >> 1));
  2237. 2173   1      
  2238. 2174   1                      ucH_Min_Margin  = 128 - 50;   
  2239. 2175   1                      Set_H_Position();
  2240. 2176   1              }
  2241. 2177   1              Set_Phase(stMUD.PHASE);
  2242. 2178   1              */
  2243. 2179   1      ////////////////////////////////////////////////////////////////////////////////
  2244. 2180   1      
  2245. 2181   1      /////////////////////////////////////////////////////////////////////////////////
  2246. 2182   1      
  2247. 2183   1          if(ucH_Min_Margin == (128 - (64 - (ucTemp1 >> 1))))
  2248. 2184   1              {
  2249. 2185   2              if(usTemp != usIPH_ACT_STA)
  2250. 2186   2                      {  //if usIPH_ACT_STA has been modified, turn back to original set value
  2251. 2187   3                 //stMUD.H_POSIITON also have to follow up the change of usIPH_ACT_STA
  2252. 2188   3                         //Data[3] = 78 - usIPH_ACT_STA; Save the usIPH_ACT_STA compensate value
  2253. 2189   3                         stMUD.H_POSITION = stMUD.H_POSITION - (usTemp - usIPH_ACT_STA);
  2254. 2190   3                         usIPH_ACT_STA = usTemp - 50 + (64 - (ucTemp1 >> 1));
  2255. 2191   3                         Data[3] = 0x80 | (78 - stMUD.H_POSITION);        
  2256. 2192   3               
  2257. 2193   3                      }
  2258. 2194   2                      else
  2259. 2195   2                      {
  2260. 2196   3                          usIPH_ACT_STA   = usIPH_ACT_STA - 50 + (64 - (ucTemp1 >> 1));
  2261. 2197   3                              Data[3] = 0;
  2262. 2198   3                      }
  2263. 2199   2      
  2264. 2200   2                      // Save Frame-Sync Settings
  2265. 2201   2                 Data[0] = 4;
  2266. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 37  
  2267. 2202   2                 Data[1] = ADDR_EROM1;
  2268. 2203   2                 Data[2] = (ucMode_Curr - 1);          
  2269. 2204   2                         
  2270. 2205   2                 
  2271. 2206   2      
  2272. 2207   2                       if(0x80 < stMUD.H_POSITION)
  2273. 2208   2               {
  2274. 2209   3                   if(stMUD.H_POSITION - 0x80 < 10)
  2275. 2210   3                   {
  2276. 2211   4                       usIPH_ACT_STA += (stMUD.H_POSITION - 0x80);
  2277. 2212   4                       Data[3] += (stMUD.H_POSITION - 0x80);
  2278. 2213   4                       stMUD.H_POSITION = 0x80;                            
  2279. 2214   4                    }
  2280. 2215   3      
  2281. 2216   3               }
  2282. 2217   2               else if(0x80 > stMUD.H_POSITION)
  2283. 2218   2               {
  2284. 2219   3                    if(0x80 - stMUD.H_POSITION < 10)
  2285. 2220   3                    {
  2286. 2221   4                       usIPH_ACT_STA -= (0x80 - stMUD.H_POSITION);
  2287. 2222   4      
  2288. 2223   4                                   Data[3] = (0x80 - stMUD.H_POSITION ) | 0x80;
  2289. 2224   4                       stMUD.H_POSITION = 0x80;
  2290. 2225   4      
  2291. 2226   4                    }
  2292. 2227   3               }
  2293. 2228   2      
  2294. 2229   2                      I2CWrite(Data);
  2295. 2230   2              Delay_Xms(SET_2404_DELAY);
  2296. 2231   2      
  2297. 2232   2                      ucH_Min_Margin  = 128 - 50;   
  2298. 2233   2                      Set_H_Position();
  2299. 2234   2              
  2300. 2235   2              }
  2301. 2236   1              Set_Phase(stMUD.PHASE);
  2302. 2237   1          if (ERROR_SUCCEED == (Result & 0x80))   Save_MUD(ucMode_Curr);
  2303. 2238   1      
  2304. 2239   1          RTDSetByte(STATUS0_01, 0x00);  // Clear status
  2305. 2240   1          RTDSetByte(STATUS1_1F, 0x00);  // Clear status
  2306. 2241   1      
  2307. 2242   1      /////////////////////////////////////////////////////////////////////////////////
  2308. 2243   1      
  2309. 2244   1          // Restore ADC Gain/Offset
  2310. 2245   1      //  SetADC_GainOffset();
  2311. 2246   1         
  2312. 2247   1          bAutoInProgress = 0;
  2313. 2248   1          Set_Phase(stMUD.PHASE);
  2314. 2249   1              
  2315. 2250   1          return Result;
  2316. 2251   1      }
  2317. 2252          
  2318. 2253          unsigned char Auto_Balance(void)
  2319. 2254          {
  2320. 2255   1          unsigned char Result, Curr_PosV;
  2321. 2256   1      
  2322. 2257   1          bAutoInProgress = 1;
  2323. 2258   1      
  2324. 2259   1          Curr_PosV   = stMUD.V_POSITION;     // Save current stMUD.V_POSITION
  2325. 2260   1      
  2326. 2261   1          if (ucV_Max_Margin < stMUD.V_POSITION)
  2327. 2262   1          {
  2328. 2263   2              stMUD.V_POSITION    = ucV_Max_Margin;
  2329. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 38  
  2330. 2264   2              Set_V_Position();
  2331. 2265   2          }
  2332. 2266   1      
  2333. 2267   1          // Do ADC gain/offset adjust
  2334. 2268   1          Result  = Tune_Balance();
  2335. 2269   1      
  2336. 2270   1          // Restore vertical position
  2337. 2271   1          if (Curr_PosV != stMUD.V_POSITION)
  2338. 2272   1          {
  2339. 2273   2              stMUD.V_POSITION    = Curr_PosV;
  2340. 2274   2              Set_V_Position();
  2341. 2275   2          }
  2342. 2276   1      
  2343. 2277   1          if (ERROR_SUCCEED != Result)
  2344. 2278   1          {
  2345. 2279   2              // Restore ADC Gain/Offset
  2346. 2280   2              Load_GUD2();
  2347. 2281   2              SetADC_GainOffset();
  2348. 2282   2          }
  2349. 2283   1          else
  2350. 2284   1          {
  2351. 2285   2              stGUD0.CONTRAST         = 50;
  2352. 2286   2              stGUD0.RTD_R_CONTRAST   = 50;
  2353. 2287   2              stGUD0.RTD_G_CONTRAST   = 50;
  2354. 2288   2              stGUD0.RTD_B_CONTRAST   = 50;
  2355. 2289   2              stGUD0.RTD_R_BRIGHT     = 50;
  2356. 2290   2              stGUD0.RTD_G_BRIGHT     = 50;
  2357. 2291   2              stGUD0.RTD_B_BRIGHT     = 50;
  2358. 2292   2              Set_Bright_Contrast();
  2359. 2293   2      
  2360. 2294   2      #if(ANALOG_CONTRAST)
  2361. 2295   2              SetADC_Gain();
  2362. 2296   2      #endif
  2363. 2297   2      
  2364. 2298   2              Save_GUD0();
  2365. 2299   2      /*
  2366. 2300   2              if (0 != stGUD2.AD_G_GAIN && 255 != stGUD2.AD_G_GAIN && 0 != stGUD2.AD_G_OFFSET && 255 != stGUD2.A
  2367.              -D_G_OFFSET)
  2368. 2301   2              {
  2369. 2302   2                  stGUD2.AD_G_GAIN    += 1;
  2370. 2303   2                  stGUD2.AD_G_OFFSET  += 1;
  2371. 2304   2              }
  2372. 2305   2              SetADC_GainOffset();
  2373. 2306   2      */
  2374. 2307   2              Save_GUD2();
  2375. 2308   2          }
  2376. 2309   1      
  2377. 2310   1          bAutoInProgress = 0;
  2378. 2311   1      
  2379. 2312   1          return Result;
  2380. 2313   1      }
  2381. 2314          
  2382. 2315          unsigned char Tune_Balance(void)
  2383. 2316          {
  2384. 2317   1          unsigned char   Color, Count, Result, Margin, FineTune, Offset_Stop,ucTemp = 0;
  2385. 2318   1          
  2386. 2319   1          FineTune    = 0;
  2387. 2320   1      
  2388. 2321   1      #if(ANALOG_CONTRAST)
  2389. 2322   1          ucTemp = stGUD0.CONTRAST;
  2390. 2323   1          stGUD0.CONTRAST = 50;
  2391. 2324   1              SetADC_Gain();
  2392. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 39  
  2393. 2325   1      #endif
  2394. 2326   1      
  2395. 2327   1          RTDCodeW(ADC_DEFAULT);
  2396. 2328   1          
  2397. 2329   1          // Get usVer_Start, usVer_End, usH_Start, usH_Start
  2398. 2330   1          if (ERROR_SUCCEED != Measure_PositionN(0x40))   return ERROR_ABORT;
  2399. 2331   1      
  2400. 2332   1          if (ERROR_SUCCEED != Measure_Color(SELECT_RED, COLOR_MAX))      return ERROR_ABORT;
  2401. 2333   1          if (0x60 > Data[0])     return ERROR_ABORT;
  2402. 2334   1          
  2403. 2335   1          Margin  = Data[0];
  2404. 2336   1      
  2405. 2337   1          if (ERROR_SUCCEED != Measure_Color(SELECT_GREEN, COLOR_MAX))    return ERROR_ABORT;
  2406. 2338   1          if (0x60 > Data[0])     return ERROR_ABORT;
  2407. 2339   1      
  2408. 2340   1          Margin  = Margin > Data[0] ? Data[0] : Margin;
  2409. 2341   1      
  2410. 2342   1          if (ERROR_SUCCEED != Measure_Color(SELECT_BLUE, COLOR_MAX))     return ERROR_ABORT;
  2411. 2343   1          if (0x60 > Data[0])     return ERROR_ABORT;
  2412. 2344   1      
  2413. 2345   1          Margin  = Margin > Data[0] ? Data[0] : Margin;
  2414. 2346   1      
  2415. 2347   1          Margin  = (Margin - 0x20) & 0xfc;
  2416. 2348   1      
  2417. 2349   1          // Get usVer_Start, usVer_End, usH_Start, usH_Start
  2418. 2350   1          if (ERROR_SUCCEED != Measure_PositionN(Margin))   return ERROR_ABORT;
  2419. 2351   1      
  2420. 2352   1      
  2421. 2353   1      #if(0)
  2422.                   SetADC_Offset();
  2423.               #else
  2424. 2356   1              SetADC_GainOffset();
  2425. 2357   1      #endif
  2426. 2358   1      
  2427. 2359   1              Offset_Stop = 0;
  2428. 2360   1          Color   = SELECT_BLUE;
  2429. 2361   1          do
  2430. 2362   1          {
  2431. 2363   2              Count   = 0x30;
  2432. 2364   2              
  2433. 2365   2              do
  2434. 2366   2              {
  2435. 2367   3      #if (0)
  2436.                           
  2437.                          if(Offset_Stop == 0)
  2438.                          { 
  2439.                                          ///////////////////////////////
  2440.                                          // Minimum Adjustment (0x02) //
  2441.                                          ///////////////////////////////
  2442.                                          if (ERROR_SUCCEED != Measure_Color(Color, COLOR_MIN))   return ERROR_ABORT;
  2443.                                          Margin  = Data[0];
  2444.               
  2445.                                          if (0x0a < Margin)
  2446.                                          {
  2447.                                                  Change_ADC_Offset(Color, 0x08, 0);          // Increase Offset; Decrease Brightness
  2448.                                          }
  2449.                                          else if (0x02 < Margin)
  2450.                                          {
  2451.                                                  Change_ADC_Offset(Color, Margin - 0x02, 0); // Increase Offset; Decrease Brightness
  2452.                                          }
  2453.                                          else if (0x02 > Margin)
  2454.                                          {
  2455. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 40  
  2456.                                                  if (0x00 == Margin && 0 == FineTune)
  2457.                                                          Change_ADC_Offset(Color, 0x06, 1);              // Decrease Offset; Increase Brightness
  2458.                                                  else
  2459.                                                  {
  2460.                                                          Change_ADC_Offset(Color, 0x02 - Margin, 1);     // Decrease Offset; Increase Brightness
  2461.               
  2462.                                                          if(Color == SELECT_RED)
  2463.                                                          {
  2464.                                                                  Offset_Stop = 1;
  2465.                                                                  SetADC_Gain();
  2466.                                                                  Color   = SELECT_BLUE << 1;
  2467.                                                          }
  2468.                                                          break;
  2469.                                                  }
  2470.                                          }
  2471.                                  }
  2472.                                  else
  2473.                                  {
  2474.                                          ///////////////////////////////
  2475.                                          // Maximum Adjustment (0xf2) //
  2476.                                          ///////////////////////////////
  2477.                                          if (ERROR_SUCCEED != Measure_Color(Color, COLOR_MAX))   return ERROR_ABORT;
  2478.                                          Margin  = Data[0];
  2479.               
  2480.                                          if (0xf2 < Margin) 
  2481.                                          {
  2482.                                                  Result  = Margin - 0xf2;
  2483.                                                  
  2484.                                                  // Non-zero return value of Change_ADC_Gain() means ADC gain reaches maximum.
  2485.                                                  if (Change_ADC_Gain(Color, Result, 0))              // Increase Gain; Decrease Contrast
  2486.                                                  {
  2487.                                                          if (Change_ADC_Offset(Color, 4, 0))     break;  // Increase Offset; Decrease Brightness
  2488.                                                  }
  2489.                                          }
  2490.                                          else if (0xf2 > Margin)
  2491.                                          {
  2492.                                                  Result  = 0xf2 - Margin;
  2493.               
  2494.                                                  // Non-zero return value of Change_ADC_Gain() means ADC gain reaches minimum.                
  2495.                                                  if (Change_ADC_Gain(Color, Result, 1))                  // Decrease Gain; Increase Contrast
  2496.                                                  {
  2497.                                                          if (Change_ADC_Offset(Color, 4, 1))     break;  // Decrease Offset; Increase Brightness
  2498.                                                  }
  2499.                                          }
  2500.                                          else
  2501.                                                  break;
  2502.                                  }
  2503.               
  2504.                                  if (8 < Count && 0xf3 >= Result && 0xf1 <= Result && 0x03 >= Margin && 0x01 <= Margin)
  2505.                                  {
  2506.                                          FineTune    = 1;
  2507.                                          Count       = 8;
  2508.                                  }
  2509.               #endif
  2510. 2441   3                              
  2511. 2442   3      
  2512. 2443   3      #if(1)
  2513. 2444   3                  ///////////////////////////////
  2514. 2445   3                  // Maximum Adjustment (0xf2) //
  2515. 2446   3                  ///////////////////////////////
  2516. 2447   3                  if (ERROR_SUCCEED != Measure_Color(Color, COLOR_MAX))   return ERROR_ABORT;
  2517. 2448   3                  Margin  = Data[0];
  2518. C51 COMPILER V7.06   LCD_AUTO                                                              11/21/2005 13:47:25 PAGE 41  
  2519. 2449   3      
  2520. 2450   3                  if (0xf2 < Margin)
  2521. 2451   3                  {
  2522. 2452   4                      Result  = Margin - 0xf2;
  2523. 2453   4                      
  2524. 2454   4                      // Non-zero return value of Change_ADC_Gain() means ADC gain reaches maximum.
  2525. 2455   4                      if (Change_ADC_Gain(Color, Result, 0))              // Increase Gain; Decrease Contrast
  2526. 2456   4                      {
  2527. 2457   5                          if (Change_ADC_Offset(Color, 4, 0))     break;  // Increase Offset; Decrease Brightnes
  2528.              -s
  2529. 2458   5                      }
  2530. 2459   4                  }
  2531. 2460   3                  else if (0xf2 > Margin)
  2532. 2461   3                  {
  2533. 2462   4                      Result  = 0xf2 - Margin;
  2534. 2463   4      
  2535. 2464   4                      // Non-zero return value of Change_ADC_Gain() means ADC gain reaches minimum.
  2536. 2465   4                      if (Change_ADC_Gain(Color, Result, 1))              // Decrease Gain; Increase Contrast
  2537. 2466   4                      {
  2538. 2467   5                          if (Change_ADC_Offset(Color, 4, 1))     break;  // Decrease Offset; Increase Brightnes
  2539.              -s
  2540. 2468   5                      }
  2541. 2469   4                  }
  2542. 2470   3      
  2543. 2471   3                  Result  = Margin;
  2544. 2472   3      
  2545. 2473   3                  ///////////////////////////////
  2546. 2474   3                  // Minimum Adjustment (0x02) //
  2547. 2475   3                  ///////////////////////////////
  2548. 2476   3