本帖最后由 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[16];
- int Ix[4];
- int Iy[4];
- 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[0] = data_buf[1];
- Iy[0] = data_buf[2];
- s = data_buf[3];
- Ix[0] += (s & 0x30) <<4;
- Iy[0] += (s & 0xC0) <<2;
-
- Ix[1] = data_buf[4];
- Iy[1] = data_buf[5];
- s = data_buf[6];
- Ix[1] += (s & 0x30) <<4;
- Iy[1] += (s & 0xC0) <<2;
-
- Ix[2] = data_buf[7];
- Iy[2] = data_buf[8];
- s = data_buf[9];
- Ix[2] += (s & 0x30) <<4;
- Iy[2] += (s & 0xC0) <<2;
-
- Ix[3] = data_buf[10];
- Iy[3] = data_buf[11];
- s = data_buf[12];
- Ix[3] += (s & 0x30) <<4;
- Iy[3] += (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>
复制代码
|