Advertisement
Guest User

OV7670 test

a guest
Jul 15th, 2013
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.64 KB | None | 0 0
  1. #include "ov7670.h"
  2.  
  3. void camera_setup() {
  4.   // Set up TWI for 100khz
  5.   Wire.begin();
  6.   TWSR &= ~3; // Disable TWI prescaler
  7.   TWBR = 72; // CLOCK / (16 + 2*TWBR*Prescaler)
  8.  
  9.   // General setup
  10.   writeReg(0x15, 32);  // PCLK does not toggle on HBLANK COM10 vsync falling
  11.   //writeReg(REG_RGB444, 0x00);  // Disable RGB444
  12.   //writeReg(REG_COM11, 0xE2); // Enable night mode 1/8 frame rate
  13.   writeReg(REG_TSLB, 0x04); // 0D = UYVY, 04 = YUYV
  14.   writeReg(REG_COM13, 0x88); // with REG_TSLB
  15.  
  16.   /* // Set up for RGB565
  17.   writeReg(REG_COM7, 0x04); // Enable RGB + color bar disable
  18.   writeReg(REG_COM15, 0xD0); // Set rgb565 with full range
  19.   writeReg(REG_COM3, 0x04); // Enable DCW (required for manual scaling?)
  20.   */
  21.   writeReg(REG_COM7, 0x08);
  22.   writeReg(REG_COM17, 0x00);
  23.   writeReg(REG_COM15, 0xC0);
  24.   writeReg(REG_COM3, 0x08);
  25.   //writeReg(REG_COM14, 0x08);
  26.  
  27.   // Set to QQVGA mode (160x120)
  28.   /*
  29.   writeReg(REG_COM14, 0x1A); // Divide by 4
  30.   writeReg(SCALE_DCW, 0x22); // Downsample by 4
  31.   writeReg(SCALE_PCLK, 0xF2); // Divide clock by 4
  32.   writeReg(REG_HSTART, 0x16);
  33.   writeReg(REG_HSTOP, 0x04);
  34.   writeReg(REG_HREF, 0xA4);
  35.   writeReg(REG_VSTART, 0x02);
  36.   writeReg(REG_VSTOP, 0x7A);
  37.   writeReg(REG_VREF, 0x0A);
  38.   writeReg(REG_COM1, 0x00);
  39.   */
  40.   /*
  41.   writeReg(REG_HSTART, 0x13);
  42.   writeReg(REG_HSTOP, 0x01);
  43.   writeReg(REG_HREF, 0xB6);
  44.   writeReg(REG_VSTART, 0x02);
  45.   writeReg(REG_VSTOP, 0x7A);
  46.   writeReg(REG_VREF, 0x0A);
  47.   writeReg(REG_COM1, 0x00);
  48.   */
  49.  
  50.   // Color setup
  51.   /*
  52.   writeReg(REG_COM8, 0x8F); // AGC AWB AEC Unlimited step size
  53.   writeReg(0xAA, 0x14); // Average based AEC algorithm
  54.   writeReg(REG_BRIGHT, 0x00); // 0x00 (brightness 0), 0x18 (brightness +1), 0x98 (brightness -1)
  55.   writeReg(REG_CONTRAS, 0x40); // 0x40 (contrast 0), 0x50 (contrast +1), 0x38 (contrast -1)
  56.   writeReg(0xB1, 0x04); // Automatic black level calibration
  57.   writeReg(MTX1, 0x80);
  58.   writeReg(MTX2, 0x80);
  59.   writeReg(MTX3, 0x00);
  60.   writeReg(MTX4, 0x22);
  61.   writeReg(MTX5, 0x5E);
  62.   writeReg(MTX6, 0x80);
  63.   writeReg(MTXS, 0x9E);
  64.   writeReg(AWBC7, 0x88);
  65.   writeReg(AWBC8, 0x88);
  66.   writeReg(AWBC9, 0x44);
  67.   writeReg(AWBC10, 0x67);
  68.   writeReg(AWBC11, 0x49);
  69.   writeReg(AWBC12, 0x0E);
  70.   writeReg(REG_GFIX, 0x00);
  71.   writeReg(AWBCTR3, 0x0A);
  72.   writeReg(AWBCTR2, 0x55);
  73.   writeReg(AWBCTR1, 0x11);
  74.   writeReg(AWBCTR0, 0x9F);
  75.   writeReg(0xB0, 0x84); // Not sure what this does
  76.   writeReg(REG_COM16, COM16_AWBGAIN); // Disable auto denoise and edge enhancement
  77.   writeReg(0x4C, 0x00); // Disable denoise
  78.   writeReg(0x76, 0x00); // Disable denoise
  79.   writeReg(0x77, 0x00); // Disable denoise
  80.   writeReg(0x7B, 0x04); // Brighten shadows a bit, end point 4
  81.   writeReg(0x7C, 0x08); // Brighten shadows a bit, end point 8
  82.   writeReg(REG_COM9, 0x6A); // Max gain to 128x
  83.   */
  84.   writeReg(0x11, 0x03); // Scale clock
  85.  
  86.   delay(100); // Wait for communication to finish
  87. }
  88.  
  89. void captureImage() {
  90.   unsigned long line = 176 * 2;
  91.   unsigned long chunk = line * 4;
  92.   unsigned long numBytes = 50688;
  93.   unsigned long start = 0;
  94.   Serial.println(chunk);
  95.   Serial.println(numBytes);
  96.   uint8_t *pixels = (uint8_t *)malloc(sizeof(uint8_t) * chunk);
  97.  
  98.   while(start < numBytes) {
  99.     captureChunk(start, chunk, pixels);
  100.     start += chunk;
  101.   }
  102.   free(pixels);
  103. }
  104.  
  105. void captureChunk(unsigned long wait, unsigned long chunk, uint8_t *pixels) {
  106.   unsigned int n = 0;
  107.   while(!(PIND & 8)) {} // Wait for VSYNC high
  108.   while(PIND & 8) {} // Wait for VSYNC low
  109.  
  110.   while(wait--) {
  111.     while(!(PIND & 4)) {};
  112.     while(PIND & 4) {};
  113.   }
  114.   while(chunk--) {
  115.     while(!(PIND & 4)) {};
  116.     pixels[n++] = (uint8_t)((PINC & 0x0F) | (PIND & 0xF0));
  117.     while(PIND & 4) {};
  118.   }
  119.    
  120.   //Serial.write(pixels, n);
  121.   chunk = 0;
  122.   uint16_t line;
  123.   while(chunk < n) {
  124.     line = 176 * 2;
  125.     while(line--) {
  126.      Serial.write(pixels[chunk++]);
  127.     }
  128.   }
  129. }
  130.  
  131. /* Write 2 byte value regDat to the camera register addressed by regID */
  132. byte writeReg(int regID, int regDat) {
  133.     Wire.beginTransmission(sensor_addr >> 1);
  134.     Wire.write(regID & 0x00FF);    
  135.     Wire.write(regDat & 0x00FF);    
  136.     byte result = Wire.endTransmission();
  137.     delay(1);
  138.     return result;
  139. }
  140.  
  141. /* Read a 2 byte value from the camera addressed at regID, and store it at
  142.  * memory location pointed to by regDat. Return 1 on success, 0 on failure. */
  143. byte readReg8(uint8_t regID, uint8_t* regDat) {
  144.     Wire.beginTransmission(sensor_addr >> 1);
  145.     Wire.write(regID & 0x00FF);    
  146.     Wire.endTransmission();
  147.     Wire.requestFrom((sensor_addr >> 1),1);
  148.     if(Wire.available()) {
  149.         *regDat = Wire.read();
  150.         delay(1);
  151.         return 1;
  152.     } else {
  153.         return 0;
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement