I already check most of the topic about ttl camera with arduino in this forum..
but I still can not find out how to deal with this big trouble,I use the board MEGA2560,amd my code is as follow:
#include <SdFat.h>
const uint8_t chipSelect = SS;
SdFat sd;
SdFile myFile;
byte incomingbyte;
int a=0x0000,j=0,k=0,count=0; //Read Starting address
uint8_t MH,ML;
boolean EndFlag=0;
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
void changebaudrate();
ArduinoOutStream cout(Serial);
#define error(s) sd.errorHalt_P(PSTR(s))
void setup()
{
Serial.begin(19200);
Serial1.begin(38400);
pinMode(3, OUTPUT);
if (!sd.begin(3, SPI_HALF_SPEED)) sd.initErrorHalt();
if (!myFile.open("test16.jpg", O_WRITE | O_CREAT | O_AT_END)) {
sd.errorHalt("opening test.txt for write failed");
}
}
void loop()
{
SendResetCmd();
delay(3000);
// changebaudrate();
// delay(200);
// Serial1.end();
// delay(200);
// Serial1.begin(9600);
// delay(200);
byte a[32];
SendTakePhotoCmd();
delay(30);
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
}
// delay(30);
while(!EndFlag)
{
delay(15);
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(30);
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
k++;
if((k>5)&&(j<32)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-1]==0xFF)&&(a[j]==0xD9)) //Check if the picture is over
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++)
{ if(a[j]<0x10)
Serial.print("0");
Serial.print(a[j],HEX);
myFile.write(a[j]);
Serial.print(" ");
} //Send jpeg picture over the serial port
Serial.println();
}
myFile.close();
while(1);
}
//Send Reset command
void SendResetCmd()
{
Serial1.write(0x56);
Serial1.write(byte(0x00));
Serial1.write(0x26);
Serial1.write(byte(0x00));
}
//Send take picture command
void SendTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(byte(0x00));
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(byte(0x00));
}
//Read data
void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
Serial1.write(0x56);
Serial1.write(byte(0x00));
Serial1.write(0x32);
Serial1.write(0x0c);
Serial1.write(byte(0x00));
Serial1.write(0x0a);
Serial1.write(byte(0x00));
Serial1.write(byte(0x00));
Serial1.write(MH);
Serial1.write(ML);
Serial1.write(byte(0x00));
Serial1.write(byte(0x00));
Serial1.write(byte(0x00));
Serial1.write(0x20);
Serial1.write(byte(0x00));
Serial1.write(0x0a);
a+=0x20; //address increases 32??set according to buffer size
}
void StopTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(byte(0x00));
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(0x03);
}
void changebaudrate()
{
Serial1.write(0x56);
Serial1.write(byte(0x00));
Serial1.write(0x24);
Serial1.write(0x03);
Serial1.write(0x01);
Serial1.write(0xae);
Serial1.write(0xc8);
}
my photo just like the attachment,it is always have lots of broken parts in it..
Can anyone help my please?Thanks a lot!
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.