⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ov7670.c

📁 STM32摄像头程序
💻 C
字号:

/*
GUANFU_WANG
*/


#include "ov7670.h"
#include "delay.h"
#include "ov7670config.h"
#include "stm32f10x_lib.h"

extern const char change_reg[CHANGE_REG_NUM][2];
////////////////////////////
//功能:提供7660时钟
//返回:无
void XCLK_init_ON(void)
{
  
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    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
}
void XCLK_init_OFF(void)
{
  
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void ov7670_GPIO_Init(void)
{
  
  GPIO_InitTypeDef GPIO_InitStructure;
   // Enable GPIOC  clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  GPIO_InitStructure.GPIO_Pin =  OV7670_VSYNC_BIT;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
 
}

/********************************************************************
函数功能:7670中断配制
入口参数:无。
返    回:无。
备    注:无。
********************************************************************/
void ov7670_Interrupts_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  
#ifdef  VECT_TAB_RAM  
  /* Set the Vector Table base location at 0x20000000 */ 
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */ 
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif

  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  
  /* Enable the EXTI9_5 Interrupt 
  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  */
  
  
    /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  
  /* Enable the EXTI9_5 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  

  

}



void OV7670_EXTI_Config(void)
{
  EXTI_InitTypeDef EXTI_InitStructure;
  
 // GPIO_EXTILineConfig(GPIO_PORT_SOURCE_HREF_CMOS, GPIO_PIN_SOURCE_HREF_CMOS);
 /// EXTI_InitStructure.EXTI_Line = EXTI_LINE_HREF_CMOS;
 /// EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
 // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
 // EXTI_InitStructure.EXTI_LineCmd = ENABLE;
 // EXTI_Init(&EXTI_InitStructure);
//  EXTI_GenerateSWInterrupt(EXTI_LINE_HREF_CMOS);
  

  GPIO_EXTILineConfig(GPIO_PORT_SOURCE_VSYNC_CMOS, GPIO_PIN_SOURCE_VSYNC_CMOS);
  EXTI_InitStructure.EXTI_Line = EXTI_LINE_VSYNC_CMOS;
  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_LINE_VSYNC_CMOS);

}








////////////////////////////
//功能:写OV7660寄存器
//返回:1-成功	0-失败
unsigned char wrOV7670Reg(unsigned char regID, unsigned char regDat)
{
		startSCCB();
	if(0==SCCBwriteByte(0x42))
	{
		stopSCCB();
		return(0);
	}
	delay_us(100);
  	if(0==SCCBwriteByte(regID))
	{
		stopSCCB();
		return(0);
	}
	delay_us(100);
  	if(0==SCCBwriteByte(regDat))
	{
		stopSCCB();
		return(0);
	}
  	stopSCCB();
	
  	return(1);
}
////////////////////////////
//功能:读OV7660寄存器
//返回:1-成功	0-失败
unsigned char rdOV7670Reg(unsigned char regID, unsigned char *regDat)
{
	//通过写操作设置寄存器地址
	startSCCB();
	if(0==SCCBwriteByte(0x42))
	{
		stopSCCB();
		return(0);
	}
	delay_us(100);
  	if(0==SCCBwriteByte(regID))
	{
		stopSCCB();
		return(0);
	}
	stopSCCB();
	
	delay_us(100);
	
	//设置寄存器地址后,才是读
	startSCCB();
	if(0==SCCBwriteByte(0x43))
	{
		stopSCCB();
		return(0);
	}
	delay_us(100);
  	*regDat=SCCBreadByte();
  	noAck();
  	stopSCCB();
  	return(1);
}




/* OV7670_init() */
//返回1成功,返回0失败
unsigned char OV7670_init(void)
{
	unsigned char temp;
	
	unsigned int i=0;

	//uchar ovidmsb=0,ovidlsb=0;
	ov7670_GPIO_Init();
	SCCB_GPIO_Config();//io init..
        XCLK_init_ON();
	temp=0x80;
	if(0==wrOV7670Reg(0x12, temp)) //Reset SCCB
	{
          return 0 ;
	}
	delay_ms(50);

  
        for(i=0;i<CHANGE_REG_NUM;i++)
	{
		if( 0==wrOV7670Reg(change_reg[i][0],change_reg[i][1]))
		{
			return 0;
		}
	}

	return 0x01; //ok
} 

void set_cif(void)   
{   
  // unsigned char regval;  
   
    while(1!= wrOV7670Reg(0x12, 0x24));   
   // wrOV7670Reg(0x40, 0xC0);   
    wrOV7670Reg(0x17, 0x15);   
    wrOV7670Reg(0x18, 0x0B);   
    wrOV7670Reg(0x19, 0x03);   
    wrOV7670Reg(0x1A, 0x7b);
    
    wrOV7670Reg(0x03, 0x0a); 
    wrOV7670Reg(0x32, 0xb6);
    
    //wrOV7670Reg(0x0C, 0x08); 

    wrOV7670Reg(0x70, 0x3A); 
    wrOV7670Reg(0x71, 0x35);  
    wrOV7670Reg(0x72, 0x11);   
    wrOV7670Reg(0x73, 0xF2); 
    wrOV7670Reg(0xA2, 0x02);
    
    
    
    
} 

 void set_qvga(void)   
{   
    while(1!= wrOV7670Reg(0x12, 0x14));
   // wrOV7670Reg(0x40, 0xd0);     
    wrOV7670Reg(0x17, 0x16);   
    wrOV7670Reg(0x18, 0x04);   
    wrOV7670Reg(0x19, 0x02);   
    wrOV7670Reg(0x1A, 0x7B); 
    wrOV7670Reg(0x32, 0x40);
     
    wrOV7670Reg(0x03, 0x0a); 
  //  wrOV7670Reg(0x3E, 0x1A); 
    
    wrOV7670Reg(0x70, 0x3A); 
    wrOV7670Reg(0x71, 0x35);  
    wrOV7670Reg(0x72, 0x11);   
    wrOV7670Reg(0x73, 0xF9); 
    wrOV7670Reg(0xA2, 0x02);
    
}   
   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -