public
Last active

Backup of some arduino code to handle OV7670

  • Download Gist
gistfile1.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
#define SIO_C 2
#define SIO_D 4
#define SIO_CLOCK_DELAY 100
 
void setup()
{
pinMode(8,OUTPUT);
// while(1)
// {
// digitalWrite(8,HIGH);
// delayMicroseconds(SIO_CLOCK_DELAY);
// digitalWrite(8,LOW);
// delayMicroseconds(SIO_CLOCK_DELAY);
// }
Serial.begin(9600);
Serial.println("Start InitOV7670 test program");
digitalWrite(8,HIGH);delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(8,LOW);delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(8,HIGH);delayMicroseconds(SIO_CLOCK_DELAY);
 
if(InitOV7670())
Serial.println("InitOV7670 OK");
else
Serial.println("InitOV7670 NG");
}
 
void loop()
{
}
 
 
void InitSCCB(void) //SCCB初期化
{
pinMode(SIO_C,OUTPUT);
pinMode(SIO_D,OUTPUT);
digitalWrite(SIO_C,HIGH);
digitalWrite(SIO_D,HIGH);
Serial.println("InitSCCB - PortDirectionSet & Set High OK");
}
 
void StartSCCB(void) //SCCBスタート
{
Serial.println("StartSCCB");
 
digitalWrite(SIO_D,HIGH);
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(SIO_C,HIGH);
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(SIO_D,LOW);
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(SIO_C,LOW);
delayMicroseconds(SIO_CLOCK_DELAY);
}
 
void StopSCCB(void) //SCCBストップ
{
//Serial.println("StopSCCB");
 
digitalWrite(SIO_D,LOW);
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(SIO_C,HIGH);
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(SIO_D,HIGH);
delayMicroseconds(SIO_CLOCK_DELAY);
}
 
char SCCBWrite(byte m_data)
{
unsigned char j,tem;
//Serial.print("SCCBWrite 0x");
//Serial.println(m_data,HEX);
 
//Serial.print("SCCBWrite");
for(j=0;j<8;j++) //循环8次发送数据
{
if((m_data<<j)&0x80)
{
digitalWrite(SIO_D,HIGH);
}
else
{
digitalWrite(SIO_D,LOW);
}
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(SIO_C,HIGH);
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(SIO_C,LOW);
delayMicroseconds(SIO_CLOCK_DELAY);
}
 
//Serial.println("");
//delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(8,LOW); //debug
pinMode(SIO_D,INPUT); //SIO_Dのバスをスレーブ(OV7670)に渡す
digitalWrite(SIO_D,LOW); //プルアップ防止
delayMicroseconds(SIO_CLOCK_DELAY);
 
digitalWrite(8,HIGH); //debug
digitalWrite(SIO_C,HIGH);
delayMicroseconds(SIO_CLOCK_DELAY);
 
//Serial.println(" Write done");
digitalWrite(8,LOW); //debug
if(digitalRead(SIO_D)==HIGH)
{
//SIO_D=Hなら失敗
tem=0;
Serial.println("SCCBWrite NG");
}
else
{
//SIO_D=Lなら成功
tem=1;
//Serial.println("SCCBWrite OK");
}
digitalWrite(SIO_C,LOW);
delayMicroseconds(SIO_CLOCK_DELAY);
pinMode(SIO_D,OUTPUT); //SIO_Dのバスをマスター(Arduino)に戻す
//delayMicroseconds(SIO_CLOCK_DELAY);
//digitalWrite(SIO_D,LOW);
//delayMicroseconds(SIO_CLOCK_DELAY);
 
//pinMode(SIO_C,OUTPUT); //SIO_Cのバスをマスター(Arduino)に戻す
 
return tem;
}
 
int InitOV7670(void)
{
char temp;
int i=0;
 
InitSCCB();
 
temp=0x80;
if(WriteOV7670(0x12, temp)==0) //Reset SCCB
{
Serial.println("Resetting SCCB NG");
return 0 ;
}
//Serial.println("Resetting SCCB OK");
//delayMicroseconds(10);
 
// for(i=0;i<CHANGE_REG_NUM;i++)
// {
// if( 0==wrOV7670Reg(pgm_read_byte( &change_reg[i][0]),pgm_read_byte( &change_reg[i][1]) ))
// {
// return 0;
// }
// }
 
return 0x01; //ok
}
 
////////////////////////////
//機能:OV7660レジスタに書き込む
//返値:成功=1 失敗=0
int WriteOV7670(char regID, char regDat)
{
StartSCCB();
if(SCCBWrite(0x42)==0)
{
Serial.println(" Write Error 0x42");
StopSCCB();
return(0);
}
delayMicroseconds(SIO_CLOCK_DELAY);
if(SCCBWrite(regID)==0)
{
StopSCCB();
return(0);
}
delayMicroseconds(SIO_CLOCK_DELAY);
if(SCCBWrite(regDat)==0)
{
StopSCCB();
return(0);
}
StopSCCB();
return(1);
}

Hi! did you manage to activate the OV7670 from an Arduino? I am looking for a way to get pictures onto an SD card. Thanks!

So could you tell me how to connect 0v7670 to Arduino Uno?

I am just starting out with this camera. After looking over your code, I assume you are using pins 2 and 4 for data and clock for the IO pins on the camera. Am I correct that pin 8 appears to generate a 10mhz XCLK signal for the module? Also, does this sketch simply initialize the camera and it's registers or does it handle the D0 thru D7 parallel data pins as well?

It works!!
After over 100+hrs of squeezing life out of this camera, today for once, I get an 'InitOV7670 OK'! Next step, raw pixel data... (I also love how clean and easy your code is to follow - arigato)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.