Kamera_DCMI

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

////////////////////////////////////////////////////////////////////////////////

/////////////////////// PINOVI /////////////////////////////////////////////////


/*
XCLK - PD13
PCLK - PD0
VSYNC - PD2
HREF - PD1
RST - PB7
PWDN - PC7
D0 - PC0
D1 - PC1
D2 - PC2
D3 - PC3
D4 - PA4
D5 - PA5
D6 - PA6
D7 - PA7
SCCB SCL - PE2
SCCB SDA - PE3
LED_L - PE12
LED_R - PE15
T_L - PE0
T_R - PA10
*/
////////////////////////////////////////////////////////////////////////////////
#include <built_in.h>
#define USB_BUFF 64
#define W_IM 384
#define H_IM 300
#define LEN_IM 115200
#define VST 90
#define HOFFCNT 128
//#define NUM_WORD 32000
#define OV7670_REG_NUM 114
#define OV7670_ADDR 0x42
//USB komunikacija
char receive, cnt;
char readbuff[USB_BUFF];
char writebuff[USB_BUFF];
char receivebuff[USB_BUFF];
unsigned int count;
//******************************************************************
//Globalne promenljive
unsigned long int i, j, k;
unsigned char image[115200];
char buffer;
unsigned long int long_buffer;
unsigned int position[20];
unsigned int clock;

bit ENBL;
bit CAP_OK;
//bit CROP;
bit EVEN;
unsigned int line_cnt;
unsigned int pixel_cnt;
unsigned int sample_cnt;
const unsigned char OV7670_Reg[OV7670_REG_NUM][2] = {
/* OV7670 QVGA */
{0x3a,0x04},{0x40,0x10},{0x12,0x10/*0x14*/},{0x32,0x80},{0x17,0x16},{0x18,0x04},
{0x19,0x02},{0x1a,0x7b},{0x03,0x06},{0x0c,0x00/*0x0c*/},{0x15,0x42/*0x02*/},
{0x3e,0x00},
{0x70,0x00},{0x71,0x01},{0x72,0x11},{0x73,0x09},{0xa2,0x02},{0x11,0x00},
{0x7a,0x20},{0x7b,0x1c},{0x7c,0x28},{0x7d,0x3c},{0x7e,0x55},{0x7f,0x68},
{0x80,0x76},{0x81,0x80},{0x82,0x88},{0x83,0x8f},{0x84,0x96},{0x85,0xa3},
{0x86,0xaf},{0x87,0xc4},{0x88,0xd7},{0x89,0xe8},{0x13,0xe0},{0x00,0x00},
{0x10,0x00},{0x0d,0x00},{0x14,0x20},{0xa5,0x05},{0xab,0x07},{0x24,0x75},
{0x25,0x63},{0x26,0xA5},{0x9f,0x78},{0xa0,0x68},{0xa1,0x03},{0xa6,0xdf},
{0xa7,0xdf},{0xa8,0xf0},{0xa9,0x90},{0xaa,0x94},{0x13,0xe5},{0x0e,0x61},
{0x0f,0x4b},{0x16,0x02},{0x1e,0x37},{0x21,0x02},{0x22,0x91},{0x29,0x07},
{0x33,0x0b},{0x35,0x0b},{0x37,0x1d},{0x38,0x71},{0x39,0x2a},{0x3c,0x78},
{0x4d,0x40},{0x4e,0x20},{0x69,0x5d},{0x6b,0x40},{0x74,0x19},{0x8d,0x4f},
{0x8e,0x00},{0x8f,0x00},{0x90,0x00},{0x91,0x00},{0x92,0x00},{0x96,0x00},
{0x9a,0x80},{0xb0,0x84},{0xb1,0x0c},{0xb2,0x0e},{0xb3,0x82},{0xb8,0x0a},
{0x43,0x14},{0x44,0xf0},{0x45,0x34},{0x46,0x58},{0x47,0x28},{0x48,0x3a},
{0x59,0x88},{0x5a,0x88},{0x5b,0x44},{0x5c,0x67},{0x5d,0x49},{0x5e,0x0e},
{0x64,0x04},{0x65,0x20},{0x66,0x05},{0x94,0x04},{0x95,0x08},{0x6c,0x0a},
{0x6d,0x55},{0x4f,0x80},{0x50,0x80},{0x51,0x00},{0x52,0x22},{0x53,0x5e},
{0x54,0x80},{0x6e,0x11},{0x6f,0x9f},{0x55,0x00},{0x56,0x40},{0x57,0x80}
};
///////////////////////// PINOVI ///////////////////////////////////////////////
// Kamera OV7670
// XCLK je preko PWM modula dodeljen pinu PD13
sbit XCLK at GPIOD_ODR.B13;
sbit HREF at GPIOD_IDR.B1;
sbit RST at GPIOB_ODR.B7;
sbit PWDN at GPIOC_ODR.B7;
//Interfejs covek - masina
sbit T_L at GPIOE_IDR.B0;
sbit T_R at GPIOA_IDR.B10;
sbit LED_L at GPIOE_ODR.B12;
sbit LED_R at GPIOE_ODR.B15;
// Software I2C konekcija
//sbit Soft_I2C_Scl_Output at GPIOE_ODR.B2;
//sbit Soft_I2C_Scl_Input at GPIOE_IDR.B2;
//sbit Soft_I2C_Sda_Output at GPIOE_ODR.B3;
//sbit Soft_I2C_Sda_Input at GPIOE_IDR.B3;

