OV7670 + 23LC1024 + Bluetooth
Dependencies: FastPWM MODSERIAL mbed
OV7670.cpp
00001 #include "OV7670.h" 00002 00003 OV7670::OV7670( 00004 PinName sda, // Camera I2C port 00005 PinName scl, // Camera I2C port 00006 PinName xclk,// Camera XCLK 00007 PinName pclkPin,// PCLK 00008 PinName vs, // VSYNC 00009 PinName hr, // HREF 00010 00011 PortName port, // 8bit bus port 00012 int mask // 0000 0111 4000 0111 1000 0000 0000 0000 = 0x07878000 00013 00014 ) : camera(sda,scl), data(port, mask), inPort(Port2, 0x0000001C), spi(p5, p6, p7), ram(spi, p8) 00015 { 00016 camera.stop(); 00017 camera.frequency(OV7670_I2CFREQ); 00018 xclkPin = xclk; 00019 spi.frequency(16000000); 00020 size = 0; 00021 } 00022 00023 void OV7670::Init() { 00024 FastPWM clkOut(xclkPin); 00025 double pclk_period_us = (1.0 / 16000000.0)*1000000; 00026 clkOut.period_us(pclk_period_us); 00027 clkOut.pulsewidth_us(pclk_period_us/2.0); 00028 wait_ms(50); 00029 } 00030 00031 void OV7670::WriteReg(int addr, int data) { 00032 // WRITE 0x42, ADDR, DATA 00033 camera.start(); 00034 camera.write(OV7670_WRITE); 00035 wait_us(OV7670_WRITEWAIT); 00036 camera.write(addr); 00037 wait_us(OV7670_WRITEWAIT); 00038 camera.write(data); 00039 camera.stop(); 00040 } 00041 00042 int OV7670::ReadReg(int addr) { 00043 int data; 00044 00045 // WRITE 0x42,ADDR 00046 camera.start(); 00047 camera.write(OV7670_WRITE); 00048 wait_us(OV7670_WRITEWAIT); 00049 camera.write(addr); 00050 camera.stop(); 00051 wait_us(OV7670_WRITEWAIT); 00052 00053 // WRITE 0x43,READ 00054 camera.start(); 00055 camera.write(OV7670_READ); 00056 wait_us(OV7670_WRITEWAIT); 00057 data = camera.read(OV7670_NOACK); 00058 camera.stop(); 00059 00060 return data; 00061 } 00062 00063 void OV7670::Reset(void) { 00064 WriteReg(0x12,0x80) ; // RESET CAMERA 00065 wait_ms(200) ; 00066 } 00067 00068 void OV7670::InitQQVGA() { 00069 // QQVGA RGB444 00070 WriteReg(REG_CLKRC,0x03); 00071 WriteReg(REG_COM11,0x0A) ; 00072 WriteReg(REG_TSLB,0x04); 00073 WriteReg(REG_COM7,0x04) ; 00074 00075 WriteReg(REG_RGB444, 0x00); // Disable RGB 444? 00076 WriteReg(REG_COM15, 0xD0); // Set RGB 565? 00077 00078 WriteReg(REG_HSTART,0x16) ; 00079 WriteReg(REG_HSTOP,0x04) ; 00080 WriteReg(REG_HREF,0x24) ; 00081 WriteReg(REG_VSTART,0x02) ; 00082 WriteReg(REG_VSTOP,0x7a) ; 00083 WriteReg(REG_VREF,0x0a) ; 00084 WriteReg(REG_COM10,0x02) ; 00085 WriteReg(REG_COM3, 0x04); 00086 WriteReg(REG_COM14, 0x1a); 00087 WriteReg(REG_MVFP,0x27) ; 00088 WriteReg(0x72, 0x22); 00089 WriteReg(0x73, 0xf2); 00090 00091 // COLOR SETTING 00092 WriteReg(0x4f,0x80); 00093 WriteReg(0x50,0x80); 00094 WriteReg(0x51,0x00); 00095 WriteReg(0x52,0x22); 00096 WriteReg(0x53,0x5e); 00097 WriteReg(0x54,0x80); 00098 WriteReg(0x56,0x40); 00099 WriteReg(0x58,0x9e); 00100 WriteReg(0x59,0x88); 00101 WriteReg(0x5a,0x88); 00102 WriteReg(0x5b,0x44); 00103 WriteReg(0x5c,0x67); 00104 WriteReg(0x5d,0x49); 00105 WriteReg(0x5e,0x0e); 00106 WriteReg(0x69,0x00); 00107 WriteReg(0x6a,0x40); 00108 WriteReg(0x6b,0x0a); 00109 WriteReg(0x6c,0x0a); 00110 WriteReg(0x6d,0x55); 00111 WriteReg(0x6e,0x11); 00112 WriteReg(0x6f,0x9f); 00113 00114 WriteReg(0xb0,0x84); 00115 size = 160 * 120 * 2; 00116 } 00117 00118 void OV7670::scopeTest(Serial *host) { 00119 Timer t1, t2; 00120 int LED = 0x80000000; 00121 PortInOut status(Port1, LED); 00122 status = LED; 00123 int cycles = 1000000; 00124 while(inPort & 0x00000008) {}; 00125 while(!(inPort & 0x00000008)) {}; 00126 t1.start(); 00127 while(cycles--) { 00128 while(!(inPort & 0x00000010)) {}; 00129 while(inPort & 0x00000010) {}; 00130 } 00131 status = 0; 00132 t1.stop(); 00133 printf("One million cycles: %d\n\r", t1.read_us()); 00134 //printf("Avg on, off = %d, %d\n\r", t1.read_us()/100, t2.read_us()/100); 00135 } 00136 00137 int OV7670::captureImage() { 00138 int d, i=0; 00139 int byte = 0; 00140 Timer t1; 00141 // Prepare SRAM output 00142 ram.startWriteSequence(); 00143 // Read in the first half of the image 00144 while(inPort & 0x00000008) {}; // Wait for VSYNC low 00145 while(!(inPort & 0x00000008)) {}; // Wait for VSYNC high 00146 //t1.start(); 00147 while(inPort & 0x00000008) { // While VSYNC high 00148 while((inPort & 0x00000008) && !(inPort & 0x00000004)) {}; // stall while VSYNC high and HREF low 00149 if(!(inPort & 0x00000008)) { // if VSYNC low 00150 //printf("Interrupted!\n\r"); 00151 break; 00152 } 00153 while (inPort & 0x00000004) { // While HREF high 00154 while(!(inPort & 0x00000010)) {}; // Wait for PCLK high 00155 d = data; 00156 byte = (d & 0x07800000) >> 19; // bit26 to bit7 00157 byte |= (d & 0x00078000) >> 15; // bit18 to bit3 00158 //arr1[i] = byte; 00159 ram.writeSequence1(byte); 00160 while(inPort & 0x00000010) {}; // Wait for PCLK low 00161 while(!(inPort & 0x00000010)) {}; // Wait for PCLK high 00162 d = data; 00163 byte = (d & 0x07800000) >> 19; // bit26 to bit7 00164 byte |= (d & 0x00078000) >> 15; // bit18 to bit3 00165 ram.writeSequence1(byte); 00166 //arr2[i++] = byte; 00167 while(inPort & 0x00000010) {}; 00168 i++; 00169 } 00170 } 00171 //t1.stop(); 00172 ram.stopSequence(); 00173 //printf("Image read: %d\n\r", t1.read_us()); 00174 return i*2; 00175 } 00176 00177 00178 void OV7670::sendImage(Serial *dest, int numBytes) { 00179 Timer t; 00180 Serial host = *dest; 00181 // Write the image to the serial host 00182 ram.startReadSequence(); 00183 t.start(); 00184 for (int i = 0; i < numBytes; i++) { 00185 while(!(host.writeable())) {} 00186 host.putc(ram.readSequence1()); 00187 } 00188 t.stop(); 00189 printf("BT time: %d\n\r", t.read_us()); 00190 ram.stopSequence(); 00191 } 00192
Generated on Sun Jul 31 2022 14:40:01 by 1.7.2