Shop OBEX P1 Docs P2 Docs Learn Events
Laser Ping and the P2 — Parallax Forums

Laser Ping and the P2

iseriesiseries Posts: 1,492
edited 2021-01-12 11:17 in Propeller 2
I was trying to use my Laser Ping sensor with my P2 and was not having any luck.

First I can't use pulse_in or pulse_out as these functions use CTRA, PHSA, and FRQB registers that are no longer there.

So I created my own functions but don't know if there is a better way. Apparently no one has tried to use these devices with the P2.
#include <stdio.h>
#include <propeller.h>
#include "simpletools.h"

void PulseOut(int, int);
int PulseIn(int);


#define PIN 16


int main(int argc, char** argv)
{
    int i;
    
    _pinl(PIN);
    while (1)
    {
        _pinl(PIN);
        _waitms(90);
        PulseOut(PIN, 5);
        i = PulseIn(PIN, 1);
        printf("Time: %d\n", i);
        _waitms(1000);
	}
}

void PulseOut(int pin, int t)
{
    int i;
    
    i = _pinr(pin);
    if (i == 0)
    	_pinh(pin);
    else
    	_pinl(pin);
    _waitus(t);
    if (i == 0)
    	_pinl(pin);
    else
    	_pinh(pin);
}

int PulseIn(int pin, int s)
{
    int i;
    
    _pinf(pin);
    i = _getus();
    while ((_pinr(pin) != s) && ((_getus() - i) < 1000));
    i = _getus();
    while ((_pinr(pin) == s) && ((_getus() - i) < 100000));
    i = _getus() - i;
    return i;
}

Also I needed a simple serial device so that I could put the Laser ping into serial mode using the same pin for output and input.

I needed to first open a serial device for output at 9600 and output two letters and then receive the serial data back from the Laser ping sensor.
  _pinl(_Pin);
  _waitms(250);

  serial_open( -1, _Pin, 0, 9600);
  
  serial_txChar('I');
  serial_txChar('I');

  serial_close();
  serial_open(_Pin, -1, 0, 9600);

Mike


Sign In or Register to comment.