본문 바로가기
MCU/STM32

[STM32F429]DCMI(Digital camera interface)와 OV7670 CAMERA-(2) 코드(CAMERA)

by neohtux 2018. 9. 21.
728x90


CAMERA.h


#define WRITE_SLAVE_ADDR                0x42
#define READ_SLAVE_ADDR                 0x43

#define DEVICE_REG_NUM                  140 //121
#define I2C_DIR_TR                      0x00000001U

#define QVGA_RESOLUTION                 (320*240)
void SCCB_write_reg(uint8_t reg_addr, uint8_t data);
void CAMERA_Init(void);
int SCCB_read_reg(uint8_t reg_addr);
void ov7670_capture_snapthot();


CAMERA.c



#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_dcmi.h"
#include "CAMERA.h"
#include "LCD.h"
extern DCMI_HandleTypeDef hdcmi;
extern I2C_HandleTypeDef hi2c2;
uint8_t SCCB_txBuf[2];

int error=0;
const uint8_t ov7670_reg[DEVICE_REG_NUM][2]=
{
  {0x15,0x18},
   {0x12, 0x14},   // QVGA, RGB
  {0x8C, 0x00},   // RGB444 Disable
  {0x40, 0x10 + 0xc0},   // RGB565, 00 - FF
  {0x3A, 0x04 + 8},   // UYVY (why?)
  {0x3D, 0x80 + 0x00},   // gamma enable, UV auto adjust, UYVY
  {0xB0, 0x84}, // important

  /* clock related */
  {0x0C, 0x04},  // DCW enable
  {0x3E, 0x19},  // manual scaling, pclk/=2
  {0x70, 0x3A},  // scaling_xsc
  {0x71, 0x35},  // scaling_ysc
  {0x72, 0x11}, // down sample by 2
  {0x73, 0xf1}, // DSP clock /= 2

  /* windowing (empirically decided...) */
  {0x17, 0x16},   // HSTART
  {0x18, 0x04},   // HSTOP
  {0x32, 0x80},   // HREF
  {0x19, 0x03},   // VSTART =  14 ( = 3 * 4 + 2)
  {0x1a, 0x7b},   // VSTOP  = 494 ( = 123 * 4 + 2)
  {0x03, 0x0a},   // VREF (VSTART_LOW = 2, VSTOP_LOW = 2)

  /* color matrix coefficient */


  {0x4f, 0xb3},
  {0x50, 0xb3},
  {0x51, 0x00},
  {0x52, 0x3d},
  {0x53, 0xa7},
  {0x54, 0xe4},
  {0x58, 0x9e},


  /* 3a */
//  {0x13, 0x84},
//  {0x14, 0x0a},   // AGC Ceiling = 2x
//  {0x5F, 0x2f},   // AWB B Gain Range (empirically decided)
//                  // without this bright scene becomes yellow (purple). might be because of color matrix
//  {0x60, 0x98},   // AWB R Gain Range (empirically decided)
//  {0x61, 0x70},   // AWB G Gain Range (empirically decided)
  {0x41, 0x38},   // edge enhancement, de-noise, AWG gain enabled


  /* gamma curve */
  {0x7b, 16},
  {0x7c, 30},
  {0x7d, 53},
  {0x7e, 90},
  {0x7f, 105},
  {0x80, 118},
  {0x81, 130},
  {0x82, 140},
  {0x83, 150},
  {0x84, 160},
  {0x85, 180},
  {0x86, 195},
  {0x87, 215},
  {0x88, 230},
  {0x89, 244},
  {0x7a, 16},
{ 0xa5, 0x05 }, //
{ 0xab, 0x07 }, //
{ 0x24, 0x95 }, //
{ 0x25, 0x33 }, //
{ 0x26, 0xe3 }, //
{ 0x9f, 0x78 }, //
{ 0xa0, 0x68 }, //
{ 0xa1, 0x03 }, //
{ 0xa6, 0xd8 }, //
{ 0xa7, 0xd8 }, //
{ 0xa8, 0xf0 }, //
{ 0xa9, 0x90 }, //
{ 0xaa, 0x94 }, //
{ 0x10, 0x00 },
                
{ 0x43, 0x0a }, //
{ 0x44, 0xf0 }, //
{ 0x45, 0x34 }, //
{ 0x46, 0x58 }, 
{ 0x47, 0x28 }, //
{ 0x48, 0x3a }, //
{ 0x59, 0x88 }, //
{ 0x5a, 0x88 }, //
{ 0x5b, 0x44 }, //
{ 0x5c, 0x67 }, //
{ 0x5d, 0x49 }, //
{ 0x5e, 0x0e }, //
{ 0x6c, 0x0a }, //
{ 0x6d, 0x55 }, //
{ 0x6e, 0x11 }, //
{ 0x6f, 0x9f }, //
{ 0x6a, 0x40 }, //
{ 0x01, 0x40 }, //
{ 0x02, 0x60 }, //
{ 0x13, 0xe7 },

  /* fps */
//  {0x6B, 0x4a}, //PLL  x4
//  {0x11, 0x80}, // pre-scalar = 1/1

  /* others */
  {0x1E, 0x03}, 
//  {0x42, 0x08}, // color bar
	
};
void SCCB_write_reg(uint8_t reg_addr, uint8_t data)
{
 SCCB_txBuf[0]=reg_addr;
 SCCB_txBuf[1]=data;
 if(HAL_I2C_Master_Transmit(&hi2c2,(uint16_t)WRITE_SLAVE_ADDR,(uint8_t*)SCCB_txBuf,2,10000)!=HAL_OK)
 {
 }
  
}
void CAMERA_Init(void){
  for(int i=0; i<DEVICE_REG_NUM, i++)
  {
    SCCB_write_reg(ov7670_reg[i][0],ov7670_reg[i][1]);
  }
}
void ov7670_capture()
{
    if(HAL_DCMI_Start_DMA(&hdcmi,DCMI_MODE_CONTINUOUS,((uint32_t)0x60000002),65535)!=HAL_OK)
      error++;  

}

300x250

댓글