Hello everyone.. i am trying to take picture via this camera ( https://s3.amazonaws.com/linksprite/camera/JPEG_camera_uartinterface_TTL/JPEG_Camera_2M_Manual.pdf /) and save it on SD card using Teensy 3.1.
when i used the code on arduino it was working perfectly but there were some speed issues so i switched to teensy. i don't know what problem is coming but the image is not opening..the image viewer says "invalid image". i checked the image file..it has acknowledgement numbers "76 00 32" etc. which gets deleted automatically in arduino. i tried using level shifter but no success.
the code is given below.. Please help/guide me...
Thank you
// * SD card attached to SPI bus as follows:
// * MOSI - pin 11
// * MISO - pin 12
// * CLK - pin 13
// * CS - pin 4
//*******************************************************
//#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
//SoftwareSerial Serial3(5,6); // Set Arduino pin 4 and 5 as softserial
byte ZERO = 0x00;
byte incomingbyte;
long int j=0,k=0,count=0,i=0x0000;
uint8_t MH,ML;
boolean EndFlag=0;
File myFile;
void SendResetCmd()
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x26);
Serial3.write(ZERO);
}
/*************************************/
/* Set ImageSize :
/* <1> 0x22 : 160*120
/* <2> 0x11 : 320*240
/* <3> 0x00 : 640*480
/* <4> 0x1D : 800*600
/* <5> 0x1C : 1024*768
/* <6> 0x1B : 1280*960
/* <7> 0x21 : 1600*1200
/************************************/
void SetImageSizeCmd(byte Size)
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x54);
Serial3.write(0x01);
Serial3.write(Size);
}
/*************************************/
/* Set BaudRate :
/* <1> 0xAE : 9600
/* <2> 0x2A : 38400
/* <3> 0x1C : 57600
/* <4> 0x0D : 115200
/* <5> 0xAE : 128000
/* <6> 0x56 : 256000
/*************************************/
void SetBaudRateCmd(byte baudrate)
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x24);
Serial3.write(0x03);
Serial3.write(0x01);
Serial3.write(baudrate);
}
void SendTakePhotoCmd()
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x36);
Serial3.write(0x01);
Serial3.write(ZERO);
}
void SendReadDataCmd()
{
MH=i/0x100;
ML=i%0x100;
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x32);
Serial3.write(0x0c);
Serial3.write(ZERO);
Serial3.write(0x0a);
Serial3.write(ZERO);
Serial3.write(ZERO);
Serial3.write(MH);
Serial3.write(ML);
Serial3.write(ZERO);
Serial3.write(ZERO);
Serial3.write(ZERO);
Serial3.write(0x20);
Serial3.write(ZERO);
Serial3.write(0x0a);
i+=0x20;
}
void StopTakePhotoCmd()
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x36);
Serial3.write(0x01);
Serial3.write(0x03);
}
#include <SdFat.h>
SdFat sd;
SdFile myFile;
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
delay(400); // catch Due reset problem
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED))
{
sd.initErrorHalt();
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Serial.println("please wait....");
}
void loop()
{
byte a[32];
int ii;
Serial3.begin(115200);
delay(200);
SendResetCmd();//Wait 2-3 second to send take picture command
delay(2000);
SetBaudRateCmd(0xAE);
delay(100);
Serial3.begin(9600);
delay(100);
SetImageSizeCmd(0x00);
delay(100);
SendTakePhotoCmd();
delay(3000);
while(Serial3.available()>0)
{
incomingbyte=Serial3.read();
}
// open the file for write at end like the Native SD library
if (!myFile.open("pic.jpg", O_RDWR | O_CREAT | O_AT_END))
{
sd.errorHalt("opening pic.jpg for write failed");
}
// if the file opened okay, write to it:
else
{
Serial.print("Writing to pic.jpg...");CHK WHAT GOES WHERE
myFile = SD.open("pic.jpg", FILE_WRITE); //DOUBTFUL LINE
while(!EndFlag)
{
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(5);
while(Serial3.available()>0)
{
incomingbyte=Serial3.read();
k++;
delayMicroseconds(100);
if((k>5)&&(j<32)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-1]==0xFF)&&(a[j]==0xD9)) //tell if the picture is finished
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++)
{
if(a[j]<0x10)
Serial.print("0");
Serial.print(a[j],HEX); // observe the image through serial port
Serial.print(" ");
}
for(ii=0; ii<count; ii++)
myFile.write(a[ii]);
Serial.println("");
}
}
myFile.close();
Serial.print("Finished writing data to file");
while(1);
}
when i used the code on arduino it was working perfectly but there were some speed issues so i switched to teensy. i don't know what problem is coming but the image is not opening..the image viewer says "invalid image". i checked the image file..it has acknowledgement numbers "76 00 32" etc. which gets deleted automatically in arduino. i tried using level shifter but no success.
the code is given below.. Please help/guide me...
Thank you
// * SD card attached to SPI bus as follows:
// * MOSI - pin 11
// * MISO - pin 12
// * CLK - pin 13
// * CS - pin 4
//*******************************************************
//#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
//SoftwareSerial Serial3(5,6); // Set Arduino pin 4 and 5 as softserial
byte ZERO = 0x00;
byte incomingbyte;
long int j=0,k=0,count=0,i=0x0000;
uint8_t MH,ML;
boolean EndFlag=0;
File myFile;
void SendResetCmd()
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x26);
Serial3.write(ZERO);
}
/*************************************/
/* Set ImageSize :
/* <1> 0x22 : 160*120
/* <2> 0x11 : 320*240
/* <3> 0x00 : 640*480
/* <4> 0x1D : 800*600
/* <5> 0x1C : 1024*768
/* <6> 0x1B : 1280*960
/* <7> 0x21 : 1600*1200
/************************************/
void SetImageSizeCmd(byte Size)
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x54);
Serial3.write(0x01);
Serial3.write(Size);
}
/*************************************/
/* Set BaudRate :
/* <1> 0xAE : 9600
/* <2> 0x2A : 38400
/* <3> 0x1C : 57600
/* <4> 0x0D : 115200
/* <5> 0xAE : 128000
/* <6> 0x56 : 256000
/*************************************/
void SetBaudRateCmd(byte baudrate)
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x24);
Serial3.write(0x03);
Serial3.write(0x01);
Serial3.write(baudrate);
}
void SendTakePhotoCmd()
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x36);
Serial3.write(0x01);
Serial3.write(ZERO);
}
void SendReadDataCmd()
{
MH=i/0x100;
ML=i%0x100;
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x32);
Serial3.write(0x0c);
Serial3.write(ZERO);
Serial3.write(0x0a);
Serial3.write(ZERO);
Serial3.write(ZERO);
Serial3.write(MH);
Serial3.write(ML);
Serial3.write(ZERO);
Serial3.write(ZERO);
Serial3.write(ZERO);
Serial3.write(0x20);
Serial3.write(ZERO);
Serial3.write(0x0a);
i+=0x20;
}
void StopTakePhotoCmd()
{
Serial3.write(0x56);
Serial3.write(ZERO);
Serial3.write(0x36);
Serial3.write(0x01);
Serial3.write(0x03);
}
#include <SdFat.h>
SdFat sd;
SdFile myFile;
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
delay(400); // catch Due reset problem
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED))
{
sd.initErrorHalt();
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Serial.println("please wait....");
}
void loop()
{
byte a[32];
int ii;
Serial3.begin(115200);
delay(200);
SendResetCmd();//Wait 2-3 second to send take picture command
delay(2000);
SetBaudRateCmd(0xAE);
delay(100);
Serial3.begin(9600);
delay(100);
SetImageSizeCmd(0x00);
delay(100);
SendTakePhotoCmd();
delay(3000);
while(Serial3.available()>0)
{
incomingbyte=Serial3.read();
}
// open the file for write at end like the Native SD library
if (!myFile.open("pic.jpg", O_RDWR | O_CREAT | O_AT_END))
{
sd.errorHalt("opening pic.jpg for write failed");
}
// if the file opened okay, write to it:
else
{
Serial.print("Writing to pic.jpg...");CHK WHAT GOES WHERE
myFile = SD.open("pic.jpg", FILE_WRITE); //DOUBTFUL LINE
while(!EndFlag)
{
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(5);
while(Serial3.available()>0)
{
incomingbyte=Serial3.read();
k++;
delayMicroseconds(100);
if((k>5)&&(j<32)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-1]==0xFF)&&(a[j]==0xD9)) //tell if the picture is finished
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++)
{
if(a[j]<0x10)
Serial.print("0");
Serial.print(a[j],HEX); // observe the image through serial port
Serial.print(" ");
}
for(ii=0; ii<count; ii++)
myFile.write(a[ii]);
Serial.println("");
}
}
myFile.close();
Serial.print("Finished writing data to file");
while(1);
}