Advertisement
Guest User

ard mega

a guest
Jun 2nd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Pins configurations:
  2. // SDA/SIOD ---> pin A4 (for Arduino UNO) | pin 20/SDA (for Arduino MEGA)
  3. // SCL/SIOC ---> pin A5 (for Arduino UNO) | pin 21/SCL (for Arduino MEGA)
  4. // MCLK/XCLK --> pin 11 (for Arduino UNO) | pin 10 (for Arduino MEGA)
  5. // PCLK -------> pin 2
  6. // VS/VSYNC ---> pin 3
  7. // HS/HREF ----> pin 8
  8. // D0 ---------> pin A0
  9. // D1 ---------> pin A1
  10. // D2 ---------> pin A2
  11. // D3 ---------> pin A3
  12. // D4 ---------> pin 4
  13. // D5 ---------> pin 5
  14. // D6 ---------> pin 6
  15. // D7 ---------> pin 7
  16.  
  17. #include <Wire.h>
  18.  
  19. #define CAMERA_ADDRESS 0x21
  20.  
  21. // Definitions of functions for manipulating the Arduino boards pins according to each Arduino board registers, so the code will work for both Arduino UNO and Arduino MEGA:
  22. // The only change is the connections of the SDA/SIOD, SCL/SIOC and MCLK/XCLK pins to each board (see the pins configurations above).
  23. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // If you are using Arduino MEGA the IDE will automatically define the "__AVR_ATmega1280__" or "__AVR_ATmega2560__" constants.
  24. #define TIMER2_PWM_A_PIN_MODE_OUTPUT() ({ DDRB |= 0b00010000; })
  25. #define PIN2_DIGITAL_READ() ({ (PINE & 0b00010000) == 0 ? LOW : HIGH; })
  26. #define PIN3_DIGITAL_READ() ({ (PINE & 0b00100000) == 0 ? LOW : HIGH; })
  27. #define PIN4_DIGITAL_READ() ({ (PING & 0b00100000) == 0 ? LOW : HIGH; })
  28. #define PIN5_DIGITAL_READ() ({ (PINE & 0b00001000) == 0 ? LOW : HIGH; })
  29. #define PIN6_DIGITAL_READ() ({ (PINH & 0b00001000) == 0 ? LOW : HIGH; })
  30. #define PIN7_DIGITAL_READ() ({ (PINH & 0b00010000) == 0 ? LOW : HIGH; })
  31. #define PIN8_DIGITAL_READ() ({ (PINH & 0b00100000) == 0 ? LOW : HIGH; })
  32. #define PINA0_DIGITAL_READ() ({ (PINF & 0b00000001) == 0 ? LOW : HIGH; })
  33. #define PINA1_DIGITAL_READ() ({ (PINF & 0b00000010) == 0 ? LOW : HIGH; })
  34. #define PINA2_DIGITAL_READ() ({ (PINF & 0b00000100) == 0 ? LOW : HIGH; })
  35. #define PINA3_DIGITAL_READ() ({ (PINF & 0b00001000) == 0 ? LOW : HIGH; })
  36. #elif defined(__AVR_ATmega328P__) // If you are using Arduino UNO the IDE will automatically define the "__AVR_ATmega328P__" constant.
  37. #define TIMER2_PWM_A_PIN_MODE_OUTPUT() ({ DDRB |= 0b00001000; })
  38. #define PIN2_DIGITAL_READ() ({ (PIND & 0b00000100) == 0 ? LOW : HIGH; })
  39. #define PIN3_DIGITAL_READ() ({ (PIND & 0b00001000) == 0 ? LOW : HIGH; })
  40. #define PIN4_DIGITAL_READ() ({ (PIND & 0b00010000) == 0 ? LOW : HIGH; })
  41. #define PIN5_DIGITAL_READ() ({ (PIND & 0b00100000) == 0 ? LOW : HIGH; })
  42. #define PIN6_DIGITAL_READ() ({ (PIND & 0b01000000) == 0 ? LOW : HIGH; })
  43. #define PIN7_DIGITAL_READ() ({ (PIND & 0b10000000) == 0 ? LOW : HIGH; })
  44. #define PIN8_DIGITAL_READ() ({ (PINB & 0b00000001) == 0 ? LOW : HIGH; })
  45. #define PINA0_DIGITAL_READ() ({ (PINC & 0b00000001) == 0 ? LOW : HIGH; })
  46. #define PINA1_DIGITAL_READ() ({ (PINC & 0b00000010) == 0 ? LOW : HIGH; })
  47. #define PINA2_DIGITAL_READ() ({ (PINC & 0b00000100) == 0 ? LOW : HIGH; })
  48. #define PINA3_DIGITAL_READ() ({ (PINC & 0b00001000) == 0 ? LOW : HIGH; })
  49. #endif
  50.  
  51.  
  52. void initializePWMTimer() {
  53.   cli();
  54.   TIMER2_PWM_A_PIN_MODE_OUTPUT(); // Set the A PWM pin of TIMER2 to output
  55.   ASSR &= ~(_BV(EXCLK) | _BV(AS2));
  56.   TCCR2A = (1 << COM2A0) | (1 << WGM21) | (1 << WGM20);
  57.   TCCR2B = (1 << WGM22) | (1 << CS20);
  58.   OCR2A = 0;
  59.   sei();
  60. }
  61.  
  62. byte readCameraRegister(byte registerId) {
  63.   Wire.beginTransmission(CAMERA_ADDRESS);
  64.   Wire.write(registerId);
  65.   Wire.endTransmission();
  66.   Wire.requestFrom(CAMERA_ADDRESS, 1);
  67.   while (Wire.available() <= 0);
  68.   byte registerValue = Wire.read();
  69.   delay(1);
  70.   return registerValue;
  71. }
  72.  
  73. void writeCameraRegister(byte registerId, byte registerValue) {
  74.   Wire.beginTransmission(CAMERA_ADDRESS);
  75.   Wire.write(registerId);
  76.   Wire.write(registerValue);
  77.   Wire.endTransmission();
  78.   delay(1);
  79. }
  80.  
  81. void captureFrame(unsigned int frameWidth, unsigned int frameHeight) {
  82.   Serial.print("*RDY*"); // send to the frame capture software a "start of frame" message for beginning capturing
  83.  
  84.   delay(1000);
  85.  
  86.   cli(); // disable all interrupts during frame capture (because it needs to be as fast as possible)
  87.   while (PIN3_DIGITAL_READ() == LOW); // wait until VS/VSYNC pin is high
  88.   while (PIN3_DIGITAL_READ() == HIGH); // wait until VS/VSYNC pin is low
  89.   unsigned int tempWidth = 0;
  90.   while (frameHeight--) {
  91.     tempWidth = frameWidth;
  92.     while (tempWidth--) {
  93.       while (PIN2_DIGITAL_READ() == LOW); // wait until PCLK pin is high
  94.       while (PIN2_DIGITAL_READ() == HIGH); // wait until PCLK pin is low
  95.       byte byteToWrite = 0b00000000;
  96.       byteToWrite |= ((PIN7_DIGITAL_READ() == HIGH) << 7);
  97.       byteToWrite |= ((PIN6_DIGITAL_READ() == HIGH) << 6);
  98.       byteToWrite |= ((PIN5_DIGITAL_READ() == HIGH) << 5);
  99.       byteToWrite |= ((PIN4_DIGITAL_READ() == HIGH) << 4);
  100.       byteToWrite |= ((PINA3_DIGITAL_READ() == HIGH) << 3);
  101.       byteToWrite |= ((PINA2_DIGITAL_READ() == HIGH) << 2);
  102.       byteToWrite |= ((PINA1_DIGITAL_READ() == HIGH) << 1);
  103.       byteToWrite |= ((PINA0_DIGITAL_READ() == HIGH));
  104.       UDR0 = byteToWrite; // send data via serial connection with UART register (we need to use the serial register directly for fast transfer)
  105.       while (PIN2_DIGITAL_READ() == LOW); // wait until PCLK pin is high
  106.       while (PIN2_DIGITAL_READ() == HIGH); // wait until PCLK pin is low
  107.       // ignore each second byte (for a grayscale image we only need each first byte, which represents luminescence)
  108.     }
  109.   }
  110.   sei(); // enable all interrupts
  111.  
  112.   delay(1000);
  113. }
  114.  
  115.  
  116. void setup() {
  117.   initializePWMTimer();
  118.   Wire.begin();
  119.   Serial.begin(1000000); // the frame capture software communicates with the Arduino at a baud rate of 1MHz
  120. }
  121.  
  122. void loop() {
  123.   captureFrame(320, 240); // capture a frame at QVGA resolution (320 x 240)
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement