interrupts and sonar

Started by juju1414 September 24, 2003
Hello,
I have been trying to get my sonars(srf04's) to work for some
time now, with no luck. The problem is the interrupts. I have
gotten the interrupts to trigger when they recieve the echo, but
they seem to give out random values for the time that the interrupt
occured. Here is some of the code:

//when recieves an echo
#pragma interrupt_handler SonarOne()
void SonarOne()
{echotime1 = TC4;
disttime1 = echotime - pingtime1;
TFLG1 = 0x10;
puts("sonaronerecieve");
} //triggers for three sonars
void SonarTrigger()
{

PTH |= 0x01; //set bit 0 high
delay(3);
PTH &= ~0x01; //set bit 0 low again
pingtime1 = time;
delay(30);

PTH |= 0x02; //set bit 1 high
delay(3);
PTH &= ~0x02; //set bit 1 low again
pingtime2 = time;
delay(30);

PTH |= 0x04; //set bit 2 high
delay(3);
PTH &= ~0x04; //set bit 2 low again
pingtime3 = time;
delay(30);

printf(" %d %d %d \n", disttime1, echotime1, pingtime1);
}

//Create delays
//delay(10); = Wait 10 ms

void delay(unsigned int ms)
{
int i;

while (ms > 0)
{
i = D_1MS;
while (i >0)
{
i = i - 1;
}
ms = ms - 1;
}
} /* Timer initialization
TC0 used for Left Drive motor encoder
TC1 used for Right Drive motor encoder
TC2 used for SonarThree
TC3 used for SonarTwo
TC4 used for SonarOne
*/
void timerinit()
{
TCTL4 = 0x05; //TC0 and TC1 capture rising edge
(0x05)
TCTL3 = 0x42; //TC2 - TC5 capture the falling edge(42)
TIE = 0xFF; //TC0 - TC5 cause interrupt(0x03)
(45)
TSCR1 = 0x80; //turn timer on (0x80)
TFLG1 = 0xFF;

when I run the program, without moving the sonar, i get results like
this
the fist number should be the distance, the second is TC4 at time of
echo, and the third is the time of the trigger. The first two
numbers should be close, since the sonar is not moving at all.
thevalue for TC4 seems random, which causes the distance also to be
random.

sonaronerecieve
-2918 -2595 323
sonaronerecieve
-31782 -31450 332
sonaronerecieve
-7896 -7555 341
sonaronerecieve
28494 28843 349
sonaronerecieve
-90 268 358
sonaronerecieve
4665 5032 367
sonaronerecieve
-5891 -5516 375
sonaronerecieve
32212 32596 384
sonaronerecieve
3300 3693 393
sonaronerecieve
-12106 -11704 402
sonaronerecieve
11424 11834 410
sonaronerecieve
-14273 -13854 419
sonaronerecieve
4964 5392 428
sonaronerecieve
-10847 -10410 437
sonaronerecieve
10323 10769 446
sonaronerecieve
-18036 -17582 454
sonaronerecieve

Any ideas what is wrong, or suggestions to get this working?
Thank you for your time, any help is appreciated!
~Julian