hi, im using the msp430F5529 LP with code composer, 16 maxbotix sonars, and 2 mux, i want to be able to read each one on the damand.
the code segment:
volatile uint32_t upd1 = 0; // Ultrasonic Pulse Duration (500 ns resolution)
volatile uint32_t upd2 = 0; // Ultrasonic Pulse Duration (500 ns resolution)
//sensor functions
uint32_t ReadSensor4(int num); // Reading from port 2.5
uint32_t ReadSensor5(int num); // Reading from port 2.5
int ChangeSensor(int num); // choose sensor config
void initSonar () {
P2DIR &= ~(BIT4+BIT5) ; // Comparison is done on port 2.4
P2SEL |= BIT4 + BIT5 ; // function in 2.4 is used
TA2CTL = TASSEL_2 | MC_2 | ID_3 | TACLR | TAIE;
TA2CCTL0 = OUTMOD_4; // Toggle mode
TA2CCTL1 = CM_3 | SCS | CAP | CCIE; // Rising and falling, sync, capture, interrupt
TA2CCTL2 = CM_3 | SCS | CAP | CCIE; // Rising and falling, sync, capture, interrupt
TA2CCTL1 &= ~CCIE;
TA2CCTL2 &= ~CCIE;
P4DIR |= BIT0; //pins controlling the mux
P3DIR |= BIT7;
P8DIR |= BIT2;
ChangeSensor(0);
return;
}
int ChangeSensor(int num) {
switch(num) { // Msb: 4.0 3.7 8.2 LSB
case 0:
{
P4OUT &= ~BIT0;
P3OUT &= ~BIT7;
P8OUT &= ~BIT2;
break;
}
case 1:
{
P4OUT &= ~BIT0;
P3OUT &= ~BIT7;
P8OUT |= BIT2;
break;
}
case 2:
{
P4OUT &= ~BIT0;
P3OUT |= BIT7;
P8OUT &= ~BIT2;
break;
}
case 3:
{
P4OUT &= ~BIT0;
P3OUT |= BIT7;
P8OUT |= BIT2;
break;
}
case 4:
{
P4OUT |= BIT0;
P3OUT &= ~BIT7;
P8OUT &= ~BIT2;
break;
}
case 5:
{
P4OUT |= BIT0;
P3OUT &= ~BIT7;
P8OUT |= BIT2;
break;
}
case 6:
{
P4OUT |= BIT0;
P3OUT |= BIT7;
P8OUT &= ~BIT2;
break;
}
case 7:
{
P4OUT |= BIT0;
P3OUT |= BIT7;
P8OUT |= BIT2;
break;
}
}
return num;
}
uint32_t ReadSensor4(int num) {
if (num > 7) {return 0;}
ChangeSensor(num);
upd1 = 0;
TA2CCTL1 |= CCIE;
MsecDelay(1);
while (upd1 == 0);
TA2CCTL1 &= ~CCIE;
return ((upd1 * 20) / 147);
}
uint32_t ReadSensor5(int num) {
if (num > 7) {return 0;}
ChangeSensor(num);
upd2 = 0;
TA2CCTL2 |= CCIE;
MsecDelay(1);
while (upd2 == 0);
TA2CCTL2 &= ~CCIE;
return ((upd2 * 20) / 147);
}
uint32_t ReadSensor(int num) {
if (num < 0 ) { return 0;}
if (num < 8 ) { return ReadSensor4(num);}
if (num < 16) { return ReadSensor5((num - 8));}
return 0;
}
// the two sonar sensors . port 2.4(upd1) and 2.5(upd2)
static volatile uint32_t rise1 = 0; // Pulse rise time from port 2.4
static volatile uint32_t fall1 = 0; // Pulse fall time from port 2.4
static volatile uint32_t rise2 = 0; // Pulse rise time from port 2.5
static volatile uint32_t fall2 = 0 ; // Pulse fall time from port 2.5
#pragma vector = TIMER2_A1_VECTOR // Timer vectored interrupt
__interrupt void timer2_a2_isr_SONAR(void) //
{ // //
switch(TA2IV) { // TA2IV must be read to reset interrupt
case 0:break; // Vector 0 - no interrupt
case 0x02: // 0x02 == CCR1 (capture/compare)
if(TA2CCTL1 & CCI) { // - Rising edge
rise1 = TA2CCR1; // Save rise time
fall1 = 0; // Reset fall time
} else { // - Falling edge
if(rise1) { // Make sure rising edge has occurred
// Handle possible pending overflow interrupt
if((TA2CTL & TAIFG) && (TA2R < 0x1000)) {
fall1 += 0xffff;
fall2 += 0xffff;
}
fall1 += TA2CCR1; // Get fall time, add to overflow time
if(!upd1) {upd1 = fall1 - rise1;} // Update time if mainline code is ready for it
rise1 = 0; // Clear rise time to ensure next rising edge is used
} //
} //
break; //
case 0x04: //// 0x04 == CCR2 (capture/compare)
if(TA2CCTL2 & CCI) { // - Rising edge
rise2 = TA2CCR2; // Save rise time
fall2 = 0; // Reset fall time
} else { // - Falling edge
if(rise2) { // Make sure rising edge has occurred
// Handle possible pending overflow interrupt
if((TA2CTL & TAIFG) && (TA2R < 0x1000)) {
fall1 += TA2CCR0;
fall2 += TA2CCR0;
}
fall2 += TA2CCR2; // Get fall time, add to overflow time
if(!upd2) {upd2 = fall2 - rise2;} // Update time if mainline code is ready for it
rise2 = 0; // Clear rise time to ensure next rising edge is used
} //
} //
break;
case 0x0E: // 0x0E == TAIFG (overflow)
fall1 += 0xffff; // Update overflow time
fall2 += 0xffff;
break; //
} //
} //
is based on a code given here to me before.
0-7 is mux 2.4
8-15 is mux 2.5
if i read from mux 0 and 9 everthing is OK . but if i read from the same mux , lets say 0 and then 1 repeatedly , every once in a while there is a drop.
e.g , lets say the distances are 100 and 50 , the reading will look something like this:
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 0 20 50 70 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 0 10 40 90 100 100 100 ....
my guess is it's somthing to do with the ovf, it was 0x0A at first but it never reached it, now it does, but the broblem remains... pls help...