#include "sccb.h"
#include "delay.h"
#include "STC_NEW_8051.h"
#define CAM_OV6620 0xC0
#define CAM_OV7620 0x42
#define CAM_OV7670 0x42
//#define SCCB_SDA P3^7
//#define SCCB_SCL P3^6
sbit SCCB_SDA = P0^0;
sbit SCCB_SCL = P0^1;
#define CLOCK_LOW() (SCCB_SCL = 0)
#define CLOCK_HIGH() (SCCB_SCL = 1)
#define DATA_LOW() (SCCB_SDA = 0)
#define DATA_HIGH() (SCCB_SDA = 1)
//void sccb_init(void)
//{
// GPIO_InitTypeDef GPIO_InitStructure;
//
// GPIO_InitStructure.GPIO_Pin = SCCB_SDA | SCCB_SCL;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
//}
//void CLOCK_LOW(void)
//{
// SCCB_SCL = 0;
//}
//void CLOCK_HIGH(void)
//{
// SCCB_SCL = 1;
//}
//
//void DATA_LOW(void)
//{
// SCCB_SDA = 0;
//}
//void DATA_HIGH(void)
//{
// SCCB_SDA = 1;
//}
void sccb_start(void)
{
CLOCK_HIGH();
DATA_HIGH();
DelayUs2x(1);
DATA_LOW();
DelayUs2x(1);
CLOCK_LOW();
DelayUs2x(1);
}
void sccb_end(void)
{
DATA_LOW();
DelayUs2x(1);
CLOCK_HIGH();
DelayUs2x(1);
DATA_HIGH();
DelayUs2x(1);
}
void sccb_sendbyte( unsigned char value )
{
unsigned char i=0; //并行数据转串行输出,串行数据输出的顺序为先高位再低位
for(i=0;i<8;i++)
{
if( value & 0x80 )
DATA_HIGH();
else
DATA_LOW();
DelayUs2x(1);
CLOCK_HIGH();
DelayUs2x(1);
CLOCK_LOW();
DelayUs2x(1);
DATA_LOW();
DelayUs2x(1);
value<<=1;
}
//第9位,Don’t Care
DATA_HIGH();
DelayUs2x(1);
CLOCK_HIGH();
DelayUs2x(1);
CLOCK_LOW();
DelayUs2x(1);
}
void sccb_receivebyte(unsigned char *value)
{
int i=0;
int svalue=0;
int pvalue = 0;
// DDRB &= ~(1<<SCCB_SDA); //把SDA输出改变为输入
// sda_configinput();
//串行数据转并行输入,高位在前
for(i=7;i>=0;i--)
{
CLOCK_HIGH();
DelayUs2x(1);
//svalue = (PORTB>>1);
svalue= SCCB_SDA;//GPIO_ReadInputDataBit(SCCB_SDA_PORT,SCCB_SDA);
CLOCK_LOW();
DelayUs2x(1);
pvalue |= svalue<<i;
}
// DDRB |=(1<<SCCB_SDA); //再把SDA改回为输出
// sda_configoutput();
//第9位,N.A.
DATA_HIGH();
DelayUs2x(1);
CLOCK_HIGH();
DelayUs2x(1);
CLOCK_LOW();
DelayUs2x(1);
*value = (unsigned char)(pvalue & 0xff);
}
void sccb_senddata(unsigned char subaddr,unsigned char value)
{
//3相写传输周期
sccb_start(); //启动SCCB
sccb_sendbyte(CAM_OV6620); //OV6620设备从地址,写操作
sccb_sendbyte(subaddr); //设备内存地址
sccb_sendbyte(value); //写数据字节
sccb_end(); //结束SCCB
DelayUs2x(10);
}
unsigned char sccb_receivedata(unsigned char subaddr)
{
unsigned char temp;
//2相写传输周期
sccb_start(); //启动SCCB
sccb_sendbyte(CAM_OV6620); //OV7620设备从地址,写操作
sccb_sendbyte(subaddr); //设备内存地址
sccb_end(); //结束SCCB
//2相读传输周期
sccb_start(); //启动SCCB
sccb_sendbyte(CAM_OV6620|1); //OV7620设备从地址,读操作
sccb_receivebyte(&temp); //读字节
sccb_end(); //结束SCCB
return temp;
}