Shop OBEX P1 Docs P2 Docs Learn Events
PING connected to a PIC16F876 — Parallax Forums

PING connected to a PIC16F876

nullvaluenullvalue Posts: 8
edited 2009-04-08 23:33 in Robotics
Hi, I'm interfacing a PING))) device directly to a PIC 16F876, using the HI-TECH PICC Lite compiler. I'm running into a funky issue..

In my code, I'm trying to trigger the burst and listen back for the results. Here's some psuedocode of what I'm doing, and the issue I'm seeing. The PING Signal is connected to my RB7 pin.

Set RB7 = Output
Set RB7 = High
Wait 10uS
Set RB7 = Low
Set RB7 = Input
....read in results..

However, once I set the RB7 as an input, the signal on the PING goes high and stays high. I cannot detect any kind of pulse back from the device. Has anyone else run into this? Why would my PIC pin go high when I switch the I/O direction to Input?

Does anyone have any sample code in PICC for interfacing with the PING?

Thanks

EDIT: I should also note, that once I set the RB7 pin High, the ACT LED comes on and stays lit.

Post Edited (nullvalue) : 4/8/2009 3:15:12 PM GMT

Comments

  • nullvaluenullvalue Posts: 8
    edited 2009-04-08 15:35
    Here's my actual code:

    RB7=1; //Set signal high
    __delay_us(10); //wait 10uS
    RB7=0; //Set signal low
    TRISB7=1; //Swtich to Input
    while(!RB7) {} //Wait for rising edge from input
    j=0; //reset counter
    while(RB7) { //loop w/ 1 uS delay, waiting for
    __delay_us(1); //signal to go low
    j++;
    }
    LFT_PWM=j/145; //divide max 18500uS / 145 for max 127
    TRISB7=0; //Switch to Output

    For now I'm using the results to drive a motor through PWM.

    With the code as is, it hangs inside the while(RB7) { __delay_us(1); j++; } loop. Apparently after seeing the rising edge of the echo return pulse, the pin is never set low. (If i jump the signal pin to GND the code continues)..... Not sure what's going on... any help is appreciated.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-08 15:54
    What you've written looks like it ought to work. Like you said, there's something funky going on.

    Check your wiring. The ACT LED should only come on briefly when RB7 is pulsed. If you have
    access to an oscilloscope or logic analyzer, it would be helpful to look at your signal to see what's
    actually present.
  • nullvaluenullvalue Posts: 8
    edited 2009-04-08 16:00
    Thanks, unfortunately I don't have access to a scope. ACT does come on briefly if I never switch my PIC pin to an input. If I just pulse it.. However, the moment I set that pin as an input it latches high for some reason.. argh.. [noparse]:)[/noparse]
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-04-08 16:04
    Does your I/O pin perhaps have an internal pull-up enabled?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • nullvaluenullvalue Posts: 8
    edited 2009-04-08 16:07
    Thanks for the idea, but I had already thought of that.. PORT B does in fact have internal pullups, so I tried using a PIN on PORTA (which doesn't have any kind of internal pullups) and I get the same results.
  • nullvaluenullvalue Posts: 8
    edited 2009-04-08 17:07
    In doing some more testing with my board, it appears if I disconnect the PING device, switch all pins to input (disable internal pullups), all pins latch high somehow. They do not float as I would expect. I can't think of a time I've run into this before, but it's obvious the problem is NOT with the PING but for some reason my all my inputs latch high. STRANGE. But I'm just a hobbyist so its likely I have something hooked up wrong.

    Would a pull-down resistor help? I'm kind of at a loss.

    I am going to try putting this on a breadboard to see if the problem remains or if its just my circuit. Maybe my PIC's bad?
  • nullvaluenullvalue Posts: 8
    edited 2009-04-08 17:27
    Ok.. same results on a breadboard. inputs always "float"/latch high. I don't get it.. [noparse]:([/noparse]
  • nullvaluenullvalue Posts: 8
    edited 2009-04-08 21:00
    Well I tried interfacing to the PING w/ a PIC16F84 that I had laying around and it's working fine. So, not sure on the '876 but I've just ordered a few new ones so we'll see when they get here. I've had that chip for a number of years and have reused it quite a number of times, so odds are there is something wrong with it.
  • nullvaluenullvalue Posts: 8
    edited 2009-04-08 23:33
    I got it working with the 876! My hunch worked, I added a 10k pull-down resistor to my PIC input pin and its working like a champ.. Not sure why I need it, but I'm not going to complain.....it's working.. smile.gif
Sign In or Register to comment.