STM32H750获取OV7670摄像头图像及上位机解码(一维码&二维码)

Posted PegasusYu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32H750获取OV7670摄像头图像及上位机解码(一维码&二维码)相关的知识,希望对你有一定的参考价值。

STM32H750获取OV7670摄像头图像及上位机解码(一维码&二维码)

1. 目的

针对静止拍摄图像场景,实现STM32H750对30万像素OV7670摄像头进行图像捕获,并通过串口将数据送到上位机软件进行解码。
本文可作为STM32H7及STM32F7系列驱动OV7670摄像头的代码参考,通过DCMI的CROP功能,使用者可以从OV7670最大输出分辨率(640×480)中“剪”出更小分辨率图片。

2. 场景参数说明

  1. 上位机与STM32H750通过USB全速虚拟串口(可用12000000波特率)或UART串口(230400波特率)通讯及接收图像数据。
  2. STM32H750通过DCMI总线接口与OV7670摄像头DVP接口连接,通过IIC总线与OV7670 SCCB配置端口连接,并通过GPIO连接控制摄像头端的Reset和PowerDown管脚。
  3. STM32H750从OV7670摄像头获取640×480分辨率的RGB图像,本实验不针对有SRAM扩展的场景,也不针对直接传送数据至嵌入式LCD显示的场景,因此,内部SRAM分区用于存放一帧数据*(640×480×2 bytes)的空间不足,因此,利用STM32 DCMI CROP功能,获取多帧图像的不同部分实现拼接效果,并最终将一整帧数据传送到上位机。
  4. 开发环境为STM32CUBEIDE(HAL库)

3. 通讯协议

  1. 上位机通过串口发送0x01指令到STM32, STM32接收指令后,回复0x55 0xaa xx三个字节,其中0x55 0xaa指示有效的回复,xx为摄像头类型说明,便于上位机程序进行后续数据的识别处理,当前xx==0x01为OV7670摄像头。
  2. STM32从OV7670摄像头分帧获取不同部分并发送整帧640×480×2的RGB565数据到上位机。
  3. 上位机进行图像的显示,并可选进行一维码和二维码的识别(基于zbar开源库)。
  4. 简化型设计,无校验方式

4. STM32H750时钟配置

对于需要STM32H750输出24MHz时钟给OV7670摄像头模块的场景,可以用STM32 MCO功能输出24MHz时钟。如果摄像头模块自带晶振,不需要STM32提供时钟。
在这里插入图片描述

在这里插入图片描述

5. STM32H750通讯接口配置

USB虚拟串口:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

USART1串口配置:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

6. 摄像头接口配置

DCMI接口:
STM32H7的HAL库DCMI接口,从1.8版本升级1.9以上版本后,配置及函数存在问题。因此这里的参数配置(“Parameter Settings”)部分,会在程序里面重新配置。
在这里插入图片描述
而其它部分正常配置:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
需要单独对DCMI接口的HSYNC和VSYNC做输入GPIO的配置:
在这里插入图片描述

OV7670的SCCB接口时序通过STM32的GPIO管脚模拟,不采用专用的IIC管脚。OV7670的Reset和PowerDown信号,也通过2个GPIO进行管理控制。
在这里插入图片描述

保存,并生成初始代码,再进行功能代码的编写。
在这里插入图片描述

7. OV7670接口及配置代码

编写ov7670.h文件:

#include "stm32h7xx_hal.h"
#ifndef _OV7670_H
#define _OV7670_H

//for not open-drain bus
/*
 * SIOC: PE7
 * SIOD: PE8
 * VSYNC: PB7
 * HREF: PA4
 * PCLK: PA6
 * XCLK: PA8 //24MHz, optional to use
 * D7: PB9
 * D6: PB8
 * D5: PD3
 * D4: PC11
 * D3: PE1
 * D2: PC8
 * D1: PC7
 * D0: PC6
 * RESET: PD10
 * PWDN: PD11
 *
 *
 */

#define SCCB_SCL_L    		HAL_GPIO_WritePin(GPIOE,GPIO_PIN_7,GPIO_PIN_RESET)
#define SCCB_SCL_H    		HAL_GPIO_WritePin(GPIOE,GPIO_PIN_7,GPIO_PIN_SET)
#define SCCB_SDA_L    		HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_RESET)
#define SCCB_SDA_H    		HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_SET)

#define SCCB_READ_SDA    	HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_8)
#define SCCB_ID_W   	    0X42  			//OV7670 ID for Write
#define SCCB_ID_R   	    0X43  			//OV7670 ID for Read

#define OV7670_PWDN           HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11,GPIO_PIN_SET)
#define OV7670_PWUP           HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11,GPIO_PIN_RESET)
#define OV7670_RST  	      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_10,GPIO_PIN_RESET)
#define OV7670_RUN  	      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_10,GPIO_PIN_SET)
#define OV7670_VSYNC 	      HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)
#define OV7670_HREF  	      HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4)
#define OV7670_PCLK  	      HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6)


void SCCB_Start(void);
void SCCB_Stop(void);
void SCCB_No_Ack(void);
uint8_t SCCB_WR_Byte(uint8_t data);
uint8_t SCCB_RD_Byte(void);
uint8_t SCCB_WR_Reg(uint8_t reg,uint8_t data);
uint8_t SCCB_RD_Reg(uint8_t reg);
uint32_t tickdelay;

void SCCB_SDA_IN(void);
void SCCB_SDA_OUT(void);

#define ticknumber 12*10

void SCCB_Rst(void);


/***********************************/
void OV7670_640_480_RGB565_Init(void);
void OV7670_Light_Mode(uint8_t mode);
void OV7670_Color_Saturation(uint8_t sat);
void OV7670_Brightness(uint8_t bright);
void OV7670_Contrast(uint8_t contrast);
void OV7670_Special_Effects(uint8_t eft);
   
#endif


编写ov7670.c文件:

#include <ov7670.h>

//for not open-drain bus

void SCCB_Start(void)
{
    SCCB_SDA_H;
    SCCB_SCL_H;

    tickdelay = ticknumber;while(tickdelay--);
    SCCB_SDA_L;

    tickdelay = ticknumber;while(tickdelay--);
    SCCB_SCL_L;
}


void SCCB_Stop(void)
{
    SCCB_SDA_L;

    tickdelay = ticknumber;while(tickdelay--);
    SCCB_SCL_H;

    tickdelay = ticknumber;while(tickdelay--);
    SCCB_SDA_H;

    tickdelay = ticknumber;while(tickdelay--);
}

void SCCB_No_Ack(void)
{
	HAL_Delay(1);
	SCCB_SDA_H;
	SCCB_SCL_H;

	tickdelay = ticknumber;while(tickdelay--);
	SCCB_SCL_L;

	tickdelay = ticknumber;while(tickdelay--);
	SCCB_SDA_L;

	tickdelay = ticknumber;while(tickdelay--);
}

uint8_t SCCB_WR_Byte(uint8_t dat)
{
	uint8_t j,res;
	for(j=0;j<8;j++)
	{
		if(dat&0x80)SCCB_SDA_H;
		else SCCB_SDA_L;
		dat<<=1;

		tickdelay = ticknumber;while(tickdelay--);
		SCCB_SCL_H;

		tickdelay = ticknumber;while(tickdelay--);
		SCCB_SCL_L;
	}
	SCCB_SDA_IN();

	tickdelay = ticknumber;while(tickdelay--);
	SCCB_SCL_H;

	tickdelay = ticknumber;while(tickdelay--);
	if(SCCB_READ_SDA)res=1;
	else res=0;
	SCCB_SCL_L;
	SCCB_SDA_OUT();
	return res;
}

uint8_t SCCB_RD_Byte(void)
{
	uint8_t temp=0,j;
	SCCB_SDA_IN();
	for(j=8;j>0;j--)
	{

		tickdelay = ticknumber;while(tickdelay--);
	    SCCB_SCL_H;
		temp=temp<<1;
		if(SCCB_READ_SDA)temp++;

		tickdelay = ticknumber;while(tickdelay--);
		SCCB_SCL_L;
	}
	SCCB_SDA_OUT();
	return temp;
}

uint8_t SCCB_WR_Reg(uint8_t reg,uint8_t data)
{
	uint8_t res=0;
	SCCB_Start();
	if(SCCB_WR_Byte(SCCB_ID_W))res=1;

	tickdelay = ticknumber;while(tickdelay--);
	if(SCCB_WR_Byte(reg))res=1;

	tickdelay = ticknumber;while(tickdelay--);
  	if(SCCB_WR_Byte(data))res=1;
  	SCCB_Stop();
  	return	res;
}

uint8_t SCCB_RD_Reg(uint8_t reg)
{
	uint8_t val=0;
	SCCB_Start();
	SCCB_WR_Byte(SCCB_ID_W);

	tickdelay = ticknumber;while(tickdelay--);
  	SCCB_WR_Byte(reg);

  	tickdelay = ticknumber;while(tickdelay--);
	SCCB_Stop();

	tickdelay = ticknumber;while(tickdelay--);

	SCCB_Start();
	SCCB_WR_Byte(SCCB_ID_R);

	tickdelay = ticknumber;while(tickdelay--);
  	val=SCCB_RD_Byte();
  	SCCB_No_Ack();
  	SCCB_Stop();
  	return val;
}


void SCCB_SDA_IN(void)
{
	  GPIO_InitTypeDef GPIO_InitStruct = {0};
	  __HAL_RCC_GPIOE_CLK_ENABLE();
	  GPIO_InitStruct.Pin = GPIO_PIN_8;
	  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	  GPIO_InitStruct.Pull = GPIO_PULLUP;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
}

void SCCB_SDA_OUT(void)
{
	  GPIO_InitTypeDef GPIO_InitStruct = {0};
	  __HAL_RCC_GPIOE_CLK_ENABLE();
	  GPIO_InitStruct.Pin = GPIO_PIN_8;
	  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
}

