OV7740 之STM32 源代码
扫描二维码
随时随地手机看文章
/*Includes------------------------------------------------------------------*/
#include"stm32f10x.h"
#include"stm32f10x_exti.h"
#include"OV7740.h"
#include"OV7740Reg.h"
#include"sccb.h"
#include"misc.h"
#include"delay.h"
/*Privatedefine------------------------------------------------------------*/
#defineOV7740_HREF_BITGPIO_Pin_2
#defineOV7740_VSYNC_BITGPIO_Pin_4
#defineGPIO_PIN_PCLKGPIO_Pin_1
#defineGPIO_PIN_RESTGPIO_Pin_5
#defineGPIO_PIN_XCLKGPIO_Pin_8
#defineGPIO_PCLK_PORTGPIOB
#defineGPIO_VSYNC_CMOSGPIOB
#defineRCC_APB2Periph_GPIO_VSYNC_CMOSRCC_APB2Periph_GPIOB
#defineGPIO_PIN_VSYNC_CMOSGPIO_Pin_4//GPIO_Pin_4
#defineEXTI_LINE_VSYNC_CMOSEXTI_Line4//EXTI_Line4
#defineGPIO_PORT_SOURCE_VSYNC_CMOSGPIO_PortSourceGPIOB
#defineGPIO_PIN_SOURCE_VSYNC_CMOSGPIO_PinSource4//GPIO_PinSource14
#defineHREF_HGPIOB->BSRR=GPIO_Pin_2//
#defineHREF_LGPIOB->BRR=GPIO_Pin_2//
#defineXCLK_HGPIOA->BSRR=GPIO_Pin_8
#defineXCLK_LGPIOA->BRR=GPIO_Pin_8
#defineCAMRST_HGPIOB->BSRR=GPIO_Pin_5
#defineCAMRST_LGPIOB->BRR=GPIO_Pin_5
/*Privatevariables---------------------------------------------------------*/
/*Privatefunctions---------------------------------------------------------*/
voidOV7740_XCLK_ON(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_MCOConfig(RCC_MCO_HSE);//hsi//指定引脚上输出时钟源PA8
}
voidOV7740_GPIO_Init(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=OV7740_VSYNC_BIT;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=OV7740_HREF_BIT|GPIO_PIN_REST;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
voidOV7740_Interrupts_Config(void)
{
NVIC_InitTypeDefNVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//从优先级
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
voidOV7740_EXTI_Config(void)//
{
EXTI_InitTypeDefEXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_GenerateSWInterrupt(EXTI_Line4);
}
u8OV7740_writeReg(u8regID,u8regDat)
{
while(SCCB_Start()==0);
if(0==SCCB_writeByte(SCCB_7740_WRADDR))
{
SCCB_Stop();
return(0);
}
if(0==SCCB_writeByte(regID))
{
SCCB_Stop();
return(0);
}
if(0==SCCB_writeByte(regDat))
{
SCCB_Stop();
return(0);
}
SCCB_Stop();
return(1);
}
u8OV7740_readReg(u8regID,u8*regDat)
{
//通过写操作设置寄存器地址
while(SCCB_Start()==0);
if(0==SCCB_writeByte(SCCB_7740_WRADDR))
{
SCCB_Stop();
return(0);
}
if(0==SCCB_writeByte(regID))
{
SCCB_Stop();
return(0);
}
SCCB_Stop();
//设置寄存器地址后,才是读
while(SCCB_Start()==0);
if(0==SCCB_writeByte(SCCB_7740_RDADDR))
{
SCCB_Stop();
return(0);
}
*regDat=SCCB_readByte();
SCCB_Stop();
return(1);
}
u8OV7740_init(void)
{
inti,n;
u8val;
SCCB_GPIO_Init();
OV7740_GPIO_Init();
OV7740_XCLK_ON();
CAMRST_L;
delay_us(100);
CAMRST_H;
delay_us(100);
if(0==OV7740_writeReg(SCCB_REG12,0x80))return0;
Delay_ms(10);
if(0==OV7740_writeReg(SCCB_REG12,0x51))return0;
delay_us(10);
OV7740_readReg(0x0A,&val);
n=sizeof(RegConf0)/sizeof(UINT8);
for(i=0;i { if(0==OV7740_writeReg(RegConf0[i][0],RegConf0[i][1])) { return0; } delay_us(10); } OV7740_setExposure(15); OV7740_setGainVal(0); OV7740_setVWindow(230); OV7740_Interrupts_Config(); OV7740_EXTI_Config(); return1; } voidOV7740_setGainVal(intx) { //OV7740_readReg(0x15,&val); OV7740_writeReg(0x00,Testgain[x][0]); OV7740_writeReg(0x15,Testgain[x][1]); } voidOV7740_setExposure(intexposure) { OV7740_writeReg(0x10,exposure&0xff); OV7740_writeReg(0x0f,(exposure>>8)&0xff); } u8setGain(intmacro,intmicro) { u8getGain[8]={7,0,1,3,7,15,31,63}; u8r00,r15; /*if(macro==0){OV7740_writeReg(0x13,0x87);return0;}*/ if(macro>7)return0; if(micro>15)return0; r15=getGain[macro]>>4; r00=(getGain[macro]<<4)+micro; /*OV7740_writeReg(0x13,0x00);手动修改*/ OV7740_writeReg(0x00,r00); if(r15)OV7740_writeReg(0x15,r15); return1; } //evenforstartline voidOV7740_setVWindow(intstartline) { OV7740_writeReg(0x19,startline>>1); OV7740_writeReg(0x1a,(20>>1));//20lines,>>1 } voidOV7740_setLines(intlines) { OV7740_writeReg(0x1a,(lines>>1));//20lines,>>1 } u8OV7740_test(void) { SCCB_GPIO_Init(); OV7740_GPIO_Init(); OV7740_XCLK_ON(); CAMRST_L; delay_us(100); CAMRST_H; delay_us(100); if(0==OV7740_writeReg(0x12,0x80))return0; delay_us(10); if(0==OV7740_writeReg(0x12,0x40))return0; delay_us(10); OV7740_setVWindow(230); if(0==OV7740_writeReg(SCCB_REG38,0x07))return0; d