Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arduino Pro Micro vs OV7670
- // Discussion: http://forum.arduino.cc/index.php?topic=159557.msg2815774#msg2815774
- // Album: http://imgur.com/a/nurwJ
- // Last updated version at http://pastebin.com/GcT3gnmw
- /*----------------
- Read registers of OV7670.
- pin 9 -> 4.7k -> XCLK -> 4.7k -> GND
- pin A5 -> SIOC -> 3.3k -> +3.3V
- pin A4 -> SIOD -> 3.3k -> +3.3V
- A0-A3 -> D0-D3
- 4-7 -> D4-D7
- --------------------*/
- #include "ov7670_reg.h"
- #include <Wire.h>
- const int HREF = 8;
- const int VSYNC = 16;
- const int PCLK = 15;
- void captureImg() {
- // PIND is 0..7, PINB is 8..13, PINC is A0..A5 (e.g. pin 10 would be PINB & 4)
- const int w = 320;
- const int h = 240/4;
- unsigned char buf[w*2];
- while (digitalRead(VSYNC) == LOW);
- while (digitalRead(VSYNC) == HIGH);
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- while (digitalRead(PCLK) == HIGH);
- buf[x] = (PINC & 15) | (PIND & 240); // pins A0..A3 and 4..7 (PINC & 00001111, PIND & 11110000)
- //while (digitalRead(PCLK) == LOW);
- //while (digitalRead(PCLK) == HIGH);
- //while (digitalRead(PCLK) == LOW);
- }
- Serial.write(buf, w);
- }
- }
- void calcHREF() {
- int HREF_Count = 0;
- while (digitalRead(VSYNC) == LOW) {}
- while (digitalRead(VSYNC) == HIGH) {}
- while (digitalRead(VSYNC) == LOW) {
- if (digitalRead(HREF) == HIGH) {
- while (digitalRead(HREF) == HIGH) {}
- HREF_Count++;
- }
- }
- Serial.print("Total HRef=");
- Serial.println(HREF_Count++);
- }
- const int addr = 0x21; // OV-7670 has address 0x21
- uint8_t wrReg(uint8_t reg, uint8_t dat) {
- Wire.beginTransmission(addr);
- Wire.write(reg & 0x00FF);
- Wire.write(dat & 0x00FF);
- byte result = Wire.endTransmission();
- delay(1);
- return result;
- }
- void wrSensorRegs8_8(const struct regval_list reglist[]) {
- uint8_t reg_addr, reg_val;
- const struct regval_list *next = reglist;
- while ((reg_addr != 0xff) | (reg_val != 0xff)) {
- reg_addr = pgm_read_byte(&next->reg_num);
- reg_val = pgm_read_byte(&next->value);
- wrReg(reg_addr, reg_val);
- next++;
- }
- }
- void readRegisters(char addr) {
- byte val;
- for (int i = 0; i < 256; i++) {
- // set address
- Wire.beginTransmission(addr);
- Wire.write(i); //reg address low byte
- Wire.endTransmission();
- // read 1 byte
- Wire.requestFrom(addr, 1);
- while (Wire.available()) {
- val = Wire.read();
- }
- Serial.print("REG 0x"); Serial.print(i, HEX); Serial.print(": 0x"); Serial.println(val, HEX);
- //Serial.write(val);
- }
- }
- void setup() {
- pinMode(9, OUTPUT);
- pinMode(HREF, INPUT);
- pinMode(VSYNC, INPUT);
- pinMode(PCLK, INPUT);
- //Generating 8MHZ PWM on pin9
- TCCR1B |= (1 << CS10); //selecting prescaler 0b001 (Tclk/1)
- TCCR1B &= ~((1 << CS12) | (1 << CS11)); // turn off CS12 and CS11 bits
- TCCR1A |= ((1 << WGM11) | (1 << WGM10)); //Configure timer 1 for TOP mode (with TOP = OCR1A)
- TCCR1B |= ((1 << WGM13) | (1 << WGM12));
- TCCR1A |= (1 << COM1A0); // Enable timer 1 Compare Output channel A in toggle mode
- TCCR1A &= ~(1 << COM1A1);
- TCNT1 = 0;
- OCR1A = 0; //for 8MHz: 0, for 10kHz: 799
- Wire.begin();
- Serial.begin(115200);
- // camInit
- wrReg(0x12, 0x80);
- delayMicroseconds(100);
- wrSensorRegs8_8(ov7670_default_regs);
- wrReg(REG_COM10, 32);//PCLK does not toggle on HBLANK.
- // setRes
- wrReg(REG_COM3, 4); // REG_COM3 enable scaling
- wrSensorRegs8_8(qvga_ov7670);
- // setColor
- wrSensorRegs8_8(yuv422_ov7670);
- wrReg(0x11, 11);
- }
- void loop() {
- if (Serial.available()) {
- char a = Serial.read();
- if (a == '1') {
- captureImg();
- } else if (a == '2') {
- readRegisters(0x21);
- calcHREF();
- } else {
- Serial.println("OK");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement