0.png (54.96 KB, 下載次數: 131)
下載附件
2016-10-11 18:24 上傳
本攝像頭ov7670驅動程序已經通過本人的驗證可以正常運行,不同的stm32開發板只需要修改引腳即可使用,所有內容打包下載:
stm32驅動攝像頭ov7670源程序.7z
(380.01 KB, 下載次數: 637)
2024-11-14 15:20 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
下面是部分源碼預覽:
- /*
- wangguanfu@163.com
- 未經過本人許可禁止任何商業用途
- */
- #include "Sensor.h"
- #include "delay.h"
- #include "Sensor_config.h"
- #include "stm32f10x.h"
- extern const char OV7670_reg[OV7670_REG_NUM][2];
- ////////////////////////////
- //功能:提供時鐘
- //guanfu_wang
- /*
- 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
- }
- */
- /////////////////////////////
- //VSYNC GPIO INIT
- //guanfu_wang
- void Sensor_GPIO_Init(void)//
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- // Enable GPIOC clock
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- GPIO_InitStructure.GPIO_Pin = Sensor_VSYNC_BIT;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;////上拉
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- }
- //////////////////////////////////////////////////
- ////函數功能:中斷配制
- //guanfu_wang
- void Sensor_Interrupts_Config(void)//VSYNC 中斷
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
-
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
-
- /* Enable the EXTI9_5 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
-
- }
- ////////////////////////////////////////////////////////////////////////
- void Sensor_EXTI_Config(void)//VSYNC 中斷
- {
- EXTI_InitTypeDef EXTI_InitStructure;
-
- 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_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- EXTI_GenerateSWInterrupt(EXTI_LINE_VSYNC_CMOS);
- }
- ///////////////////////////////////////////////////////////////////////////
- //功能:寫OV7660寄存器
- //返回:1-成功 0-失敗
- //guanfu_wang
- unsigned char wr_Sensor_Reg(unsigned char regID, unsigned char regDat)
- {
- startSCCB();//發送SCCB 總線開始傳輸命令
- if(0==SCCBwriteByte(0x42))//寫地址
- {
- stopSCCB();//發送SCCB 總線停止傳輸命令
- return(0);//錯誤返回
- }
- delay_us(20);
- if(0==SCCBwriteByte(regID))//積存器ID
- {
- stopSCCB();//發送SCCB 總線停止傳輸命令
- return(0);//錯誤返回
- }
- delay_us(20);
- if(0==SCCBwriteByte(regDat))//寫數據到積存器
- {
- stopSCCB();//發送SCCB 總線停止傳輸命令
- return(0);//錯誤返回
- }
- stopSCCB();//發送SCCB 總線停止傳輸命令
-
- return(1);//成功返回
- }
- ////////////////////////////
- //功能:讀OV7660寄存器
- //返回:1-成功 0-失敗
- //guanfu_wang
- unsigned char rd_Sensor_Reg(unsigned char regID, unsigned char *regDat)
- {
- //通過寫操作設置寄存器地址
- startSCCB();
- if(0==SCCBwriteByte(0x42))//寫地址
- {
- stopSCCB();//發送SCCB 總線停止傳輸命令
- return(0);//錯誤返回
- }
- delay_us(20);
- if(0==SCCBwriteByte(regID))//積存器ID
- {
- stopSCCB();//發送SCCB 總線停止傳輸命令
- return(0);//錯誤返回
- }
- stopSCCB();//發送SCCB 總線停止傳輸命令
-
- delay_us(20);
-
- //設置寄存器地址后,才是讀
- startSCCB();
- if(0==SCCBwriteByte(0x43))//讀地址
- {
- stopSCCB();//發送SCCB 總線停止傳輸命令
- return(0);//錯誤返回
- }
- delay_us(20);
- *regDat=SCCBreadByte();//返回讀到的值
- noAck();//發送NACK命令
- stopSCCB();//發送SCCB 總線停止傳輸命令
- return(1);//成功返回
- }
- /* Sensor_init() */
- //返回1成功,返回0失敗
- //guanfu_wang
- unsigned char Sensor_init(void)
- {
- unsigned char temp;
-
- unsigned int i=0;
- // XCLK_init_ON();//開啟MCO功能 提供時鐘給CMOS傳感器
- //uchar ovidmsb=0,ovidlsb=0;
- Sensor_GPIO_Init();
- SCCB_GPIO_Config();//io init..
-
- temp=0x80;
- if(0==wr_Sensor_Reg(0x12, temp)) //Reset SCCB
- {
- return 0 ;//錯誤返回
- }
- Delay(1);
- if(0==rd_Sensor_Reg(0x0b, &temp))//讀ID
- {
- return 0 ;//錯誤返回
- }
-
- if(temp==0x73)//OV7670
- {
- for(i=0;i<OV7670_REG_NUM;i++)
- {
- if( 0==wr_Sensor_Reg(OV7670_reg[i][0],OV7670_reg[i][1]))
- {
- return 0;//錯誤返回
- }
- }
-
- Sensor_EXTI_Config();
- Sensor_Interrupts_Config();
- }
- return 0x01; //ok
-
- }
- ///////////////////
復制代碼- /*
- wangguanfu@163.com
- 未經過本人許可禁止任何商業用途
- */
- #include "sccb.h"
- #include "delay.h"
- #include "stm32f10x.h"
- /*
- -----------------------------------------------
- 功能: 初始化模擬SCCB接口
- 參數: 無
- 返回值: 無
- -----------------------------------------------
- */
- void SCCB_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable GPIOC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = SCCB_SIC_BIT|SCCB_SID_BIT;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- }
- void SCCB_SID_GPIO_OUTPUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable GPIOC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = SCCB_SID_BIT;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void SCCB_SID_GPIO_INPUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable GPIOC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = SCCB_SID_BIT;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉 若無外部上拉電阻 必須內部有上拉
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- /*
- -----------------------------------------------
- 功能: start命令,SCCB的起始信號
- 參數: 無
- 返回值: 無
- -----------------------------------------------
- */
- void startSCCB(void)
- {
- SCCB_SID_H(); //數據線高電平
- delay_us(50);
- SCCB_SIC_H(); //在時鐘線高的時候數據線由高至低
- delay_us(50);
-
- SCCB_SID_L();
- delay_us(50);
- SCCB_SIC_L(); //數據線恢復低電平,單操作函數必要
- delay_us(50);
- }
- /*
- -----------------------------------------------
- 功能: stop命令,SCCB的停止信號
- 參數: 無
- 返回值: 無
- -----------------------------------------------
- */
- void stopSCCB(void)
- {
- SCCB_SID_L();
- delay_us(50);
-
- SCCB_SIC_H();
- delay_us(50);
-
- SCCB_SID_H();
- delay_us(50);
-
- }
- /*
- -----------------------------------------------
- 功能: noAck,用于連續讀取中的最后一個結束周期
- 參數: 無
- 返回值: 無
- -----------------------------------------------
- */
- void noAck(void)
- {
-
- SCCB_SID_H();
- delay_us(50);
-
- SCCB_SIC_H();
- delay_us(50);
-
- SCCB_SIC_L();
- delay_us(50);
-
- SCCB_SID_L();
- delay_us(50);
- }
- /*
- -----------------------------------------------
- 功能: 寫入一個字節的數據到SCCB
- 參數: 寫入數據
- 返回值: 發送成功返回1,發送失敗返回0
- -----------------------------------------------
- */
- unsigned char SCCBwriteByte(unsigned char m_data)
- {
- unsigned char j,tem;
- for(j=0;j<8;j++) //循環8次發送數據
- {
- if((m_data<<j)&0x80)
- {
- SCCB_SID_H();
- }
- else
- {
- SCCB_SID_L();
- }
- delay_us(50);
- SCCB_SIC_H();
- delay_us(50);
- SCCB_SIC_L();
- delay_us(2);
- }
- //delay_us(50);
- SCCB_SID_IN;/*設置SDA為輸入*/
- delay_us(10);
- SCCB_SIC_H();
- delay_us(80);
- if(SCCB_SID_STATE){tem=0;} //SDA=1發送失敗,返回0}
- else {tem=1;} //SDA=0發送成功,返回1
- SCCB_SIC_L();
- delay_us(50);
- SCCB_SID_OUT;/*設置SDA為輸出*/
- return (tem);
- }
- /*
- -----------------------------------------------
- 功能: 一個字節數據讀取并且返回
- 參數: 無
- 返回值: 讀取到的數據
- -----------------------------------------------
- */
- unsigned char SCCBreadByte(void)
- {
- unsigned char read,j;
- read=0x00;
-
- SCCB_SID_IN;/*設置SDA為輸入*/
- delay_us(50);
- for(j=8;j>0;j--) //循環8次接收數據
- {
- //delay_us(100);
- SCCB_SIC_H();
- delay_us(50);
- read=read<<1;
- if(SCCB_SID_STATE)
- {
- read=read+1;
- }
- SCCB_SIC_L();
- delay_us(50);
- }
- SCCB_SID_OUT;/*設置SDA為輸出*/
- return(read);
- }
復制代碼
|