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)
{
START:
TR0 = 0;
TH0 = 0;
TL0 = 0;
TRIG = 0; //Sends a negative pulse,
delay(100);
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
com_putchar(TL0);
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.