Advertisement
joric

ov7670-wire.ino

Jun 25th, 2016
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.75 KB | None | 0 0
  1. // Arduino Pro Micro vs OV7670
  2. // Discussion: http://forum.arduino.cc/index.php?topic=159557.msg2815774#msg2815774
  3. // Album: http://imgur.com/a/nurwJ
  4. // Last updated version at http://pastebin.com/GcT3gnmw
  5.  
  6. /*----------------
  7.   Read registers of OV7670.
  8.  
  9.   pin 9  -> 4.7k -> XCLK -> 4.7k -> GND
  10.   pin A5  -> SIOC -> 3.3k -> +3.3V
  11.   pin A4  -> SIOD -> 3.3k -> +3.3V
  12.  
  13.   A0-A3 -> D0-D3
  14.   4-7 -> D4-D7
  15.   --------------------*/
  16. #include "ov7670_reg.h"
  17. #include <Wire.h>
  18.  
  19. const int HREF = 8;
  20. const int VSYNC = 16;
  21. const int PCLK = 15;
  22.  
  23. void captureImg() {    
  24.   // PIND is 0..7, PINB is 8..13, PINC is A0..A5 (e.g. pin 10 would be PINB & 4)  
  25.   const int w = 320;
  26.   const int h = 240/4;
  27.   unsigned char buf[w*2];
  28.  
  29.   while (digitalRead(VSYNC) == LOW);
  30.   while (digitalRead(VSYNC) == HIGH);
  31.   for (int y = 0; y < h; y++) {
  32.     for (int x = 0; x < w; x++) {
  33.       while (digitalRead(PCLK) == HIGH);
  34.       buf[x] = (PINC & 15) | (PIND & 240); // pins A0..A3 and 4..7 (PINC & 00001111, PIND & 11110000)
  35.       //while (digitalRead(PCLK) == LOW);
  36.       //while (digitalRead(PCLK) == HIGH);
  37.       //while (digitalRead(PCLK) == LOW);
  38.     }
  39.     Serial.write(buf, w);
  40.   }
  41. }
  42.  
  43. void calcHREF() {
  44.   int HREF_Count = 0;
  45.   while (digitalRead(VSYNC) == LOW) {}
  46.   while (digitalRead(VSYNC) == HIGH) {}
  47.   while (digitalRead(VSYNC) == LOW) {
  48.     if (digitalRead(HREF) == HIGH) {
  49.       while (digitalRead(HREF) == HIGH) {}
  50.       HREF_Count++;
  51.     }
  52.   }
  53.   Serial.print("Total HRef=");
  54.   Serial.println(HREF_Count++);
  55. }
  56.  
  57.  
  58. const int addr = 0x21; // OV-7670 has address 0x21
  59.  
  60. uint8_t wrReg(uint8_t reg, uint8_t dat) {
  61.   Wire.beginTransmission(addr);
  62.   Wire.write(reg & 0x00FF);
  63.   Wire.write(dat & 0x00FF);
  64.   byte result = Wire.endTransmission();
  65.   delay(1);
  66.   return result;
  67. }
  68.  
  69. void wrSensorRegs8_8(const struct regval_list reglist[]) {
  70.   uint8_t reg_addr, reg_val;
  71.   const struct regval_list *next = reglist;
  72.   while ((reg_addr != 0xff) | (reg_val != 0xff)) {
  73.     reg_addr = pgm_read_byte(&next->reg_num);
  74.     reg_val = pgm_read_byte(&next->value);
  75.     wrReg(reg_addr, reg_val);
  76.     next++;
  77.   }
  78. }
  79.  
  80. void readRegisters(char addr) {
  81.   byte val;
  82.   for (int i = 0; i < 256; i++) {
  83.     // set address
  84.     Wire.beginTransmission(addr);
  85.     Wire.write(i);  //reg address low byte
  86.     Wire.endTransmission();
  87.  
  88.     // read 1 byte
  89.     Wire.requestFrom(addr, 1);
  90.     while (Wire.available()) {
  91.       val = Wire.read();
  92.     }
  93.  
  94.     Serial.print("REG 0x"); Serial.print(i, HEX); Serial.print(": 0x"); Serial.println(val, HEX);
  95.     //Serial.write(val);
  96.   }
  97. }
  98.  
  99. void setup() {
  100.   pinMode(9, OUTPUT);
  101.   pinMode(HREF, INPUT);
  102.   pinMode(VSYNC, INPUT);
  103.   pinMode(PCLK, INPUT);  
  104.  
  105.   //Generating 8MHZ PWM on pin9
  106.   TCCR1B |= (1 << CS10);  //selecting prescaler 0b001 (Tclk/1)
  107.   TCCR1B &= ~((1 << CS12) | (1 << CS11)); // turn off CS12 and CS11 bits
  108.   TCCR1A |= ((1 << WGM11) | (1 << WGM10)); //Configure timer 1 for TOP mode (with TOP = OCR1A)
  109.   TCCR1B |= ((1 << WGM13) | (1 << WGM12));
  110.   TCCR1A |= (1 << COM1A0); // Enable timer 1 Compare Output channel A in toggle mode
  111.   TCCR1A &= ~(1 << COM1A1);
  112.   TCNT1 = 0;
  113.   OCR1A = 0; //for 8MHz: 0, for 10kHz: 799
  114.  
  115.   Wire.begin();
  116.   Serial.begin(115200);
  117.  
  118.   // camInit
  119.   wrReg(0x12, 0x80);
  120.   delayMicroseconds(100);
  121.   wrSensorRegs8_8(ov7670_default_regs);
  122.   wrReg(REG_COM10, 32);//PCLK does not toggle on HBLANK.
  123.   // setRes
  124.   wrReg(REG_COM3, 4); // REG_COM3 enable scaling
  125.   wrSensorRegs8_8(qvga_ov7670);
  126.   // setColor
  127.   wrSensorRegs8_8(yuv422_ov7670);
  128.   wrReg(0x11, 11);
  129. }
  130.  
  131. void loop() {
  132.  
  133.   if (Serial.available()) {
  134.     char a = Serial.read();
  135.     if (a == '1') {
  136.       captureImg();
  137.     } else if (a == '2') {
  138.       readRegisters(0x21);
  139.       calcHREF();
  140.     } else {
  141.       Serial.println("OK");
  142.     }
  143.   }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement