KLCarithers Robotics

SRF05 mode 1 with mux and ext interrupt

Kyle L. Carithers

 

 

The SRF05 is an excellent sonar module that I highly recommend.  The module is accurate and easy to interface.  The code shown below was designed for an Atmega128 with a 16MHz clock and represents one way to efficiently read sonar values.  The code was developed in C using WinAVR’s Programmer’s Notepad.

 

This code uses five modules in the two pin mode, mode 1.  Port A is used for triggering the sonar.  Each trigger input on the sonar modules are connected to separate pins on port A.  The output lines of all five sonar modules are connected to a digital eight to one multiplexer, or mux.  The mux used three input pins from the MAVRIC to determine which sonar signal to send to the MAVRIC.  The output of the mux was connected to an external interrupt pin on the MAVRIC.

 

A simpler method using the sonar in mode 2 has the processor count until the falling edge of the sonar signal.  Click for an explanation of this method and example code.  

 

The functions named multiplexerX(), where X is the desired sonar number, are called one by one every 50ms.  Each function triggers the desired sonar and sets the digital control pins for the multiplexer.  These three pins, located on port G, determine which sonar signal will be sent to pin E7.  E7 is external interrupt seven.  At the falling edge of the sonar output, the external interrupt updates the sonar value of the sonar specified by the most recent multiplexer function called.

 

long int count0;

 

int sonar_distance_0;

int sonar_distance_1;

int sonar_distance_2;

int sonar_distance_3;

int sonar_distance_4;

 

int checking_mux;

 

int checking_multiplexer;

 

int mux_counter;

 

void init_timer0(void)

{

            count0=0;

            TIFR  |= _BV(OCIE0);

            TCCR0=0x2A;

            TCNT0  = 0;

            TIMSK |= _BV(OCIE0);

            OCR0=0x32;

}

 

SIGNAL(SIG_OUTPUT_COMPARE0)

{

            if(count0==1)

            {

                        multiplexer0();

            }

            if(count0==1250)

            {

                        multiplexer1();

            }

            if(count0==2500)

            {

                        multiplexer2();

            }

            if(count0==3750)

            {

                        multiplexer3();

            }

            if(count0==5000)

            {

                        multiplexer4();

            }

            if(count0==6250)

            {

                        count0=0;

            }

           

            count0++;

            TCNT2 = 0;

}

 

 

 

 

void init_extinterrupt(void)

{

///set up interrupts for falling edge INT7///

EICRA=0x00;

EICRB=0x80;

EIMSK=0x80;

EIFR=0x80;

}

 

SIGNAL(SIG_INTERRUPT7)

{

            if(checking_mux==0)

                        {

                        sonar_distance_0=count0-mux_counter;          

                        checking_mux=10;

                        }

            if(checking_mux==1)

                        {

                        sonar_distance_1=count0-mux_counter;          

                        checking_mux=10;

                        }

            if(checking_mux==2)

                        {

                        sonar_distance_2=count0-mux_counter;          

                        checking_mux=10;

                        }

            if(checking_mux==3)

                        {

                        sonar_distance_3=count0-mux_counter;          

                        checking_mux=10;

                        }         

            if(checking_mux==4)

                        {

                        sonar_distance_4=count0-mux_counter;          

                        checking_mux=10;

                        }

}

 

int multiplexer0 (void)

{         

            int i=0;

                       

            PORTG = 0b00000100;

           

            PORTA |= 0b00000001; //send burst//

           

            while(i<1000){i=i+1;}

                       

            PORTA &= 0b11111110; //end burst//

 

            mux_counter=count0;

            checking_mux=0;

}

 

int multiplexer1 (void)

{         

            int i=0;

                       

            PORTG = 0b00000011;

           

            PORTA |= 0b00000010; //send burst//

           

            while(i<1000){i=i+1;}

                       

            PORTA &= 0b11111101; //end burst//

           

            mux_counter=count0;

            checking_mux=1;

}

 

int multiplexer2 (void)

{         

            int i=0;

 

            PORTG = 0b00000101;

           

            PORTA |= 0b00000100; //send burst//

           

            while(i<1000){i=i+1;}

                       

            PORTA &= 0b11111011; //end burst//

 

            while(i<1000){i=i+1;}

           

            mux_counter=count0;

            checking_mux=2;

}

 

int multiplexer3 (void)

{         

            int i=0;

 

            PORTG = 0b00000010;

           

            PORTA |= 0b00001000; //send burst//

           

            while(i<1000){i=i+1;}

                       

            PORTA &= 0b11110111; //end burst//

 

            while(i<1000){i=i+1;}

           

            mux_counter=count0;

            checking_mux=3;

}

 

int multiplexer4 (void)

{         

            int i=0;

 

            PORTG = 0b00000110;

           

            PORTA |= 0b00010000; //send burst//

           

            while(i<1000){i=i+1;}

                       

            PORTA &= 0b11101111; //end burst//

 

            while(i<1000){i=i+1;}

           

            mux_counter=count0;

            checking_mux=4;

}

 

int main (void)

{

            DDRE = 0b11111111;

            DDRA = 0b11111111;

            DDRG = 0b00000111;

 

            init_extinterrupt();

            init_timer0();

            sei();

}