Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSerial motor_sn7544
ov7670.h
00001 #include "mbed.h" 00002 #include "ov7670reg.h" 00003 00004 #define OV7670_WRITE (0x42) 00005 #define OV7670_READ (0x43) 00006 #define OV7670_WRITEWAIT (20) 00007 #define OV7670_NOACK (0) 00008 #define OV7670_REGMAX (201) 00009 #define OV7670_I2CFREQ (10000) 00010 00011 // 00012 // OV7670 + FIFO AL422B camera board test 00013 //wen =1일 때 read시작 wen = 0일때 안읽음 00014 class OV7670 00015 { 00016 public: 00017 I2C _i2c ; 00018 InterruptIn vsync,href; 00019 DigitalOut wen ; 00020 PortIn data ; 00021 DigitalOut rrst,oe,rclk,wrst ; 00022 volatile int LineCounter ; 00023 volatile int LastLines ; 00024 volatile bool CaptureReq ; 00025 volatile bool Busy ; 00026 volatile bool Done ; 00027 00028 OV7670( 00029 PinName sda,// Camera I2C port 00030 PinName scl,// Camera I2C port 00031 PinName vs, // VSYNC 00032 PinName hr, // HREF 00033 PinName we, // WEN 00034 PortName port, // 8bit bus port 00035 int mask, // 0b0000_0M65_4000_0321_L000_0000_0000_0000 = 0x07878000 00036 PinName rt, // /RRST 00037 PinName o, // /OE 00038 PinName rc, 00039 PinName wt // RCLK 00040 ) : _i2c(sda,scl),vsync(vs),href(hr),wen(we),data(port,mask),rrst(rt),oe(o),rclk(rc),wrst(wt) 00041 { 00042 _i2c.stop() ; 00043 _i2c.frequency(OV7670_I2CFREQ) ; 00044 vsync.fall(this,&OV7670::VsyncHandler) ; 00045 // vsync.rise(this,&OV7670::VsyncHandler) ; 00046 CaptureReq = false ; 00047 Busy = false ; 00048 Done = false ; 00049 LineCounter = 0 ; 00050 rrst = 1 ; 00051 00052 oe = 1 ; 00053 rclk = 1 ; 00054 wen = 0 ; 00055 } 00056 00057 // capture request 00058 void CaptureNext(void) 00059 { 00060 CaptureReq = true ; 00061 Busy = true ; 00062 } 00063 00064 // capture done? (with clear) 00065 bool CaptureDone(void) 00066 { 00067 bool result ; 00068 if (Busy) { 00069 result = false ; 00070 } else { 00071 result = Done ; 00072 Done = false ; 00073 } 00074 return result ; 00075 } 00076 00077 // write to camera 00078 void WriteReg(int addr,int data) 00079 { 00080 wait_us(10); 00081 int status; 00082 char data_write[2]; 00083 data_write[0]= addr; 00084 data_write[1]= data; 00085 status = _i2c.write(OV7670_WRITE,data_write,2,0); 00086 if(status!=0) { 00087 printf("I2C configuration error(adr: %x)!\r\n",addr); 00088 while(1) { 00089 } 00090 } 00091 } 00092 00093 // read from camera 00094 int ReadReg(int addr) 00095 { 00096 char data_write[2]; 00097 char data_read[2]; 00098 int data; 00099 int status; 00100 00101 data_write[0]=addr; 00102 _i2c.write(OV7670_WRITE, data_write, 1, 0); 00103 status =_i2c.read(OV7670_READ, data_read, 1, 0); 00104 data = (int)data_read[0]; 00105 // printf("Data is %x\r\n",data); 00106 // printf("Status is %d\r\n",status); 00107 return data ; 00108 } 00109 00110 void Reset(void) 00111 { 00112 00113 WriteReg(0x12,0x80) ; // RESET CAMERA 00114 wait_ms(200) ; 00115 wait_us(1) ; 00116 00117 } 00118 00119 void InitQQVGA565(bool flipv,bool fliph) 00120 { 00121 // QQVGA RGB565 00122 WriteReg(REG_CLKRC,0x80); 00123 WriteReg(REG_COM11,0x0A) ; 00124 WriteReg(REG_TSLB,0x04); 00125 WriteReg(REG_COM7,0x04) ; 00126 WriteReg(REG_RGB444, 0x00); 00127 WriteReg(REG_COM15, 0xd0); 00128 WriteReg(REG_HSTART,0x16) ; 00129 WriteReg(REG_HSTOP,0x04) ; 00130 WriteReg(REG_HREF,0x24) ; 00131 WriteReg(REG_VSTART,0x02) ; 00132 WriteReg(REG_VSTOP,0x7a) ; 00133 WriteReg(REG_VREF,0x0a) ; 00134 WriteReg(REG_COM10,0x02) ; 00135 WriteReg(REG_COM3, 0x04); 00136 WriteReg(REG_COM14, 0x1a); 00137 WriteReg(REG_MVFP,0x07 | (flipv ? 0x10:0) | (fliph ? 0x20:0)) ; 00138 WriteReg(0x72, 0x22); 00139 WriteReg(0x73, 0xf2); 00140 00141 // COLOR SETTING 00142 WriteReg(0x4f,0x80); 00143 WriteReg(0x50,0x80); 00144 WriteReg(0x51,0x00); 00145 WriteReg(0x52,0x22); 00146 WriteReg(0x53,0x5e); 00147 WriteReg(0x54,0x80); 00148 WriteReg(0x56,0x40); 00149 WriteReg(0x58,0x9e); 00150 WriteReg(0x59,0x88); 00151 WriteReg(0x5a,0x88); 00152 WriteReg(0x5b,0x44); 00153 WriteReg(0x5c,0x67); 00154 WriteReg(0x5d,0x49); 00155 WriteReg(0x5e,0x0e); 00156 WriteReg(0x69,0x00); 00157 WriteReg(0x6a,0x40); 00158 WriteReg(0x6b,0x0a); 00159 WriteReg(0x6c,0x0a); 00160 WriteReg(0x6d,0x55); 00161 WriteReg(0x6e,0x11); 00162 WriteReg(0x6f,0x9f); 00163 00164 WriteReg(0xb0,0x84); 00165 } 00166 00167 void InitQVGA565(bool flipv,bool fliph) 00168 { 00169 // QVGA RGB565 00170 WriteReg(REG_CLKRC,0x80); 00171 WriteReg(REG_COM11,0x0A) ; 00172 WriteReg(REG_TSLB,0x04); 00173 WriteReg(REG_COM7,0x04) ; 00174 WriteReg(REG_RGB444, 0x00); 00175 WriteReg(REG_COM15, 0xd0); 00176 WriteReg(REG_HSTART,0x16) ; 00177 WriteReg(REG_HSTOP,0x04) ; 00178 WriteReg(REG_HREF,0x80) ; 00179 WriteReg(REG_VSTART,0x02) ; 00180 WriteReg(REG_VSTOP,0x7a) ; 00181 WriteReg(REG_VREF,0x0a) ; 00182 WriteReg(REG_COM10,0x02) ; 00183 WriteReg(REG_COM3, 0x04); 00184 WriteReg(REG_COM14, 0x19); 00185 WriteReg(REG_MVFP,0x07 | (flipv ? 0x10:0) | (fliph ? 0x20:0)) ; 00186 WriteReg(0x72, 0x11); 00187 WriteReg(0x73, 0xf1); 00188 00189 // COLOR SETTING 00190 WriteReg(0x4f,0x80); 00191 WriteReg(0x50,0x80); 00192 WriteReg(0x51,0x00); 00193 WriteReg(0x52,0x22); 00194 WriteReg(0x53,0x5e); 00195 WriteReg(0x54,0x80); 00196 WriteReg(0x56,0x40); 00197 WriteReg(0x58,0x9e); 00198 WriteReg(0x59,0x88); 00199 WriteReg(0x5a,0x88); 00200 WriteReg(0x5b,0x44); 00201 WriteReg(0x5c,0x67); 00202 WriteReg(0x5d,0x49); 00203 WriteReg(0x5e,0x0e); 00204 WriteReg(0x69,0x00); 00205 WriteReg(0x6a,0x40); 00206 WriteReg(0x6b,0x0a); 00207 WriteReg(0x6c,0x0a); 00208 WriteReg(0x6d,0x55); 00209 WriteReg(0x6e,0x11); 00210 WriteReg(0x6f,0x9f); 00211 00212 WriteReg(0xb0,0x84); 00213 } 00214 void InitQQVGAYUV(bool flipv,bool fliph) 00215 { 00216 WriteReg(REG_COM10,0x02) ; 00217 // WriteReg(0x11,0x81); 00218 WriteReg(REG_HSTART,0x16) ; 00219 WriteReg(REG_HSTOP,0x04) ; 00220 WriteReg(REG_HREF,0x24) ; 00221 WriteReg(REG_VSTART,0x02) ; 00222 WriteReg(REG_VSTOP,0x7a) ; 00223 WriteReg(REG_VREF,0x0a) ; 00224 WriteReg(REG_COM14, 0x1a); 00225 WriteReg(0x72, 0x22); 00226 WriteReg(0x73, 0xf2); 00227 WriteReg(0x70,0x3A); 00228 WriteReg(0x71,0x35); 00229 WriteReg(REG_TSLB,0x0C); 00230 WriteReg(0xA2,0x02); 00231 00232 00233 WriteReg(REG_COM7, 0x00); // YUV 00234 WriteReg(REG_COM17, 0x00); // color bar disable 00235 WriteReg(REG_COM3, 0x04); 00236 WriteReg(0x8C, 0x00);//RGB444 00237 WriteReg(0x04, 0x00);//COM1 00238 WriteReg(0x40, 0xC0);//COM15 00239 WriteReg(0x14, 0x1A);//COM9 00240 WriteReg(0x3D, 0x40);//COM13 00241 00242 00243 /* END MARKER */ 00244 } 00245 00246 00247 // vsync handler 00248 void VsyncHandler(void) 00249 { 00250 // Capture Enable 00251 if (CaptureReq) { 00252 wrst=0; 00253 wait_us(1); 00254 wen = 1 ; 00255 wrst=1; 00256 Done = false ; 00257 CaptureReq = false ; 00258 } else { 00259 wen = 0 ; 00260 if (Busy) { //next exit 00261 Busy = false ; 00262 Done = true ; 00263 } 00264 } 00265 00266 // Hline Counter 00267 LastLines = LineCounter ; 00268 LineCounter = 0 ; 00269 } 00270 00271 // href handler 00272 void HrefHandler(void) 00273 { 00274 LineCounter++ ; 00275 } 00276 00277 // Data Read 00278 int ReadOneByte(void) 00279 { 00280 int result ; 00281 rclk = 1 ; 00282 wait_us(1); 00283 result = data.read() ; 00284 rclk = 0 ; 00285 return result ; 00286 } 00287 00288 // Data Read (PortIn) 00289 int ReadOneWord(void) 00290 { 00291 // int r,r1,r2,r3,r4 ; 00292 // rclk = 1 ; 00293 // r = data ; 00294 // rclk = 0 ; 00295 // r1 = r & 0x07800000 ; 00296 // r1 = r1 >> (26-7-0) ; // bit26 to bit7 00297 // r2 = r & 0x00078000 ; 00298 // r2 = r2 >> (18-3-0) ; // bit18 to bit3 00299 // rclk = 1 ; 00300 // r = data ; 00301 // rclk = 0 ; 00302 // r3 = r & 0x07800000 ; 00303 // r3 = r3 >> (26-7-8) ; // bit26 to bit7 00304 // r4 = r & 0x00078000 ; 00305 // r4 = r4 >> (18-3-8) ; // bit18 to bit3 00306 00307 int r,r1,r2; 00308 rclk=1; 00309 wait_us(1); 00310 r = data.read(); 00311 rclk=0; 00312 // wait_us(50); 00313 r1 = r; 00314 wait_us(1); 00315 rclk=1; 00316 wait_us(1); 00317 r= data.read(); 00318 rclk=0; 00319 r2 = r; 00320 00321 // wait_us(50); 00322 return r2 ; 00323 } 00324 00325 // Data Start 00326 void ReadStart(void) 00327 { 00328 rrst=0; 00329 oe = 0 ; 00330 rclk = 0 ; 00331 wait_us(1); 00332 rclk = 1 ; 00333 wait_us(1) ; 00334 rclk = 0 ; 00335 rrst=1; 00336 00337 // wait_us(1); 00338 00339 } 00340 00341 // Data Stop 00342 void ReadStop(void) 00343 { 00344 oe = 1 ; 00345 ReadOneByte() ; 00346 rclk = 1 ; 00347 } 00348 }; 00349
Generated on Wed Jul 13 2022 07:13:54 by
