Skip to content

Instantly share code, notes, and snippets.

@muhammadyaseen
Forked from freespace/gist:2585921
Last active February 22, 2023 08:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhammadyaseen/75490348a4644dcbc70f to your computer and use it in GitHub Desktop.
Save muhammadyaseen/75490348a4644dcbc70f to your computer and use it in GitHub Desktop.
Arduino SCCB Driver for OV7670 Camera Module
#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 Initialization
{
pinMode(SIO_C,OUTPUT);
pinMode(SIO_D,OUTPUT);
digitalWrite(SIO_C,HIGH);
digitalWrite(SIO_D,HIGH);
Serial.println("InitSCCB - Port Direction Set & Set High OK");
}
void StartSCCB(void) //SCCB Start
{
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 Stop
{
//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);
}
bool SCCBWrite(byte m_data)
{
unsigned char j;
bool success;
for ( j = 0; j < 8; j++ ) //Loop transmit data 8 times
{
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);
}
digitalWrite(8,LOW); //debug
pinMode(SIO_D,INPUT); // I pass a bus of SIO_D to slave (OV7670)
digitalWrite(SIO_D,LOW); // Pull-up prevention --this line is not present in embedded programmer lib
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(8,HIGH); //debug
digitalWrite(SIO_C,HIGH);
delayMicroseconds(SIO_CLOCK_DELAY);
digitalWrite(8,LOW); //debug
if(digitalRead(SIO_D)==HIGH)
success= false;
else
success= true;
digitalWrite(SIO_C,LOW);
delayMicroseconds(SIO_CLOCK_DELAY);
pinMode(SIO_D,OUTPUT); //Return the bus of SIO_D to master (Arduino)
//delayMicroseconds(SIO_CLOCK_DELAY);
//digitalWrite(SIO_D,LOW);
//delayMicroseconds(SIO_CLOCK_DELAY);
//pinMode(SIO_C,OUTPUT); //Return the bus of SIO_C to master (Arduino)
return success;
}
bool InitOV7670(void)
{
char temp = 0x80;
InitSCCB();
if( ! WriteOV7670(0x12, temp) ) //Reset SCCB
{
Serial.println("Resetting SCCB Failed");
return false;
}
return true;
}
////////////////////////////
//To write to the OV7660 register:
// function Return value: Success = 1 failure = 0
bool WriteOV7670(char regID, char regDat)
{
StartSCCB();
if( ! SCCBWrite(0x42) )
{
Serial.println(" Write Error 0x42");
StopSCCB();
return false;
}
delayMicroseconds(SIO_CLOCK_DELAY);
if( ! SCCBWrite(regID) )
{
StopSCCB();
return false;
}
delayMicroseconds(SIO_CLOCK_DELAY);
if( ! SCCBWrite(regDat) )
{
StopSCCB();
return false;
}
StopSCCB();
return true;
}
@Madhuri1398
Copy link

Can you share the hardware setup

@muhammadyaseen
Copy link
Author

Can you share the hardware setup

Hi @Madhuri1398 , it was a very long time ago. I don't really remember hardware details at this point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment