Advertisement
Guest User

ov7670 arduino uno example code

a guest
Apr 9th, 2013
7,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.56 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SdFat.h>
  3. #include <SdFatUtil.h>
  4. // file system
  5. uint8_t buf2[512];
  6. SdFat sd;
  7. //uint8_t* buf;
  8. // test file
  9. SdFile file;
  10. // SD chip select pin
  11. const uint8_t chipSelect = 9;
  12. uint8_t sensor_addr = 0x42;
  13. #define REG_COM1    0x04    /* Control 1 */
  14. #define REG_COM7    0x12    /* Control 7 */
  15. #define   COM7_RESET      0x80    /* Register reset */
  16. #define   COM7_FMT_MASK   0x38
  17. #define   COM7_FMT_VGA    0x00
  18. #define   COM7_FMT_CIF    0x20    /* CIF format */
  19. #define   COM7_FMT_QVGA   0x10    /* QVGA format */
  20. #define   COM7_FMT_QCIF   0x08    /* QCIF format */
  21. #define   COM7_RGB    0x04    /* bits 0 and 2 - RGB format */
  22. #define   COM7_YUV    0x00    /* YUV */
  23. #define   COM7_BAYER      0x01    /* Bayer format */
  24. #define   COM7_PBAYER     0x05    /* "Processed bayer" */
  25. #define REG_RGB444  0x8c    /* RGB 444 control */
  26. #define   R444_ENABLE     0x02    /* Turn on RGB444, overrides 5x5 */
  27. #define   R444_RGBX   0x01    /* Empty nibble at end */
  28. #define REG_COM9    0x14    /* Control 9  - gain ceiling */
  29. #define REG_COM10   0x15    /* Control 10 */
  30. #define REG_COM13   0x3d    /* Control 13 */
  31. #define   COM13_GAMMA     0x80    /* Gamma enable */
  32. #define   COM13_UVSAT     0x40    /* UV saturation auto adjustment */
  33. #define   COM13_UVSWAP    0x01    /* V before U - w/TSLB */
  34. #define REG_COM15   0x40    /* Control 15 */
  35. #define   COM15_R10F0     0x00    /* Data range 10 to F0 */
  36. #define   COM15_R01FE     0x80    /*            01 to FE */
  37. #define   COM15_R00FF     0xc0    /*            00 to FF */
  38. #define   COM15_RGB565    0x10    /* RGB565 output */
  39. #define   COM15_RGB555    0x30    /* RGB555 output */
  40. #define REG_COM11   0x3b    /* Control 11 */
  41. #define   COM11_NIGHT     0x80    /* NIght mode enable */
  42. #define   COM11_NMFR      0x60    /* Two bit NM frame rate */
  43. #define   COM11_HZAUTO    0x10    /* Auto detect 50/60 Hz */
  44. #define   COM11_50HZ      0x08    /* Manual 50Hz select */
  45. #define   COM11_EXP   0x02
  46. #define REG_COM16   0x41    /* Control 16 */
  47. #define   COM16_AWBGAIN   0x08    /* AWB gain enable */
  48. #define REG_COM17       0x42    /* Control 17 */
  49. #define COM17_AECWIN    0xc0    /* AEC window - must match COM4 */
  50. #define COM17_CBAR      0x08    /* DSP Color bar */
  51. #define REG_TSLB    0x3a    /* lots of stuff */
  52. #define   TSLB_YLAST      0x04    /* UYVY or VYUY - see com13 */
  53. #define MTX1            0x4f    /* Matrix Coefficient 1 */
  54. #define MTX2            0x50    /* Matrix Coefficient 2 */
  55. #define MTX3            0x51    /* Matrix Coefficient 3 */
  56. #define MTX4            0x52    /* Matrix Coefficient 4 */
  57. #define MTX5            0x53    /* Matrix Coefficient 5 */
  58. #define MTX6            0x54    /* Matrix Coefficient 6 */
  59. #define REG_CONTRAS     0x56    /* Contrast control */
  60. #define MTXS            0x58    /* Matrix Coefficient Sign */
  61. #define AWBC7           0x59    /* AWB Control 7 */
  62. #define AWBC8           0x5a    /* AWB Control 8 */
  63. #define AWBC9           0x5b    /* AWB Control 9 */
  64. #define AWBC10          0x5c    /* AWB Control 10 */
  65. #define AWBC11          0x5d    /* AWB Control 11 */
  66. #define AWBC12          0x5e    /* AWB Control 12 */
  67. #define REG_GFIX        0x69    /* Fix gain control */
  68. #define GGAIN           0x6a    /* G Channel AWB Gain */
  69. #define DBLV            0x6b    
  70. #define AWBCTR3         0x6c    /* AWB Control 3 */
  71. #define AWBCTR2         0x6d    /* AWB Control 2 */
  72. #define AWBCTR1         0x6e    /* AWB Control 1 */
  73. #define AWBCTR0         0x6f    /* AWB Control 0 */
  74. #define REG_COM8    0x13    /* Control 8 */
  75. #define   COM8_FASTAEC    0x80    /* Enable fast AGC/AEC */
  76. #define   COM8_AECSTEP    0x40    /* Unlimited AEC step size */
  77. #define   COM8_BFILT      0x20    /* Band filter enable */
  78. #define   COM8_AGC    0x04    /* Auto gain enable */
  79. #define   COM8_AWB    0x02    /* White balance enable */
  80. #define   COM8_AEC    0x01    /* Auto exposure enable */
  81. #define REG_COM3        0x0c    /* Control 3 */
  82. #define COM3_SWAP       0x40    /* Byte swap */
  83. #define COM3_SCALEEN    0x08    /* Enable scaling */
  84. #define COM3_DCWEN      0x04    /* Enable downsamp/crop/window */
  85. #define REG_BRIGHT      0x55    /* Brightness */
  86. // Serial output stream
  87. // store error strings in flash to save RAM
  88. uint32_t bgnBlock, endBlock;
  89. #define error(s) sd.errorHalt_P(PSTR(s))
  90. #define useSdCard  PORTB|=4; PORTB&=~2
  91. #define UseFastWR
  92. #define useLed
  93. void setup()
  94. {
  95.     Wire.begin();
  96.     pinMode(10,OUTPUT);
  97.     digitalWrite(10,HIGH);
  98.     DDRB|=47;//clock as output and SPI pins as output except MISO
  99.     PORTB|=6;//set both CS pins to high
  100.     DDRC&=~15;//low d0-d3 camera
  101.     DDRD&=~252;//d7-d4 and interupt pins
  102.   #ifdef useLed
  103.   DDRD|=1;//pin 0 as output
  104.   PORTD&=~1;//turn off led
  105.   //set pin 1 as input
  106.   DDRD&=~2;
  107.   PORTB|=2;//enable pullup
  108.   #endif
  109.     //DDRB&=~2;//href as input
  110.     //EICRA=11;//int0 rising int1 falling
  111.     //EIMSK=2;//disable int0 and enable int1
  112.     //disable pin change interupt
  113.     //PCICR=0;
  114.     //PCMSK0=2;
  115.  
  116.     //set up twi for 100khz
  117.     TWSR&=~3;//disable prescaler for TWI
  118.     TWBR=72;//set to 100khz
  119.     //enable serial
  120.     //UBRR0H = (unsigned char)(MYUBRR>>8);
  121.     //UBRR0L = (unsigned char)MYUBRR&255;
  122.   #ifndef useLed
  123.     UBRR0H=0;
  124.     UBRR0L=207;//3 = 0.5M 2M baud rate = 0 7 = 250k 207 is 9600 baud rate
  125.     UCSR0A|=2;//double speed aysnc
  126.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);//Enable receiver and transmitter
  127.     UCSR0C=6;//async 1 stop bit 8bit char no parity bits
  128.          PgmPrintln("Setting up Camera Registers");
  129.          #endif
  130.     //set up camera
  131.     wrReg(0x15,32);//pclk does not toggle on HBLANK COM10 vsync falling
  132.     //wrReg(0x11,32);//using scaler for divider
  133.     wrReg(REG_RGB444, 0x00);             // Disable RGB444
  134.     wrReg(REG_COM11,226);//enable nigh mode 1/8 frame rate COM11*/
  135.     wrReg(REG_TSLB,0x04);                // 0D = UYVY  04 = YUYV    
  136.     wrReg(REG_COM13,0x88);               // connect to REG_TSLB
  137.     //wrReg(REG_COM13,0x8);               // connect to REG_TSLB disable gamma
  138.     #ifdef rgb565
  139.     wrReg(REG_COM7, 0x04);           // RGB + color bar disable
  140.         wrReg(REG_COM15, 0xD0);          // Set rgb565 with Full range    0xD0
  141.     #else
  142.     wrReg(REG_COM7, 0x00);           // YUV
  143.         wrReg(REG_COM17, 0x00);          // color bar disable
  144.     wrReg(REG_COM15, 0xC0);          // Set normal rgb with Full range
  145.     #endif
  146.         //wrReg(REG_COM3, 0x04);
  147.     #ifdef qqvga || ifdef qvga
  148.     wrReg(REG_COM3,4);    // REG_COM3
  149.     #else
  150.     wrReg(REG_COM3,0);    // REG_COM3
  151.     #endif
  152.         //wrReg(0x3e,0x00);        //  REG_COM14
  153.      //   wrReg(0x72,0x11);        //
  154.        // wrReg(0x73,0xf0);        //
  155.     #ifdef qqvga
  156.     wrReg(REG_COM14, 0x1a);          // divide by 4
  157.         wrReg(0x72, 0x22);               // downsample by 4
  158.         wrReg(0x73, 0xf2);               // divide by 4
  159.         wrReg(REG_HSTART,0x16);
  160.         wrReg(REG_HSTOP,0x04);
  161.         wrReg(REG_HREF,0xa4);          
  162.         wrReg(REG_VSTART,0x02);
  163.         wrReg(REG_VSTOP,0x7a);
  164.         wrReg(REG_VREF,0x0a);
  165.     #endif
  166.     #ifdef qvga
  167.     wrReg(REG_COM14, 0x19);        
  168.         wrReg(0x72, 0x11);
  169.         wrReg(0x73, 0xf1);
  170.         wrReg(REG_HSTART,0x16);
  171.         wrReg(REG_HSTOP,0x04);
  172.         wrReg(REG_HREF,0x24);            
  173.         wrReg(REG_VSTART,0x02);
  174.         wrReg(REG_VSTOP,0x7a);
  175.         wrReg(REG_VREF,0x0a);
  176.     #else
  177.         wrReg(0x32,0xb6);        // HREF  
  178.         wrReg(0x17,0x13);        // HSTART
  179.         wrReg(0x18,0x01);        // HSTOP
  180.         wrReg(0x19,0x02);        // VSTART
  181.         wrReg(0x1a,0x7a);        // VSTOP
  182.         wrReg(0x03,0x0a);        // VREF
  183.     #endif
  184.        // wrReg(0x70, 0x3a);       // Scaling Xsc
  185.         //wrReg(0x71, 0x35);       // Scaling Ysc
  186.         //wrReg(0xA2, 0x02);       // pixel clock delay
  187.  
  188.         wrReg(REG_COM1, 0x00);
  189.   // COLOR SETTING
  190.          
  191.     wrReg(REG_COM8,0x8F);        // AGC AWB AEC Unlimited step size
  192.     wrReg(0xAA,0x14);            // Average-based AEC algorithm
  193.     wrReg(REG_BRIGHT,0x00);      // 0x00(Brightness 0) - 0x18(Brightness +1) - 0x98(Brightness -1)
  194.     wrReg(REG_CONTRAS,0x40);     // 0x40(Contrast 0) - 0x50(Contrast +1) - 0x38(Contrast -1)
  195.    // wrReg(0xB1,0xB1);            // Automatic Black level Calibration
  196.     wrReg(0xB1,4);//really enable auto black level calibration
  197.     wrReg(MTX1,0x80);        
  198.     wrReg(MTX2,0x80);      
  199.     wrReg(MTX3,0x00);        
  200.     wrReg(MTX4,0x22);        
  201.     wrReg(MTX5,0x5e);        
  202.     wrReg(MTX6,0x80);        
  203.     wrReg(MTXS,0x9e);        
  204.     wrReg(AWBC7,0x88);
  205.     wrReg(AWBC8,0x88);
  206.     wrReg(AWBC9,0x44);
  207.     wrReg(AWBC10,0x67);
  208.     wrReg(AWBC11,0x49);
  209.     wrReg(AWBC12,0x0e);
  210.     wrReg(REG_GFIX,0x00);
  211.     //wrReg(GGAIN,0x40);
  212.     wrReg(AWBCTR3,0x0a);
  213.     wrReg(AWBCTR2,0x55);
  214.     wrReg(AWBCTR1,0x11);
  215.     wrReg(AWBCTR0,0x9f);
  216.  
  217.     wrReg(0xb0,0x84);//not sure what this does
  218.     wrReg(REG_COM16,COM16_AWBGAIN);//disable auto denoise and edge enhancment
  219.     //wrReg(REG_COM16,0);
  220.     wrReg(0x4C,0);//disable denoise
  221.     wrReg(0x76,0);//disable denoise
  222.     wrReg(0x77,0);//disable denoise
  223.     wrReg(0x7B,4);//brighten up shadows a bit end point 4
  224.     wrReg(0x7C,8);//brighten up shadows a bit end point 8
  225.     //wrReg(0x88,238);//darken highligts end point 176
  226.     //wrReg(0x89,211);//try to get more highlight detail
  227.     //wrReg(0x7A,60);//slope
  228.     //wrReg(0x26,0xB4);//lower maxium stable operating range for AEC
  229.     //hueSatMatrix(0,100);
  230.     //ov7670_store_cmatrix();
  231.     //wrReg(0x20,12);//set ADC range to 1.5x
  232.     wrReg(REG_COM9,0x6A);//max gain to 128x
  233.     #ifdef qvga
  234.     wrReg(0x11,4);//using scaler for divider
  235.     #else
  236.     wrReg(0x11,9);//using scaler for divider
  237.     #endif
  238.         //cout << pstr("Free RAM: ") << FreeRam() << endl;
  239.         //wrReg(0x31,0xFF);//HSYNC delay
  240.     //wrReg(0x2A, 12);
  241.         delay(100);//i2c uses interupts to write data wait for all bytes to be written
  242.     cli();//disable interupts
  243.        
  244.         useSdCard;
  245.         #ifndef useLed
  246.     PgmPrintln("About to Start SD card");
  247.     #else
  248.     PORTD|=1;//turn on led
  249.     #endif
  250.         if (!sd.begin(chipSelect, SPI_FULL_SPEED))
  251.       sd.initErrorHalt("Error starting Sd card");
  252.     //enable spi
  253.     SPCR=80;//spi enable master
  254.     SPSR=1;//double speed
  255.   /*PgmPrintln("------sd1 root-------");
  256.  
  257.   sd.ls();*/
  258.       /*    //enable spi
  259.     SPCR=80;//spi enable master
  260.     SPSR=1;//double speed*/
  261.     spiCSt();
  262.     spiWrB(1);
  263.     spiWrB(64);//sequental mode
  264.     spiCSt();
  265.     spiWrB(2);//sequental write mode
  266.     spiWrB(0);//24 bit address
  267.     spiWrB(0);
  268.     spiWrB(0);
  269.        // useSDcard();
  270.        #ifndef useLed
  271.           PgmPrintln("Ready to Begin");
  272.       #endif
  273.         //sd.ls();
  274.         //uint8_t* buf = (uint8_t*)sd.vol()->cacheClear();
  275.         //while (1){}
  276. }
  277. void loop()
  278. {
  279.   /* Note: One frame is broken down into 5 smaller captures this allows for a whole frame to be captured at 640x480 which is nice
  280.   however it could mean that there could be slight differences between each frame capture
  281.   I think the improvment of resolution outweights the cons of the slight changes per frame if any
  282.   The image will take about 15 seconds to reach the computer per frame*/
  283.   static uint16_t countFrames;
  284.   #ifndef useLed
  285.   PgmPrintln("Send One key to start image save");
  286.   RdSerial();
  287.  
  288.   for (uint16_t z=0;z<4;z++)
  289.   {
  290.       #else//do not use loop when using led
  291.       if ((PIND&2)==0)//is the button on pin 1 pressed?
  292.       {
  293.        //if so blink led quickly
  294.        sei();
  295.        while (1)
  296.        {
  297.         PORTD^=1;
  298.         delay(50);
  299.        }
  300.       }
  301.       else
  302.         PORTD&=~1;//led on when writing to files off when taking picture
  303.       #endif
  304.   //memset(buf,0,sizeof(buf));
  305.   sprintf_P((char *)buf2,PSTR("frame%d.yuv"),countFrames);
  306.   #ifndef useLed
  307.   PgmPrintln("Saving to:");
  308.   StringRam((char *)buf2);
  309.   serialWrB('\n');
  310.   #else
  311.   PORTD|=1;
  312.   #endif
  313.   sd.remove((char*)buf2);
  314.     // create a contiguous file
  315.   if (!file.createContiguous(sd.vwd(),(char *) buf2, 614400)) {
  316.     error("createContiguous failed");
  317.   }
  318.   #ifndef useLed
  319.   PgmPrintln("File created");
  320.   #endif
  321.   // get the location of the file's blocks
  322.  
  323.   if (!file.contiguousRange(&bgnBlock, &endBlock)) {
  324.     error("contiguousRange failed");
  325.   }
  326.     // tell card to setup for multiple block write with pre-erase
  327.  
  328.   if (!sd.card()->erase(bgnBlock, endBlock)) error("card.erase failed");
  329.   if (!sd.card()->writeStart(bgnBlock, 1200)) {
  330.     error("writeStart failed");
  331.   }//1200 is the block count filesize/512
  332.     // PgmPrintln("File prepared");
  333.   //PgmPrintln("About to start Image Capture");
  334.   captureImg(0,0,1280,96);//each pixel is 2 bytes so 1280 instead of 640 for width
  335.   //sendRam(1280,96);
  336.   //fixPat();
  337.   saveImg(240);
  338.   captureImg(1280,96,1280,96);
  339.   //sendRam(1280,96);
  340.   saveImg(240);
  341.   captureImg(1280,192,1280,96);
  342.   //sendRam(1280,96);
  343.   saveImg(240);
  344.   captureImg(1280,288,1280,96);
  345.   //sendRam(1280,96);
  346.   saveImg(240);
  347.   captureImg(1280,384,1280,96);
  348.   //sendRam(1280,96);
  349.   saveImg(240);
  350.  
  351.   /*file.sync();
  352.   file.rewind();
  353.   readImgSerial(1280,480);*/
  354.   if (!sd.card()->writeStop()) error("writeStop failed");
  355.   if (file.close() == 0) {
  356.     error("File close failed");
  357.   }
  358.   countFrames++;
  359.     #ifndef useLed
  360.       PgmPrintln("File closed");
  361.   }
  362.   #endif
  363.  
  364. }
  365. void fixPat(void)
  366. {
  367.     spiCSt();
  368.     spiWrB(2);//sequental write mode
  369.     spiWrB(0);//24 bit address
  370.     spiWrB(0);
  371.     spiWrB(0);
  372.     uint32_t x;
  373.     uint8_t y=0;
  374.  
  375.     for (x=0;x<122880;x++)
  376.     {
  377.         SPDR=y;
  378.         y++;
  379.         while(!(SPSR & (1<<SPIF))) {}
  380.     }
  381. }
  382. byte wrReg(int regID, int regDat)
  383. {
  384.      //   Serial.println("Started");  
  385.     Wire.beginTransmission(sensor_addr >> 1);
  386.      //    Serial.println("Began");  
  387.     Wire.write(regID & 0x00FF);    
  388.     Wire.write(regDat & 0x00FF);    
  389.     if(Wire.endTransmission())
  390.     {
  391.         return 0;
  392.                 PORTB|=32;
  393.                 while (1) {}
  394.     }
  395.    //     Serial.println("Done!");
  396.     delay(1);
  397.     return(1);
  398. }
  399.  
  400. void readImgSerial(uint16_t w,uint16_t h)
  401. {
  402.     //_delay_ms(2000);
  403.     uint16_t wl,hl;
  404.     for (hl=0;hl<h;hl++)
  405.     {
  406.         StringPgm(PSTR("RDY"));
  407.         for (wl=0;wl<w;wl++)
  408.         {
  409.             while ( !( UCSR0A & (1<<UDRE0)) ) {} //wait for byte to transmit
  410.             //SPDR=0;//send dummy value to get byte back
  411.             //while(!(SPSR & (1<<SPIF))) {}
  412.             UDR0=file.read();
  413.         }
  414.     }
  415.     while ( !( UCSR0A & (1<<UDRE0)) ) {} //wait for byte to transmit
  416. }
  417.  
  418. byte rdSensorReg8_8(uint8_t regID, uint8_t* regDat)
  419. {
  420.     Wire.beginTransmission(sensor_addr >> 1);
  421.     Wire.write(regID & 0x00FF);    
  422.     Wire.endTransmission();
  423.     Wire.requestFrom((sensor_addr >> 1),1);
  424.     if(Wire.available())
  425.         *regDat = Wire.read();  
  426.     delay(1);
  427.     return(1);
  428. }
  429. void captureImg(uint16_t ws,uint16_t hs,uint16_t wg,uint8_t hg)
  430. {//TODO: speed up this loop it misses one byte partway through the first line
  431. //right now I am correcting this in the image converter software it is just one byte and not very
  432. //noticable but it would still be nice to fix in my opion I do not have this issue when this loop is compiled with avr-gcc with a setting of -O2
  433.   #ifdef useLed
  434.   PORTD&=~1;//turn off led taking picture
  435.   #endif
  436.     uint16_t ls2,lg2;
  437.     //skip 1 multiplies skip 2 same with get1 and get2
  438.     //first wait for vsync it is on pin 3 (counting from 0) portD
  439.     //start spi ram
  440.   cli();//make sure interupts are off
  441.     spiCSt();
  442.     spiWrB(2);//sequental write mode
  443.     spiWrB(0);//24 bit address
  444.     spiWrB(0);
  445.     spiWrB(0);
  446.     while (!(PIND&8)) {}//wait for high
  447.     while ((PIND&8)) {}//wait for low
  448.     if (hs != 0)
  449.     {
  450.         //for (ls1=0;ls1<hs;ls1++)
  451.         while (hs--)
  452.         {
  453.             //for (ls2=0;ls2<ws;ls2++)
  454.             ls2=ws;
  455.             while (ls2--)
  456.             {
  457.                 while (!(PIND&4)) {}//wait for high
  458.                 while ((PIND&4)) {}//wait for low
  459.             }
  460.         }// while (--hs);
  461.     }
  462.     //for (lg1=0;lg1<hg;lg1++)
  463.     while ((uint8_t)hg--)
  464.     {
  465.         //for (lg2=0;lg2<wg;lg2++)
  466.         lg2=wg;
  467.         while (lg2--)
  468.         {
  469.             while (!(PIND&4)) {}//wait for high
  470.             SPDR=(uint8_t)(PINC&15)|(PIND&240);
  471.             while ((PIND&4)) {}//wait for low
  472.         }
  473.     }
  474. }
  475. void sendRam(uint16_t w,uint16_t h)
  476. {
  477.     spiCSt();
  478.     spiWrB(3);//sequental read mode
  479.     spiWrB(0);//24 bit address
  480.     spiWrB(0);
  481.     spiWrB(0);
  482.     //_delay_ms(2000);
  483.     uint16_t wl,hl;
  484.     for (hl=0;hl<h;hl++)
  485.     {
  486.         StringPgm(PSTR("RDY"));
  487.         for (wl=0;wl<w;wl++)
  488.         {
  489.             while ( !( UCSR0A & (1<<UDRE0)) ) {} //wait for byte to transmit
  490.             SPDR=0;//send dummy value to get byte back
  491.             while(!(SPSR & (1<<SPIF))) {}
  492.             UDR0=SPDR;
  493.         }
  494.     }
  495.     while ( !( UCSR0A & (1<<UDRE0)) ) {} //wait for byte to transmit
  496.     //StringPgm(PSTR("RDY"));
  497.     //_delay_ms(10);
  498.     //UDR0=0xFF;
  499. }
  500. void spiCSt(void)
  501. {
  502.     //toggles spi CS used for reseting sram
  503.     PORTB|=6;//cs high
  504.     //_delay_ms(1);
  505.     PORTB&=~4;//cs low
  506. }
  507. void spiWrB(uint8_t dat)
  508. {
  509.     SPDR = dat;
  510.     // Wait for transmission complete
  511.     while(!(SPSR & (1<<SPIF))) {}
  512. }
  513. uint8_t RdSerial(void)
  514. {
  515.     /* Wait for data to be received */
  516.     while ( !(UCSR0A & (1<<RXC0)) );
  517.     /* Get and return received data from buffer */
  518.     return UDR0;
  519. }
  520. void saveImg(uint16_t sizeSect)
  521. {
  522.   //size is size in bytes/512 so for 1280x96 bytes it is 240
  523.   //PgmPrintln("Saving part");
  524.     #ifdef useLed
  525.   PORTD|=1;//turn on led saving picture
  526.   #endif
  527.   uint32_t address=0;
  528.   //PgmPrintln("About to Save Image");
  529.   for (uint16_t x=0;x<sizeSect;x++)
  530.   {
  531.   spiCSt();
  532.   spiWrB(3);//sequental read mode
  533.   sramAddress(address);
  534.   for (uint16_t y=0;y<512;y++)
  535.   {
  536.     SPDR=0;//send dummy value to get byte back
  537.     while(!(SPSR & (1<<SPIF))) {}
  538.     buf2[y]=SPDR;
  539.   }
  540.   useSdCard;
  541.   if (!sd.card()->writeData(buf2)) error("writeData failed");
  542.   address+=sizeof(buf2);
  543.   //PgmPrintln("Done B");
  544.   }
  545.   //PgmPrintln("Done");
  546. }
  547. void sramAddress(uint32_t address)
  548. {
  549.   spiWrB((uint8_t)(address >> 16) & 0xff);
  550.   spiWrB((uint8_t)(address >> 8) & 0xff);
  551.   spiWrB((uint8_t)address);
  552. }
  553. void serialWrB(uint8_t dat)
  554. {
  555.         while ( !( UCSR0A & (1<<UDRE0)) ) {}
  556.     UDR0=dat;
  557.     while ( !( UCSR0A & (1<<UDRE0)) ) {} //wait for byte to transmit
  558. }
  559. void StringPgm(char * str)
  560. {
  561.     do {
  562.         serialWrB(pgm_read_byte_near(str));
  563.     } while(pgm_read_byte_near(++str));
  564. }
  565. void StringRam(char * str)
  566. {
  567.     do {
  568.         serialWrB(*str);
  569.     } while(*++str);
  570. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement