A program that records images from the OV7670 camera and displays them on a SPI TFT display. Working at 15fps

Dependencies:   mbed TFT_fonts_old

Files at this revision

API Documentation at this revision

Comitter:
ms523
Date:
Sat Mar 31 10:05:59 2012 +0000
Parent:
0:aabbf2286bf2
Commit message:
Working at 15fps for 160*120 RGB565 video

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
ov7670.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Mar 27 19:10:18 2012 +0000
+++ b/main.cpp	Sat Mar 31 10:05:59 2012 +0000
@@ -14,16 +14,20 @@
 OV7670 camera(
     p28,p27,       // SDA,SCL(I2C / SCCB)
     p21,p22,p20,   // VSYNC,HREF,WEN(FIFO)
-    p19,p18,p17,p16,p15,p14,p13,p12, // D7-D0
+    Port0,0x07800033,
     p23,p24,p25) ; // RRST,OE,RCLK
 
 // the TFT is connected to SPI pin 5-7
-SPI_TFT TFT(p5, p6, p7, p8, p9,"TFT"); // mosi, miso, sclk, cs, reset
+SPI_TFT TFT(p5, p6, p7, p8, p14,"TFT"); // mosi, miso, sclk, cs, reset
 
 Serial pc(USBTX,USBRX);
 
 #define SIZEX (160)
 #define SIZEY (120)
+#define SIZE 19200
+
+unsigned char bank0 [SIZE];
+unsigned char *bank1 = (unsigned char *)(0x2007C000);
 
 int main() {
 
@@ -35,72 +39,50 @@
     TFT.set_font((unsigned char*) Arial12x12);  // Select the font
     TFT.set_orientation(3);                     // Select orientation
 
-    printf("Testing...");
-
-    int i ;
-    pc.baud(115200) ;
-    pc.printf("Camera resetting..\r\n") ;
-
+    // Reset camera on power up
     camera.Reset() ;
 
-    pc.printf("Before Init...\r\n") ;
-    pc.printf("AD : +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F") ;
-    for (i=0; i<OV7670_REGMAX; i++) {
-        int data ;
-        data = camera.ReadReg(i) ; // READ REG
-        if ((i & 0x0F) == 0) {
-            pc.printf("\r\n%02X : ",i) ;
-        }
-        pc.printf("%02X ",data) ;
-    }
-    pc.printf("\r\n") ;
-
+    // Set up for 160*120 pixels RGB565
     camera.InitQQVGA() ;
+    
+    // Print message to screen
+    pc.printf("Hit Any Key to stop RGBx160x120 Capture Data.\r\n");
+          
+    // Kick things off by capturing an image
+    camera.CaptureNext() ;
+    while (camera.CaptureDone() == false) ;
 
-    pc.printf("After Init...\r\n") ;
-    pc.printf("AD : +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F") ;
-    for (i=0; i<OV7670_REGMAX; i++) {
-        int data ;
-        data = camera.ReadReg(i) ; // READ REG
-        if ((i & 0x0F) == 0) {
-            pc.printf("\r\n%02X : ",i) ;
+    // Now enter the main loop
+    while (!pc.readable()) {
+
+        // Start reading in the image data from the camera hardware buffer                   
+        camera.ReadStart();
+
+        // Read in the first half of the image
+        for (int i = 0; i < SIZE; i++) {
+            bank0[i] =  camera.ReadOneByte();
         }
-        pc.printf("%02X ",data) ;
-    }
-    pc.printf("\r\n") ;
 
-    // CAPTURE and SEND LOOP
-    while (1) {
-        pc.printf("Hit Any Key to send RGBx160x120 Capture Data.\r\n") ;
-        while (!pc.readable()) ;
-        pc.getc() ;
+        // Read in the second half of the image
+        for (int i = 0; i < SIZE ; i++) {
+            bank1[i] =  camera.ReadOneByte();
+        }  
+        
+        // Stop reading the image
+        camera.ReadStop() ; 
+        
+        // Immediately request the next image to be captured (takes around 45ms)
         camera.CaptureNext() ;
-        while (camera.CaptureDone() == false) ;
-        pc.printf("*\r\n") ;
-        camera.ReadStart() ;
-        i = 0 ;
-        int colour;
         
-        for (int y = 0; y < SIZEY; y++) {
-            int r,g,b,d1,d2 ;
-            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
-                b = (d1 & 0x0F) ;
-                g = (d2 & 0xF0) >> 4 ;
-                r = (d2 & 0x0F) ;
-                pc.printf ("%1X%1X%1X",r,g,b) ;
-    
-                // Calculate colour
-                colour = r << 11;
-                colour += g << 5;
-                colour += b;
-                
-                // Display pixel on TFT screen
-                TFT.pixel(x, y, colour);
-            }
-            pc.printf("\r\n") ;
-        }
-        camera.ReadStop() ;
+        // Use this time to display the image on the screen
+        // Display the top half
+        TFT.Bitmap(0,0,160,60,bank1);
+        // Display the bottom half
+        TFT.Bitmap(0,60,160,60,bank0);
+        
+        /* Note: we still have around 15ms left here to do other stuff */
+        
+        // Now wait for the image to finish being captured
+        while (camera.CaptureDone() == false) ;
     }
 }
--- a/ov7670.h	Tue Mar 27 19:10:18 2012 +0000
+++ b/ov7670.h	Sat Mar 31 10:05:59 2012 +0000
@@ -17,20 +17,23 @@
     I2C camera ;
     InterruptIn vsync,href;
     DigitalOut wen ;
-    BusIn data ;
+    //BusIn data ;
+    PortIn data;
     DigitalOut rrst,oe,rclk ;
     volatile int LineCounter ;
     volatile int LastLines ;
     volatile bool CaptureReq ;
     volatile bool Busy ;
-    volatile bool Done ;
-
+    volatile bool Done ;    
+        
     OV7670(
         PinName sda,// Camera I2C port
         PinName scl,// Camera I2C port
         PinName vs, // VSYNC
         PinName hr, // HREF
         PinName we, // WEN
+        
+        /*
         PinName d7, // D7
         PinName d6, // D6
         PinName d5, // D5
@@ -39,10 +42,15 @@
         PinName d2, // D2
         PinName d1, // D1
         PinName d0, // D0
+        */
+        PortName port, // 8bit bus port
+        int mask, // 0b0000_0M65_4000_0321_L000_0000_0000_0000 = 0x07878000
+        
         PinName rt, // /RRST
         PinName o,  // /OE
         PinName rc  // RCLK      
-        ) : camera(sda,scl),vsync(vs),href(hr),wen(we),data(d0,d1,d2,d3,d4,d5,d6,d7),rrst(rt),oe(o),rclk(rc)
+        //) : camera(sda,scl),vsync(vs),href(hr),wen(we),data(d0,d1,d2,d3,d4,d5,d6,d7),rrst(rt),oe(o),rclk(rc)
+        ) : camera(sda,scl),vsync(vs),href(hr),wen(we),data(port,mask),rrst(rt),oe(o),rclk(rc)
     {
         camera.stop() ;
         camera.frequency(OV7670_I2CFREQ) ;
@@ -125,8 +133,12 @@
         WriteReg(REG_COM11,0x0A) ;
         WriteReg(REG_TSLB,0x04);
         WriteReg(REG_COM7,0x04) ;
-        WriteReg(REG_RGB444, 0x02);
-        WriteReg(REG_COM15, 0xd0);
+        
+        //WriteReg(REG_RGB444, 0x02);
+        //WriteReg(REG_COM15, 0xd0);
+        WriteReg(REG_RGB444, 0x00);     // Disable RGB 444?
+        WriteReg(REG_COM15, 0xD0);      // Set RGB 565?
+        
         WriteReg(REG_HSTART,0x16) ;
         WriteReg(REG_HSTOP,0x04) ;
         WriteReg(REG_HREF,0x24) ;
@@ -136,6 +148,7 @@
         WriteReg(REG_COM10,0x02) ;
         WriteReg(REG_COM3, 0x04);
         WriteReg(REG_COM14, 0x1a);
+        WriteReg(REG_MVFP,0x27) ;
         WriteReg(0x72, 0x22);
         WriteReg(0x73, 0xf2);
 
@@ -197,12 +210,20 @@
     // Data Read
     int ReadOneByte(void)
     {
-        int result ;
-        rclk = 1 ;
-//        wait_us(1) ;
-        result = data ;
-        rclk = 0 ;
-        return result ;
+        int result;
+        rclk = 1;
+        result = data;
+
+        // Shift the bits around to form the byte
+        int top = result >> 19;         // Isolate the top nibble
+        int middle = result >> 2;       // Isolate bits 2 & 3
+        result = result & 0x00000003;   // Isolate bits 0 & 1
+        
+        result += middle;
+        result += top;
+       
+        rclk = 0;
+        return result;
     }
     
     // Data Start