//------------------------------------------------------------------------------
//Prekidna rutina - USB
void USB0Interrupt() iv IVT_INT_OTG_FS{
USB_Interrupt_Proc();
}

//------------------------------------------------------------------------------
//Prekidna rutina - VSYNC
void VSYNC_Int() iv IVT_INT_EXTI2 ics ICS_AUTO {
EXTI_PR.B2 = 1;
/* if (CAP) {
ENBL = 1;
}*/
LED_R = !LED_R;
if (GPIOD_ODR.B2) {
ENBL = 0;

}
else {
ENBL = 1;
line_cnt = 0;
pixel_cnt = 0;
sample_cnt = 0;
//LED_R = !LED_R;
}
}

//------------------------------------------------------------------------------
//Prekidna rutina - HREF
void HREF_Int() iv IVT_INT_EXTI1 ics ICS_AUTO {
EXTI_PR.B1 = 1;
if (GPIOD_ODR.B1) {
EVEN = 1;
ENBL = 1;
/* if ((line_cnt < VST) | (line_cnt > (VST + H_IM))) {
CROP = 1;
}
else {
CROP = 0;
} */
}
else {
line_cnt++;
ENBL = 0;
//LED_L = !LED_L;
}

//------------------------------------------------------------------------------
//Prekidna rutina - PCLK
void PCLK_Int() iv IVT_INT_EXTI0 ics ICS_AUTO {
EXTI_PR.B0 = 1;
LED_L = ENBL;
//LED_R = CAP_OK;
if (ENBL & CAP_OK) {
// if ((pixel_cnt > HOFFCNT) & (pixel_cnt < (HOFFCNT + W_IM))) {
//if (!EVEN) {
image[sample_cnt] = (GPIOC_IDR & 0b00001111) | (GPIOA_IDR &
0b11110000);
sample_cnt++;
if (sample_cnt == (LEN_IM - 1)) {
CAP_OK = 0;
sample_cnt = 0;
}
pixel_cnt++;
//}
// }
}
EVEN = !EVEN;
}

//------------------------------------------------------------------------------
//Konfiguracija kamere
void OV7670_Configure() {
Soft_I2C_Init();
Delay_us(25);
for (i = 0; i <= OV7670_REG_NUM; i++){
Soft_I2C_Start();
Soft_I2C_Write(OV7670_ADDR);
Delay_us(1);
Soft_I2C_Write(OV7670_Reg[i][0]);
Delay_us(1);
Soft_I2C_Write(OV7670_Reg[i][1]);
Delay_us(5);
Soft_I2C_Stop();
Delay_us(25);
}
}

//------------------------------------------------------------------------------
//Konfiguracija eksternog prekida
void Init_Interrupt() {
SYSCFGEN_bit = 1;
SYSCFG_EXTICR1 = 0b0000001100110011;
//SYSCFG_EXTICR1 = 0b0000001100000000;
EXTI_RTSR = 0x00000007;
//EXTI_RTSR = 0x00000000;
EXTI_FTSR = 0x00000006;
//EXTI_FTSR = 0x00000004;
EXTI_IMR |= 0x00000007;
//EXTI_IMR |= 0x00000004;
NVIC_IntEnable(IVT_INT_EXTI0);
NVIC_IntEnable(IVT_INT_EXTI1);
NVIC_IntEnable(IVT_INT_EXTI2);
}

void Frame_Capture() {
CAP_OK = 1;
while(!ENBL) {
XCLK = 1;
asm nop;
asm nop;
XCLK = 0;
asm nop;
asm nop;
}
for (i = 0; i < LEN_IM; i++) {
XCLK = 1;
//asm nop;
buffer = GPIOC_IDR;
asm nop;
XCLK = 0;
asm nop;
asm nop;
asm nop;
XCLK = 1;
//asm nop;
if (HREF) {
image[i] = (GPIOC_IDR & 0b00001111) | (GPIOA_IDR & 0b11110000);
}
asm nop;
XCLK = 0;
asm nop;
asm nop;
asm nop;
}
CAP_OK = 0;
ENBL = 0;
}

///////////////////////// GLAVNA FUNKCIJA //////////////////////////////////////


//******************************************************************************
void main() {
//Inicijalizacija USB modula
HID_Enable(&readbuff,&writebuff);
//DCMI interfejs, pinovi RST i PWDN
GPIO_Config(&GPIOB_BASE, _GPIO_PINMASK_7,
_GPIO_CFG_MODE_OUTPUT | _GPIO_CFG_SPEED_MAX);
GPIO_Config(&GPIOC_BASE, _GPIO_PINMASK_7,
_GPIO_CFG_MODE_OUTPUT | _GPIO_CFG_SPEED_MAX);
//GPIO_Config(&GPIOD_BASE, _GPIO_PINMASK_13,
// _GPIO_CFG_MODE_OUTPUT | _GPIO_CFG_SPEED_MAX);
GPIO_Config(&GPIOD_BASE, _GPIO_PINMASK_0 | _GPIO_PINMASK_1 | _GPIO_PINMASK_2,
_GPIO_CFG_MODE_INPUT | _GPIO_CFG_PULL_DOWN);
//DCMI interfejs, D0 - D7
GPIO_Config(&GPIOA_BASE, _GPIO_PINMASK_4 | _GPIO_PINMASK_5 | _GPIO_PINMASK_6 |
_GPIO_PINMASK_7,
_GPIO_CFG_MODE_INPUT | _GPIO_CFG_PULL_DOWN);
GPIO_Config(&GPIOC_BASE, _GPIO_PINMASK_0 | _GPIO_PINMASK_1 | _GPIO_PINMASK_2 |
_GPIO_PINMASK_3,
_GPIO_CFG_MODE_INPUT | _GPIO_CFG_PULL_DOWN);
//Kamera, PCLK, VSYNC, HREF
//GPIO_Digital_Input(&GPIOD_BASE, _GPIO_PINMASK_0);
//GPIO_Digital_Input(&GPIOD_BASE, _GPIO_PINMASK_1);
//GPIO_Digital_Input(&GPIOD_BASE, _GPIO_PINMASK_2);
//GPIO na ploci, LED i tasteri
GPIO_Config(&GPIOA_BASE, _GPIO_PINMASK_10, _GPIO_CFG_MODE_INPUT |
_GPIO_CFG_PULL_DOWN);
GPIO_Config(&GPIOE_BASE, _GPIO_PINMASK_0, _GPIO_CFG_MODE_INPUT |
_GPIO_CFG_PULL_DOWN);
GPIO_Config(&GPIOE_BASE, _GPIO_PINMASK_12 | _GPIO_PINMASK_15,
_GPIO_CFG_MODE_OUTPUT | _GPIO_CFG_SPEED_MAX);
ENBL = 0;
//CROP = 0;
CAP_OK = 0;
EVEN = 0;
line_cnt = 0;
pixel_cnt = 0;
sample_cnt = 0;
RST = 0; //Resetuj kameru
PWDN = 0;
//Inicijalizuj prekidne rutine
Init_Interrupt();
//Koristi PWM modul za zadavanje clock-a kameri, frekvencija 12MHz
clock = PWM_TIM4_Init(10000000);
PWM_TIM4_Set_Duty(clock/2, _PWM_NON_INVERTED, _PWM_CHANNEL2);
PWM_TIM4_Start(_PWM_CHANNEL2, &_GPIO_MODULE_TIM4_CH2_PD13);

RST = 1;

//Beskonacna petlja
while(1) {
/* XCLK = 1;
//asm nop;
buffer = GPIOC_IDR;
asm nop;
XCLK = 0;
asm nop;
asm nop;
asm nop;
XCLK = 1;
//asm nop;
if (HREF) {
image[i] = (GPIOC_IDR & 0b00001111) | (GPIOA_IDR & 0b11110000);
}
asm nop;
XCLK = 0;
asm nop;
asm nop;
asm nop; */
receive = HID_read();
if (receive != 0) {
for(cnt=0; cnt < USB_BUFF; cnt++) {
receivebuff[cnt] = readbuff[cnt];
}
}
if (receivebuff[0] == 97) {
LED_R = !LED_R;
//HID_Write(&writebuff,USB_BUFF);
}
else if (receivebuff[0] == 98) {
receivebuff[0] = 0;
CAP_OK = 1;
while (CAP_OK) {
;
//LED_R = CAP_OK;
}
for (i = 0; i < LEN_IM/USB_BUFF; i++) {
for (cnt=0; cnt < USB_BUFF; cnt++) {
writebuff[cnt] = image[i*USB_BUFF + cnt];
}
HID_Write(&writebuff,USB_BUFF);
}
/* for (i = 0; i < 2*NUM_WORD/USB_BUFF; i++) {
for (cnt=0; cnt < USB_BUFF; cnt++) {
if (cnt % 2 == 0) {
long_buffer = image[i*USB_BUFF/2 + cnt/2];
buffer = Hi(long_buffer);
}
else {
long_buffer = image[i*USB_BUFF/2 + cnt/2];
buffer = Highest(long_buffer);
}
writebuff[cnt] = buffer;
}
HID_Write(&writebuff,USB_BUFF);
} */
}
}
}

You might also like