hello everbody,
first of new arduino programing,
i using timer 3 interrupt on arduino due.
my code below,.
my problem when print (number of interrupt - number of readings) offset of 170 1ms period of time.
if reduce period , offset increases.
for further increase not constant. can understand why not constant small period,.
but why offset 1ms.
my idea read analog value fast , store in sd card.
my code below
#include <sd.h>
#include <duetimer.h>
#include <spi.h>
file myfile;
unsigned long mytime=0;
boolean read=false;
boolean save=false;
const int arraysize=512;
int numberofintrupt=0;
int numberofread=0;
int i=0;
int k=0;
int j=0;
boolean one=true;
int sensval=0;
byte evenarray[arraysize];
byte oddarray[arraysize];
boolean = false;
const int chipselect = 4;
void setup() {
serial.begin(250000); //buad rate
serial.print("initializing sd card...");
if (!sd.begin(4)) {
serial.println("initialization failed!");
return;
}
serial.println("initialization done.");
if(sd.exists("sensval.bin")){sd.remove("sensval.bin");}
myfile=sd.open("sensval.bin", file_write);
digitalwrite(13,low);
timer3.attachinterrupt(myisr);
timer3.start(1000);
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void myisr(){
numberofintrupt++;
read=true;
}
void analogread(){
numberofread++;
if(even){
sensval=analogread(0);
evenarray=sensval;
i++;
evenarray=sensval>>2;}
else {
sensval=analogread(0);
oddarray=sensval;
i++;
oddarray=sensval>>2;
}
i++;
if(i==512){
even=!even;
save=true;
j++;
i=0;
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void loop() {
if(read){
read=false;
analogread();
}
if(save){
savetocard();
save=false;
k++;
serial.println(numberofintrupt-numberofread);
// serial.println(j-k);
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void savetocard(){
if (even){
if(myfile){
myfile.write(oddarray,512);
}}
else{
if(myfile){
myfile.write(evenarray,512);
}}
myfile.write(0x0a);
myfile.write(0x0a);
if(k==5){
myfile.close();
digitalwrite(13,high);}
}
first of new arduino programing,
i using timer 3 interrupt on arduino due.
my code below,.
my problem when print (number of interrupt - number of readings) offset of 170 1ms period of time.
if reduce period , offset increases.
for further increase not constant. can understand why not constant small period,.
but why offset 1ms.
my idea read analog value fast , store in sd card.
my code below
#include <sd.h>
#include <duetimer.h>
#include <spi.h>
file myfile;
unsigned long mytime=0;
boolean read=false;
boolean save=false;
const int arraysize=512;
int numberofintrupt=0;
int numberofread=0;
int i=0;
int k=0;
int j=0;
boolean one=true;
int sensval=0;
byte evenarray[arraysize];
byte oddarray[arraysize];
boolean = false;
const int chipselect = 4;
void setup() {
serial.begin(250000); //buad rate
serial.print("initializing sd card...");
if (!sd.begin(4)) {
serial.println("initialization failed!");
return;
}
serial.println("initialization done.");
if(sd.exists("sensval.bin")){sd.remove("sensval.bin");}
myfile=sd.open("sensval.bin", file_write);
digitalwrite(13,low);
timer3.attachinterrupt(myisr);
timer3.start(1000);
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void myisr(){
numberofintrupt++;
read=true;
}
void analogread(){
numberofread++;
if(even){
sensval=analogread(0);
evenarray=sensval;
i++;
evenarray=sensval>>2;}
else {
sensval=analogread(0);
oddarray=sensval;
i++;
oddarray=sensval>>2;
}
i++;
if(i==512){
even=!even;
save=true;
j++;
i=0;
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void loop() {
if(read){
read=false;
analogread();
}
if(save){
savetocard();
save=false;
k++;
serial.println(numberofintrupt-numberofread);
// serial.println(j-k);
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void savetocard(){
if (even){
if(myfile){
myfile.write(oddarray,512);
}}
else{
if(myfile){
myfile.write(evenarray,512);
}}
myfile.write(0x0a);
myfile.write(0x0a);
if(k==5){
myfile.close();
digitalwrite(13,high);}
}
edit: corrected code.
please, read this (#7) use code tags post code, look
is interrupt necessary?
you remove interrupt handle , modify loop be:
and read every 1000 milliseconds.
please, read this (#7) use code tags post code, look
code: [select]
like this
is interrupt necessary?
you remove interrupt handle , modify loop be:
code: [select]
uint32_t sampletimer;
void loop() {
if (millis() - sampletimer > 1000) {
analogread();
sampletimer = millis();
}
if (save) {
savetocard();
save = false;
k++;
serial.println(numberofintrupt - numberofread);
// serial.println(j-k);
}
}
and read every 1000 milliseconds.
Arduino Forum > Using Arduino > Programming Questions > Arduino Due timer 3 interrupt problem
arduino
Comments
Post a Comment