thesedays i performing some project with due.
i use interrupt(ISR) and digital trigger, and analogRead function.
as shown in under code, I programmed.
when D13 is LOW, interrupt code would be active.
and every 1000ms, I want measure the analogRead(5).
I confirmed that interrupt code was perfoming.
but analogRead(5)'s value is trash..
It shows the trash value.
I cant solve this problem.
plz give me a answer or helps.
#define tri 13 // DIO 13 set as trigger source
unsigned long sta; // Start microseconds value
unsigned long sto; // Stop microseconds value
unsigned long ad0[200]; // ad0 to ad3 arrays initailize - index 200
unsigned long ad1[200];
unsigned long ad2[200];
unsigned long ad3[200];
unsigned int av0 = 0; // peak voltages of each AIs
unsigned int av1 = 0;
unsigned int av2 = 0;
unsigned int av3 = 0;
unsigned int at0 = 0; // peak index of each arrays
unsigned int at1 = 0;
unsigned int at2 = 0;
unsigned int at3 = 0;
unsigned int state = 0;
double X = 0;
double Y = 0;
long pretime = 0;
int have = 0;
long interval = 1000;
void setup() {
Serial.begin(9600);
ADC->ADC_MR |= 0x80; // these lines set free running mode on ads 6 (pin A0 to A3 - see Due Pinout Diagram thread)
ADC->ADC_CHER = 0xF0; // this is for AD0 to AD3
ADC->ADC_CHER = 0x8;
pinMode(tri, INPUT);
}
void loop() {
state = digitalRead(tri); //tri port states read
if (state == 0) // if state is HIGH
{
sta = micros(); //start time read
unsigned int i;
for (int i = 0; i < 200; i++) {
while ((ADC->ADC_ISR & 0xF0) != 0xF0); // wait for four conversions
ad0 = ADC->ADC_CDR[7]; // read data on A0 pin to A3
ad1 = ADC->ADC_CDR[6];
ad2 = ADC->ADC_CDR[5];
ad3 = ADC->ADC_CDR[4];
* }*
* sto = micros(); // stop time read*
* av0 = av1 = av2 = av3 = at0 = at1 = at2 = at3 = 0;*
* for (i = 0; i < 200; i++) // this FOR loop for find the peak voltage and its peak index of array*
* {*
_ if (ad0 > av0)
{ av0 = ad0*;
at0 = i;
}
if (ad1 > av1)
{ av1 = ad1;
at1 = i;
}
if (ad2 > av2)
{ av2 = ad2;
at2 = i;
}
if (ad3 > av3)
{ av3 = ad3;
at3 = i;
}
}
// at hear must be programed about position detection algorithm*
* double T1 = abs(double(at0) - double(at1));
double T2 = abs(double(at2) - double(at3));
T1 = (T1 * (double(sto - sta) / double(200))) * double(0.000001);
T2 = (T2 * (double(sto - sta) / double(200))) * double(0.000001);
double AT = 0;
int Q = 0;
double D = 0;
if (av0 > av1 && av3 > av2)
{ AT = atan2(double(av3), double(av0));
AT = double(AT) / double(PI) * double(180);
Q = 1;
D = T1;
}
if (av0 > av1 && av3 < av2)
{ AT = atan2(double(av2), double(av0));
AT = double(AT) / double(PI) * double(180);
Q = 2;
D = T1;
}
if (av0 < av1 && av3 < av2)
{ AT = atan2(double(av2), double(av1));
AT = double(AT) / double(PI) * double(180);
Q = 3;
D = T2;
}
if (av0 < av1 && av3 > av2)
{ AT = atan2(double(av3), double(av1));
AT = double(AT) / double(PI) * double(180);
Q = 4;
D = T2;
}
D = (double(-40000) * D) + double(52);
if (D >= 50)
{
D = 50;
}
if (D < 5)
{
D = 5;
}
else*
* {
D = D;
}
if (Q == 1)
{
AT = double(90) - AT;
}
if (Q == 2)
{
AT = double(90) + AT;
}
if (Q == 3)
{
AT = double(270) - AT;
}
if (Q == 4)
{
AT = double(270) + AT;
}
AT = AT / double(180) * double(PI);
X = cos(AT) * D;
Y = sin(AT) * D;
have = 1;
}
long curtime = millis();
long diff = long(curtime) - long(pretime);
if (diff > interval)
{
pretime = curtime;
int value = analogRead(A5);
Serial.print("/e");
Serial.print(value);
Serial.print("/x");
Serial.print(X);
Serial.print("/y");
Serial.println(Y);
X = Y = 0;
}
if (diff < 0)
{
pretime = 0;
}
}*_