First release.
Sample code of section 4 in Oct 2014 issue of the Interface Magazine, published by CQ publishing in Japan. CQ出版社インターフェース誌 2014年10月号4章に掲載のサンプルコードです.
LPC1768にトラ技OV7670モジュールを接続し,撮影した画像をBMPファイルとしてフラッシュメモリに保存します(PCから見ることができます).このコードのうちカメラ制御部には,Sadaei Osakabe氏のコードを流用させていただいています.
Diff: main.cpp
- Revision:
- 0:33db72b46e9b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Aug 26 16:47:47 2014 +0000 @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2014, Sumio Morioka + * All rights reserved. + * + * This source code was originally written by Dr.Sumio Morioka for use in the Oct 2014 issue of + * "the Interface magazine", published by CQ publishing Co.Ltd in Japan (http://www.cqpub.co.jp). + * The author has no responsibility on any results caused by using this code. + * + * - Distribution date of this code: Aug 26, 2014 + * - Author: Dr.Sumio Morioka (http://www002.upp.so-net.ne.jp/morioka) + * + * + * IMPORTANT NOTICE: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mbed.h" +#include "TextLCD.h" +#include "LocalFileSystem.h" + +#include "ov7670.h" + +TextLCD lcd(p24, p26, p27, p28, p29, p30); +DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); +PwmOut sv0(p21), sv1(p22), sv2(p23); + +LocalFileSystem local("webfs"); + +OV7670 camera( + p9,p10, // SDA,SCL(I2C / SCCB) + p5,p6,p7, // VSYNC,HREF,WEN(FIFO) + p20,p19,p18,p17,p16,p15,p14,p13, // D7-D0 + p8,p11,p12); // RRST,OE,RCLK + +Timer tmr; + +//#define QQVGA +#define QVGA +//#define VGA34 + +#ifdef QQVGA +#define SIZEX 160 +#define SIZEY 120 +#endif + +#ifdef QVGA +#define SIZEX 320 +#define SIZEY 240 +#endif + +#ifdef VGA34 +#define SIZEX 480 +#define SIZEY 360 +#endif + + +void cam_cap(void); + +int main() +{ + led1 = 0; + led2 = 0; + led3 = 0; + led4 = 0; + + sv0.period_us(20000); // 20ms + sv0.pulsewidth_us(5000); // 5ms + + sv1.period_us(20000); // 20ms + sv1.pulsewidth_us(10000); // 10ms + + sv2.period_us(20000); // 20ms + sv2.pulsewidth_us(15000); // 15ms + + //////////////////////////////////////////////////////////////////////////// + camera.WriteReg(0x12, 0x80); // com7; reset + wait_ms(200); + + camera.InitDefaultReg(); + + // negate vsync + camera.WriteReg(0x15, 0x02); // com10; negative vsync + +#ifdef QQVGA + camera.InitQQVGA(); +#endif +#ifdef QVGA + camera.InitQVGA(); +#endif +#ifdef VGA34 + camera.InitVGA_3_4(); +#endif + + // data format + camera.WriteReg(0x12, 0x04 + 0); // com7 RGB (bit1...test pattern) + camera.WriteReg(0x40, 0xD0); // com15 RGB565 + camera.WriteReg(0x8c, 0x00); // RGB444 + + wait_ms(300); + + lcd.locate(0, 0); + lcd.printf("Capture"); + tmr.start(); // timer + + cam_cap(); + + tmr.stop(); // timer + lcd.locate(0, 1); + lcd.printf("%dms", tmr.read_ms()); + + ////////////////////////////////////////////////// + while (1) { + led1 = 1; +// wait_ms(100); + } + +} + + +//void cam_capture(char *input, char *output) // compile error +//void cam_cap(Arguments* input, Reply* output) +void cam_cap(void) +{ + FILE *fp_bmp; + unsigned int d1, d2; + unsigned char sort[3]; + + led2 = 1; + + fp_bmp = fopen("/webfs/cam.bmp", "wb"); + + ///////////////////////// + // file header + ///////////////////////// + fprintf(fp_bmp, "BM"); + int val = 14 + 40 + SIZEX * SIZEY * 3; // file size + fprintf(fp_bmp, "%c%c%c%c", val % 0x100, val / 0x100, val / 0x10000, val / 0x1000000); + fprintf(fp_bmp, "%c%c%c%c%c%c%c%c", 0, 0, 0, 0, 0x36, 0, 0, 0); + + ///////////////////////// + // information header + ///////////////////////// + fprintf(fp_bmp, "%c%c%c%c", 0x28, 0, 0, 0); // header size + fprintf(fp_bmp, "%c%c%c%c", SIZEX % 0x100, SIZEX / 0x100, SIZEX / 0x10000, SIZEX / 0x1000000); + fprintf(fp_bmp, "%c%c%c%c", SIZEY % 0x100, SIZEY / 0x100, SIZEY / 0x10000, SIZEY / 0x1000000); + fprintf(fp_bmp, "%c%c", 1, 0); // # of plane + fprintf(fp_bmp, "%c%c", 24, 0); // bit count + fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0); // compression + val = SIZEX * SIZEY * 3; // data size + fprintf(fp_bmp, "%c%c%c%c", val % 0x100, val / 0x100, val / 0x10000, val / 0x1000000); + fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0); + fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0); + fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0); + fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0); + + camera.CaptureNext(); // sample start! + + while(camera.CaptureDone() == false) + ; + + camera.ReadStart(); // reset pointer + + led3 = 1; + + for (int y = 0;y < SIZEY;y++) { + for (int x = 0;x < SIZEX;x++) { + d1 = camera.ReadOneByte() ; // upper nibble is XXX , lower nibble is B + d2 = camera.ReadOneByte() ; // upper nibble is G , lower nibble is R + + // RGB565 + sort[0] = ((d1 & 0xF8) >> 3) << 3; // R + sort[1] = ( ((d1 & 0x07) << 3) + ((d2 & 0xE0) >> 5) ) << 2; // G + sort[2] = (d2 & 0x1F) << 3; // B + + fprintf(fp_bmp, "%c%c%c", sort[2], sort[1], sort[0]); // B,G,R + } + } + + camera.ReadStop(); + fclose(fp_bmp); + + led2 = 0; + led3 = 0; +// led4 = 0; +} + +// end of file