/*******************************************************************************
*  Copyright (C) 2010 - All Rights Reserved
*		
* 软件作者:	骑飞家族
* 版权所有: 骑飞电子	
* 创建日期:	2010年6月10日 
* 软件历史:	2010年11月2日修改
* Version:  1.0 
* Demo 淘宝地址：http://store.taobao.com/shop/view_shop.htm?asker=wangwang&shop_nick=qifeidianzi
**********************************************************************************************************************************************
懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒
懒懒懒懒懒懒懒懒懒懒懒一懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒困一懒懒懒懒懒懒懒懒懒懒懒懒懒懒一一一一一一一懒懒懒懒懒懒懒
懒懒困一一一一懒一一一一一一二懒懒懒懒困一一一一一一一一懒懒懒懒懒懒懒懒懒四厲懒懒一一懒懒懒一懒懒懒懒懒懒懒懒懒一一一一四四一一一懒懒懒懒懒懒
懒懒懒懒懒懒一懒懒懒一二一懒懒懒懒懒懒一一一一四厲二一四懒一一懒懒懒懒懒四一一一一一一一一一一一懒懒懒懒懒懒懒懒四懒懒四一一一一厲懒懒懒懒懒懒
懒懒懒四厲厲一懒懒厲懒懒懒四懒懒懒懒懒懒懒懒懒懒懒困一懒一一懒懒懒懒懒懒懒一一厲厲一一厲厲厲一一懒懒懒懒懒懒厲厲厲厲厲厲一一厲厲厲懒懒懒懒懒懒
懒懒懒一懒二一一一一一一一一一一一懒懒懒懒懒懒懒懒二一一困懒懒懒懒懒懒懒懒一一一一一一一一一一四懒懒懒懒懒一一一一一一一一一一一一一一一懒懒懒
懒懒懒一一一一一厲二一一厲一懒厲懒懒懒懒懒懒懒懒懒一一一一一懒懒懒懒懒懒懒一一懒懒一一懒懒懒一困懒懒懒懒懒一一四懒懒懒懒一一懒懒困一一四懒懒懒
懒懒懒懒懒懒懒一困一懒一厲一懒懒懒懒懒懒懒懒懒懒懒困一懒懒一一懒懒懒懒懒懒一一一一一一一一一一厲懒懒懒懒懒懒懒懒懒懒懒懒一一厲懒懒懒懒懒懒懒懒
懒二一一一懒四困厲一一一厲一懒懒懒懒懒懒懒懒懒懒懒懒一一懒懒懒懒懒懒懒懒懒懒懒懒懒四一厲懒懒懒懒懒懒一懒懒懒懒懒懒懒懒懒一一厲懒懒懒懒懒懒懒懒
懒懒懒懒懒懒一懒懒懒懒懒一一懒懒懒懒懒懒懒懒懒懒懒懒厲一一一一一一困懒懒懒懒懒懒懒懒一一一一一一一一一懒懒懒懒懒困懒懒厲一一懒懒懒懒懒懒懒懒懒
懒懒懒懒一一一懒懒困一一一一懒懒懒懒懒懒懒懒懒懒懒懒懒懒一一一一一懒懒懒懒懒懒懒懒懒懒一一一一一一一懒懒懒懒懒懒懒一一一一四懒懒懒懒懒懒懒懒懒
懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒
**********************************************************************************************************************************************/

#include <c8051f330.h>
#include <english_16x8.h>

///////////////////////////////////////////////////////
#define	BLACK	0x0000
#define	BLUE	0x001F
#define	RED 	0xF800
#define	GREEN 	0x07E0
#define CYAN	0x07FF
#define MAGENTA 0xF81F
#define YELLOW	0xFFE0
#define WHITE	0xFFFF		

#define AM          011
#define TIME 		   100

#define SDA_IN()		{P0MDOUT= 0X00;}
#define SDA_OUT()		{P0MDOUT= 0XFF;}
//iic example transplant
#define  SYSCLK        24500000     
#define  SMB_FREQUENCY  50000         
                                       
                                      

#define  WRITE          0x00          
#define  READ           0x01        


#define  EEPROM_ADDR    0x42

#define  SMB_MTSTA      0xE0           
#define  SMB_MTDB       0xC0           
#define  SMB_MRDB       0x80           


unsigned char* pSMB_DATA_IN;           
                                       

unsigned char SMB_SINGLEBYTE_OUT;      

unsigned char* pSMB_DATA_OUT;          
                                       

unsigned char SMB_DATA_LEN;            
unsigned char WORD_ADDR;               


unsigned char TARGET;                 

bit SMB_BUSY=0;                        
bit SMB_RW;                            
bit SMB_SENDWORDADDR;                 
bit SMB_RANDOMREAD;    
bit SMB_ACKPOLL; 
unsigned char FAIL = 0; 
unsigned long NUM_ERRORS;  

sfr16    TMR3RL   = 0x92;  
sfr16    TMR3     = 0x94;   
sbit Tft_RS_PIN=P0^6;
sbit Tft_WR_PIN=P0^7;
sbit Tft_RST_PIN=P0^4;


sbit I2C_SDA=P0^0;
sbit I2C_SCL=P0^1;
sbit Cmos_VSYNC=P0^5;
sbit Cmos_CS_PIN=P0^2;
sbit WR_CONTROL_PIN=P0^3;


void Ext_Interrupt_Init (void);

void Tft_Init(void);
void TftClear(unsigned int Color);
void Tft_WR_DATA16(unsigned int inputdata);
void TftWrite16(unsigned int index);
void TftWrite(unsigned int index,unsigned int inputdata);
void TftWR_Str(unsigned int x,unsigned int y,unsigned char *s,unsigned int color,unsigned int xcolor);
void TftWR_SingleStr(unsigned char inputdata,unsigned int color,unsigned int xcolor);
void Tft_SetBackground(unsigned int StartX,unsigned int StartY,unsigned int LongX,unsigned int LongY);
void Tft_SetCursor(unsigned int Xpos, unsigned int Ypos);

unsigned char WrCmos7670(unsigned char RegAddress, unsigned char RegData);
unsigned char RdCmos7670Reg(unsigned char RegAddress, unsigned char *RegData);
void Cmos7670_Size(unsigned int Startx,unsigned int Starty,unsigned int width,unsigned int height);
void my_delay_ms(unsigned int time);
unsigned char Cmos7670_init(void);
void Set_Cmos7670Reg(void);

void delay_us(unsigned int time);
void delay_ms(unsigned int time);
void delay( unsigned long cnt);

///////////////////////////////////////////
void InitI2C(void);           

void SMBus_Init (void);
void OSCILLATOR_Init (void);
void Port_Init(void);

void Timer1_Init (void);
void Timer3_Init (void);
void SMBus_ISR (void);
void Timer3_ISR (void);

void SMB_Write (void);
void SMB_Read (void);



void main(void)
{

		PCA0MD &= ~0x40; 
		OSCILLATOR_Init();
		Port_Init();

		Timer1_Init (); 		                                  
		Timer3_Init (); 
		SMBus_Init (); 	

		WR_CONTROL_PIN=1;
		Tft_Init( ); 
		TftClear(BLACK);
		TftWR_Str(60,70,"C8051F330 Camera",RED,BLACK);
		TftWR_Str(60,100,"QI FEI DIAN ZI VER1.0",RED,BLACK); 
		TftWR_Str(60,130,"OV7670 Initializing......",RED,BLACK);
		delay_ms(3000);

	    EIE1 |= 0x01;      
	    EA = 1;

		while(1!=Cmos7670_init());
		delay_ms(3000);
		TftClear(BLACK);
		TftWR_Str(60,110,"OV7670  Init  SUCCESS!  ",RED,BLACK);
		delay_ms(5000);
		TftClear(BLACK);

		Ext_Interrupt_Init( );
		EA = 1; 
		while(1);

  
}

void SMBus_Init (void)
{
   SMB0CF = 0x5D;      
   SMB0CF |= 0x80;    
}
void Timer1_Init (void)
{

#if ((SYSCLK/SMB_FREQUENCY/3) < 255)
   #define SCALE 1
      CKCON |= 0x08;                 
#elif ((SYSCLK/SMB_FREQUENCY/4/3) < 255)
   #define SCALE 4
      CKCON |= 0x01;
      CKCON &= ~0x0A;                
#endif

   TMOD = 0x20;       
   TH1 = -(SYSCLK/SMB_FREQUENCY/SCALE/3);

   TL1 = TH1;                        

   TR1 = 1;                          
}

void Timer3_Init (void)
{
   TMR3CN = 0x00;                    

   CKCON &= ~0x40;                   

   TMR3RL = -(SYSCLK/12/40);          
   TMR3 = TMR3RL;                      
   EIE1 |= 0x80;                       
   TMR3CN |= 0x04;                    
}



void OSCILLATOR_Init (void)
{
    OSCICN  = 0x83; 
	RSTSRC =	0x04;                
}
void SMB_Write (void)
{
   while (SMB_BUSY);                  
   SMB_BUSY = 1;                       
   SMB_RW = 0;                       
   STA = 1;                          
}
void SMB_Read (void)
{
   while (SMB_BUSY); 
   SMB_BUSY = 1;     
   SMB_RW = 1;     
   STA = 1;  
   while (SMB_BUSY); 
}

void Port_Init(void)
{
  	
  	P0MDIN = 0xFF;
	P1MDIN = 0XFF;
	P0MDOUT= 0XFC; 
	P1MDOUT= 0XFF;
  	P0SKIP = 0X00;
	P1SKIP = 0X00;
    XBR0   = 0x04; 
    XBR1   = 0x40; 
  	P0 = 0xFF;
}



void Ext_Interrupt_Init (void)    
{
    IT01CF    = 0x0d;
	IT0=1;
    IE        = 0x01;
}


void INT0_ISR (void) interrupt 0  
{

	EA=0;
	IE0=0; 

	P0MDOUT= 0xff;
	WR_CONTROL_PIN=1;
	Cmos_CS_PIN=1;

	TftWrite(0x0020,0x0000);
	TftWrite(0x0021,0x013f); 
	TftWrite(0x0050,0x00);
	TftWrite(0x0051,239);
	TftWrite(0x0052,0x00);
	TftWrite(0x0053,319);
	TftWrite(0x0003,0x1018);
	TftWrite16(0x0022);


	Tft_RS_PIN=1;
	Tft_WR_PIN=0;


	WR_CONTROL_PIN=0;
	Cmos_CS_PIN=0;

		EA=1;

}

void SMBus_ISR (void) interrupt 7
{

   static char i;                      

   static bit SEND_START = 0;         

   switch (SMB0CN & 0xF0)              
   {
      case SMB_MTSTA:
         SMB0DAT = TARGET;           
         SMB0DAT &= 0xFE; 
         SMB0DAT |= SMB_RW;  
         STA = 0; 
         i = 0; 
         break;
      case SMB_MTDB:
         if (ACK)  
         {                           
            if (SEND_START)
            {
               STA = 1;
               SEND_START = 0;
               break;
            }
            if(SMB_SENDWORDADDR)     
            {
               SMB_SENDWORDADDR = 0;   
               SMB0DAT = WORD_ADDR;   

               if (SMB_RANDOMREAD)
               {
                  SEND_START = 1; 
                  SMB_RW = READ;
               }

               break;
            }

            if (SMB_RW==WRITE)  
            {

               if (i < SMB_DATA_LEN)  
               {

                  SMB0DAT = *pSMB_DATA_OUT;
                  pSMB_DATA_OUT++;
                  i++;
               }
               else
               {
                 STO = 1;           
                 SMB_BUSY = 0;  
               }
            }
            else {} 
         }
         else                      
         {
            if(SMB_ACKPOLL)
            {
               STA = 1;           
            }
            else
            {
               FAIL = 1;   
            }        
         }
         break;
      case SMB_MRDB:
         if ( i < SMB_DATA_LEN )      
         {
            *pSMB_DATA_IN = SMB0DAT;   
            pSMB_DATA_IN++;            
            i++;                       
            ACK = 1;                  
                                      

         }

         if (i == SMB_DATA_LEN)        
         {
            SMB_BUSY = 0;              
            ACK = 0;                   
                                       
            STO = 1;                 
         }

         break;

      default:
         FAIL = 1;                    
                                     
         break;
   }

   if (FAIL)                           
   {
      SMB0CF &= ~0x80;                
      SMB0CF |= 0x80;
      STA = 0;
      STO = 0;
      ACK = 0;

      SMB_BUSY = 0;                  

      //FAIL = 0;
   }

   SI = 0;                          
}


void Timer3_ISR (void) interrupt 14
{
   SMB0CF &= ~0x80;                  
   SMB0CF |= 0x80;                  
   TMR3CN &= ~0x80;                    
   STA = 0;
   SMB_BUSY = 0;                     
}
void Tft_Init(void)
{
	 	
	Tft_RST_PIN=1;
	delay_ms(10); 
	Tft_RST_PIN=0;
	delay_ms(10);
	Tft_RST_PIN=1;
	delay_ms(150);


		TftWrite(0x00e5, 0x8000);	
		TftWrite(0x0000, 0x0001);	
		TftWrite(0x0001, 0x0100);
		TftWrite(0x0002, 0x0700);
	#if AM==000       
		TftWrite(0x0003,0x1000);
	#elif AM==001        
		TftWrite(0x0003,0x1008);      
	#elif AM==010  
	    TftWrite(0x0003,0x1010);        
	#elif AM==011
		TftWrite(0x0003,0x1018);
	#elif AM==100  
		TftWrite(0x0003,0x1020);      
	#elif AM==101  
		TftWrite(0x0003,0x1028);      
	#elif AM==110  
		TftWrite(0x0003,0x1030);      
	#elif AM==111  
		TftWrite(0x0003,0x1038);
	#endif
		TftWrite(0x0004, 0x0000);
		TftWrite(0x0008, 0x0202);	
		TftWrite(0x0009, 0x0000);	
		TftWrite(0x000A, 0x0000);
		TftWrite(0x000C, 0x0000);	
		TftWrite(0x000D, 0x0000);	
		TftWrite(0x000F, 0x0000);	
		TftWrite(0x0010, 0x0000);	
		TftWrite(0x0011, 0x0007);	
		TftWrite(0x0012, 0x0000);	
		TftWrite(0x0013, 0x0000);	
		delay_ms(1000);
		TftWrite(0x0010, 0x17B0);	
		TftWrite(0x0011, 0x0007);	
		delay_ms(1000);
		TftWrite(0x0012, 0x013A);	
		delay_ms(1000);
		TftWrite(0x0013, 0x1A00);	
		TftWrite(0x0029, 0x000c);		
		delay_ms(1000);
		TftWrite(0x0030, 0x0000);	
		TftWrite(0x0031, 0x0505);	
		TftWrite(0x0032, 0x0004);	
		TftWrite(0x0035, 0x0006);	
		TftWrite(0x0036, 0x0707);	
		TftWrite(0x0037, 0x0105);	
		TftWrite(0x0038, 0x0002);	
		TftWrite(0x0039, 0x0707);	
		TftWrite(0x003C, 0x0704);	
		TftWrite(0x003D, 0x0807);	
		TftWrite(0x0050, 0x0000);
		TftWrite(0x0051, 0x00EF);
		TftWrite(0x0052, 0x0000);
		TftWrite(0x0053, 0x013F);
		TftWrite(0x0060, 0x2700);
		TftWrite(0x0061, 0x0001);
		TftWrite(0x006A, 0x0000);
	#if AM==000         
		TftWrite(0x0020,0x00ef);
		TftWrite(0x0021,0x013f);      
	#elif AM==001
		TftWrite(0x0020,0x00ef);
		TftWrite(0x0021,0x013f);      
	#elif AM==010
		TftWrite(0x0020,0x0000);
		TftWrite(0x0021,0x013f);      
	#elif AM==011
		TftWrite(0x0020,0x0000);
		TftWrite(0x0021,0x013f);       
	#elif AM==100
		TftWrite(0x0020,0x00ef);
		TftWrite(0x0021,0x0000);      
	#elif AM==101  
		TftWrite(0x0020,0x00ef);
		TftWrite(0x0021,0x0000);      
	#elif AM==110
		TftWrite(0x0020,0x0000);
		TftWrite(0x0021,0x0000);      
	#elif AM==111
		TftWrite(0x0020,0x0000);
		TftWrite(0x0021,0x0000);         
	#endif  	
		TftWrite(0x0080, 0x0000);	
		TftWrite(0x0081, 0x0000);
		TftWrite(0x0082, 0x0000);
		TftWrite(0x0083, 0x0000);
		TftWrite(0x0084, 0x0000);	
		TftWrite(0x0085, 0x0000);
		TftWrite(0x0090, 0x0010);	
		TftWrite(0x0092, 0x0000);
		TftWrite(0x0093, 0x0003);
		TftWrite(0x0095, 0x0110);
		TftWrite(0x0097, 0x0000);	
		TftWrite(0x0098, 0x0000);	
		TftWrite(0x0007, 0x0173);	
		delay_ms(1000);


  
}


void Tft_WR_DATA16(unsigned int inputdata)
{
     unsigned int inputdatah;
	 unsigned int inputdatal;
	 unsigned int x;
	 x=inputdata;
	 inputdatal=inputdata&0x00ff;
	 inputdatah=(inputdata&0xff00)>>8;
	 P1MDOUT= 0XFF;
	 Tft_RS_PIN=1;
	 P1=0x00;
	 P1=inputdatah; 
     Tft_WR_PIN=0;
     Tft_WR_PIN=1;
	 P1=0x00;
	 P1=inputdatal;
     Tft_WR_PIN=0;
     Tft_WR_PIN=1;	 
	 P1MDOUT= 0X00;
  
}


void TftWrite16(unsigned int index)
{   
     unsigned int indexh;
	 unsigned int indexl;
	 indexh=(index&0xff00)>>8;
	 indexl=index&0x00ff;
	 P1MDOUT= 0XFF;
	 Tft_RS_PIN=0;

	 P1=0x00;
     P1=indexh; 
	 Tft_WR_PIN=0;
     Tft_WR_PIN=1;

     P1=0x00;
	 P1=indexl;

	 Tft_WR_PIN=0;
     Tft_WR_PIN=1;

	 P1MDOUT= 0X00;

}


void TftWrite(unsigned int index,unsigned int inputdata)
{
    unsigned int x;
	unsigned int y;
    unsigned int indexh;
	unsigned int indexl;
	unsigned int inputdatah;
	unsigned int inputdatal;
	x=index;
	y=inputdata;
    indexh=(index&0xff00)>>8;
	indexl=index&0x00ff;
	inputdatah=(inputdata&0xff00)>>8;
	inputdatal=inputdata&0x00ff;
	P1MDOUT= 0XFF;
	Tft_RS_PIN= 0;

	P1=0;	
	P1=indexh;
	Tft_WR_PIN=0;   
	Tft_WR_PIN=1;
	P1=0;
	P1=indexl; 
	Tft_WR_PIN=0;   
	Tft_WR_PIN=1;

	Tft_RS_PIN=1; 
	P1=0;
	P1=inputdatah;
    Tft_WR_PIN=0;	
    Tft_WR_PIN=1;
	P1=0;
	P1=inputdatal; 
    Tft_WR_PIN=0;	
    Tft_WR_PIN=1;

	P1MDOUT= 0X00;

}

void TftClear(unsigned int Color)
{
  unsigned long index = 0;

	TftWrite(0x0050,0x00);
	TftWrite(0x0051,239);
	TftWrite(0x0052,0x00);
	TftWrite(0x0053,319);
	TftWrite(0x0020,0x0000);
	TftWrite(0x0021,0x0000);  
	TftWrite16(0x0022);    

  for(index = 0; index < 76800; index++)
  {
    Tft_WR_DATA16(Color);
   
  } 
 
}

void TftWR_SingleStr(unsigned char inputdata,unsigned int color,unsigned int xcolor)
{

  unsigned char avl,i,n;
  TftWrite16(0x0022);  
  for (i=0;i<16;i++)
  { 
    avl=(english[inputdata-32][i]);
	  for (n=0;n<8;n++)
	   {
	     if(avl&0x80) Tft_WR_DATA16(color);
             else Tft_WR_DATA16(xcolor);
           
	     avl<<=1;
	   }
	}
}
void TftWR_Str(unsigned int x,unsigned int y,unsigned char *s,unsigned int color,unsigned int xcolor)
{
 unsigned int k=0;
 while (*s) 
  {
     Tft_SetBackground(y,x+k,15,8);
     TftWR_SingleStr( *s,color,xcolor);
     k=k+8;
     s++;
  
  }

}  


void Tft_SetBackground(unsigned int StartX,unsigned int StartY,unsigned int LongX,unsigned int LongY)
{
  
#if AM==000    
	Tft_SetCursor(StartX+LongX-1,312-StartY+LongY-1);

#elif AM==001
	Tft_SetCursor(StartX+LongX-1,312-StartY+LongY-1);
     
#elif AM==010
	Tft_SetCursor(StartX,312-StartY+LongY-1);
     
#elif AM==011 
	Tft_SetCursor(StartX,312-StartY+LongY-1);
     
#elif AM==100
	Tft_SetCursor(StartX+LongX-1,312-StartY);     
     
#elif AM==101
	Tft_SetCursor(StartX+LongX-1,312-StartY);     
     
#elif AM==110
	Tft_SetCursor(StartX,312-StartY); 
     
#elif AM==111
	Tft_SetCursor(StartX,312-StartY);  
     
#endif
     
	TftWrite(0x0050,StartX);
	TftWrite(0x0051,StartX+LongX-1);
	TftWrite(0x0052,312-StartY);
	TftWrite(0x0053,312-StartY+LongY-1);
}

void Tft_SetCursor(unsigned int Xpos, unsigned int Ypos)
{
 
  TftWrite(0x20, Xpos);
  TftWrite(0x21, Ypos);
}



void Set_Cmos7670Reg(void)
{


	WrCmos7670(0x3a, 0x04);
	WrCmos7670(0x40, 0xd0);
	WrCmos7670(0x12, 0x14);
	WrCmos7670(0x32, 0x80);
	WrCmos7670(0x17, 0x16);
	WrCmos7670(0x18, 0x04);
	WrCmos7670(0x19, 0x02);
	WrCmos7670(0x1a, 0x7b);
	WrCmos7670(0x03, 0x06);
	WrCmos7670(0x0c, 0x04);
	WrCmos7670(0x3e, 0x00);
	WrCmos7670(0x70, 0x3a);
	WrCmos7670(0x71, 0x35);
	WrCmos7670(0x72, 0x11);
	WrCmos7670(0x73, 0x00);
	WrCmos7670(0xa2, 0x02);
	WrCmos7670(0x11, 0x81);
	
	WrCmos7670(0x7a, 0x20);
	WrCmos7670(0x7b, 0x1c);
	WrCmos7670(0x7c, 0x28);
	WrCmos7670(0x7d, 0x3c);
	WrCmos7670(0x7e, 0x55);
	WrCmos7670(0x7f, 0x68);
	WrCmos7670(0x80, 0x76);
	WrCmos7670(0x81, 0x80);
	WrCmos7670(0x82, 0x88);
	WrCmos7670(0x83, 0x8f);
	WrCmos7670(0x84, 0x96);
	WrCmos7670(0x85, 0xa3);
	WrCmos7670(0x86, 0xaf);
	WrCmos7670(0x87, 0xc4);
	WrCmos7670(0x88, 0xd7);
	WrCmos7670(0x89, 0xe8);
	
	WrCmos7670(0x13, 0xe0);
	WrCmos7670(0x00, 0x00);
	
	WrCmos7670(0x10, 0x00);
	WrCmos7670(0x0d, 0x00);
	WrCmos7670(0x14, 0x28);
	WrCmos7670(0xa5, 0x05);
	WrCmos7670(0xab, 0x07);
	WrCmos7670(0x24, 0x75);
	WrCmos7670(0x25, 0x63);
	WrCmos7670(0x26, 0xA5);
	WrCmos7670(0x9f, 0x78);
	WrCmos7670(0xa0, 0x68);
	WrCmos7670(0xa1, 0x03);
	WrCmos7670(0xa6, 0xdf);
	WrCmos7670(0xa7, 0xdf);
	WrCmos7670(0xa8, 0xf0);
	WrCmos7670(0xa9, 0x90);
	WrCmos7670(0xaa, 0x94);
	WrCmos7670(0x13, 0xe5);

	WrCmos7670(0x0e, 0x61);
	WrCmos7670(0x0f, 0x4b);
	WrCmos7670(0x16, 0x02);
	WrCmos7670(0x1e, 0x07);
	WrCmos7670(0x21, 0x02);
	WrCmos7670(0x22, 0x91);
	WrCmos7670(0x29, 0x07);
	WrCmos7670(0x33, 0x0b);
	WrCmos7670(0x35, 0x0b);
	WrCmos7670(0x37, 0x1d);
	WrCmos7670(0x38, 0x71);
	WrCmos7670(0x39, 0x2a);
	WrCmos7670(0x3c, 0x78);
	WrCmos7670(0x4d, 0x40);
	WrCmos7670(0x4e, 0x20);
	WrCmos7670(0x69, 0x00);
	WrCmos7670(0x6b, 0x60);
	WrCmos7670(0x74, 0x19);
	WrCmos7670(0x8d, 0x4f);
	WrCmos7670(0x8e, 0x00);
	WrCmos7670(0x8f, 0x00);
	WrCmos7670(0x90, 0x00);
	WrCmos7670(0x91, 0x00);
	WrCmos7670(0x92, 0x00);
	WrCmos7670(0x96, 0x00);
	WrCmos7670(0x9a, 0x80);
	WrCmos7670(0xb0, 0x84);
	WrCmos7670(0xb1, 0x0c);
	WrCmos7670(0xb2, 0x0e);
	WrCmos7670(0xb3, 0x82);
	WrCmos7670(0xb8, 0x0a);



	WrCmos7670(0x43, 0x14);
	WrCmos7670(0x44, 0xf0);
	WrCmos7670(0x45, 0x34);
	WrCmos7670(0x46, 0x58);
	WrCmos7670(0x47, 0x28);
	WrCmos7670(0x48, 0x3a);
	WrCmos7670(0x59, 0x88);
	WrCmos7670(0x5a, 0x88);
	WrCmos7670(0x5b, 0x44);
	WrCmos7670(0x5c, 0x67);
	WrCmos7670(0x5d, 0x49);
	WrCmos7670(0x5e, 0x0e);
	WrCmos7670(0x64, 0x04);
	WrCmos7670(0x65, 0x20);
	WrCmos7670(0x66, 0x05);
	WrCmos7670(0x94, 0x04);
	WrCmos7670(0x95, 0x08);
	WrCmos7670(0x6c, 0x0a);
	WrCmos7670(0x6d, 0x55);
	WrCmos7670(0x6e, 0x11);
	WrCmos7670(0x6f, 0x9f);
	WrCmos7670(0x6a, 0x40);
	WrCmos7670(0x01, 0x40);
	WrCmos7670(0x02, 0x40);
	WrCmos7670(0x13, 0xe7);
	WrCmos7670(0x15, 0x00);  
	
	
	WrCmos7670(0x4f, 0x80);
	WrCmos7670(0x50, 0x80);
	WrCmos7670(0x51, 0x00);
	WrCmos7670(0x52, 0x22);
	WrCmos7670(0x53, 0x5e);
	WrCmos7670(0x54, 0x80);
	WrCmos7670(0x58, 0x9e);
	
	WrCmos7670(0x41, 0x08);
	WrCmos7670(0x3f, 0x00);
	WrCmos7670(0x75, 0x05);
	WrCmos7670(0x76, 0xe1);
	WrCmos7670(0x4c, 0x00);
	WrCmos7670(0x77, 0x01);
	WrCmos7670(0x3d, 0xc2);	
	WrCmos7670(0x4b, 0x09);
	WrCmos7670(0xc9, 0x60);
	WrCmos7670(0x41, 0x38);
	WrCmos7670(0x56, 0x40);
	
	WrCmos7670(0x34, 0x11);
	WrCmos7670(0x3b, 0x02); 
								
	WrCmos7670(0xa4, 0x89);
	WrCmos7670(0x96, 0x00);
	WrCmos7670(0x97, 0x30);
	WrCmos7670(0x98, 0x20);
	WrCmos7670(0x99, 0x30);
	WrCmos7670(0x9a, 0x84);
	WrCmos7670(0x9b, 0x29);
	WrCmos7670(0x9c, 0x03);
	WrCmos7670(0x9d, 0x4c);
	WrCmos7670(0x9e, 0x3f);
	WrCmos7670(0x78, 0x04);
	
}

