新手入门
- 积分
- 13
- 金钱
- 13
- 注册时间
- 2024-1-6
- 在线时间
- 2 小时
|
1金钱
在网上参考一一篇,有自己改动了一下,连线后摄像头没有反应,想知道是代码问题吗,现在只要串口调试有图像,先不需要显示屏有图像,怎么判断到底摄像头有没被启动。
这是ov7670.h
#include "stm32f10x.h" // Device header
#include "SCCB.h"
#include "OLED.h"
#include "Delay.h"
#include "Serial.h"
/* 0100 0010 --- 写地址 *** 0100 0011 ---读地址 */
#define OV7670_ADDRESS 0x42
/* @brief 下面几个函数主要是用于便捷修改串口电平 */
uint8_t OV7670_VS(void)
{
return GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12);
}
void OV7670_W_RRST(uint8_t BitValue)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)BitValue);
delay_us(10);
}void OV7670_W_WRST(uint8_t BitValue)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_12, (BitAction)BitValue);
delay_us(10);
}
void OV7670_W_WEN(uint8_t BitValue)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_13, (BitAction)BitValue);
delay_us(10);
}
void OV7670_W_OE(uint8_t BitValue)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_14, (BitAction)BitValue);
delay_us(10);
}
void OV7670_W_RCLK(uint8_t BitValue)
{
GPIO_WriteBit(GPIOA, GPIO_Pin_11, (BitAction)BitValue);
delay_us(10);
}
/*
* @brief OV7670引脚初始化函数,在OV7670_Init() 里触发,不需外部调用
* @param 无
* @retval 无
*/
void OV7670_Pin_Init()
{
GPIO_InitTypeDef GPIO_InitStruct; /* 结构体定义 */
/* WRST WEN OE 和 RRST IO口初始化 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* GPIOB使能 */
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0 |GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14; /* PB0 PB12,PB13,PB14 */
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; /* 推挽输出 */
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; /* 50MHz*/
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_0 |GPIO_Pin_12 | GPIO_Pin_14);/* 给WRST OE 和 RRST初始赋值 */
GPIO_ResetBits(GPIOB,GPIO_Pin_13);/* 给 WEN 赋初值 */
/* RCLK IO口初始化 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* GPIOA使能 */
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_11; /* PA11 */
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; /* 推挽输出 */
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; /* 50MHz*/
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_SetBits(GPIOA,GPIO_Pin_11);/* 给RCLK初始赋值 */
/* VS IO口初始化 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* GPIOA使能 */
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12; /* PA12 */
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; /* 上拉输入 */
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; /* 50MHz*/
GPIO_Init(GPIOA,&GPIO_InitStruct);
/* D0-D7 IO口初始化 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* GPIOA使能 */
GPIO_InitStruct.GPIO_Pin=0xFF; /* PA0-PA7 */
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; /* 上拉输入 */
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; /* 50MHz*/
GPIO_Init(GPIOA,&GPIO_InitStruct);
}
/*
* @brief OV7670写寄存器
* @param 写入寄存器的地址
* @param 写入的内容(一个字节数据)
* @retval 无
*/
void OV7670_WriteReg(uint8_t RegAddress, uint8_t Data)
{
SCCB_Start();
SCCB_SendByte(OV7670_ADDRESS);
SCCB_ReceiveAck(); //SCCB_ReceiveAck()==0说明上一步执行成功,下面的两个也是
SCCB_SendByte(RegAddress);
SCCB_ReceiveAck();
SCCB_SendByte(Data);
SCCB_ReceiveAck();
SCCB_Stop();
}
/*
* @brief OV7670读取寄存器
* @param 读取寄存器的地址
* @retval 相应地址的寄存器的数据
*/
uint8_t OV7670_ReadReg(uint8_t RegAddress)
{
uint8_t Data;
SCCB_Start();
SCCB_SendByte(OV7670_ADDRESS);
SCCB_ReceiveAck(); //SCCB_ReceiveAck()==0说明上一步执行成功,下面的两个也是
SCCB_SendByte(RegAddress);
SCCB_ReceiveAck();
SCCB_Stop(); //这里的STOP很必要!!!!是SCCB不同于I2C的地方!!!
SCCB_Start();
SCCB_SendByte(OV7670_ADDRESS | 0x01);
SCCB_ReceiveAck();
Data = SCCB_ReceiveByte();
SCCB_SendNA();
SCCB_Stop();
return Data;
}
/*
* @brief 参照其他教程的寄存器设置
*/
void OV7670_RegExample(void)
{
OV7670_WriteReg(0x3a, 0x04);
OV7670_WriteReg(0x40, 0xd0);
OV7670_WriteReg(0x12, 0x14);
OV7670_WriteReg(0x32, 0x80);
OV7670_WriteReg(0x17, 0x16);
OV7670_WriteReg(0x18, 0x04);
OV7670_WriteReg(0x19, 0x02);
OV7670_WriteReg(0x1a, 0x7b);
OV7670_WriteReg(0x03, 0x06);
OV7670_WriteReg(0x0c, 0x00);
OV7670_WriteReg(0x15, 0x00);
OV7670_WriteReg(0x3e, 0x00);
OV7670_WriteReg(0x70, 0x3a);
OV7670_WriteReg(0x71, 0x35);
OV7670_WriteReg(0x72, 0x11);
OV7670_WriteReg(0x73, 0x00);
OV7670_WriteReg(0xa2, 0x02);
OV7670_WriteReg(0x11, 0x81);
OV7670_WriteReg(0x7a, 0x20);
OV7670_WriteReg(0x7b, 0x1c);
OV7670_WriteReg(0x7c, 0x28);
OV7670_WriteReg(0x7d, 0x3c);
OV7670_WriteReg(0x7e, 0x55);
OV7670_WriteReg(0x7f, 0x68);
OV7670_WriteReg(0x80, 0x76);
OV7670_WriteReg(0x81, 0x80);
OV7670_WriteReg(0x82, 0x88);
OV7670_WriteReg(0x83, 0x8f);
OV7670_WriteReg(0x84, 0x96);
OV7670_WriteReg(0x85, 0xa3);
OV7670_WriteReg(0x86, 0xaf);
OV7670_WriteReg(0x87, 0xc4);
OV7670_WriteReg(0x88, 0xd7);
OV7670_WriteReg(0x89, 0xe8);
OV7670_WriteReg(0x13, 0xe0);
OV7670_WriteReg(0x00, 0x00);
OV7670_WriteReg(0x10, 0x00);
OV7670_WriteReg(0x0d, 0x00);
OV7670_WriteReg(0x14, 0x28);
OV7670_WriteReg(0xa5, 0x05);
OV7670_WriteReg(0xab, 0x07);
OV7670_WriteReg(0x24, 0x75);
OV7670_WriteReg(0x25, 0x63);
OV7670_WriteReg(0x26, 0xA5);
OV7670_WriteReg(0x9f, 0x78);
OV7670_WriteReg(0xa0, 0x68);
OV7670_WriteReg(0xa1, 0x03);
OV7670_WriteReg(0xa6, 0xdf);
OV7670_WriteReg(0xa7, 0xdf);
OV7670_WriteReg(0xa8, 0xf0);
OV7670_WriteReg(0xa9, 0x90);
OV7670_WriteReg(0xaa, 0x94);
OV7670_WriteReg(0x13, 0xe5);
OV7670_WriteReg(0x0e, 0x61);
OV7670_WriteReg(0x0f, 0x4b);
OV7670_WriteReg(0x16, 0x02);
OV7670_WriteReg(0x1e, 0x37);
OV7670_WriteReg(0x21, 0x02);
OV7670_WriteReg(0x22, 0x91);
OV7670_WriteReg(0x29, 0x07);
OV7670_WriteReg(0x33, 0x0b);
OV7670_WriteReg(0x35, 0x0b);
OV7670_WriteReg(0x37, 0x1d);
OV7670_WriteReg(0x38, 0x71);
OV7670_WriteReg(0x39, 0x2a);
OV7670_WriteReg(0x3c, 0x78);
OV7670_WriteReg(0x4d, 0x40);
OV7670_WriteReg(0x4e, 0x20);
OV7670_WriteReg(0x69, 0x00);
OV7670_WriteReg(0x6b, 0x40);
OV7670_WriteReg(0x74, 0x19);
OV7670_WriteReg(0x8d, 0x4f);
OV7670_WriteReg(0x8e, 0x00);
OV7670_WriteReg(0x8f, 0x00);
OV7670_WriteReg(0x90, 0x00);
OV7670_WriteReg(0x91, 0x00);
OV7670_WriteReg(0x92, 0x00);
OV7670_WriteReg(0x96, 0x00);
OV7670_WriteReg(0x9a, 0x80);
OV7670_WriteReg(0xb0, 0x84);
OV7670_WriteReg(0xb1, 0x0c);
OV7670_WriteReg(0xb2, 0x0e);
OV7670_WriteReg(0xb3, 0x82);
OV7670_WriteReg(0xb8, 0x0a);
OV7670_WriteReg(0x43, 0x14);
OV7670_WriteReg(0x44, 0xf0);
OV7670_WriteReg(0x45, 0x34);
OV7670_WriteReg(0x46, 0x58);
OV7670_WriteReg(0x47, 0x28);
OV7670_WriteReg(0x48, 0x3a);
OV7670_WriteReg(0x59, 0x88);
OV7670_WriteReg(0x5a, 0x88);
OV7670_WriteReg(0x5b, 0x44);
OV7670_WriteReg(0x5c, 0x67);
OV7670_WriteReg(0x5d, 0x49);
OV7670_WriteReg(0x5e, 0x0e);
OV7670_WriteReg(0x64, 0x04);
OV7670_WriteReg(0x65, 0x20);
OV7670_WriteReg(0x66, 0x05);
OV7670_WriteReg(0x94, 0x04);
OV7670_WriteReg(0x95, 0x08);
OV7670_WriteReg(0x6c, 0x0a);
OV7670_WriteReg(0x6d, 0x55);
OV7670_WriteReg(0x4f, 0x80);
OV7670_WriteReg(0x50, 0x80);
OV7670_WriteReg(0x51, 0x00);
OV7670_WriteReg(0x52, 0x22);
OV7670_WriteReg(0x53, 0x5e);
OV7670_WriteReg(0x54, 0x80);
OV7670_WriteReg(0x09, 0x03);
OV7670_WriteReg(0x6e, 0x11);
OV7670_WriteReg(0x6f, 0x9f);
OV7670_WriteReg(0x55, 0x00);
OV7670_WriteReg(0x56, 0x40);
OV7670_WriteReg(0x57, 0x40);
OV7670_WriteReg(0x6a, 0x40);
OV7670_WriteReg(0x01, 0x40);
OV7670_WriteReg(0x02, 0x40);
OV7670_WriteReg(0x13, 0xe7);
OV7670_WriteReg(0x15, 0x00);
OV7670_WriteReg(0x58, 0x9e);
OV7670_WriteReg(0x41, 0x08);
OV7670_WriteReg(0x3f, 0x00);
OV7670_WriteReg(0x75, 0x05);
OV7670_WriteReg(0x76, 0xe1);
OV7670_WriteReg(0x4c, 0x00);
OV7670_WriteReg(0x77, 0x01);
OV7670_WriteReg(0x3d, 0xc2);
OV7670_WriteReg(0x4b, 0x09);
OV7670_WriteReg(0xc9, 0x60);
OV7670_WriteReg(0x41, 0x38);
OV7670_WriteReg(0x34, 0x11);
OV7670_WriteReg(0x3b, 0x02);
OV7670_WriteReg(0xa4, 0x89);
OV7670_WriteReg(0x96, 0x00);
OV7670_WriteReg(0x97, 0x30);
OV7670_WriteReg(0x98, 0x20);
OV7670_WriteReg(0x99, 0x30);
OV7670_WriteReg(0x9a, 0x84);
OV7670_WriteReg(0x9b, 0x29);
OV7670_WriteReg(0x9c, 0x03);
OV7670_WriteReg(0x9d, 0x4c);
OV7670_WriteReg(0x9e, 0x3f);
OV7670_WriteReg(0x78, 0x04);
OV7670_WriteReg(0x79, 0x01);
OV7670_WriteReg(0xc8, 0xf0);
OV7670_WriteReg(0x79, 0x0f);
OV7670_WriteReg(0xc8, 0x00);
OV7670_WriteReg(0x79, 0x10);
OV7670_WriteReg(0xc8, 0x7e);
OV7670_WriteReg(0x79, 0x0a);
OV7670_WriteReg(0xc8, 0x80);
OV7670_WriteReg(0x79, 0x0b);
OV7670_WriteReg(0xc8, 0x01);
OV7670_WriteReg(0x79, 0x0c);
OV7670_WriteReg(0xc8, 0x0f);
OV7670_WriteReg(0x79, 0x0d);
OV7670_WriteReg(0xc8, 0x20);
OV7670_WriteReg(0x79, 0x09);
OV7670_WriteReg(0xc8, 0x80);
OV7670_WriteReg(0x79, 0x02);
OV7670_WriteReg(0xc8, 0xc0);
OV7670_WriteReg(0x79, 0x03);
OV7670_WriteReg(0xc8, 0x40);
OV7670_WriteReg(0x79, 0x05);
OV7670_WriteReg(0xc8, 0x30);
OV7670_WriteReg(0x79, 0x26);
OV7670_WriteReg(0x09, 0x00);
}
/*
* @brief 寄存器初始化
*/
void OV7670_Configure(void)
{
OLED_ShowString(1,1,"Init...");
OV7670_WriteReg(0x12,0x80);//寄存器复位,所有寄存器复位为初始默认值
delay_us(50);
OLED_Clear();
OV7670_RegExample();/* 参照其他教程的寄存器设置,具体寄存器对应的功能暂未弄清 */
/*设置测试图案输出 这里设置的是输出八色彩条*/
OV7670_WriteReg(0x70, 0x3A);
OV7670_WriteReg(0x71, 0xB5);
/*应当是数据手册有误,数据手册里写的是:
(0x70[7],0x71[7])=(1,0)输出的是八色彩条 ×
(0x70[7],0x71[7])=(0,1)输出的是Shifting “1” ×
而实际上是反过来的
(0x70[7],0x71[7])=(1,0)输出的是Shifting “1” ✓
(0x70[7],0x71[7])=(0,1)输出的是八色彩条 ✓
*/
}
/*
* @brief OV7670初始化
* @param 无
* @retval 无
*/
void OV7670_Init(void)
{
SCCB_Init();//SCCB初始化
OV7670_Pin_Init();//引脚初始化
OV7670_Configure();//寄存器预设
}
/*
* @brief FIFO读取图像信息
* @param 无
* @retval 无
*/
uint8_t OV7670_State=0;
void OV7670_FIFOGetPic(void)
{
if(OV7670_State==0)
{
while(OV7670_VS()==0);/* 保证进入一个新的帧周期 */
OV7670_W_WRST(0);//写指针复位
OV7670_W_WRST(1);
OV7670_W_WEN(1);//写使能
OV7670_State=1;//下一次进入OV7670_FIFOGetPic()进入else
}
else
{
OV7670_W_WRST(0);//写指针复位
OV7670_W_WRST(1);
OV7670_W_WEN(0);//写失能
}
}
/*
* @brief STM32从OV7670读取图像信息
* @param 无
* @retval 无
*/
uint8_t frame[320*40];
void OV7670_STM32GetPic(void)
{
uint32_t i;
OV7670_W_RRST(0);//读指针复位
OV7670_W_RRST(1);//复位完恢复原状态
OV7670_W_OE(0);//输出使能
for(i=0;i<320*40;i++)
{
OV7670_W_RCLK(0);
frame=GPIOA->IDR&0xFF;//先提取数据再通过串口发送
OV7670_W_RCLK(1);
}
OV7670_W_RRST(0);//读指针复位
OV7670_W_RRST(1);//复位完恢复原状态
OV7670_W_OE(1);//输出失能
Serial_SendByte(0x01);//调试工具要求在发送图像数据之前要发0x01和0xFE
Serial_SendByte(0xFE);
for(i=0;i<320*40;i++)
{
Serial_SendByte(frame);
OLED_ShowNum(4,1,i,8);//通过OLED打印i值用于观测数据传输进度
}
Serial_SendByte(0xFE);//调试工具要求在发送图像数据之后要发0xFE和0x01
Serial_SendByte(0x01);
OLED_ShowString(1,1,"SUCCESS");
OV7670_State=0;
}
这个是main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "OV7670.h"
#include "Serial.h"
/*
*器件引脚 对应STM32引脚 备注
**********OV7670**********
@SCL PB10 软件I2C
@SDA PB11
@WRST PB12 写指针复位(低电平使能)
@WEN PB13 从电路图中看,WE=(WEN*HERF)的非(WEN和OV7670的HREF通过与非门得到FIFO的WE)
WE低电平写使能,HREF高电平时有数据输出,而WE=(WEN*HERF)的非
因而要将写控制权给HREF时应时WEN拉高
WEN写失能(高电平使能)
@OE PB14 输出使能(低电平使能)
@RRST PB0 读指针复位(低电平使能)
@RCLK PA11 FIFO读时钟(STM32输入)
@/RE none 硬件设计上已将其和GND相连,自动使能
@VS(VSYNC) PA12
@D0-D7 PA0-PA7 数据输出口
***********串口***********
@Serial PA9(TX) PA10(RX) 电脑用的调试助手是“山外多功能调试助手”
********OLED显示屏********
@OLDE PB8(SCL)PB9(SDA)
*/
int main()
{
//OLED_Init();
OV7670_Init();
Serial_Init();/* 波特率9600(在SYSCLOCK为72MHz时正常传输)*/
while(1)
{
//OLED_ShowString(1,1,"Welcome!");
OV7670_FIFOGetPic();
delay_ms(50);
OV7670_FIFOGetPic();//第二次FIFO读取使FIFO读失能
delay_ms(50);
OV7670_STM32GetPic();
delay_ms(500);
}
}
|
|