Test Code for OV7670 Camera module with FIFO AL422

Dependencies:   MODSERIAL mbed ov7670

Dependents:   OV7670_Test_Code

You can find more information in this page: https://mbed.org/users/edodm85/notebook/ov7670-camera-module/

Committer:
edodm85
Date:
Sat Feb 15 15:01:55 2014 +0000
Revision:
4:d0d4760d32b2
Parent:
3:b4e0cefc37f6
Updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edodm85 0:19429e334b75 1 /*
edodm85 0:19429e334b75 2 * Author: Edoardo De Marchi
edodm85 3:b4e0cefc37f6 3 * Date: 07/04/13
edodm85 0:19429e334b75 4 * Notes: OV7670 + FIFO AL422B camera test
edodm85 0:19429e334b75 5 */
edodm85 0:19429e334b75 6
edodm85 0:19429e334b75 7 #include "main.h"
edodm85 0:19429e334b75 8
edodm85 2:bbd557817319 9 #define VGA 307200 //640*480
edodm85 2:bbd557817319 10 #define QVGA 76800 //320*240
edodm85 2:bbd557817319 11 #define QQVGA 19200 //160*120
edodm85 2:bbd557817319 12
edodm85 2:bbd557817319 13 static char format = ' ';
edodm85 2:bbd557817319 14 static int resolution = 0;
edodm85 0:19429e334b75 15
edodm85 0:19429e334b75 16
edodm85 0:19429e334b75 17 void rxCallback(MODSERIAL_IRQ_INFO *q)
edodm85 0:19429e334b75 18 {
edodm85 0:19429e334b75 19 new_send = true;
edodm85 0:19429e334b75 20 }
edodm85 0:19429e334b75 21
edodm85 0:19429e334b75 22 int main()
edodm85 0:19429e334b75 23 {
edodm85 0:19429e334b75 24 pc.baud(921600);
edodm85 0:19429e334b75 25 pc.printf("SystemCoreClock: %dMHz\r\n", SystemCoreClock/1000000); // print the clock frequency
edodm85 0:19429e334b75 26 led4 = 0;
edodm85 0:19429e334b75 27
edodm85 0:19429e334b75 28 t.start();
edodm85 0:19429e334b75 29 pc.attach(&rxCallback, MODSERIAL::RxIrq);
edodm85 0:19429e334b75 30
edodm85 0:19429e334b75 31 while(1)
edodm85 0:19429e334b75 32 {
edodm85 4:d0d4760d32b2 33 if(new_send)
edodm85 4:d0d4760d32b2 34 {
edodm85 0:19429e334b75 35 int i = 0;
edodm85 0:19429e334b75 36
edodm85 0:19429e334b75 37 while(pc.readable())
edodm85 0:19429e334b75 38 {
edodm85 0:19429e334b75 39 word[i] = pc.getc();
edodm85 0:19429e334b75 40 i++;
edodm85 0:19429e334b75 41 }
edodm85 0:19429e334b75 42 parse_cmd();
edodm85 0:19429e334b75 43 }
edodm85 0:19429e334b75 44 wait_ms(50);
edodm85 0:19429e334b75 45 }
edodm85 0:19429e334b75 46 }
edodm85 0:19429e334b75 47
edodm85 0:19429e334b75 48
edodm85 0:19429e334b75 49
edodm85 4:d0d4760d32b2 50 void parse_cmd()
edodm85 4:d0d4760d32b2 51 {
edodm85 0:19429e334b75 52 new_send = false;
edodm85 0:19429e334b75 53
edodm85 2:bbd557817319 54 if(strcmp("snap", word) == 0)
edodm85 0:19429e334b75 55 {
edodm85 2:bbd557817319 56 CameraSnap();
edodm85 0:19429e334b75 57 memset(word, 0, sizeof(word));
edodm85 0:19429e334b75 58 }else
edodm85 3:b4e0cefc37f6 59 if(strcmp("init_bw_VGA", word) == 0) // Set up for 640*480 pixels RAW
edodm85 0:19429e334b75 60 {
edodm85 2:bbd557817319 61 format = 'b';
edodm85 2:bbd557817319 62 resolution = VGA;
edodm85 2:bbd557817319 63 if(camera.Init('b', VGA) != 1)
edodm85 2:bbd557817319 64 {
edodm85 2:bbd557817319 65 pc.printf("Init Fail\r\n");
edodm85 2:bbd557817319 66 }
edodm85 2:bbd557817319 67 pc.printf("Initializing done\r\n");
edodm85 2:bbd557817319 68 memset(word, 0, sizeof(word));
edodm85 2:bbd557817319 69 }else
edodm85 2:bbd557817319 70 if(strcmp("init_yuv_QVGA", word) == 0) // Set up for 320*240 pixels YUV422
edodm85 2:bbd557817319 71 {
edodm85 2:bbd557817319 72 format = 'y';
edodm85 2:bbd557817319 73 resolution = QVGA;
edodm85 2:bbd557817319 74 if(camera.Init('b', QVGA) != 1)
edodm85 2:bbd557817319 75 {
edodm85 2:bbd557817319 76 pc.printf("Init Fail\r\n");
edodm85 2:bbd557817319 77 }
edodm85 2:bbd557817319 78 pc.printf("Initializing done\r\n");
edodm85 0:19429e334b75 79 memset(word, 0, sizeof(word));
edodm85 2:bbd557817319 80 }else
edodm85 2:bbd557817319 81 if(strcmp("init_rgb_QVGA", word) == 0) // Set up for 320*240 pixels RGB565
edodm85 2:bbd557817319 82 {
edodm85 2:bbd557817319 83 format = 'r';
edodm85 2:bbd557817319 84 resolution = QVGA;
edodm85 2:bbd557817319 85 if(camera.Init('r', QVGA) != 1)
edodm85 2:bbd557817319 86 {
edodm85 2:bbd557817319 87 pc.printf("Init Fail\r\n");
edodm85 2:bbd557817319 88 }
edodm85 2:bbd557817319 89 pc.printf("Initializing done\r\n");
edodm85 2:bbd557817319 90 memset(word, 0, sizeof(word));
edodm85 2:bbd557817319 91 }else
edodm85 2:bbd557817319 92 if(strcmp("init_bw_QVGA", word) == 0) // Set up for 320*240 pixels YUV (Only Y)
edodm85 0:19429e334b75 93 {
edodm85 2:bbd557817319 94 format = 'b';
edodm85 2:bbd557817319 95 resolution = QVGA;
edodm85 2:bbd557817319 96 if(camera.Init('b', QVGA) != 1)
edodm85 2:bbd557817319 97 {
edodm85 2:bbd557817319 98 pc.printf("Init Fail\r\n");
edodm85 2:bbd557817319 99 }
edodm85 2:bbd557817319 100 pc.printf("Initializing done\r\n");
edodm85 2:bbd557817319 101 memset(word, 0, sizeof(word));
edodm85 2:bbd557817319 102 }else
edodm85 2:bbd557817319 103 if(strcmp("init_yuv_QQVGA", word) == 0) // Set up for 160*120 pixels YUV422
edodm85 2:bbd557817319 104 {
edodm85 2:bbd557817319 105 format = 'y';
edodm85 2:bbd557817319 106 resolution = QQVGA;
edodm85 2:bbd557817319 107 if(camera.Init('b', QQVGA) != 1)
edodm85 2:bbd557817319 108 {
edodm85 2:bbd557817319 109 pc.printf("Init Fail\r\n");
edodm85 2:bbd557817319 110 }
edodm85 2:bbd557817319 111 pc.printf("Initializing done\r\n");
edodm85 2:bbd557817319 112 memset(word, 0, sizeof(word));
edodm85 2:bbd557817319 113 }else
edodm85 2:bbd557817319 114 if(strcmp("init_rgb_QQVGA", word) == 0) // Set up for 160*120 pixels RGB565
edodm85 2:bbd557817319 115 {
edodm85 2:bbd557817319 116 format = 'r';
edodm85 2:bbd557817319 117 resolution = QQVGA;
edodm85 2:bbd557817319 118 if(camera.Init('r', QQVGA) != 1)
edodm85 0:19429e334b75 119 {
edodm85 0:19429e334b75 120 pc.printf("Init Fail\r\n");
edodm85 0:19429e334b75 121 }
edodm85 0:19429e334b75 122 pc.printf("Initializing done\r\n");
edodm85 0:19429e334b75 123 memset(word, 0, sizeof(word));
edodm85 0:19429e334b75 124 }else
edodm85 2:bbd557817319 125 if(strcmp("init_bw_QQVGA", word) == 0) // Set up for 160*120 pixels YUV (Only Y)
edodm85 2:bbd557817319 126 {
edodm85 2:bbd557817319 127 format = 'b';
edodm85 2:bbd557817319 128 resolution = QQVGA;
edodm85 2:bbd557817319 129 if(camera.Init('b', QQVGA) != 1)
edodm85 0:19429e334b75 130 {
edodm85 0:19429e334b75 131 pc.printf("Init Fail\r\n");
edodm85 0:19429e334b75 132 }
edodm85 0:19429e334b75 133 pc.printf("Initializing done\r\n");
edodm85 0:19429e334b75 134 memset(word, 0, sizeof(word));
edodm85 0:19429e334b75 135 }else
edodm85 0:19429e334b75 136 if(strcmp("reset", word) == 0)
edodm85 0:19429e334b75 137 {
edodm85 0:19429e334b75 138 mbed_reset();
edodm85 0:19429e334b75 139 }else
edodm85 0:19429e334b75 140 if(strcmp("time", word) == 0)
edodm85 0:19429e334b75 141 {
edodm85 2:bbd557817319 142 pc.printf("Tot time acq + send (mbed): %dms\r\n", t2-t1);
edodm85 0:19429e334b75 143 memset(word, 0, sizeof(word));
edodm85 4:d0d4760d32b2 144 }else
edodm85 4:d0d4760d32b2 145 if(strcmp("reg_status", word) == 0)
edodm85 4:d0d4760d32b2 146 {
edodm85 4:d0d4760d32b2 147 int i = 0;
edodm85 4:d0d4760d32b2 148 pc.printf("AD : +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F");
edodm85 4:d0d4760d32b2 149 for (i=0;i<OV7670_REGMAX;i++)
edodm85 4:d0d4760d32b2 150 {
edodm85 4:d0d4760d32b2 151 int data;
edodm85 4:d0d4760d32b2 152 data = camera.ReadReg(i); // READ REG
edodm85 4:d0d4760d32b2 153 if ((i & 0x0F) == 0)
edodm85 4:d0d4760d32b2 154 {
edodm85 4:d0d4760d32b2 155 pc.printf("\r\n%02X : ",i);
edodm85 4:d0d4760d32b2 156 }
edodm85 4:d0d4760d32b2 157 pc.printf("%02X ",data);
edodm85 4:d0d4760d32b2 158 }
edodm85 4:d0d4760d32b2 159 pc.printf("\r\n");
edodm85 2:bbd557817319 160 }
edodm85 2:bbd557817319 161
edodm85 0:19429e334b75 162 memset(word, 0, sizeof(word));
edodm85 0:19429e334b75 163
edodm85 0:19429e334b75 164 }
edodm85 0:19429e334b75 165
edodm85 0:19429e334b75 166
edodm85 4:d0d4760d32b2 167 void CameraSnap()
edodm85 4:d0d4760d32b2 168 {
edodm85 0:19429e334b75 169 led4 = 1;
edodm85 2:bbd557817319 170
edodm85 2:bbd557817319 171 // Kick things off by capturing an image
edodm85 0:19429e334b75 172 camera.CaptureNext();
edodm85 2:bbd557817319 173 while(camera.CaptureDone() == false);
edodm85 0:19429e334b75 174 // Start reading in the image data from the camera hardware buffer
edodm85 0:19429e334b75 175 camera.ReadStart();
edodm85 2:bbd557817319 176 t1 = t.read_ms();
edodm85 0:19429e334b75 177
edodm85 2:bbd557817319 178 for(int x = 0; x<resolution; x++)
edodm85 0:19429e334b75 179 {
edodm85 2:bbd557817319 180 // Read in the first half of the image
edodm85 3:b4e0cefc37f6 181 if(format == 'b' && resolution != VGA)
edodm85 2:bbd557817319 182 {
edodm85 2:bbd557817319 183 camera.ReadOnebyte();
edodm85 2:bbd557817319 184 }else
edodm85 2:bbd557817319 185 if(format == 'y' || format == 'r')
edodm85 2:bbd557817319 186 {
edodm85 2:bbd557817319 187 pc.putc(camera.ReadOnebyte());
edodm85 2:bbd557817319 188 }
edodm85 2:bbd557817319 189 // Read in the Second half of the image
edodm85 2:bbd557817319 190 pc.putc(camera.ReadOnebyte()); // Y only
edodm85 0:19429e334b75 191 }
edodm85 2:bbd557817319 192
edodm85 2:bbd557817319 193 camera.ReadStop();
edodm85 4:d0d4760d32b2 194 t2 = t.read_ms();
edodm85 0:19429e334b75 195
edodm85 4:d0d4760d32b2 196 camera.CaptureNext();
edodm85 4:d0d4760d32b2 197 while(camera.CaptureDone() == false);
edodm85 0:19429e334b75 198
edodm85 2:bbd557817319 199 pc.printf("Snap_done\r\n");
edodm85 0:19429e334b75 200 led4 = 0;
edodm85 2:bbd557817319 201 }