unsigned char  WrCmos7670(unsigned char RegAddress, unsigned char RegData)
{
   while (SMB_BUSY);                   
   SMB_BUSY = 1;                      
   TARGET = EEPROM_ADDR;     
   SMB_RW = WRITE;        
   SMB_SENDWORDADDR = 1;   
   SMB_RANDOMREAD = 0;  
   SMB_ACKPOLL = 1;  
   WORD_ADDR = RegAddress;  

   SMB_SINGLEBYTE_OUT = RegData; 
   pSMB_DATA_OUT = &SMB_SINGLEBYTE_OUT;

   SMB_DATA_LEN = 1;    
   STA = 1;
  while(SMB_BUSY); 
   return(~FAIL);

}

unsigned char Cmos7670_init(void)
{
	unsigned char mmm;	
	unsigned int i=0;
	
	InitI2C();

	mmm=0x80;
	if(0==WrCmos7670(0x12, mmm)) 
	{
		return 0 ;
	}
	delay_ms(10);

  	Set_Cmos7670Reg();
	if(FAIL) 
	{
		return 0 ;
	}

	return 1; 
} 


void InitI2C(void)
{
   
	P0MDIN = 0XFF;
	P0MDOUT= 0XFF;

   
}

void delay_us(unsigned int time)
 {     
   while (time--);
 }		  

	 
void delay_ms(unsigned int time)
	 {
	  while(time--) delay_us(100);  		 
	 }
	 
void delay( unsigned long cnt)
{
	 unsigned int i = 0;

	while(cnt--)
	{
		for (i = 0; i < 2; i++)
		{
		//__nop();
		}
	}
}