void SCCB_Rst(void)
{
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11,GPIO_PIN_RESET)	;
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_10,GPIO_PIN_RESET)	;
	HAL_Delay(5);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_10,GPIO_PIN_SET)	;
	HAL_Delay(5);
}



//--------------OV7670 Functions--------------//
void  OV7670_640_480_RGB565_Init(void)
{
    SCCB_WR_Reg(0x11, 0x02);        // 30fps
    SCCB_WR_Reg(0x6b, 0x8a);        // pclk*6
    SCCB_WR_Reg(0x3A, 0x04);
    SCCB_WR_Reg(0x12, 0x04);        //RGB
    SCCB_WR_Reg(0x17, 0x13);
    SCCB_WR_Reg(0x18, 0x01);
    SCCB_WR_Reg(0x32, 0xB6);
    SCCB_WR_Reg(0x19, 0x02);
    SCCB_WR_Reg(0x1A, 0x7A);
    SCCB_WR_Reg(0x03, 0x0F);
    SCCB_WR_Reg(0x0C, 0x00);
    SCCB_WR_Reg(0x3E, 0x00);
    SCCB_WR_Reg(0x70, 0x3A);
    SCCB_WR_Reg(0x71, 0x35);
    SCCB_WR_Reg(0x72, 0x11);
    SCCB_WR_Reg(0x73, 0xF0);
    SCCB_WR_Reg(0xA2, 0x3B);
    SCCB_WR_Reg(0x1E, 0x0F);
    SCCB_WR_Reg(0x7a, 0x20);
    SCCB_WR_Reg(0x7b, 0x03);
    SCCB_WR_Reg(0x7c, 0x0a);
    SCCB_WR_Reg(0x7d, 0x1a);
    SCCB_WR_Reg(0x7e, 0x3f);
    SCCB_WR_Reg(0x7f, 0x4e);
    SCCB_WR_Reg(0x80, 0x5b);
    SCCB_WR_Reg(0x81, 0x68);
    SCCB_WR_Reg(0x82, 0x75);
    SCCB_WR_Reg(0x83, 0x7f);
    SCCB_WR_Reg(0x84, 0x89);
    SCCB_WR_Reg(0x85, 0x9a);
    SCCB_WR_Reg(0x86, 0xa6);
    SCCB_WR_Reg(0x87, 0xbd);
    SCCB_WR_Reg(0x88, 0xd3);
    SCCB_WR_Reg(0x89, 0xe8);
    SCCB_WR_Reg(0x13, 0xE0);
    SCCB_WR_Reg(0x00, 0x00);
    SCCB_WR_Reg(0x10, 0x00);
    SCCB_WR_Reg(0x0D, 0x50);
    SCCB_WR_Reg(0x42, 0x40);
    SCCB_WR_Reg(0x14, 0x28);
    SCCB_WR_Reg(0xA5, 0x03);
    SCCB_WR_Reg(0xAB, 0x03);
    SCCB_WR_Reg(0x24, 0x50);
    SCCB_WR_Reg(0x25, 0x43);
    SCCB_WR_Reg(0x26, 0xa3);
    SCCB_WR_Reg(0x9F, 0x78);
    SCCB_WR_Reg(0xA0, 0x68);
    SCCB_WR_Reg(0xA1, 0x03);
    SCCB_WR_Reg(0xA6, 0xd2);
    SCCB_WR_Reg(0xA7, 0xd2);
    SCCB_WR_Reg(0xA8, 0xF0);
    SCCB_WR_Reg(0xA9, 0x80);
    SCCB_WR_Reg(0xAA, 0x14);
    SCCB_WR_Reg(0x13, 0xE5);
    SCCB_WR_Reg(0x0E, 0x61);
    SCCB_WR_Reg(0x0F, 0x4B);         // Flip (bit4) & Mirror (bit5)
    SCCB_WR_Reg(0x16, 0x02);
    SCCB_WR_Reg(0x21, 0x02);
    SCCB_WR_Reg(0x22, 0x91);
    SCCB_WR_Reg(0x29, 0x07);
    SCCB_WR_Reg(0x33, 0x0B);
    SCCB_WR_Reg(0x35, 0x0B);
    SCCB_WR_Reg(0x37, 0x1D);
    SCCB_WR_Reg(0x38, 0x71);
    SCCB_WR_Reg(0x39, 0x2A);
    SCCB_WR_Reg(0x3C, 0x78);
    SCCB_WR_Reg(0x4D, 0x40);
    SCCB_WR_Reg(0x4E, 0x20);
    SCCB_WR_Reg(0x69, 0x00);
    SCCB_WR_Reg(0x74, 0x10);
    SCCB_WR_Reg(0x8D, 0x4F);
    SCCB_WR_Reg(0x8E, 0x00);
    SCCB_WR_Reg(0x8F, 0x00);
    SCCB_WR_Reg(0x90, 0x00);
    SCCB_WR_Reg(0x91, 0x00);
    SCCB_WR_Reg(0x96, 0x00);
    SCCB_WR_Reg(0x9A, 0x80);
    SCCB_WR_Reg(0xB0, 0x84);
    SCCB_WR_Reg(0xB1, 0x0C);
    SCCB_WR_Reg(0xB2, 0x0E);
    SCCB_WR_Reg(0xB3, 0x82);
    SCCB_WR_Reg(0xB8, 0x0A);
    SCCB_WR_Reg(0x43, 0x02);
    SCCB_WR_Reg(0x44, 0xf2);
    SCCB_WR_Reg(0x45, 0x46);
    SCCB_WR_Reg(0x46, 0x63);
    SCCB_WR_Reg(0x47, 0x32);
    SCCB_WR_Reg(0x48, 0x3b);
    SCCB_WR_Reg(0x59, 0x92);
    SCCB_WR_Reg(0x5a, 0x9b);
    SCCB_WR_Reg(0x5b, 0xa5);
    SCCB_WR_Reg(0x5c, 0x7a);
    SCCB_WR_Reg(0x5d, 0x4a);
    SCCB_WR_Reg(0x5e, 0x0a);
    SCCB_WR_Reg(0x6c, 0x0a);
    SCCB_WR_Reg(0x6d, 0x55);
    SCCB_WR_Reg(0x6e, 0x11);
    SCCB_WR_Reg(0x6f, 0x9e);
    SCCB_WR_Reg(0x6A, 0x40);
    SCCB_WR_Reg(0x01, 0x40);
    SCCB_WR_Reg(0x02, 0x40);
    SCCB_WR_Reg(0x13, 0xf7);
    SCCB_WR_Reg(0x4f, 0x9c);
    SCCB_WR_Reg(0x50, 0x99);
    SCCB_WR_Reg(0x51, 0x02);
    SCCB_WR_Reg(0x52, 0x29);
    SCCB_WR_Reg(0x53, 0x8b);
    SCCB_WR_Reg(0x54, 0xb5);
    SCCB_WR_Reg(0x58, 0x1e);
    SCCB_WR_Reg(0x62, 0x08);
    SCCB_WR_Reg(0x63, 0x10);
    SCCB_WR_Reg(0x64, 0x04);
    SCCB_WR_Reg(0x65, 0x00);
    SCCB_WR_Reg(0x66, 0x05);
    SCCB_WR_Reg(0x94, 0x04);
    SCCB_WR_Reg(0x95, 0x06);
    SCCB_WR_Reg(0x41, 0x08);
    SCCB_WR_Reg(0x3F, 0x00);
    SCCB_WR_Reg(0x75, 0x44);
    SCCB_WR_Reg(0x76, 0xe1);
    SCCB_WR_Reg(0x4C, 0x00);
    SCCB_WR_Reg(0x77, 0x01);
    SCCB_WR_Reg(0x3D, 0xC0);
    SCCB_WR_Reg(0x4B, 0x09);
    SCCB_WR_Reg(0xC9, 0x60);
    SCCB_WR_Reg(0x41, 0x38);
    SCCB_WR_Reg(0x56, 0x40);
    SCCB_WR_Reg(0x34, 0x11);
    SCCB_WR_Reg(0x3b, 0x02);
    SCCB_WR_Reg(0xa4, 0x88);        //disable  night mode
    SCCB_WR_Reg(0x92, 0x00);
    SCCB_WR_Reg(0x96, 0x00);
    SCCB_WR_Reg(0x97, 0x30);
    SCCB_WR_Reg(0x98, 0x20);
    SCCB_WR_Reg(0x99, 0x20);
    SCCB_WR_Reg(0x9A, 0x84);
    SCCB_WR_Reg(0x9B, 0x29);
    SCCB_WR_Reg(0x9C, 0x03);
    SCCB_WR_Reg(0x9D, 0x99);
    SCCB_WR_Reg(0x9E, 0x7F);
    SCCB_WR_Reg(0x78, 0x00);
    SCCB_WR_Reg(0x79, 0x01);
    SCCB_WR_Reg(0xc8, 0xf0);
    SCCB_WR_Reg(0x79, 0x0f);
    SCCB_WR_Reg(0xc8, 0x00);
    SCCB_WR_Reg(0x79, 0x10);
    SCCB_WR_Reg(0xc8, 0x7e);
    SCCB_WR_Reg(0x79, 0x0a);
    SCCB_WR_Reg(0xc8, 0x80);
    SCCB_WR_Reg(0x79, 0x0b);
    SCCB_WR_Reg(0xc8, 0x01);
    SCCB_WR_Reg(0x79, 0x0c);
    SCCB_WR_Reg(0xc8, 0x0f);
    SCCB_WR_Reg(0x79, 0x0d);
    SCCB_WR_Reg(0xc8, 0x20);
    SCCB_WR_Reg(0x79, 0x09);
    SCCB_WR_Reg(0xc8, 0x80);
    SCCB_WR_Reg(0x79, 0x02);
    SCCB_WR_Reg(0xc8, 0xc0);
    SCCB_WR_Reg(0x79, 0x03);
    SCCB_WR_Reg(0xc8, 0x40);
    SCCB_WR_Reg(0x79, 0x05);
    SCCB_WR_Reg(0xc8, 0x30);
    SCCB_WR_Reg(0x79, 0x26);
    SCCB_WR_Reg(0x3b, 0x02);
    SCCB_WR_Reg(0x43, 0x02);
    SCCB_WR_Reg(0x44, 0xf2);
    SCCB_WR_Reg(0x30, 0x4F);
    SCCB_WR_Reg(0x09, 0x00);

     SCCB_WR_Reg(0x15, 0x00);
	 SCCB_WR_Reg(0x40, 0xd0);
	 SCCB_WR_Reg(0x8c, 0x00);


}

void OV7670_config_window(uint16_t startx,uint16_t starty,uint16_t width, uint16_t height)
{
uint16_t endx=(startx+width*2)%784;
uint16_t endy=(starty+height*2);
uint16_t x_reg, y_reg;
uint16_t temp;

SCCB_WR_Reg(0x12, 0x14);

x_reg = SCCB_RD_Reg(0x32);
x_reg &= 0xC0;

y_reg = SCCB_RD_Reg(0x03);
y_reg &= 0xF0;

temp = x_reg|((endx&0x7)<<3)|(startx&0x7);
SCCB_WR_Reg(0x32, temp );
temp = (startx&0x7F8)>>3;
SCCB_WR_Reg(0x17, temp );
temp = (endx&0x7F8)>>3;
SCCB_WR_Reg(0x18, temp );

temp = y_reg|((endy&0x3)<<2)|(starty&0x3);
SCCB_WR_Reg(0x03, temp );
temp = (starty&0x3FC)>>2;
SCCB_WR_Reg(0x19, temp );
temp = (endy&0x3FC)>>2;
SCCB_WR_Reg(0x1A, temp );
}

//Light_Mode
//0:auto
//1:sunny
//2,cloudy
//3,office
//4,home
void OV7670_Light_Mode(uint8_t mode)
{
	uint8_t reg13val=0XE7;
	uint8_t reg01val=0;
	uint8_t reg02val=0;
	switch(mode)
	{
		case 1://sunny
			reg13val=0XE5;
			reg01val=0X5A;
			reg02val=0X5C;
			break;
		case 2://cloudy
			reg13val=0XE5;
			reg01val=0X58;
			reg02val=0X60;
			break;
		case 3://office
			reg13val=0XE5;
			reg01val=0X84;
			reg02val=0X4c;
			break;
		case 4://home
			reg13val=0XE5;
			reg01val=0X96;
			reg02val=0X40;
			break;
	}
	SCCB_WR_Reg(0X13,reg13val);
	SCCB_WR_Reg(0X01,reg01val);
	SCCB_WR_Reg(0X02,reg02val);
}
//Color_Saturation
//0:-2
//1:-1
//2,0
//3,1
//4,2
void OV7670_Color_Saturation(uint8_t sat)
{
	uint8_t reg4f5054val=0X80;
 	uint8_t reg52val=0X22;
	uint8_t reg53val=0X5E;
 	switch(sat)
	{
		case 0://-2
			reg4f5054val=0X40;
			reg52val=0X11;
			reg53val=0X2F;
			break;
		case 1://-1
			reg4f5054val=0X66;
			reg52val=0X1B;
			reg53val=0X4B;
			break;
		case 3://1
			reg4f5054val=0X99;
			reg52val=0X28;
			reg53val=0X71;
			break;
		case 4://2
			reg4f5054val=0XC0;
			reg52val=0X33;
			reg53val=0X8D;
			break;
	}
	SCCB_WR_Reg(0X4F,reg4f5054val);
	SCCB_WR_Reg(0X50,reg4f5054val);
	SCCB_WR_Reg(0X51,0X00);
	SCCB_WR_Reg(0X52,reg52val);
	SCCB_WR_Reg(0X53,reg53val);
	SCCB_WR_Reg(0X54,reg4f5054val);
	SCCB_WR_Reg(0X58,0X9E);
}
//Brightness
//0:-2
//1:-1
//2,0
//3,1
//4,2
void OV7670_Brightness(uint8_t bright)
{
	uint8_t reg55val=0X00;
  	switch(bright)
	{
		case 0://-2
			reg55val=0XB0;
			break;
		case 1://-1
			reg55val=0X98;
			break;
		case 3://1
			reg55val=0X18;
			break;
		case 4://2
			reg55val=0X30;
			break;
	}
	SCCB_WR_Reg(0X55,reg55val);
}
//Contrast
//0:-2
//1:-1
//2,0
//3,1
//4,2
void OV7670_Contrast(uint8_t contrast)
{
	uint8_t reg56val=0X40;
  	switch(contrast)
	{
		case 0://-2
			reg56val=0X30;
			break;
		case 1://-1
			reg56val=0X38;
			break;
		case 3://1
			reg56val=0X50;
			break;
		case 4://2
			reg56val=0X60;
			break;
	}
	SCCB_WR_Reg(0X56,reg56val);
}
//Special_Effects
//0:normal
//1,negative
//2,black-white
//3,red
//4,green
//5,blue
//6,classic
void OV7670_Special_Effects(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;
	}
	SCCB_WR_Reg(0X3A,reg3aval);
	SCCB_WR_Reg(0X68,reg67val);
	SCCB_WR_Reg(0X67,reg68val);
}

以上代码实现对OV7670的接口访问, 实现OV7670初始化(配置为640×80 RGB模式),并持续向外输出图像。

8. DMA发送机制

STM32通过串口接收指令,程序里设计一个标识变量scmd用于指示接收到的指令。需要在USB虚拟串口的接收函数里进行处理:
在这里插入图片描述

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */
	  extern uint8_t scmd;
	  if(Buf[0]==0x01) scmd=0x01;

  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  return (USBD_OK);
  /* USER CODE END 6 */
}

以及在USART1的接收中断里进行处理:
在这里插入图片描述

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if (huart ==  &huart1)
	{

		if (aRxBuffer==0x01)
		{
			scmd = 0x02;
			aRxBuffer=0x00;

			HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
		}
		else
		{

		HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);

		}


	}
      return;

}

STM32向上位机发送图像数据采用USB虚拟串口或串口DMA方式,程序里设计一个标识变量tx_busy,在当前DMA发送完后指示状态:
在这里插入图片描述

static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
{
  uint8_t result = USBD_OK;
  /* USER CODE BEGIN 13 */
  extern uint8_t tx_busy ;
  tx_busy = 0;

  UNUSED(Buf);
  UNUSED(Len);
  UNUSED(epnum);
  /* USER CODE END 13 */
  return result;
}

同样,对USART1的DMA发送完处理:
在这里插入图片描述

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
  extern uint8_t tx_busy;
	if (huart ==  &huart1)
	{
		tx_busy = 0;
	}

}

9. DCMI DMA地址自增方式

初始化DCMI DMA配置时,如将接收地址设置为不自增方式。后续的图像捕获需要工作在DMA接收地址自增方式,因此单独设计两个函数可用于切换。

void DCMI_DMA_MemInc_En(void)
{
    HAL_DMA_DeInit(&hdma_dcmi);

    hdma_dcmi.Init.MemInc = DMA_MINC_ENABLE;
    if (HAL_DMA_Init(&hdma_dcmi) != HAL_OK)
    {
      Error_Handler();
    }
}

void DCMI_DMA_MemInc_Den(void)
{
    HAL_DMA_DeInit(&hdma_dcmi);
    hdma_dcmi.Init.MemInc = DMA_MINC_DISABLE;
    if (HAL_DMA_Init(&hdma_dcmi) != HAL_OK)
    {
      Error_Handler();
    }
}

10. 初始化修正函数

按照1.8版本HAL库,设计DCMI的初始化修正函数:

void PY_DCMI_Full_Init(void)
{
  hdcmi.Instance = DCMI;
  hdcmi.Init.SynchroMode = DCMI_SYNCHRO_HARDWARE;
  hdcmi.Init.PCKPolarity = DCMI_PCKPOLARITY_RISING;
  hdcmi.Init.VSPolarity = DCMI_VSPOLARITY_HIGH;
  hdcmi.Init.HSPolarity = DCMI_HSPOLARITY_LOW;
  hdcmi.Init.CaptureRate = DCMI_CR_ALL_FRAME;
  hdcmi.Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;
  hdcmi.Init.JPEGMode = DCMI_JPEG_DISABLE;
  hdcmi.Init.ByteSelectMode = DCMI_BSM_ALL;
  hdcmi.Init.ByteSelectStart = DCMI_OEBS_ODD;
  hdcmi.Init.LineSelectMode = DCMI_LSM_ALL;
  hdcmi.Init.LineSelectStart = DCMI_OELS_ODD;
  if (HAL_DCMI_Init(&hdcmi) != HAL_OK)
  {
    Error_Handler();
  }
}

11. 主函数代码

在程序跑起来后,会先对OV7670接口进行典型寄存器读取并USART1串口输出,用于识别接口时序是否正常。然后进入命令等待,接收到指令后,再从OV7670输出的图像中截取数据串口DMA输出。

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include "ov7670.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
void DCMI_DMA_MemInc_En(void);
void DCMI_DMA_MemInc_Den(void);
void PY_DCMI_Full_Init(void);
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t aRxBuffer=0;

uint8_t TxBuff[99] = {0};
uint8_t StatusFlag = 0;

uint8_t OV7670_verh = 0xff, OV7670_verl=0xff;

HAL_StatusTypeDef dcmi_dma_status = HAL_OK;

uint32_t* dcmi_data_buff;

uint32_t DCMI_RN = 0;  //row number
uint32_t DCMI_CN = 0;  //column number
uint32_t DCMI_RS = 0;  //row start
uint32_t DCMI_CS = 0;  //column start

uint8_t scmd = 0;
uint8_t tx_busy = 0;
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

DCMI_HandleTypeDef hdcmi;
DMA_HandleTypeDef hdma_dcmi;

UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_tx;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_DCMI_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	dcmi_data_buff = 0x30000000;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USB_DEVICE_Init();
  MX_DCMI_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  if (HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1)!=HAL_OK)
   {
 	  MX_USART1_UART_Init();
 	  HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
   }

    SCCB_Rst();
    OV7670_verh = SCCB_RD_Reg(0x0a);
 	OV7670_verl = SCCB_RD_Reg(0x0b);
 	HAL_UART_Transmit(&huart1, &OV7670_verh, 1, 0xFFFFFF);
 	HAL_Delay(50);
 	HAL_UART_Transmit(&huart1, &OV7670_verl, 1, 0xFFFFFF);
 	HAL_Delay(50);

    OV7670_640_480_RGB565_Init();

    OV7670_Light_Mode(0);
    OV7670_Color_Saturation(2);
    OV7670_Brightness(2);
    OV7670_Contrast(2);
    HAL_Delay(200);

    HAL_DCMI_DeInit(&hdcmi);
    PY_DCMI_Full_Init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  HAL_Delay(1);
	  if (scmd==1)
	    {
		  scmd = 0;

	   	    TxBuff[0]=0x55;TxBuff[1]=0xaa;
	   	    TxBuff[2]=0x01; //OV7670 label
	   	    tx_busy = 1;
	   	    CDC_Transmit_FS(TxBuff, 3);
	   	    while(tx_busy==1) HAL_Delay(1);

     	  dcmi_dma_status = HAL_DCMI_Init(&hdcmi);

    	  for (uint8_t i=0; i<10;i++)
    	  {
	 		     HAL_DCMI_DisableCrop (&hdcmi);

	 	    	 DCMI_RN = 48;
	 	    	 DCMI_CN = 1280;

	 	    	 DCMI_RS = 48*i;
	 	    	 DCMI_CS = 0;

	 	    	 HAL_DCMI_ConfigCrop (&hdcmi, DCMI_CS, DCMI_RS, DCMI_CN, DCMI_RN);
	 	    	 HAL_Delay(1);
	 	    	 HAL_DCMI_EnableCrop (&hdcmi);
	 	    	 HAL_Delay(1);

	 	    	 dcmi_dma_status = HAL_DCMI_Start_DMA(&hdcmi, DCMI_MODE_SNAPSHOT, dcmi_data_buff, DCMI_CN*DCMI_RN/4);

	 	    	 while(HAL_DMA_GetState(&hdcmi)==HAL_DMA_STATE_BUSY) ;
	 	    	 HAL_DCMI_Stop(&hdcmi);

 	    	     tx_busy = 1;
		    	 CDC_Transmit_FS((uint8_t *)dcmi_data_buff, 61440);
		    	 while(tx_busy!=0) ;
    	  }

	    }

	  if (scmd==2)
	    {
		  scmd = 0;

      	  DCMI_DMA_MemInc_En();
	   	  TxBuff[0]=0x55;TxBuff[1]=0xaa;
	   	  TxBuff[2]=0x01; //OV7670 label
          HAL_UART_Transmit(&huart1, TxBuff, 3, 0xFFFFFF);
          HAL_Delay(100);

     	  dcmi_dma_status = HAL_DCMI_Init(&hdcmi);

    	  for (uint8_t i=0; i<10;i++)
    	  {

	 		     HAL_DCMI_DisableCrop (&hdcmi);

	 	    	 DCMI_RN = 48;
	 	    	 DCMI_CN = 1280;

	 	    	 DCMI_RS = 48*i;
	 	    	 DCMI_CS = 0;

	 	    	 HAL_DCMI_ConfigCrop (&hdcmi, DCMI_CS, DCMI_RS, DCMI_CN, DCMI_RN);
	 	    	 HAL_Delay(1);
	 	    	 HAL_DCMI_EnableCrop (&hdcmi);
	 	    	 HAL_Delay(1);

	 	    	 dcmi_dma_status = HAL_DCMI_Start_DMA(&hdcmi, DCMI_MODE_SNAPSHOT, dcmi_data_buff, DCMI_CN*DCMI_RN/4);
                 while(HAL_DMA_GetState(&hdcmi)==HAL_DMA_STATE_BUSY) ;
 	 	    	 HAL_DCMI_Stop(&hdcmi);

 	    	     tx_busy = 1;
		    	 HAL_UART_Transmit_DMA(&huart1, (uint8_t *)dcmi_data_buff, 61440);
		    	 while(tx_busy!=0) HAL_Delay(1);

    	  }



	    }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Supply configuration update enable
  */
  HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 32;
  RCC_OscInitStruct.PLL.PLLN = 480;
  RCC_OscInitStruct.PLL.PLLP = 2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_1;
  RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  RCC_OscInitStruct.PLL.PLLFRACN = 0;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI48, RCC_MCODIV_2);
}

/**
  * @brief DCMI Initialization Function
  * @param None
  * @retval None
  */
static void MX_DCMI_Init(void)
{

  /* USER CODE BEGIN DCMI_Init 0 */

  /* USER CODE END DCMI_Init 0 */

  /* USER CODE BEGIN DCMI_Init 1 */

  /* USER CODE END DCMI_Init 1 */
  hdcmi.Instance = DCMI;
  hdcmi.Init.SynchroMode = DCMI_SYNCHRO_EMBEDDED;
  hdcmi.Init.PCKPolarity = DCMI_PCKPOLARITY_RISING;
  hdcmi.Init.CaptureRate = DCMI_CR_ALL_FRAME;
  hdcmi.Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;
  hdcmi.Init.SyncroCode.FrameEndCode = 0;
  hdcmi.Init.SyncroCode.FrameStartCode = 0;
  hdcmi.Init.SyncroCode.LineStartCode = 0;
  hdcmi.Init.SyncroCode.LineEndCode = 0;
  hdcmi.Init.JPEGMode = DCMI_JPEG_DISABLE;
  hdcmi.Init.ByteSelectMode = DCMI_BSM_ALL;
  hdcmi.Init.ByteSelectStart = DCMI_OEBS_ODD;
  hdcmi.Init.LineSelectMode = DCMI_LSM_ALL;
  hdcmi.Init.LineSelectStart = DCMI_OELS_ODD;
  if (HAL_DCMI_Init(&hdcmi) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN DCMI_Init 2 */

  /* USER CODE END DCMI_Init 2 */

}

/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 230400;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Stream0_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
  /* DMA1_Stream1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7|GPIO_PIN_8, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_RESET);

  /*Configure GPIO pins : PE7 PE8 */
  GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  /*Configure GPIO pins : PD10 PD11 */
  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /*Configure GPIO pin : PA8 */
  GPIO_InitStruct.Pin = GPIO_PIN_8;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if (huart ==  &huart1)
	{

		if (aRxBuffer==0x01)
		{
			scmd = 0x02;
			aRxBuffer=0x00;

			HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
		}
		else
		{

		HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);

		}


	}
      return;

}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
  extern uint8_t tx_busy;
	if (huart ==  &huart1)
	{
		tx_busy = 0;
	}

}

void DCMI_DMA_MemInc_En(void)
{
    HAL_DMA_DeInit(&hdma_dcmi);

    hdma_dcmi.Init.MemInc = DMA_MINC_ENABLE;
    if (HAL_DMA_Init(&hdma_dcmi) != HAL_OK)
    {
      Error_Handler();
    }
}

void DCMI_DMA_MemInc_Den(void)
{
    HAL_DMA_DeInit(&hdma_dcmi);
    hdma_dcmi.Init.MemInc = DMA_MINC_DISABLE;
    if (HAL_DMA_Init(&hdma_dcmi) != HAL_OK)
    {
      Error_Handler();
    }
}


void PY_DCMI_Full_Init(void)
{
  hdcmi.Instance = DCMI;
  hdcmi.Init.SynchroMode = DCMI_SYNCHRO_HARDWARE;
  hdcmi.Init.PCKPolarity = DCMI_PCKPOLARITY_RISING;
  hdcmi.Init.VSPolarity = DCMI_VSPOLARITY_HIGH;
  hdcmi.Init.HSPolarity = DCMI_HSPOLARITY_LOW;
  hdcmi.Init.CaptureRate = DCMI_CR_ALL_FRAME;
  hdcmi.Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;
  hdcmi.Init.JPEGMode = DCMI_JPEG_DISABLE;
  hdcmi.Init.ByteSelectMode = DCMI_BSM_ALL;
  hdcmi.Init.ByteSelectStart = DCMI_OEBS_ODD;
  hdcmi.Init.LineSelectMode = DCMI_LSM_ALL;
  hdcmi.Init.LineSelectStart = DCMI_OELS_ODD;
  if (HAL_DCMI_Init(&hdcmi) != HAL_OK)
  {
    Error_Handler();
  }
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\\r\\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

12. STM32H750完整代码

基于STM32CUBEIDE工程:
https://download.csdn.net/download/hwytree/20372220

13. 上位机软件

配套测试用上位机软件分为一维码识别和二维码识别两个软件,下载地址:
一维码识别
https://download.csdn.net/download/hwytree/20306099
二维码识别
https://download.csdn.net/download/hwytree/20306112

14. 一维码识别效果

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

15. 二维码识别效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

16. 上位机实现解码原理范例

https://blog.csdn.net/hwytree/article/details/119004881

-End-

以上是关于STM32H750获取OV7670摄像头图像及上位机解码(一维码&二维码)的主要内容,如果未能解决你的问题,请参考以下文章

stm32h750pwm有多少个

OV7670基于FPGA的OV7670摄像头介绍和使用

stm32摄像头ov5640得到的数据有多大

基于OV7670摄像头视频传输

emWin6.x的摄像头OV7670动态图形显示

普通低速单片机驱动OV7670等摄像头为啥要用FIFO