![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ov7670 FIFO data taking program
Dependencies: OV7670 with FIFO ST7735_TFT mbed
Fork of OV7670_Testing by
ov7670.h@0:aabbf2286bf2, 2012-03-27 (annotated)
- Committer:
- ms523
- Date:
- Tue Mar 27 19:10:18 2012 +0000
- Revision:
- 0:aabbf2286bf2
- Child:
- 1:c98598814170
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ms523 | 0:aabbf2286bf2 | 1 | #include "mbed.h" |
ms523 | 0:aabbf2286bf2 | 2 | #include "ov7670reg.h" |
ms523 | 0:aabbf2286bf2 | 3 | |
ms523 | 0:aabbf2286bf2 | 4 | #define OV7670_WRITE (0x42) |
ms523 | 0:aabbf2286bf2 | 5 | #define OV7670_READ (0x43) |
ms523 | 0:aabbf2286bf2 | 6 | #define OV7670_WRITEWAIT (20) |
ms523 | 0:aabbf2286bf2 | 7 | #define OV7670_NOACK (0) |
ms523 | 0:aabbf2286bf2 | 8 | #define OV7670_REGMAX (201) |
ms523 | 0:aabbf2286bf2 | 9 | #define OV7670_I2CFREQ (50000) |
ms523 | 0:aabbf2286bf2 | 10 | |
ms523 | 0:aabbf2286bf2 | 11 | // |
ms523 | 0:aabbf2286bf2 | 12 | // OV7670 + FIFO AL422B camera board test |
ms523 | 0:aabbf2286bf2 | 13 | // |
ms523 | 0:aabbf2286bf2 | 14 | class OV7670 : public Base |
ms523 | 0:aabbf2286bf2 | 15 | { |
ms523 | 0:aabbf2286bf2 | 16 | public: |
ms523 | 0:aabbf2286bf2 | 17 | I2C camera ; |
ms523 | 0:aabbf2286bf2 | 18 | InterruptIn vsync,href; |
ms523 | 0:aabbf2286bf2 | 19 | DigitalOut wen ; |
ms523 | 0:aabbf2286bf2 | 20 | BusIn data ; |
ms523 | 0:aabbf2286bf2 | 21 | DigitalOut rrst,oe,rclk ; |
ms523 | 0:aabbf2286bf2 | 22 | volatile int LineCounter ; |
ms523 | 0:aabbf2286bf2 | 23 | volatile int LastLines ; |
ms523 | 0:aabbf2286bf2 | 24 | volatile bool CaptureReq ; |
ms523 | 0:aabbf2286bf2 | 25 | volatile bool Busy ; |
ms523 | 0:aabbf2286bf2 | 26 | volatile bool Done ; |
ms523 | 0:aabbf2286bf2 | 27 | |
ms523 | 0:aabbf2286bf2 | 28 | OV7670( |
ms523 | 0:aabbf2286bf2 | 29 | PinName sda,// Camera I2C port |
ms523 | 0:aabbf2286bf2 | 30 | PinName scl,// Camera I2C port |
ms523 | 0:aabbf2286bf2 | 31 | PinName vs, // VSYNC |
ms523 | 0:aabbf2286bf2 | 32 | PinName hr, // HREF |
ms523 | 0:aabbf2286bf2 | 33 | PinName we, // WEN |
ms523 | 0:aabbf2286bf2 | 34 | PinName d7, // D7 |
ms523 | 0:aabbf2286bf2 | 35 | PinName d6, // D6 |
ms523 | 0:aabbf2286bf2 | 36 | PinName d5, // D5 |
ms523 | 0:aabbf2286bf2 | 37 | PinName d4, // D4 |
ms523 | 0:aabbf2286bf2 | 38 | PinName d3, // D3 |
ms523 | 0:aabbf2286bf2 | 39 | PinName d2, // D2 |
ms523 | 0:aabbf2286bf2 | 40 | PinName d1, // D1 |
ms523 | 0:aabbf2286bf2 | 41 | PinName d0, // D0 |
ms523 | 0:aabbf2286bf2 | 42 | PinName rt, // /RRST |
ms523 | 0:aabbf2286bf2 | 43 | PinName o, // /OE |
ms523 | 0:aabbf2286bf2 | 44 | PinName rc // RCLK |
ms523 | 0:aabbf2286bf2 | 45 | ) : camera(sda,scl),vsync(vs),href(hr),wen(we),data(d0,d1,d2,d3,d4,d5,d6,d7),rrst(rt),oe(o),rclk(rc) |
ms523 | 0:aabbf2286bf2 | 46 | { |
ms523 | 0:aabbf2286bf2 | 47 | camera.stop() ; |
ms523 | 0:aabbf2286bf2 | 48 | camera.frequency(OV7670_I2CFREQ) ; |
ms523 | 0:aabbf2286bf2 | 49 | vsync.fall(this,&OV7670::VsyncHandler) ; |
ms523 | 0:aabbf2286bf2 | 50 | href.rise(this,&OV7670::HrefHandler) ; |
ms523 | 0:aabbf2286bf2 | 51 | CaptureReq = false ; |
ms523 | 0:aabbf2286bf2 | 52 | Busy = false ; |
ms523 | 0:aabbf2286bf2 | 53 | Done = false ; |
ms523 | 0:aabbf2286bf2 | 54 | LineCounter = 0 ; |
ms523 | 0:aabbf2286bf2 | 55 | rrst = 1 ; |
ms523 | 0:aabbf2286bf2 | 56 | oe = 1 ; |
ms523 | 0:aabbf2286bf2 | 57 | rclk = 1 ; |
ms523 | 0:aabbf2286bf2 | 58 | wen = 0 ; |
ms523 | 0:aabbf2286bf2 | 59 | } |
ms523 | 0:aabbf2286bf2 | 60 | |
ms523 | 0:aabbf2286bf2 | 61 | // capture request |
ms523 | 0:aabbf2286bf2 | 62 | void CaptureNext(void) |
ms523 | 0:aabbf2286bf2 | 63 | { |
ms523 | 0:aabbf2286bf2 | 64 | CaptureReq = true ; |
ms523 | 0:aabbf2286bf2 | 65 | Busy = true ; |
ms523 | 0:aabbf2286bf2 | 66 | } |
ms523 | 0:aabbf2286bf2 | 67 | |
ms523 | 0:aabbf2286bf2 | 68 | // capture done? (with clear) |
ms523 | 0:aabbf2286bf2 | 69 | bool CaptureDone(void) |
ms523 | 0:aabbf2286bf2 | 70 | { |
ms523 | 0:aabbf2286bf2 | 71 | bool result ; |
ms523 | 0:aabbf2286bf2 | 72 | if (Busy) { |
ms523 | 0:aabbf2286bf2 | 73 | result = false ; |
ms523 | 0:aabbf2286bf2 | 74 | } else { |
ms523 | 0:aabbf2286bf2 | 75 | result = Done ; |
ms523 | 0:aabbf2286bf2 | 76 | Done = false ; |
ms523 | 0:aabbf2286bf2 | 77 | } |
ms523 | 0:aabbf2286bf2 | 78 | return result ; |
ms523 | 0:aabbf2286bf2 | 79 | } |
ms523 | 0:aabbf2286bf2 | 80 | |
ms523 | 0:aabbf2286bf2 | 81 | // write to camera |
ms523 | 0:aabbf2286bf2 | 82 | void WriteReg(int addr,int data) |
ms523 | 0:aabbf2286bf2 | 83 | { |
ms523 | 0:aabbf2286bf2 | 84 | // WRITE 0x42,ADDR,DATA |
ms523 | 0:aabbf2286bf2 | 85 | camera.start() ; |
ms523 | 0:aabbf2286bf2 | 86 | camera.write(OV7670_WRITE) ; |
ms523 | 0:aabbf2286bf2 | 87 | wait_us(OV7670_WRITEWAIT); |
ms523 | 0:aabbf2286bf2 | 88 | camera.write(addr) ; |
ms523 | 0:aabbf2286bf2 | 89 | wait_us(OV7670_WRITEWAIT); |
ms523 | 0:aabbf2286bf2 | 90 | camera.write(data) ; |
ms523 | 0:aabbf2286bf2 | 91 | camera.stop() ; |
ms523 | 0:aabbf2286bf2 | 92 | } |
ms523 | 0:aabbf2286bf2 | 93 | |
ms523 | 0:aabbf2286bf2 | 94 | // read from camera |
ms523 | 0:aabbf2286bf2 | 95 | int ReadReg(int addr) |
ms523 | 0:aabbf2286bf2 | 96 | { |
ms523 | 0:aabbf2286bf2 | 97 | int data ; |
ms523 | 0:aabbf2286bf2 | 98 | |
ms523 | 0:aabbf2286bf2 | 99 | // WRITE 0x42,ADDR |
ms523 | 0:aabbf2286bf2 | 100 | camera.start() ; |
ms523 | 0:aabbf2286bf2 | 101 | camera.write(OV7670_WRITE) ; |
ms523 | 0:aabbf2286bf2 | 102 | wait_us(OV7670_WRITEWAIT); |
ms523 | 0:aabbf2286bf2 | 103 | camera.write(addr) ; |
ms523 | 0:aabbf2286bf2 | 104 | camera.stop() ; |
ms523 | 0:aabbf2286bf2 | 105 | wait_us(OV7670_WRITEWAIT); |
ms523 | 0:aabbf2286bf2 | 106 | |
ms523 | 0:aabbf2286bf2 | 107 | // WRITE 0x43,READ |
ms523 | 0:aabbf2286bf2 | 108 | camera.start() ; |
ms523 | 0:aabbf2286bf2 | 109 | camera.write(OV7670_READ) ; |
ms523 | 0:aabbf2286bf2 | 110 | wait_us(OV7670_WRITEWAIT); |
ms523 | 0:aabbf2286bf2 | 111 | data = camera.read(OV7670_NOACK) ; |
ms523 | 0:aabbf2286bf2 | 112 | camera.stop() ; |
ms523 | 0:aabbf2286bf2 | 113 | |
ms523 | 0:aabbf2286bf2 | 114 | return data ; |
ms523 | 0:aabbf2286bf2 | 115 | } |
ms523 | 0:aabbf2286bf2 | 116 | |
ms523 | 0:aabbf2286bf2 | 117 | void Reset(void) { |
ms523 | 0:aabbf2286bf2 | 118 | WriteReg(0x12,0x80) ; // RESET CAMERA |
ms523 | 0:aabbf2286bf2 | 119 | wait_ms(200) ; |
ms523 | 0:aabbf2286bf2 | 120 | } |
ms523 | 0:aabbf2286bf2 | 121 | |
ms523 | 0:aabbf2286bf2 | 122 | void InitQQVGA() { |
ms523 | 0:aabbf2286bf2 | 123 | // QQVGA RGB444 |
ms523 | 0:aabbf2286bf2 | 124 | WriteReg(REG_CLKRC,0x80); |
ms523 | 0:aabbf2286bf2 | 125 | WriteReg(REG_COM11,0x0A) ; |
ms523 | 0:aabbf2286bf2 | 126 | WriteReg(REG_TSLB,0x04); |
ms523 | 0:aabbf2286bf2 | 127 | WriteReg(REG_COM7,0x04) ; |
ms523 | 0:aabbf2286bf2 | 128 | WriteReg(REG_RGB444, 0x02); |
ms523 | 0:aabbf2286bf2 | 129 | WriteReg(REG_COM15, 0xd0); |
ms523 | 0:aabbf2286bf2 | 130 | WriteReg(REG_HSTART,0x16) ; |
ms523 | 0:aabbf2286bf2 | 131 | WriteReg(REG_HSTOP,0x04) ; |
ms523 | 0:aabbf2286bf2 | 132 | WriteReg(REG_HREF,0x24) ; |
ms523 | 0:aabbf2286bf2 | 133 | WriteReg(REG_VSTART,0x02) ; |
ms523 | 0:aabbf2286bf2 | 134 | WriteReg(REG_VSTOP,0x7a) ; |
ms523 | 0:aabbf2286bf2 | 135 | WriteReg(REG_VREF,0x0a) ; |
ms523 | 0:aabbf2286bf2 | 136 | WriteReg(REG_COM10,0x02) ; |
ms523 | 0:aabbf2286bf2 | 137 | WriteReg(REG_COM3, 0x04); |
ms523 | 0:aabbf2286bf2 | 138 | WriteReg(REG_COM14, 0x1a); |
ms523 | 0:aabbf2286bf2 | 139 | WriteReg(0x72, 0x22); |
ms523 | 0:aabbf2286bf2 | 140 | WriteReg(0x73, 0xf2); |
ms523 | 0:aabbf2286bf2 | 141 | |
ms523 | 0:aabbf2286bf2 | 142 | // COLOR SETTING |
ms523 | 0:aabbf2286bf2 | 143 | WriteReg(0x4f,0x80); |
ms523 | 0:aabbf2286bf2 | 144 | WriteReg(0x50,0x80); |
ms523 | 0:aabbf2286bf2 | 145 | WriteReg(0x51,0x00); |
ms523 | 0:aabbf2286bf2 | 146 | WriteReg(0x52,0x22); |
ms523 | 0:aabbf2286bf2 | 147 | WriteReg(0x53,0x5e); |
ms523 | 0:aabbf2286bf2 | 148 | WriteReg(0x54,0x80); |
ms523 | 0:aabbf2286bf2 | 149 | WriteReg(0x56,0x40); |
ms523 | 0:aabbf2286bf2 | 150 | WriteReg(0x58,0x9e); |
ms523 | 0:aabbf2286bf2 | 151 | WriteReg(0x59,0x88); |
ms523 | 0:aabbf2286bf2 | 152 | WriteReg(0x5a,0x88); |
ms523 | 0:aabbf2286bf2 | 153 | WriteReg(0x5b,0x44); |
ms523 | 0:aabbf2286bf2 | 154 | WriteReg(0x5c,0x67); |
ms523 | 0:aabbf2286bf2 | 155 | WriteReg(0x5d,0x49); |
ms523 | 0:aabbf2286bf2 | 156 | WriteReg(0x5e,0x0e); |
ms523 | 0:aabbf2286bf2 | 157 | WriteReg(0x69,0x00); |
ms523 | 0:aabbf2286bf2 | 158 | WriteReg(0x6a,0x40); |
ms523 | 0:aabbf2286bf2 | 159 | WriteReg(0x6b,0x0a); |
ms523 | 0:aabbf2286bf2 | 160 | WriteReg(0x6c,0x0a); |
ms523 | 0:aabbf2286bf2 | 161 | WriteReg(0x6d,0x55); |
ms523 | 0:aabbf2286bf2 | 162 | WriteReg(0x6e,0x11); |
ms523 | 0:aabbf2286bf2 | 163 | WriteReg(0x6f,0x9f); |
ms523 | 0:aabbf2286bf2 | 164 | |
ms523 | 0:aabbf2286bf2 | 165 | WriteReg(0xb0,0x84); |
ms523 | 0:aabbf2286bf2 | 166 | } |
ms523 | 0:aabbf2286bf2 | 167 | |
ms523 | 0:aabbf2286bf2 | 168 | |
ms523 | 0:aabbf2286bf2 | 169 | |
ms523 | 0:aabbf2286bf2 | 170 | // vsync handler |
ms523 | 0:aabbf2286bf2 | 171 | void VsyncHandler(void) |
ms523 | 0:aabbf2286bf2 | 172 | { |
ms523 | 0:aabbf2286bf2 | 173 | // Capture Enable |
ms523 | 0:aabbf2286bf2 | 174 | if (CaptureReq) { |
ms523 | 0:aabbf2286bf2 | 175 | wen = 1 ; |
ms523 | 0:aabbf2286bf2 | 176 | Done = false ; |
ms523 | 0:aabbf2286bf2 | 177 | CaptureReq = false ; |
ms523 | 0:aabbf2286bf2 | 178 | } else { |
ms523 | 0:aabbf2286bf2 | 179 | wen = 0 ; |
ms523 | 0:aabbf2286bf2 | 180 | if (Busy) { |
ms523 | 0:aabbf2286bf2 | 181 | Busy = false ; |
ms523 | 0:aabbf2286bf2 | 182 | Done = true ; |
ms523 | 0:aabbf2286bf2 | 183 | } |
ms523 | 0:aabbf2286bf2 | 184 | } |
ms523 | 0:aabbf2286bf2 | 185 | |
ms523 | 0:aabbf2286bf2 | 186 | // Hline Counter |
ms523 | 0:aabbf2286bf2 | 187 | LastLines = LineCounter ; |
ms523 | 0:aabbf2286bf2 | 188 | LineCounter = 0 ; |
ms523 | 0:aabbf2286bf2 | 189 | } |
ms523 | 0:aabbf2286bf2 | 190 | |
ms523 | 0:aabbf2286bf2 | 191 | // href handler |
ms523 | 0:aabbf2286bf2 | 192 | void HrefHandler(void) |
ms523 | 0:aabbf2286bf2 | 193 | { |
ms523 | 0:aabbf2286bf2 | 194 | LineCounter++ ; |
ms523 | 0:aabbf2286bf2 | 195 | } |
ms523 | 0:aabbf2286bf2 | 196 | |
ms523 | 0:aabbf2286bf2 | 197 | // Data Read |
ms523 | 0:aabbf2286bf2 | 198 | int ReadOneByte(void) |
ms523 | 0:aabbf2286bf2 | 199 | { |
ms523 | 0:aabbf2286bf2 | 200 | int result ; |
ms523 | 0:aabbf2286bf2 | 201 | rclk = 1 ; |
ms523 | 0:aabbf2286bf2 | 202 | // wait_us(1) ; |
ms523 | 0:aabbf2286bf2 | 203 | result = data ; |
ms523 | 0:aabbf2286bf2 | 204 | rclk = 0 ; |
ms523 | 0:aabbf2286bf2 | 205 | return result ; |
ms523 | 0:aabbf2286bf2 | 206 | } |
ms523 | 0:aabbf2286bf2 | 207 | |
ms523 | 0:aabbf2286bf2 | 208 | // Data Start |
ms523 | 0:aabbf2286bf2 | 209 | void ReadStart(void) |
ms523 | 0:aabbf2286bf2 | 210 | { |
ms523 | 0:aabbf2286bf2 | 211 | rrst = 0 ; |
ms523 | 0:aabbf2286bf2 | 212 | oe = 0 ; |
ms523 | 0:aabbf2286bf2 | 213 | wait_us(1) ; |
ms523 | 0:aabbf2286bf2 | 214 | rclk = 0 ; |
ms523 | 0:aabbf2286bf2 | 215 | wait_us(1) ; |
ms523 | 0:aabbf2286bf2 | 216 | rclk = 1 ; |
ms523 | 0:aabbf2286bf2 | 217 | wait_us(1) ; |
ms523 | 0:aabbf2286bf2 | 218 | rrst = 1 ; |
ms523 | 0:aabbf2286bf2 | 219 | } |
ms523 | 0:aabbf2286bf2 | 220 | |
ms523 | 0:aabbf2286bf2 | 221 | // Data Stop |
ms523 | 0:aabbf2286bf2 | 222 | void ReadStop(void) |
ms523 | 0:aabbf2286bf2 | 223 | { |
ms523 | 0:aabbf2286bf2 | 224 | oe = 1 ; |
ms523 | 0:aabbf2286bf2 | 225 | ReadOneByte() ; |
ms523 | 0:aabbf2286bf2 | 226 | rclk = 1 ; |
ms523 | 0:aabbf2286bf2 | 227 | } |
ms523 | 0:aabbf2286bf2 | 228 | }; |