Code for OV7670 camera with AL422 FIFO buffer

Dependencies:   BufferedSerial mbed OV7670

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //
00002 // OV7670 + FIFO AL422B camera application
00003 // Author: Martin Kráčala
00004 // Inpired by: Edoardo De Marchi, Martin Smith
00005 //
00006 #include "mbed.h"
00007 #include "BufferedSerial.h"
00008 #include "ov7670.h"
00009 
00010 #define BAUDRATE    (921600)    // supported baudrates: 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
00011 #define BUFFER_LEN  (32)
00012 #define DELAY_MS    (100)
00013 #define RMASK1      (0x0F00)
00014 #define RMASK2      (0x000F)
00015 #define ROFFSET1    (4)
00016 #define ROFFSET2    (0)
00017 #define LED_ON      (0)
00018 #define LED_OFF     (1)
00019 
00020 #define VGA         (640*480)
00021 #define QVGA        (320*240)
00022 #define QQVGA       (160*120)
00023 
00024 #define RAW         'b'
00025 #define RGB         'r'
00026 #define YUV         'y'
00027 
00028 
00029 // OV7670 also supports CIF, QCIF and QQCIF formats
00030 
00031 DigitalOut ledR(LED1,LED_OFF);
00032 DigitalOut ledG(LED2,LED_ON);
00033 DigitalOut ledB(LED3,LED_OFF);
00034 
00035 BufferedSerial pc(USBTX, USBRX);        // PTA2,PTA1
00036 
00037 OV7670 camera (
00038     PTC9,PTC8,                  // SDA,SCL(I2C)
00039     PTA13,NC,PTE2,              // VSYNC,HREF,WEN(FIFO)
00040     PortB,0x00000F0F,           // PortIn data PTB<0-3>,PTB<8-11>
00041     PTE3,PTE4,PTE5              // RRST,OE,RCLK
00042 );
00043 
00044 static char colorscheme = ' ';
00045 static int resolution = 0;
00046 char buffer_in[BUFFER_LEN];
00047 
00048 void setup(char color, int res);
00049 void cmd();
00050 
00051 int main()
00052 {
00053     // set high baud rate
00054     pc.baud(BAUDRATE);                     
00055     
00056     // send hello message via Serial-USB
00057     pc.printf("Starting FRDM-KL25Z...\r\n");
00058     
00059     // reset camera on power up
00060     camera.Reset() ;
00061 
00062     while (true)
00063     {
00064         // Look if things are in the Rx-buffer...
00065         if(pc.readable())
00066         {
00067             int i = 0;
00068             // if so, load them into buffer_in
00069             while(pc.readable())
00070             {
00071                 buffer_in[i++] = pc.getc();
00072             }
00073             // compare buffer_in with defined commands, execute
00074             cmd();
00075         }
00076         ledG = LED_OFF;
00077         wait_ms(DELAY_MS);
00078         ledG = LED_ON;
00079     }
00080 }
00081 
00082 // Camera setting setup
00083 void setup(char color, int res)
00084 {
00085     if(camera.Init(color, res) != 1) {
00086         pc.printf("Setup failed!\r\n");
00087     } else {
00088         pc.printf("Setup successful\r\n");
00089     }
00090 }
00091 
00092 // Parse command from buffer_in and execute function
00093 void cmd()
00094 {
00095     // Read all camera registers - commandline use only (for verification) 
00096     if(strcmp("reg_status\r\n", buffer_in) == 0) {
00097         int i = 0;
00098         pc.printf("AD: +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F");
00099         for (i=0; i<OV7670_REGMAX; i++) {
00100             int data;
00101             data = camera.ReadReg(i);
00102             if ((i & 0x0F) == 0) {
00103                 pc.printf("\r\n%02X: ",i);
00104             }
00105             pc.printf("%02X ",data);
00106         }
00107         pc.printf("\r\n");
00108     }
00109     // Take a picture
00110     else if(strcmp("snapshot\r\n", buffer_in) == 0) {
00111         ledR = LED_ON;
00112         // Kick things off by capturing an image
00113         camera.CaptureNext();
00114         while(camera.CaptureDone() == false);
00115         // Start reading in the image data from the camera hardware buffer
00116         camera.ReadStart();
00117         ledG = LED_OFF;
00118 
00119         for(int x = 0; x<resolution; x++) {
00120             // Read in the first half of the image
00121             if(colorscheme == RAW && resolution != VGA) {
00122                 camera.ReadOnebyte(RMASK1,ROFFSET1,RMASK2,ROFFSET2);
00123             } else if(colorscheme == YUV || colorscheme == RGB) {
00124                 pc.putc(camera.ReadOnebyte(RMASK1,ROFFSET1,RMASK2,ROFFSET2));
00125             }
00126             // Read in the Second half of the image
00127             pc.putc(camera.ReadOnebyte(RMASK1,ROFFSET1,RMASK2,ROFFSET2));      // Y only
00128         }
00129 
00130         camera.ReadStop();
00131         ledG = LED_ON;
00132 
00133         camera.CaptureNext();
00134         while(camera.CaptureDone() == false);
00135 
00136         pc.printf("Snap_done\r\n");
00137         ledR = LED_OFF;
00138     }
00139     // Set up commands...
00140     else if(strcmp("setup_RAW_VGA\r\n", buffer_in) == 0) {
00141         // VGA (640*480) RAW
00142         colorscheme = RAW;
00143         resolution = VGA;
00144         setup(colorscheme,resolution);
00145     }
00146     else if(strcmp("setup_YUV_QVGA\r\n", buffer_in) == 0)
00147     {
00148         // QVGA (320*240) YUV 4:2:2   
00149         colorscheme = YUV;
00150         resolution = QVGA;
00151         setup(RAW,resolution);
00152     }
00153     else if(strcmp("setup_RGB_QVGA\r\n", buffer_in) == 0)
00154     {
00155         // QVGA (320*240) RGB565   
00156         colorscheme = RGB;
00157         resolution = QVGA;
00158         setup(colorscheme,resolution);
00159     }
00160     else if(strcmp("setup_RAW_QVGA\r\n", buffer_in) == 0)    
00161     {
00162         // QVGA (320*240) YUV (Only Y) - monochrome    
00163         colorscheme = RAW;
00164         resolution = QVGA;
00165         setup(colorscheme,resolution);
00166     }
00167     else if(strcmp("setup_YUV_QQVGA\r\n", buffer_in) == 0)                 
00168     {
00169         // QQVGA (160*120) YUV 4:2:2
00170         colorscheme = YUV;
00171         resolution = QQVGA;
00172         setup(RAW,resolution);
00173     }
00174     else if(strcmp("setup_RGB_QQVGA\r\n", buffer_in) == 0)                 
00175     {
00176         // QQVGA (160*120) RGB565
00177         colorscheme = RGB;
00178         resolution = QQVGA;
00179         setup(colorscheme,resolution);
00180     }
00181     else if(strcmp("setup_RAW_QQVGA\r\n", buffer_in) == 0) {
00182         // QQVGA (160*120) YUV (Only Y) - monochrome
00183         colorscheme = RAW;
00184         resolution = QQVGA;
00185         setup(colorscheme,resolution);
00186     }
00187     memset(buffer_in, 0, sizeof(buffer_in));
00188 }