OV7670 FIFO 30W摄像头介绍(三) --- STM32F103驱动OV7670代码介绍

在介绍OV7670之前先附上模块链接:点击购买OV7670摄像头模块
介绍代码前,首先来看之前写的文章:
OV7670 FIFO 30W摄像头介绍(一) — 整体介绍/SCCB时序
OV7670 FIFO 30W摄像头介绍(二) — 寄存器图示说明

1)寄存器介绍

typedef enum
{
     
	ov7670_reg_gain	= 0x00,
	ov7670_reg_blue	= 0x01,
	ov7670_reg_red	= 0x02,
	ov7670_reg_vref	= 0x03,
	ov7670_reg_com1	= 0x04,
	ov7670_reg_bave	= 0x05,
	ov7670_reg_gbave=0x06,
	ov7670_reg_aechh=0x07,
	ov7670_reg_rave	= 0x08,
	ov7670_reg_com2	= 0x09,
	ov7670_reg_pid	= 0x0a,
	ov7670_reg_ver	= 0x0b,
	ov7670_reg_com3	= 0x0c,
	ov7670_reg_com4	= 0x0d,
	ov7670_reg_com5	= 0x0e,
	ov7670_reg_com6	= 0x0f,
	ov7670_reg_aech	= 0x10,
	ov7670_reg_clkrc	= 0x11,
	ov7670_reg_com7	= 0x12,
	ov7670_reg_com8	= 0x13,
	ov7670_reg_com9	= 0x14,
	ov7670_reg_com10=0x15,
	ov7670_reg_rsvd1	= 0x16,
	ov7670_reg_hstart= 0x17,
	ov7670_reg_hstop	= 0x18,
	ov7670_reg_vstart= 0x19,
	ov7670_reg_vstop	= 0x1a,
	ov7670_reg_pshft	= 0x1b,
	ov7670_reg_midh	= 0x1c,
	ov7670_reg_midl	= 0x1d,
	ov7670_reg_mvfp	= 0x1e,
	ov7670_reg_laec	= 0x1f,
	ov7670_reg_adctl0= 0x20,
	ov7670_reg_adctl1= 0x21,
	ov7670_reg_adctl2= 0x22,
	ov7670_reg_adctl3= 0x23,
	ov7670_reg_aew	= 0x24,
	ov7670_reg_aeb	= 0x25,
	ov7670_reg_vpt	= 0x26,
	ov7670_reg_bbias	= 0x27,
	ov7670_reg_gbbias=0x28,
	ov7670_reg_rsvd2	= 0x29,
	ov7670_reg_exhch= 0x2a,
	ov7670_reg_exhcl	= 0x2b,
	ov7670_reg_rbias	= 0x2c,
	ov7670_reg_advfl	= 0x2d,
	ov7670_reg_advfh	= 0x2e,
	ov7670_reg_yave	= 0x2f,
	ov7670_reg_hsyst	= 0x30,
	ov7670_reg_hsyen= 0x31,
	ov7670_reg_href	= 0x32,
	ov7670_reg_chlf	= 0x33,
	ov7670_reg_arblm	= 0x34,
	ov7670_reg_rsvd3	= 0x35,
	ov7670_reg_rsvd4	= 0x36,
	ov7670_reg_adc	= 0x37,
	ov7670_reg_acom	= 0x38,
	ov7670_reg_ofon	= 0x39,
	ov7670_reg_tslb	= 0x3a,
	ov7670_reg_com11=0x3b,
	ov7670_reg_com12=0x3c,
	ov7670_reg_com13=0x3d,
	ov7670_reg_com14=0x3e,
	ov7670_reg_edge	= 0x3f,
	ov7670_reg_com15=0x40,
	ov7670_reg_com16=0x41,
	ov7670_reg_com17=0x42,
	ov7670_reg_awbc1=0x43,
	ov7670_reg_awbc2=0x44,
	ov7670_reg_awbc3=0x45,
	ov7670_reg_awbc4=0x46,
	ov7670_reg_awbc5=0x47,
	ov7670_reg_awbc6=0x48,
	ov7670_reg_rsvd5	= 0x49,
	ov7670_reg_rsvd6	= 0x4a,
	ov7670_reg_rgb4b= 0x4b,
	ov7670_reg_dnsth	= 0x4c,
	ov7670_reg_rsvd7	= 0x4d,
	ov7670_reg_rsvd8	= 0x4e,
	ov7670_reg_mtx1	= 0x4f,
	ov7670_reg_mtx2	= 0x50,
	ov7670_reg_mtx3	= 0x51,
	ov7670_reg_mtx4	= 0x52,
	ov7670_reg_mtx5	= 0x53,
	ov7670_reg_mtx6	= 0x54,
	ov7670_reg_bright= 0x55,
	ov7670_reg_contras=0x56,
	ov7670_reg_contras_center=0x57,
	ov7670_reg_mtxs	= 0x58,
	ov7670_reg_rsvd9	= 0x59,
	ov7670_reg_rsd10= 0x5a,
	ov7670_reg_rsd11= 0x5b,
	ov7670_reg_rsd12	= 0x5c,
	ov7670_reg_rsd13	= 0x5d,
	ov7670_reg_rsd14	= 0x5e,
	ov7670_reg_rsd15	= 0x5f,
	ov7670_reg_rsd16	= 0x60,
	ov7670_reg_rsd17	= 0x61,
	ov7670_reg_lcc1	= 0x62,
	ov7670_reg_lcc2	= 0x63,
	ov7670_reg_lcc3	= 0x64,
	ov7670_reg_lcc4	= 0x65,
	ov7670_reg_lcc5	= 0x66,
	ov7670_reg_manu	= 0x67,
	ov7670_reg_manv	= 0x68,
	ov7670_reg_gfix	= 0x69,
	ov7670_reg_ggain	= 0x6a,
	ov7670_reg_dblv	= 0x6b,
	ov7670_reg_awbctr3=0x6c,
	ov7670_reg_awbctr2=0x6d,
	ov7670_reg_awbctr1=0x6e,
	ov7670_reg_awbctr0=0x6f,
	ov7670_reg_scaling_xsc=0x70,
	ov7670_reg_scaling_ysc=0x71,
	ov7670_reg_scaling_dcwctr=0x72,
	ov7670_reg_scaling_pclkdiv=0x73,
	ov7670_reg_74		= 0x74,
	ov7670_reg_75		= 0x75,
	ov7670_reg_76		= 0x76,
	ov7670_reg_77		= 0x77,
	ov7670_reg_rsd18	= 0x78,
	ov7670_reg_rsd19	= 0x79,
	ov7670_reg_slop	= 0x7a,
	ov7670_reg_cam1 = 0x7b,
	ov7670_reg_cam2 = 0x7c,
	ov7670_reg_cam3 = 0x7d,
	ov7670_reg_cam4 = 0x7e,
	ov7670_reg_cam5 = 0x7f,
	ov7670_reg_cam6	= 0x80,
	ov7670_reg_cam7	= 0x81,
	ov7670_reg_cam8	= 0x82,
	ov7670_reg_cam9	= 0x83,
	ov7670_reg_cam10=0x84,
	ov7670_reg_cam11=0x85,
	ov7670_reg_cam12=0x86,
	ov7670_reg_cam13=0x87,
	ov7670_reg_cam14=0x88,
	ov7670_reg_cam15=0x89,
	ov7670_reg_rsd20	= 0x8a,
	ov7670_reg_rsd21	= 0x8b,
	ov7670_reg_rgb444=0x8c,
	ov7670_reg_rsd22= 0x8d,
	ov7670_reg_rsd23	= 0x8e,
	ov7670_reg_rsd24	= 0x8f,
	ov7670_reg_rsd25	= 0x90,
	ov7670_reg_rsd26	= 0x91,
	ov7670_reg_dmlnl	= 0x92,
	ov7670_reg_dmlnh= 0x93,
	ov7670_reg_lcc6	= 0x94,
	ov7670_reg_lcc7	= 0x95,
	ov7670_reg_bd50st=0x9d,
	ov7670_reg_bd60st=0x9e,
	ov7670_reg_haecc1=0x9f,
	ov7670_reg_haecc2=0xa0,
	ov7670_reg_scaling_pclk_dly=0xa2,
	ov7670_reg_nt_ctl	= 0xa4,
	ov7670_reg_bd50max=0xa5,
	ov7670_reg_haecc3=0xa6,
	ov7670_reg_haecc4=0xa7,
	ov7670_reg_haecc5=0xa8,
	ov7670_reg_haecc6=0xa9,
	ov7670_reg_haecc7=0xaa,
	ov7670_reg_bd60max=0xab,
	ov7670_reg_stropt=0xac,
	ov7670_reg_str_r	= 0xad,
	ov7670_reg_str_g	= 0xae,
	ov7670_reg_str_b	= 0xaf,
	ov7670_reg_ablc1= 0xb1,
	ov7670_reg_thl_st= 0xb3,
	ov7670_reg_thl_dlt=0xb5,
	ov7670_reg_adchb= 0xbe,
	ov7670_reg_adchr	= 0xbf,
	ov7670_reg_adchgb= 0xc0,
	ov7670_reg_adchgr=0xc1,
	ov7670_reg_satctl	= 0xc9,
}ov7670_reg_index_e;	

2)函数定义

uint8_t hw_ov7670_init(void);
uint8_t hw_ov7670_control(uint8_t control);
uint8_t hw_ov7670_set_light_mode(uint8_t mode);
uint8_t hw_ov7670_set_color_saturation(uint8_t sat);
uint8_t hw_ov7670_set_brightness(uint8_t bright);
uint8_t hw_ov7670_set_contrast(uint8_t contrast);
uint8_t hw_ov7670_set_special_effect(uint8_t eft);
uint8_t hw_ov7670_set_window(uint16_t sx,uint16_t sy,uint16_t width,uint16_t height);
uint8_t hw_ov7670_is_start(void);
void hw_ov7670_get_data_send(void);

3)初始化


/******************************************************************************
 * func name   : hw_ov7670_init
 * para        : NULL
 * return      : ov7670 init result
 * description : ov7670 init
******************************************************************************/
uint8_t hw_ov7670_init()
{
     
    uint16_t i=0;
    uint8_t pid_msb,pid_lsb;
    uint8_t vid_h,vid_l;

    GPIO_InitTypeDef  GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    /* Enable ov7670 clock */
    RCC_APB2PeriphClockCmd(OV7670_FIFO_WEN_PERIPH_CLK |OV7670_FIFO_RCLK_PERIPH_CLK |OV7670_FIFO_RRST_PERIPH_CLK |
                           OV7670_FIFO_OE_PERIPH_CLK |OV7670_FIFO_WRST_PERIPH_CLK |OV7670_VSYNC_PERIPH_CLK |
                           OV7670_DATA_PERIPH_CLK  | RCC_APB2Periph_AFIO,ENABLE);

    /* GPIO init */
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin=OV7670_VSYNC;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(OV7670_VSYNC_GPIO,&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin=OV7670_FIFO_WEN;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(OV7670_FIFO_WEN_GPIO,&GPIO_InitStructure);
    GPIO_SetBits(OV7670_FIFO_WEN_GPIO,OV7670_FIFO_WEN);

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin=OV7670_FIFO_RCLK;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(OV7670_FIFO_RCLK_GPIO,&GPIO_InitStructure);
    GPIO_SetBits(OV7670_FIFO_RCLK_GPIO,OV7670_FIFO_RCLK);

    GPIO_InitStructure.GPIO_Pin  = OV7670_FIFO_RRST;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(OV7670_FIFO_RRST_GPIO, &GPIO_InitStructure);
    GPIO_SetBits(OV7670_FIFO_RRST_GPIO,OV7670_FIFO_RRST);

    GPIO_InitStructure.GPIO_Pin  = OV7670_FIFO_OE;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(OV7670_FIFO_OE_GPIO, &GPIO_InitStructure);
    GPIO_SetBits(OV7670_FIFO_OE_GPIO,OV7670_FIFO_OE);


    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin=OV7670_FIFO_WRST;
    GPIO_Init(OV7670_FIFO_WRST_GPIO, &GPIO_InitStructure);
    GPIO_SetBits(OV7670_FIFO_WRST_GPIO,OV7670_FIFO_WRST);

    GPIO_InitStructure.GPIO_Pin  = OV7670_D0 | OV7670_D1 | OV7670_D2 | OV7670_D3 |
                                   OV7670_D4 | OV7670_D5 | OV7670_D6 | OV7670_D7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(OV7670_DATA_GPIO, &GPIO_InitStructure);

    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

    protocol_sccb_init();

    if(protocol_sccb_write_reg(ov7670_reg_com7,0x80))
        return HW_ERR_OV7670_SCCB_ERR;

    hw_delay_us(500);

    vid_h = protocol_sccb_read_reg(ov7670_reg_midh);
    vid_l = protocol_sccb_read_reg(ov7670_reg_midl);
    pid_msb = protocol_sccb_read_reg(ov7670_reg_pid);
    pid_lsb = protocol_sccb_read_reg(ov7670_reg_ver);
    HW_DEBUG("PIDM 0x%02x PIDL 0x%02x,VIDH 0x%02x VIDL 0x%02x\n",pid_msb,pid_lsb,vid_h,vid_l);

    for(i=0; i<sizeof(ov7670_init_reg_tbl_new)/sizeof(ov7670_init_reg_tbl_new[0]); i++)
    {
     
        protocol_sccb_write_reg(ov7670_init_reg_tbl_new[i][0],ov7670_init_reg_tbl_new[i][1]);
    }

    GPIO_EXTILineConfig(OV7670_EXIT_GPIO,OV7670_EXIT_PIN);

    EXTI_InitStructure.EXTI_Line=OV7670_EXIT_LINE;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = OV7670_EXIT_IRQ;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =OV7670_PREE_PRIO;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = OV7670_SUB_PRIO;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    hw_ov7670_set_window(0,0,320,240);
    hw_ov7670_control(ov7670_ctl_start);

    return HW_ERR_OK;
}

4)设置亮度模式



/******************************************************************************
 * func name   : hw_ov7670_set_light_mode
 * para        : mode
 						ov7670_light_auto		--> set auto mode
 						ov7670_light_sunny		--> set sunny mode
 						ov7670_light_cloudy		--> set cloudy mode
 						ov7670_light_office		--> set office mode
 						ov7670_light_home		--> set home mode
 * return      : ov7670 hw_ov7670_set_light_mode result
 * description : set ov7670 light mode
******************************************************************************/
uint8_t hw_ov7670_set_light_mode(uint8_t mode)
{
     
    uint8_t reg13val=0xE7;
    uint8_t reg01val=0;
    uint8_t reg02val=0;
    switch(mode)
    {
     
    case ov7670_light_sunny:
        reg13val=0xE5;
        reg01val=0x5A;
        reg02val=0x5C;
        break;
    case ov7670_light_cloudy:
        reg13val=0xE5;
        reg01val=0x58;
        reg02val=0x60;
        break;
    case ov7670_light_office:
        reg13val=0xE5;
        reg01val=0x84;
        reg02val=0x4c;
        break;
    case ov7670_light_home:
        reg13val=0xE5;
        reg01val=0x96;
        reg02val=0x40;
        break;
    }
    protocol_sccb_write_reg(ov7670_reg_com8,reg13val);
    protocol_sccb_write_reg(ov7670_reg_blue,reg01val);
    protocol_sccb_write_reg(ov7670_reg_red,reg02val);
    return HW_ERR_OK;
}

