Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ov7670.h"
- void camera_setup() {
- // Set up TWI for 100khz
- Wire.begin();
- TWSR &= ~3; // Disable TWI prescaler
- TWBR = 72; // CLOCK / (16 + 2*TWBR*Prescaler)
- // General setup
- writeReg(0x15, 32); // PCLK does not toggle on HBLANK COM10 vsync falling
- //writeReg(REG_RGB444, 0x00); // Disable RGB444
- //writeReg(REG_COM11, 0xE2); // Enable night mode 1/8 frame rate
- writeReg(REG_TSLB, 0x04); // 0D = UYVY, 04 = YUYV
- writeReg(REG_COM13, 0x88); // with REG_TSLB
- /* // Set up for RGB565
- writeReg(REG_COM7, 0x04); // Enable RGB + color bar disable
- writeReg(REG_COM15, 0xD0); // Set rgb565 with full range
- writeReg(REG_COM3, 0x04); // Enable DCW (required for manual scaling?)
- */
- writeReg(REG_COM7, 0x08);
- writeReg(REG_COM17, 0x00);
- writeReg(REG_COM15, 0xC0);
- writeReg(REG_COM3, 0x08);
- //writeReg(REG_COM14, 0x08);
- // Set to QQVGA mode (160x120)
- /*
- writeReg(REG_COM14, 0x1A); // Divide by 4
- writeReg(SCALE_DCW, 0x22); // Downsample by 4
- writeReg(SCALE_PCLK, 0xF2); // Divide clock by 4
- writeReg(REG_HSTART, 0x16);
- writeReg(REG_HSTOP, 0x04);
- writeReg(REG_HREF, 0xA4);
- writeReg(REG_VSTART, 0x02);
- writeReg(REG_VSTOP, 0x7A);
- writeReg(REG_VREF, 0x0A);
- writeReg(REG_COM1, 0x00);
- */
- /*
- writeReg(REG_HSTART, 0x13);
- writeReg(REG_HSTOP, 0x01);
- writeReg(REG_HREF, 0xB6);
- writeReg(REG_VSTART, 0x02);
- writeReg(REG_VSTOP, 0x7A);
- writeReg(REG_VREF, 0x0A);
- writeReg(REG_COM1, 0x00);
- */
- // Color setup
- /*
- writeReg(REG_COM8, 0x8F); // AGC AWB AEC Unlimited step size
- writeReg(0xAA, 0x14); // Average based AEC algorithm
- writeReg(REG_BRIGHT, 0x00); // 0x00 (brightness 0), 0x18 (brightness +1), 0x98 (brightness -1)
- writeReg(REG_CONTRAS, 0x40); // 0x40 (contrast 0), 0x50 (contrast +1), 0x38 (contrast -1)
- writeReg(0xB1, 0x04); // Automatic black level calibration
- writeReg(MTX1, 0x80);
- writeReg(MTX2, 0x80);
- writeReg(MTX3, 0x00);
- writeReg(MTX4, 0x22);
- writeReg(MTX5, 0x5E);
- writeReg(MTX6, 0x80);
- writeReg(MTXS, 0x9E);
- writeReg(AWBC7, 0x88);
- writeReg(AWBC8, 0x88);
- writeReg(AWBC9, 0x44);
- writeReg(AWBC10, 0x67);
- writeReg(AWBC11, 0x49);
- writeReg(AWBC12, 0x0E);
- writeReg(REG_GFIX, 0x00);
- writeReg(AWBCTR3, 0x0A);
- writeReg(AWBCTR2, 0x55);
- writeReg(AWBCTR1, 0x11);
- writeReg(AWBCTR0, 0x9F);
- writeReg(0xB0, 0x84); // Not sure what this does
- writeReg(REG_COM16, COM16_AWBGAIN); // Disable auto denoise and edge enhancement
- writeReg(0x4C, 0x00); // Disable denoise
- writeReg(0x76, 0x00); // Disable denoise
- writeReg(0x77, 0x00); // Disable denoise
- writeReg(0x7B, 0x04); // Brighten shadows a bit, end point 4
- writeReg(0x7C, 0x08); // Brighten shadows a bit, end point 8
- writeReg(REG_COM9, 0x6A); // Max gain to 128x
- */
- writeReg(0x11, 0x03); // Scale clock
- delay(100); // Wait for communication to finish
- }
- void captureImage() {
- unsigned long line = 176 * 2;
- unsigned long chunk = line * 4;
- unsigned long numBytes = 50688;
- unsigned long start = 0;
- Serial.println(chunk);
- Serial.println(numBytes);
- uint8_t *pixels = (uint8_t *)malloc(sizeof(uint8_t) * chunk);
- while(start < numBytes) {
- captureChunk(start, chunk, pixels);
- start += chunk;
- }
- free(pixels);
- }
- void captureChunk(unsigned long wait, unsigned long chunk, uint8_t *pixels) {
- unsigned int n = 0;
- while(!(PIND & 8)) {} // Wait for VSYNC high
- while(PIND & 8) {} // Wait for VSYNC low
- while(wait--) {
- while(!(PIND & 4)) {};
- while(PIND & 4) {};
- }
- while(chunk--) {
- while(!(PIND & 4)) {};
- pixels[n++] = (uint8_t)((PINC & 0x0F) | (PIND & 0xF0));
- while(PIND & 4) {};
- }
- //Serial.write(pixels, n);
- chunk = 0;
- uint16_t line;
- while(chunk < n) {
- line = 176 * 2;
- while(line--) {
- Serial.write(pixels[chunk++]);
- }
- }
- }
- /* Write 2 byte value regDat to the camera register addressed by regID */
- byte writeReg(int regID, int regDat) {
- Wire.beginTransmission(sensor_addr >> 1);
- Wire.write(regID & 0x00FF);
- Wire.write(regDat & 0x00FF);
- byte result = Wire.endTransmission();
- delay(1);
- return result;
- }
- /* Read a 2 byte value from the camera addressed at regID, and store it at
- * memory location pointed to by regDat. Return 1 on success, 0 on failure. */
- byte readReg8(uint8_t regID, uint8_t* regDat) {
- Wire.beginTransmission(sensor_addr >> 1);
- Wire.write(regID & 0x00FF);
- Wire.endTransmission();
- Wire.requestFrom((sensor_addr >> 1),1);
- if(Wire.available()) {
- *regDat = Wire.read();
- delay(1);
- return 1;
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement