Let's Make Robots!

Need translator for SDM-IO arduino-to-PICAXE/ code help

So I've got one of these SDM-IO ultrasonic sensors. Despite the information saying that one can use it like an HC-SR04, one cannot. I've found this SDM-IO example code for arduinos:

#include "reg51.h"
#include "sio.h" 

sbit TRIG = P2^7;
sbit ECHO = P2^6; 

#define XTAL 19660800L
#define PERIOD12MS (12L * XTAL / 12L / 256L / 1000L) 

void delay(unsigned int t)
    while(t--) ;

void main (void)
    EA = 0; 

    TMOD &= ~0x0F;      // clear timer 0 mode bits
    TMOD |= 0x01;         // put timer 0 into MODE 1, 16bit 

    com_initialize ();        /* initialize interrupt driven serial I/O */
    com_baudrate (14400); /* setup for 14400 baud */ 

    EA = 1;                      // Enable Interrupts 

    while (1)

        TR0 = 0;
        TH0 = 0;
        TL0 = 0; 

        TRIG = 0;             //Sends a negative pulse,
        TRIG = 1;            //start detect
        TR0 = 1;              //start timer0
        while (ECHO)       //listen ECHO signal
            if (TH0 >= PERIOD12MS)  //The cycle period timeout
                goto START;
        TR0 = 0;             //stop timer0
        com_putchar(TH0);  //printf
        TR0 = 1;
        while (TH0 < PERIOD12MS) ;  //keep 12ms cycle period

Seeing as my understanding of C is limited at best I thought I'd consult the experts. So this sensor requires a negative pulse to activate it and it sends a negative pulse back? Also, I understand the 12ms IF-THEN labeled "The cycle period timeout." I don't understand what the "keep 12ms cycle period" code is for. It also seems to return two numbers: TH0 and TL0. What are these? The code also seems to activate an interrupt but I don't see where or what it triggers. Sorry if these questions are a little silly but I'm out of my league here.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

You will probably have to look at the files that are 'included'. They are libraries that have been written to do particular tasks and may contain more answers to some of your questions. As for the negative pulse, like some of the other US sensors I have seen, this sensor requires the trigger line to be pulled low for a short period before going high to start the measurement. The cycle period timeout must be the time required for the pulse to reach maximum distance of the sensor, so there is no reason to continue waiting and another pulse is sent out. Realize that all the code you found does is PING and then print the number if it is within the range of the sensor. Once again, you will likely have to look at the included libraries to see if you can make anymore sense of the code you have here.

This sensor is a handful. Can't use PULSIN to measure the distance. It pulls low the echo pin when it hears the sonar reflection but it doesn't employ a rising/falling edge when it starts. It seems to require time measurement from when the trigger is pulled low until the echo pin goes low and then the requisite math for distance. Looks like this PICAXE M2 chip I chose just ain't got the right timers for this project. Bugger.