5)设置色彩饱和度



/******************************************************************************
 * func name   : hw_ov7670_set_color_saturation
 * para        : sat
 						0		--> -2
 						1		--> -1
 						2		--> 0
 						3		--> 1
 						4		--> 2
 * return      : ov7670 hw_ov7670_set_color_saturation result
 * description : set ov7670 color saturation
******************************************************************************/
uint8_t hw_ov7670_set_color_saturation(uint8_t sat)
{
     
    uint8_t reg4f5054val=0x80;
    uint8_t reg52val=0x22;
    uint8_t reg53val=0x5E;
    switch(sat)
    {
     
    case 0:
        reg4f5054val=0x40;
        reg52val=0x11;
        reg53val=0x2F;
        break;
    case 1:
        reg4f5054val=0x66;
        reg52val=0x1B;
        reg53val=0x4B;
        break;
    case 3:
        reg4f5054val=0x99;
        reg52val=0x28;
        reg53val=0x71;
        break;
    case 4:
        reg4f5054val=0xC0;
        reg52val=0x33;
        reg53val=0x8D;
        break;
    }
    protocol_sccb_write_reg(ov7670_reg_mtx1,reg4f5054val);	//色彩矩阵系数1
    protocol_sccb_write_reg(ov7670_reg_mtx2,reg4f5054val);	//色彩矩阵系数2
    protocol_sccb_write_reg(ov7670_reg_mtx3,0x00);			//色彩矩阵系数3
    protocol_sccb_write_reg(ov7670_reg_mtx4,reg52val);		//色彩矩阵系数4
    protocol_sccb_write_reg(ov7670_reg_mtx5,reg53val);		//色彩矩阵系数5
    protocol_sccb_write_reg(ov7670_reg_mtx6,reg4f5054val);	//色彩矩阵系数6
    protocol_sccb_write_reg(ov7670_reg_mtxs,0x9E);			//MTXS

    return HW_ERR_OK;
}

6)设置亮度


/******************************************************************************
 * func name   : hw_ov7670_set_brightness
 * para        : bright
 						0		--> -2
 						1		--> -1
 						2		--> 0
 						3		--> 1
 						4		--> 2
 * return      : ov7670 hw_ov7670_set_brightness result
 * description : set ov7670 brightness
******************************************************************************/
uint8_t hw_ov7670_set_brightness(uint8_t bright)
{
     
    uint8_t reg55val=0x00;
    switch(bright)
    {
     
    case 0:
        reg55val=0xB0;
        break;
    case 1:
        reg55val=0x98;
        break;
    case 3:
        reg55val=0x18;
        break;
    case 4:
        reg55val=0x30;
        break;
    }
    protocol_sccb_write_reg(ov7670_reg_bright,reg55val);	//亮度调节
    return HW_ERR_OK;
}

7)设置对比度


/******************************************************************************
 * func name   : hw_ov7670_set_contrast
 * para        : bright
 						0		--> -2
 						1		--> -1
 						2		--> 0
 						3		--> 1
 						4		--> 2
 * return      : ov7670 hw_ov7670_set_contrast result
 * description : set ov7670 contrast
******************************************************************************/
uint8_t hw_ov7670_set_contrast(uint8_t contrast)
{
     
    uint8_t reg56val=0x40;
    switch(contrast)
    {
     
    case 0:
        reg56val=0x30;
        break;
    case 1:
        reg56val=0x38;
        break;
    case 3:
        reg56val=0x50;
        break;
    case 4:
        reg56val=0x60;
        break;
    }
    protocol_sccb_write_reg(ov7670_reg_contras,reg56val);	//对比度调节
    return HW_ERR_OK;
}

8)设置特殊效果

/******************************************************************************
 * func name   : hw_ov7670_set_special_effect
 * para        : bright
 						0		--> 普通模式
 						1		--> 负片
 						2		--> 黑白
 						3		--> 偏红色
 						4		--> 偏绿色
 						5		--> 偏蓝色
 						6		--> 复古
 * return      : ov7670 hw_ov7670_set_special_effect result
 * description : set ov7670 special effect
******************************************************************************/
uint8_t hw_ov7670_set_special_effect(uint8_t eft)
{
     
    uint8_t reg3aval=0X04;
    uint8_t reg67val=0XC0;
    uint8_t reg68val=0X80;
    switch(eft)
    {
     
    case 1:
        reg3aval=0X24;
        reg67val=0X80;
        reg68val=0X80;
        break;
    case 2:
        reg3aval=0X14;
        reg67val=0X80;
        reg68val=0X80;
        break;
    case 3:
        reg3aval=0X14;
        reg67val=0Xc0;
        reg68val=0X80;
        break;
    case 4:
        reg3aval=0X14;
        reg67val=0X40;
        reg68val=0X40;
        break;
    case 5:
        reg3aval=0X14;
        reg67val=0X80;
        reg68val=0XC0;
        break;
    case 6:
        reg3aval=0X14;
        reg67val=0XA0;
        reg68val=0X40;
        break;
    }
    protocol_sccb_write_reg(ov7670_reg_tslb,reg3aval);
    protocol_sccb_write_reg(ov7670_reg_manv,reg67val);
    protocol_sccb_write_reg(ov7670_reg_manu,reg68val);
    return HW_ERR_OK;
}

9)中断处理

void OV7670_EXIT_FUNC(void)
{
     
    if(EXTI_GetITStatus(OV7670_EXIT_LINE)==SET)
    {
     
        OV7670_WRST_L;
        OV7670_WRST_H;
        OV7670_WEN_H;
        ov7670_frame_interrupt++;
    }
    EXTI_ClearITPendingBit(OV7670_EXIT_LINE);
}

你可能感兴趣的:(Marvell8801,SDIO,WIFI,stm32应用,OV7670,STM32,OV7670_FIFO)