但是依旧读不出模块的ID:希望各位给与纠正
[mw_shl_code=c,true]#include "sccb.h"
#include "Delay.h"
#include "bsp_usart1.h"
void I2C_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
//SDA?è??
GPIO_InitStructure.GPIO_Pin=I2C_SDA;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIO_I2C,&GPIO_InitStructure);
//scl?è??
GPIO_InitStructure.GPIO_Pin=I2C_SCL;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIO_I2C,&GPIO_InitStructure);
I2C_SCL_H;
I2C_SDA_H;
I2C_SDA_OUT();
}
void I2C_SDA_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=I2C_SDA;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIO_I2C,&GPIO_InitStructure);
}
void I2C_SDA_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=I2C_SDA;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIO_I2C,&GPIO_InitStructure);
}
//?ú?ú????????
void I2C_Start(void)
{
I2C_SDA_H;
Delay_us(100);
I2C_SCL_H;
Delay_us(100);
I2C_SDA_L;
Delay_us(100);
I2C_SCL_L;
Delay_us(100);
}
//?ú?ú????????
void I2C_Stop(void)
{
I2C_SDA_L;
Delay_us(100);
I2C_SCL_L;
Delay_us(100);
I2C_SCL_H;
Delay_us(100);
I2C_SDA_H;
Delay_us(100);
}
//?÷?ú?ú?ú????????ACK
void I2C_Ack(void)
{
I2C_SDA_OUT();
I2C_SDA_H;
Delay_us(100);
I2C_SCL_H;
Delay_us(100);
I2C_SCL_L;
Delay_us(100);
I2C_SDA_H;
}
//?÷?ú???ú?ú????????NACK
void I2C_NAck(void)
{
I2C_SDA_H;
Delay_us(100);
I2C_SCL_H;
Delay_us(100);
I2C_SCL_L;
Delay_us(100);
I2C_SDA_L;
Delay_us(100);
}
/****?ì?é????????********/
char TestAsk(void)
{
char ask;
I2C_SCL_H;
Delay_us(100);
I2C_SDA_IN();
Delay_us(100);
ask=GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7);
Delay_us(100);
I2C_SCL_L;
Delay_us(100);
return ask;
}
unsigned char I2C_Send_Byte(unsigned char DData)
{
unsigned char j,tem;
//??°???????????????
for(j=8;j>0;j--)
{
I2C_SCL_L;
if((DData)&0x80)
{
I2C_SDA_H;
}
else
{
I2C_SDA_L;
}
DData<<=1;
Delay_us(100);
I2C_SCL_H;
Delay_us(100);
I2C_SCL_L;
Delay_us(100);
}
Delay_us(100);
I2C_SDA_IN();
Delay_us(100);
I2C_SCL_H;
Delay_us(100);
//???????????????????é??,0±í??????????????1±í????????
if(GPIO_ReadInputDataBit(GPIO_I2C,I2C_SDA)==1)
{
tem=0;
printf("\r\n????·????§°?\r\n");
}
else
{
tem=1;
printf("\r\n????·???????\r\n");
}
I2C_SCL_L;
Delay_us(100);
I2C_SDA_OUT();
return(tem);
}
unsigned char I2C_Read_Byte(void)
{
unsigned char read,j;
read=0x00;
I2C_SDA_IN();
Delay_us(100);
I2C_SCL_L;
for(j=8;j>0;j--)
{
Delay_us(100);
I2C_SCL_H;
Delay_us(100);
read<<=1;
if(GPIO_ReadInputDataBit(GPIO_I2C,I2C_SDA))
{
read |=0x01;
}
Delay_us(50);
I2C_SCL_L;
Delay_us(100);
}
return read;
}
//?????????÷????
u8 OV2640_WR_Reg(u8 reg,u8 data)
{
I2C_Start();
//;//????OV2640??ID
if(0==I2C_Send_Byte(SCCB_ID))
{
I2C_Stop();
return 0;
}
Delay_us(100);
//;//?????è????×÷???ü??
if(0==I2C_Send_Byte(reg & 0x00ff))//?è???????????·
{
I2C_Stop();
return 0;
}
Delay_ms(100);
//;//?????è??????
if(0==I2C_Send_Byte(data))
{
I2C_Stop();
return 0;
}
I2C_Stop();
return 1;
}
//?????????÷????????
u8 OV2640_RD_Data(u8 reg)
{
u8 val;
I2C_Start();
I2C_Send_Byte(SCCB_ID);//?????÷????ID
Delay_us(100);
I2C_Send_Byte(reg);//?????è????×÷???????÷
Delay_us(100);
I2C_Stop();
Delay_us(100);
I2C_Start();
I2C_Send_Byte(SCCB_ID | 0x01);
Delay_us(100);
val=I2C_Read_Byte();
I2C_NAck();
I2C_Stop();
return val;
}
[/mw_shl_code]
这里是参考原子哥的程序来读取OV2640的ID的
[mw_shl_code=c,true]u8 OV2640_ReadID(void)
{
u32 reg=0;
I2C_GPIO();
OV2640_DataInit();
PCLK_HR_VSYNC_RST_GPIOInit();
OV2640_RST_L;//????????OV2640
Delay_ms(5);
OV2640_RST_H;
OV2640_WR_Reg(0xff,0x01);//??×÷sensor?????÷
if(0== OV2640_WR_Reg(0x12,0x80))//????OV2640
{
printf("\r\nOV2640???????§°?\r\n");
LCD_DispEnCh(5,80,"OV2640 Init ERROR",RED);
}
OV2640_WR_Reg(OV2640_DSP_RA_DLMT, 0x01);//?????????·???à??????
Delay_ms(5);
reg = OV2640_RD_Data(OV2640_SENSOR_MIDH);
reg<<=8;
reg |= OV2640_RD_Data(OV2640_SENSOR_MIDL);
LCD_DispEnCh(5,100,"OV2640 ID:",RED);
printf("OV2640??ID??:%d\r\n",reg);
LCD_DisNum(65,100,reg,RED);
if( (reg!=0)&&(reg != 0xffff) )
{
printf("\r\n????OV2640??ID???·\r\n");
return 1;
}
else
{
printf("\r\n????OV2640??ID???í\r\n");
return 0;
}
}[/mw_shl_code]
|