求助:SEN0158红外定位探头使用方法
本帖最后由 Ricky 于 2014-3-26 13:22 编辑最近买了一个SEN0158红外定位探头,程序根据arduino提供的改成模拟I2C通信的STC程序,在通过串口把读到的坐标在计算机上显示,可是一直读到1023(全是FF),在红外发射管前晃来晃去没反应。发送数据都有回应,也可能是I2C程序有问题。跪求此模块datasheet或者控制指令。
另附程序:
#include <STC12C5A60S2.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
//int IRsensorAddress = 0xB0;
//int IRsensorAddress = 0x58;
int slaveAddress = 0xb0;
void Delay1us() ; //@11.0592MHz
bit I2C_Send_Byte(uchar sla, uchar dat);
bit I2C_Receive_Byte(uchar sla, uchar *dat);
void RS232_Init (void);
void RS232_Stop (void);
void RS232_Send(unsigned char ch);
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 30;
j = 190;
do
{
while (--j);
} while (--i);
}
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 22;
j = 3;
k = 227;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 108;
j = 144;
do
{
while (--j);
} while (--i);
}
void setup()
{
I2C_Send_Byte(slaveAddress,0x30); Delay1ms();
I2C_Send_Byte(slaveAddress,0x01); Delay1ms();
I2C_Send_Byte(slaveAddress,0x30); Delay1ms();
I2C_Send_Byte(slaveAddress,0x08); Delay1ms();
I2C_Send_Byte(slaveAddress,0x06); Delay1ms();
I2C_Send_Byte(slaveAddress,0x90); Delay1ms();
I2C_Send_Byte(slaveAddress,0x08); Delay1ms();
I2C_Send_Byte(slaveAddress,0xC0); Delay1ms();
I2C_Send_Byte(slaveAddress,0x1A); Delay1ms();
I2C_Send_Byte(slaveAddress,0x40); Delay1ms();
I2C_Send_Byte(slaveAddress,0x33); Delay1ms();
I2C_Send_Byte(slaveAddress,0x33); Delay1ms();
Delay10ms();
}
void main()
{
char data_buf;
int Ix;
int Iy;
int i,s;
P2M0=255;
P2M1=0;
RS232_Init();
RS232_Stop();
setup();
Delay1ms();
while(1)
{
//IR sensor read
I2C_Send_Byte(slaveAddress,0x36); Delay1ms();
//Wire.requestFrom(slaveAddress, 16); // Request the 2 byte heading (MSB comes first)
for (i=0;i<16;i++)
{
data_buf<i>=0;
}
i=0;
while(/*Wire.available() &&*/i < 16)
{
I2C_Receive_Byte(slaveAddress,&(data_buf<i>));Delay1ms();
i++;
}
Ix = data_buf;
Iy = data_buf;
s = data_buf;
Ix += (s & 0x30) <<4;
Iy += (s & 0xC0) <<2;
Ix = data_buf;
Iy = data_buf;
s = data_buf;
Ix += (s & 0x30) <<4;
Iy += (s & 0xC0) <<2;
Ix = data_buf;
Iy = data_buf;
s = data_buf;
Ix += (s & 0x30) <<4;
Iy += (s & 0xC0) <<2;
Ix = data_buf;
Iy = data_buf;
s = data_buf;
Ix += (s & 0x30) <<4;
Iy += (s & 0xC0) <<2;
RS232_Send(0XFF);Delay1ms();
for(i=0; i<16; i++)
{
RS232_Send(data_buf<i>);
Delay1ms();Delay1ms();Delay1ms();
}
/*for(i=0; i<4; i++)
{
RS232_Send(0XFF); Delay1ms();
RS232_Send(Ix<i>>>8);Delay1ms();
RS232_Send( Ix<i>&0x0f );Delay1ms();
RS232_Send(0X00);Delay1ms();
RS232_Send(Iy<i>>>8);Delay1ms();
RS232_Send( Iy<i>&0x0f );Delay1ms();
}*/
Delay10ms();Delay10ms();
}
}
</i></i></i></i></i></i></i>
建议尝试一下:参考arduino的代码, 初始化的时候一次传送两个字节
Write_2bytes(0x30,0x01); delay(10);
Leotower 发表于 2014-3-20 09:36
建议尝试一下:参考arduino的代码, 初始化的时候一次传送两个字节
Write_2bytes(0x30,0x01); delay(10); ...
改了过后还是输出一个00和15个FF,而且初始化发送代码后无应答了。。 Leotower 发表于 2014-3-20 09:36
建议尝试一下:参考arduino的代码, 初始化的时候一次传送两个字节
Write_2bytes(0x30,0x01); delay(10); ...
噢,发送2个字节之前忘记发送地址了。。成功了,真心感谢!!!! 祝贺:)如果方便,您做的应用可以发到论坛上展示一下, 期待中 请问你是用什么单片机进行下载运行的啊?
页:
[1]