// ping.c - this code will set up and run the PING ultrasound sensor // This code will not compute the distance // If the timer overflows, than nothing was seen in front of the sensor. #define PING_PORT p9_4 #define PING_PORT_DIR pd9_4 #define B4TIME_CONFIG 0x4A unsigned int Ping_Width=0xFFFF; char ping_done=0; void Ping_init(void) { DISABLE_IRQ tb4ic = 0x03; ENABLE_IRQ } #pragma INTERRUPT ping_irq void ping_irq() { if (mr3_tb4mr == 1) { // check for timer overflow tb4s=0; ping_done=1; Ping_Width= 0; return; // data invalid, so exit routine } /* check if high or low pulse width was measured */ if (PING_PORT == 1) { // this is the low width //Ping_Width = tb4; // if input is now hi, low width measured } else { Ping_Width = tb4; // input is now low, high width measured } // set ping is done flag ping_done=1; tb4s=0; } // send out a ping void Ping() { int lp=0; ping_done=0; // ok this is crazy but you can only write to this Port DIR after // PRC Reg Command prc2 = 1; // change port to output PING_PORT_DIR = 1; // send output wave PING_PORT = 1; // send out a pulse // loop to keep it high for a bit for(lp=0;lp<50;lp++) { } // stop the output wave PING_PORT = 0; // end the pulse // change port back to input prc2 = 1; PING_PORT_DIR = 0; // set port direction of Ping to input // start TimerB PWM width measure tb4=0; tb4mr = B4TIME_CONFIG; tb4s = 1; // turn on the timer } char Ping_Done() { return ping_done; } int Get_Ping_Width() { return Ping_Width; }