I made Digital Camera using Arducam & WIZwiki-W7500

Dependencies:   Arduino Arducam_OV5642 SDFileSystem Arducam_UTFT_SPI WIZnetInterface_Ricky mbed

Committer:
justinkim
Date:
Thu Oct 29 06:27:02 2015 +0000
Revision:
0:e01f64037748
Child:
1:3a14a4c84db2
spi tes failed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 0:e01f64037748 1 #include "mbed.h"
justinkim 0:e01f64037748 2
justinkim 0:e01f64037748 3 #include "UTFT_SPI.h"
justinkim 0:e01f64037748 4 #include "OV5642.h"
justinkim 0:e01f64037748 5 #include "OV5642_regs.h"
justinkim 0:e01f64037748 6 #include "SDFileSystem.h"
justinkim 0:e01f64037748 7 #include "Arduino.h"
justinkim 0:e01f64037748 8
justinkim 0:e01f64037748 9 void setup();
justinkim 0:e01f64037748 10 void loop();
justinkim 0:e01f64037748 11 void GrabImage(char* str);
justinkim 0:e01f64037748 12 void dispBitmap();
justinkim 0:e01f64037748 13 void Playback();
justinkim 0:e01f64037748 14 void dispBitmap(FILE* fname);
justinkim 0:e01f64037748 15 /* itoa: convert n to characters in s */
justinkim 0:e01f64037748 16 void itoa( unsigned long long int value, char *str)
justinkim 0:e01f64037748 17 {
justinkim 0:e01f64037748 18 int i,j;
justinkim 0:e01f64037748 19 char temp[30];
justinkim 0:e01f64037748 20 for(i=0; value > 0; i++){
justinkim 0:e01f64037748 21 str[i] = value%10+'0';
justinkim 0:e01f64037748 22 value=value/10;
justinkim 0:e01f64037748 23 }
justinkim 0:e01f64037748 24 for(j=0;i>=0;j++,i--){
justinkim 0:e01f64037748 25 temp[j]=str[i-1];
justinkim 0:e01f64037748 26 }
justinkim 0:e01f64037748 27 for(i=0;i<j;i++){
justinkim 0:e01f64037748 28 str[i]=temp[i];
justinkim 0:e01f64037748 29 }
justinkim 0:e01f64037748 30 }
justinkim 0:e01f64037748 31
justinkim 0:e01f64037748 32 #define BMPIMAGEOFFSET 66
justinkim 0:e01f64037748 33 static FILE *outFile;
justinkim 0:e01f64037748 34 static FILE *inFile;
justinkim 0:e01f64037748 35
justinkim 0:e01f64037748 36 const char bmp_header[BMPIMAGEOFFSET] =
justinkim 0:e01f64037748 37 {
justinkim 0:e01f64037748 38 0x42, 0x4D, 0x36, 0x58, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x28, 0x00,
justinkim 0:e01f64037748 39 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x03, 0x00,
justinkim 0:e01f64037748 40 0x00, 0x00, 0x00, 0x58, 0x02, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0x00, 0x00,
justinkim 0:e01f64037748 41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0xE0, 0x07, 0x00, 0x00, 0x1F, 0x00,
justinkim 0:e01f64037748 42 0x00, 0x00
justinkim 0:e01f64037748 43 };
justinkim 0:e01f64037748 44
justinkim 0:e01f64037748 45 ArduCAM myCAM(D11, D12, D13, D10, D14, D15);
justinkim 0:e01f64037748 46 //ArduLCD myGLCD(D11, D12, D13, D10);
justinkim 0:e01f64037748 47 SDFileSystem SD(PB_3, PB_2, PB_1, PB_0, "sd");
justinkim 0:e01f64037748 48 Serial pc(USBTX, USBRX);
justinkim 0:e01f64037748 49 bool isShowFlag = true;
justinkim 0:e01f64037748 50
justinkim 0:e01f64037748 51 int main()
justinkim 0:e01f64037748 52 {
justinkim 0:e01f64037748 53 //*(volatile uint32_t *)(0x41001014) = 0x0060100; //clock 48MHz
justinkim 0:e01f64037748 54
justinkim 0:e01f64037748 55 setup();
justinkim 0:e01f64037748 56
justinkim 0:e01f64037748 57 while(1)
justinkim 0:e01f64037748 58 {
justinkim 0:e01f64037748 59 loop();
justinkim 0:e01f64037748 60 }
justinkim 0:e01f64037748 61 }
justinkim 0:e01f64037748 62
justinkim 0:e01f64037748 63 void setup()
justinkim 0:e01f64037748 64 {
justinkim 0:e01f64037748 65 uint8_t vid,pid;
justinkim 0:e01f64037748 66 uint8_t temp;
justinkim 0:e01f64037748 67
justinkim 0:e01f64037748 68 pc.baud(115200);
justinkim 0:e01f64037748 69 pc.printf("ArduCAM Start!\r\n");
justinkim 0:e01f64037748 70
justinkim 0:e01f64037748 71 //Check if the ArduCAM SPI bus is OK
justinkim 0:e01f64037748 72 myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
justinkim 0:e01f64037748 73 temp = myCAM.read_reg(ARDUCHIP_TEST1);
justinkim 0:e01f64037748 74 if(temp != 0x55)
justinkim 0:e01f64037748 75 {
justinkim 0:e01f64037748 76 pc.printf("SPI interface Error!\r\n");
justinkim 0:e01f64037748 77 while(1);
justinkim 0:e01f64037748 78 }
justinkim 0:e01f64037748 79
justinkim 0:e01f64037748 80 //Change MCU mode
justinkim 0:e01f64037748 81 myCAM.set_mode(MCU2LCD_MODE);
justinkim 0:e01f64037748 82
justinkim 0:e01f64037748 83 //Initialize the LCD Module
justinkim 0:e01f64037748 84 //myGLCD.InitLCD();
justinkim 0:e01f64037748 85
justinkim 0:e01f64037748 86 //Check if the camera module type is OV5642
justinkim 0:e01f64037748 87 myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid);
justinkim 0:e01f64037748 88 myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid);
justinkim 0:e01f64037748 89 if((vid != 0x56) || (pid != 0x42))
justinkim 0:e01f64037748 90 pc.printf("Can't find OV5642 module!\r\n");
justinkim 0:e01f64037748 91 else
justinkim 0:e01f64037748 92 pc.printf("OV5642 detected\r\n");
justinkim 0:e01f64037748 93
justinkim 0:e01f64037748 94 myCAM.set_format(BMP);
justinkim 0:e01f64037748 95 myCAM.InitCAM();
justinkim 0:e01f64037748 96 }
justinkim 0:e01f64037748 97
justinkim 0:e01f64037748 98 void loop()
justinkim 0:e01f64037748 99 {
justinkim 0:e01f64037748 100 char str[8];
justinkim 0:e01f64037748 101 unsigned long previous_time = 0;
justinkim 0:e01f64037748 102 static int k = 0;
justinkim 0:e01f64037748 103 myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM
justinkim 0:e01f64037748 104
justinkim 0:e01f64037748 105 while(1)
justinkim 0:e01f64037748 106 {
justinkim 0:e01f64037748 107
justinkim 0:e01f64037748 108 if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)) //New Frame is coming
justinkim 0:e01f64037748 109 {
justinkim 0:e01f64037748 110 myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU
justinkim 0:e01f64037748 111 //myGLCD.resetXY();
justinkim 0:e01f64037748 112 myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM
justinkim 0:e01f64037748 113 while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)); //Wait for VSYNC is gone
justinkim 0:e01f64037748 114 }
justinkim 0:e01f64037748 115 else if(myCAM.get_bit(ARDUCHIP_TRIG,SHUTTER_MASK))
justinkim 0:e01f64037748 116 {
justinkim 0:e01f64037748 117 previous_time = millis();
justinkim 0:e01f64037748 118 while(myCAM.get_bit(ARDUCHIP_TRIG,SHUTTER_MASK))
justinkim 0:e01f64037748 119 {
justinkim 0:e01f64037748 120 if((millis() - previous_time) > 1500)
justinkim 0:e01f64037748 121 {
justinkim 0:e01f64037748 122 Playback();
justinkim 0:e01f64037748 123 }
justinkim 0:e01f64037748 124 }
justinkim 0:e01f64037748 125 if((millis() - previous_time) < 1500)
justinkim 0:e01f64037748 126 {
justinkim 0:e01f64037748 127 k = k + 1;
justinkim 0:e01f64037748 128 itoa(k, str);
justinkim 0:e01f64037748 129 strcat(str,".bmp"); //Generate file name
justinkim 0:e01f64037748 130 myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU, freeze the screen
justinkim 0:e01f64037748 131 GrabImage(str);
justinkim 0:e01f64037748 132 }
justinkim 0:e01f64037748 133 }
justinkim 0:e01f64037748 134 }
justinkim 0:e01f64037748 135 }
justinkim 0:e01f64037748 136
justinkim 0:e01f64037748 137
justinkim 0:e01f64037748 138 void GrabImage(char* str)
justinkim 0:e01f64037748 139 {
justinkim 0:e01f64037748 140 char VH,VL;
justinkim 0:e01f64037748 141 uint8_t buf[256];
justinkim 0:e01f64037748 142 static int k = 0;
justinkim 0:e01f64037748 143 int i,j = 0;
justinkim 0:e01f64037748 144 outFile = fopen("preview.jpg", "w");
justinkim 0:e01f64037748 145 if (!outFile)
justinkim 0:e01f64037748 146 {
justinkim 0:e01f64037748 147 pc.printf("Open File Error\r\n");
justinkim 0:e01f64037748 148 return;
justinkim 0:e01f64037748 149 }
justinkim 0:e01f64037748 150
justinkim 0:e01f64037748 151 //Switch to FIFO Mode
justinkim 0:e01f64037748 152 myCAM.write_reg(ARDUCHIP_TIM, MODE_MASK);
justinkim 0:e01f64037748 153 //Flush the FIFO
justinkim 0:e01f64037748 154 myCAM.flush_fifo();
justinkim 0:e01f64037748 155 //Start capture
justinkim 0:e01f64037748 156 myCAM.start_capture();
justinkim 0:e01f64037748 157 pc.printf("Start Capture\r\n");
justinkim 0:e01f64037748 158
justinkim 0:e01f64037748 159 //Polling the capture done flag
justinkim 0:e01f64037748 160 while(!myCAM.get_bit(ARDUCHIP_TRIG,CAP_DONE_MASK));
justinkim 0:e01f64037748 161 pc.printf("Capture Done!\r\n");
justinkim 0:e01f64037748 162
justinkim 0:e01f64037748 163 k = 0;
justinkim 0:e01f64037748 164 //Write the BMP header
justinkim 0:e01f64037748 165 for( i = 0; i < BMPIMAGEOFFSET; i++)
justinkim 0:e01f64037748 166 {
justinkim 0:e01f64037748 167 char ch = pgm_read_byte(&bmp_header[i]);
justinkim 0:e01f64037748 168 buf[k++] = ch;
justinkim 0:e01f64037748 169 }
justinkim 0:e01f64037748 170 fwrite(buf,256,1,outFile);
justinkim 0:e01f64037748 171 //Read the first dummy byte
justinkim 0:e01f64037748 172 //myCAM.read_fifo();
justinkim 0:e01f64037748 173
justinkim 0:e01f64037748 174 k = 0;
justinkim 0:e01f64037748 175 //Read 320x240x2 byte from FIFO
justinkim 0:e01f64037748 176 //Save as RGB565 bmp format
justinkim 0:e01f64037748 177 for(i = 0; i < 240; i++)
justinkim 0:e01f64037748 178 for(j = 0; j < 320; j++)
justinkim 0:e01f64037748 179 {
justinkim 0:e01f64037748 180 VH = myCAM.read_fifo();
justinkim 0:e01f64037748 181 VL = myCAM.read_fifo();
justinkim 0:e01f64037748 182 buf[k++] = VL;
justinkim 0:e01f64037748 183 buf[k++] = VH;
justinkim 0:e01f64037748 184 //Write image data to bufer if not full
justinkim 0:e01f64037748 185 if(k >= 256)
justinkim 0:e01f64037748 186 {
justinkim 0:e01f64037748 187 //Write 256 bytes image data to file from buffer
justinkim 0:e01f64037748 188 fwrite(buf,256,1,outFile);
justinkim 0:e01f64037748 189 k = 0;
justinkim 0:e01f64037748 190 }
justinkim 0:e01f64037748 191 }
justinkim 0:e01f64037748 192 //Close the file
justinkim 0:e01f64037748 193 fclose(outFile);
justinkim 0:e01f64037748 194
justinkim 0:e01f64037748 195 //Clear the capture done flag
justinkim 0:e01f64037748 196 myCAM.clear_fifo_flag();
justinkim 0:e01f64037748 197
justinkim 0:e01f64037748 198 //Switch to LCD Mode
justinkim 0:e01f64037748 199 myCAM.write_reg(ARDUCHIP_TIM, 0);
justinkim 0:e01f64037748 200 return;
justinkim 0:e01f64037748 201 }
justinkim 0:e01f64037748 202
justinkim 0:e01f64037748 203 void Playback()
justinkim 0:e01f64037748 204 {
justinkim 0:e01f64037748 205 char str[8];
justinkim 0:e01f64037748 206 int k = 0;
justinkim 0:e01f64037748 207 myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU
justinkim 0:e01f64037748 208 //myGLCD.InitLCD(PORTRAIT);
justinkim 0:e01f64037748 209
justinkim 0:e01f64037748 210 while(1)
justinkim 0:e01f64037748 211 {
justinkim 0:e01f64037748 212 k = k + 1;
justinkim 0:e01f64037748 213 itoa(k, str);
justinkim 0:e01f64037748 214 strcat(str,".bmp");
justinkim 0:e01f64037748 215 inFile = fopen("preview","r");
justinkim 0:e01f64037748 216 if (! inFile)
justinkim 0:e01f64037748 217 return;
justinkim 0:e01f64037748 218 //myGLCD.clrScr();
justinkim 0:e01f64037748 219 //myGLCD.resetXY();
justinkim 0:e01f64037748 220 dispBitmap(inFile);
justinkim 0:e01f64037748 221 fclose(inFile);
justinkim 0:e01f64037748 222 wait_ms(2000);
justinkim 0:e01f64037748 223 }
justinkim 0:e01f64037748 224 }
justinkim 0:e01f64037748 225
justinkim 0:e01f64037748 226 //Only support RGB565 bmp format
justinkim 0:e01f64037748 227 void dispBitmap(FILE* fname)
justinkim 0:e01f64037748 228 {
justinkim 0:e01f64037748 229 uint8_t buf[256];
justinkim 0:e01f64037748 230 char VH,VL;
justinkim 0:e01f64037748 231 int i,j = 0;
justinkim 0:e01f64037748 232 for(i = 0 ;i < BMPIMAGEOFFSET; i++)
justinkim 0:e01f64037748 233 fread(buf,256,1,fname);
justinkim 0:e01f64037748 234 for(i = 0; i < 320; i++)
justinkim 0:e01f64037748 235 for(j = 0; j < 240; j++)
justinkim 0:e01f64037748 236 {
justinkim 0:e01f64037748 237 VL = fread(buf,256,1,fname);
justinkim 0:e01f64037748 238 //Serial.write(VL);
justinkim 0:e01f64037748 239 VH = fread(buf,256,1,fname);
justinkim 0:e01f64037748 240 //Serial.write(VH);
justinkim 0:e01f64037748 241 //myGLCD.LCD_Write_DATA(VH,VL);
justinkim 0:e01f64037748 242 }
justinkim 0:e01f64037748 243 //myGLCD.clrXY();
justinkim 0:e01f64037748 244 }