/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_exti.h"
#include "OV7740.h"
#include "OV7740Reg.h"
#include "sccb.h"
#include "misc.h"
#include "delay.h"
/* Private define ------------------------------------------------------------*/
#define OV7740_HREF_BIT GPIO_Pin_2
#define OV7740_VSYNC_BIT GPIO_Pin_4
#define GPIO_PIN_PCLK GPIO_Pin_1
#define GPIO_PIN_REST GPIO_Pin_5
#define GPIO_PIN_XCLK GPIO_Pin_8
#define GPIO_PCLK_PORT GPIOB
#define GPIO_VSYNC_CMOS GPIOB
#define RCC_APB2Periph_GPIO_VSYNC_CMOS RCC_APB2Periph_GPIOB
#define GPIO_PIN_VSYNC_CMOS GPIO_Pin_4//GPIO_Pin_4
#define EXTI_LINE_VSYNC_CMOS EXTI_Line4//EXTI_Line4
#define GPIO_PORT_SOURCE_VSYNC_CMOS GPIO_PortSourceGPIOB
#define GPIO_PIN_SOURCE_VSYNC_CMOS GPIO_PinSource4//GPIO_PinSource14
#define HREF_H GPIOB->BSRR = GPIO_Pin_2 //
#define HREF_L GPIOB->BRR = GPIO_Pin_2 //
#define XCLK_H GPIOA->BSRR = GPIO_Pin_8
#define XCLK_L GPIOA->BRR = GPIO_Pin_8
#define CAMRST_H GPIOB->BSRR = GPIO_Pin_5
#define CAMRST_L GPIOB->BRR = GPIO_Pin_5
/* Private variables ---------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void OV7740_XCLK_ON(void)
{
GPIO_InitTypeDef GPIO_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 //Output clock source on the specified pin PA8
}
void OV7740_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_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);
}
void OV7740_Interrupts_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //Sub-priority
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void OV7740_EXTI_Config(void) //
{
EXTI_InitTypeDef EXTI_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);
}
u8 OV7740_writeReg(u8 regID, 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);
}
if(0 == SCCB_writeByte(regDat))
{
SCCB_Stop();
return(0);
}
SCCB_Stop();
return(1);
}
u8 OV7740_readReg(u8 regID, u8 *regDat)
{
//Set the register address through write operation
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();
//After setting the register address, read
while(SCCB_Start() == 0);
if(0==SCCB_writeByte(SCCB_7740_RDADDR))
{
SCCB_Stop();
return(0);
}
*regDat = SCCB_readByte();
SCCB_Stop();
return(1);
}
u8 OV7740_init(void)
{
int i,n;
u8 val;
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)) return 0;
Delay_ms(10);
if(0 == OV7740_writeReg(SCCB_REG12,0x51)) return 0;
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]))
{
return 0;
}
delay_us(10);
}
OV7740_setExposure(15);
OV7740_setGainVal(0);
OV7740_setVWindow(230);
OV7740_Interrupts_Config();
OV7740_EXTI_Config();
return 1;
}
void OV7740_setGainVal(int x)
{
//OV7740_readReg(0x15,&val);
OV7740_writeReg(0x00,Testgain[x][0]);
OV7740_writeReg(0x15,Testgain[x][1]);
}
void OV7740_setExposure(int exposure)
{
OV7740_writeReg(0x10,exposure&0xff);
OV7740_writeReg(0x0f,(exposure>>8)&0xff);
}
u8 setGain(int macro,int micro)
{
u8 getGain[8]={7,0,1,3,7,15,31,63};
u8 r00,r15;
/*if(macro ==0) {OV7740_writeReg(0x13,0x87);return 0;}*/
if (macro>7) return 0;
if (micro>15) return 0;
r15 = getGain[macro]>>4;
r00 = (getGain[macro]<<4)+micro;
/*OV7740_writeReg(0x13,0x00);manual modification*/
OV7740_writeReg(0x00,r00);
if (r15) OV7740_writeReg(0x15,r15);
return 1;
}
//even for startline
void OV7740_setVWindow(int startline)
{
OV7740_writeReg(0x19,startline>>1);
OV7740_writeReg(0x1a,(20>>1)); //20lines, >>1
}
void OV7740_setLines(int lines)
{
OV7740_writeReg(0x1a,(lines>>1)); //20lines, >>1
}
u8 OV7740_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)) return 0;
delay_us(10);
if(0 == OV7740_writeReg(0x12,0x40)) return 0;
delay_us(10);
OV7740_setVWindow(230);
if(0 == OV7740_writeReg(SCCB_REG38,0x07) )return 0;
delay_us(10);
if(0 == OV7740_writeReg(0x84,0x02) )return 0;
delay_us(10);
if(0 == OV7740_writeReg(SCCB_REG38,0x08)) return 0;
delay_us(10);
if(0 == OV7740_writeReg(0x84,0x07)) return 0;
delay_us(10);
OV7740_Interrupts_Config();
OV7740_EXTI_Config();
return 1;
}
Previous article:Use STM32CubeMx to create a USB disk reading and writing program
Next article:STM32 IWDG settings
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Help! 50Mhz sine signal output problem?
- Smart speakers are more than just listening - TI DLP Pico technology creates an excellent smart display experience
- What about emoticons?
- Design tips video generated by machine learning algorithm
- [Qinheng RISC-V core CH582] Use Bluetooth Peripheral routines to experience Bluetooth sending and receiving data functions for the first time
- EEWORLD University Hall----Live Replay: Application of NXP LPC553x in Dual Motor Control
- 【TI Course】How can I learn the complete course? How do I need to pay and how do I pay?
- E-sports support
- Infringement! Chinese company demands 10-year ban on iPhone sales and 10 billion RMB in damages
- Can stm32 separate the kernel from the application (using dynamic link libraries?)