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

📄 sccb.c

📁 SCCB协议的KEIL UVISION3的编程源码。原创
💻 C
字号:
 // OmniVision Serial Camera Control Bus (SCCB) Functional File.
//低电平持续时间	 最少1,3ns	 ------CLK
//高电平持续时间最少为	O。6ns	    -----CLK																	
#include <Reg52.h>
#include <Intrins.h>
#include<stdio.h>
sbit p32=P3^2;
sbit SIO_C=P1^1;
sbit SIO_D=P1^0;
sbit  IO=	P1^2;
//sbit SIO_D=P1^2;
//chip slave ID is 0X42(for write) and 0X43(for read)
#define OV7620_ID  0x42
void serial_init(void)        //改变你需要的位即可!
{
                 //串口初始化,晶振11.0592Hz,波特率9600
	SCON = 0x50; // Setup serial port control register 
					// Mode 1: 8-bit uart var. baud rate 
					// REN: enable receiver
	PCON &= 0x7F; // Clear SMOD bit in power ctrl reg 
					// This bit doubles the baud rate 
	TMOD &= 0xCF; // Setup timer/counter mode register 
					// Clear M1 and M0 for timer 1 
	TMOD |= 0x20; //Set M1 for 8-bit autoreload timer 
	TH1 = 0xFD; // Set autoreload value for timer 1 
					// 9600 baud with 11.0592 MHz xtal 
	TR1 = 1; // Start timer 1 ->TCON
    TI = 1; // Set TI to indicate ready to xmit.
	ES=1;
	EA=1;
   }
   void delay()
{
 unsigned char i,j,k;
  for(i=0;i<10;i++)
  {
   for(j=0;j<20;j++)
   {
    for(k=0;k<248;k++);
   }
  } 
}

void nops(void)		 //36us
{
 //_nop_();
 //_nop_();
 //_nop_();
// _nop_();
 //_nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
}

void hnops(void)
{
 //_nop_();
 //_nop_();
 //_nop_();
   _nop_();
   _nop_();
}
void OV7620_SCCB_START(void)
{
   SIO_C=1;
   nops();
   SIO_D=1;
   nops();
   SIO_D=0;
   nops();
   SIO_C=0;  
   nops();   

}
void OV7620_SCCB_END(void)
{
  //SIO_C=1;
  //nops();
   SIO_D=0;
   nops();
// SIO_D=1;
// nops();
   SIO_C=1;
   nops();
   SIO_D=1;   

}

bit  OV7620_SCCB_Readbit(void )
{
   bit temp=0;

    nops();  
    SIO_C=1;
    //input 
    nops();
    if(SIO_D)
    temp =1;
     nops();
	SIO_C=0;
    nops();   
//	 SBUF=temp;
//		 delay();

	 return  temp;

} 
void  OV7620_SCCB_Writebit(bit a)
{
    nops();  
	SIO_D=a;
    nops();
	 nops();
    SIO_C=1;
    nops();
    SIO_C=0;
    nops();      
}


void OV7620_SCCB_Writechar(unsigned char   b)
{
  	 int i = 0; 
	  SBUF=b;
		 delay(); 
    /* data */  
    for (i = 0; i < 8; i++ ) {  
        OV7620_SCCB_Writebit(b & 0x80);  
        b <<= 1;  
    }  
  
    /* 9th bit - Don't care */  
    OV7620_SCCB_Writebit(1);  
	 nops();
	 nops();
}

unsigned char  V7620_SCCB_Readchar( void)
{  
    char  i;  
    unsigned char  tmp = 0;  
    unsigned char  val;
    bit    l=0;
  	 SIO_D=1;
  	  nops();
	   nops();
    for (i = 7; i >= 0; i--) 	 //0-255  溢出     没有报错    
	{
	   l=	OV7620_SCCB_Readbit();
	    tmp<<=1;
	   if(l)
	   {//tmp = tmp | (1<<i);  
		 tmp=tmp | 0x01;
	   
	   } 
	} 

	nops();
    /* 9th bit - N.A. */  
    OV7620_SCCB_Writebit(1);  
  	 nops();
	 nops();
    val = tmp & 0xff; 
	  return  val;
}     

unsigned char OV7620_Read_SCCB_CMOS(unsigned char DeviceAddress,unsigned char RegisterAddress)
{
    
 unsigned char  val=0;
    //	 SBUF=DeviceAddress;
	//	 delay();
	//	 SBUF=RegisterAddress;
	//	 delay();

  /* 2-phase write */  
 OV7620_SCCB_START();        //Start
 OV7620_SCCB_Writechar(DeviceAddress);
 OV7620_SCCB_Writechar(RegisterAddress);
 OV7620_SCCB_END();    //stop
  nops();
   nops();
  /* 2-phase read */  
 OV7620_SCCB_START();        //Start
 OV7620_SCCB_Writechar(DeviceAddress | 0X01);
  val=V7620_SCCB_Readchar();  

 OV7620_SCCB_END();    //stop

 return val;
}

void  OV7620_Write_SCCB_COMS(unsigned char DeviceAddress,unsigned char RegisterAddress, unsigned char Write_Data)
{
  	  /* 3-phase write */  
 OV7620_SCCB_START();        //Start
 OV7620_SCCB_Writechar(DeviceAddress);
 OV7620_SCCB_Writechar(RegisterAddress);
 OV7620_SCCB_Writechar( Write_Data);
 OV7620_SCCB_END();    //stop


}
 //相当于一个二相写和一个二相读操作
unsigned char Read_CMOS(unsigned char DeviceAddress,unsigned char RegisterAddress)
{
 unsigned char  bdata  Buffer_Data;

 bit Buffer_Data0=Buffer_Data^0;
 bit Buffer_Data1=Buffer_Data^1;
 bit Buffer_Data2=Buffer_Data^2;
 bit Buffer_Data3=Buffer_Data^3;
 bit Buffer_Data4=Buffer_Data^4;
 bit Buffer_Data5=Buffer_Data^5;
 bit Buffer_Data6=Buffer_Data^6;
 bit Buffer_Data7=Buffer_Data^7;

 Buffer_Data= 0x42;
 //printf("write data to register 0x 1c is %x\n",Buffer_Data);
 F0=0; 
 //SIO_E=0;
 //nops();

 OV7620_SCCB_START();      //Start



 SIO_D=Buffer_Data7;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit7
 
 SIO_D=Buffer_Data6;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit6

 SIO_D=Buffer_Data5;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit5

 SIO_D=Buffer_Data4;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit4

 SIO_D=Buffer_Data3;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit3

 SIO_D=Buffer_Data2;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit2

 SIO_D=Buffer_Data1;
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit1

 SIO_D=Buffer_Data0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit0
 SIO_D=1;	   // don't care  1
 nops();
 nops();
 nops();


 SIO_C=1;
  nops();
 
 SIO_C=0;     
 nops();       
 //	printf("send DeviceAddress success\n");
 
 Buffer_Data=RegisterAddress; 
 //printf("write data to register 0x 1c is %x\n",Buffer_Data);
 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit7
 
 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit6

 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit5

 SIO_D=1;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit4

 SIO_D=1;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit3

 SIO_D=1;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit2

 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit1

 SIO_D=1;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit0
 SIO_D=1;	 // don't care  0 
 nops();
 nops();
 nops();

 SIO_C=1;
  nops();
 SIO_C=0;     
 nops();        //check Ack    don't care  1
//	printf("send RegisterAddress success\n");


 OV7620_SCCB_END();    //stop

  nops();
  nops();
  nops();
  nops();

 OV7620_SCCB_START();        //Start


Buffer_Data=0X43; // 0x43
// printf("write data to register 0x 1c is %x\n",Buffer_Data);
 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit7
 
 SIO_D=1;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit6

 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit5

 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit4

 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit3

 SIO_D=0;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit2

 SIO_D=1;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit1

 SIO_D=1;
  nops();
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit0
 SIO_D=1;	  //on't care  1
 nops();
 nops();
 nops();

 SIO_C=1;
  nops();


SIO_D=1; //INPUT     
 SIO_C=0;
  
 nops();       
//	printf("send DeviceAddress success\n");
//	printf("read data\n");
//read data 
 Buffer_Data=0x00; 
  nops();
 SIO_C=1;
  //input 
 nops();
 if(SIO_D)
 Buffer_Data|=0x80;
 nops();

 //SIO_D=1; //INPUT     
 SIO_C=0;
 nops();        //bit7

 SIO_C=1;
 nops();
 if(SIO_D)
 Buffer_Data|=0x40;
 nops();
 SIO_C=0;
 nops();         //bit6

 SIO_C=1;
 nops();
 if(SIO_D)
 Buffer_Data|=0x20;
 nops();

 //SIO_D=1; //INPUT     
 SIO_C=0;
 nops();         //bit5

 SIO_C=1;
 nops();
 if(SIO_D)
 Buffer_Data|=0x10;
 nops();
// SIO_D=1; //INPUT     
 SIO_C=0;
 nops();          //bit4

 SIO_C=1;
 nops();
 if(SIO_D)
 Buffer_Data|=0x08;
 nops();
 //SIO_D=1; //INPUT     
 SIO_C=0;
 nops();          //bit3

 SIO_C=1;
 nops();
 if(SIO_D)
 Buffer_Data|=0x04;
 nops();
 //SIO_D=1; //INPUT     
SIO_C=0;
 nops();          //bit2

 SIO_C=1;
 nops();
 if(SIO_D)
 Buffer_Data|=0x02;
 nops();
 //SIO_D=1; //INPUT     
SIO_C=0;
 nops();          //bit1

 SIO_C=1;
 nops();
 if(SIO_D)
 Buffer_Data|=0x01;
 nops();
 SIO_C=0;
 nops();          //bit0
 SIO_D=1;	// 低电平应答信号??????还是高电平??????
 nops();


 SIO_C=1;
  nops();
 if(SIO_D)
    { 
      if(SIO_D)
      F0=1;
    }
 else
   {
     nops();
//	printf("NA  IS O \n");
 //   printf("read data failed\n");
   }
 SIO_C=0;     
 nops();        //check Ack  	     NA BIT    MUST BE  1 

 
 //SIO_C=1;
  //nops();
 SIO_D=0;
 nops();
 //SIO_D=1;
 //nops();
 SIO_C=1;
  nops();
 SIO_D=1;       //stop
// printf("read data success\n");

// printf("read  data from register 0x 1c is %x\n",Buffer_Data);

 return Buffer_Data;
}

bit Write_COMS(unsigned char DeviceAddress,unsigned char RegisterAddress, unsigned char Write_Data)
{
 ACC=DeviceAddress;
 F0=0; 
 //SIO_E=0;
 //nops();

 SIO_C=1;
 nops();
 SIO_D=1;
 nops();
 SIO_D=0;
 nops();
 SIO_C=0;  
 nops();         //Start

 SIO_D=ACC^7;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit7
 
 SIO_D=ACC^6;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit6

 SIO_D=ACC^5;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit5

 SIO_D=ACC^4;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit4

 SIO_D=ACC^3;
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit3

 SIO_D=ACC^2;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit2

 SIO_D=ACC^1;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit1

 SIO_D=ACC^0;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit0
 SIO_D=1;
 nops();
 SIO_C=1;
 
    nops();
 SIO_C=0;     
 nops();        //check Ack    don't care
	printf("send DeviceAddress success\n");
 ACC=RegisterAddress; 

 SIO_D=ACC^7;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit7
 
 SIO_D=ACC^6;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit6

 SIO_D=ACC^5;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit5

 SIO_D=ACC^4;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit4

 SIO_D=ACC^3;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit3

 SIO_D=ACC^2;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit2

 SIO_D=ACC^1;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit1

 SIO_D=ACC^0;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit0
 SIO_D=1;
 nops();
 SIO_C=1;
 
    nops();
 SIO_C=0;     
 nops();        //check Ack 	 don't care
	printf("send RegisterAddress success\n");
	printf("write data\n");
ACC=Write_Data; 

 SIO_D=ACC^7;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit7
 
 SIO_D=ACC^6;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit6

 SIO_D=ACC^5;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit5

 SIO_D=ACC^4;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit4

 SIO_D=ACC^3;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit3

 SIO_D=ACC^2;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();         //bit2

 SIO_D=ACC^1;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit1

 SIO_D=ACC^0;
 nops();  
 SIO_C=1;
 nops();
 SIO_C=0;
 nops();        //bit0
 SIO_D=1;
 nops();
 SIO_C=1;
    nops();
 SIO_C=0;     
 nops();        //check Ack 	dont't care   0
	printf("write data success\n ");

 //SIO_C=1;
// nops();  
 SIO_D=0;
 nops();
 //SIO_D=1;
 //nops();
 SIO_C=1;
  nops();
 SIO_D=1;       //stop

 return F0;
}

void main()
{
 unsigned char 	temp;
 //bit ack;
serial_init();

//ack= Write_COMS(OV7620_write,0x13,0x31);//8位数据格式,CCIR656格式
//ack= Write_COMS(OV7620_write,0x28,0x20); //逐行扫描模式
//ack= Write_COMS(OV7620_write,0x67,0x9a); //CCIR 601 YCrCb
//ack= Write_COMS(OV7620_write,0x13,0x31);		//
//ack= Write_COMS(OV7620_write,0x13,0x31);		//
while(1)
{
   if(p32==0)	//按键消抖动  
   {
	  delay();
     if(p32==0)
     { 
       while(p32==0);
	 
	   
	  temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x11);
	  OV7620_Write_SCCB_COMS(OV7620_ID,0x11,0x00);
	  temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x11);
	  SBUF=temp;
	  delay();

	  OV7620_Write_SCCB_COMS(OV7620_ID,0x70,0x81);
	  temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x70);
	  SBUF=temp;
	  delay();

	  OV7620_Write_SCCB_COMS(OV7620_ID,0x13,0x31);
	  temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x13);
	  SBUF=temp;
	  delay();
	 OV7620_Write_SCCB_COMS(OV7620_ID,0x28,0x20);
	 temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x28);
	 SBUF=temp;
	 delay();
	 OV7620_Write_SCCB_COMS(OV7620_ID,0x67,0x1a);
	 temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x67);
	 SBUF=temp;
	 delay();
	// OV7620_Write_SCCB_COMS(OV7620_ID,0x11,0x31);
	// temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x11);
	// SBUF=temp;
	// delay();
	 //SBUF=temp;
	 //delay();

	   OV7620_Write_SCCB_COMS(OV7620_ID,0x12,0x24);	//8 BIT  DATAOUT 
	   temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x12);
       SBUF=temp;
       delay();

	// temp=OV7620_Read_SCCB_CMOS(OV7620_ID,0x13);
//	  SBUF=temp;
	//  delay();
    //
	 }
   
   };
    //printf("read data from register 0x 1d is %d\n",temp);

};

}

⌨️ 快捷键说明

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