STM32F103ZET6 drives TOF250 laser ranging sensor

Introduction of TOF250

insert image description here
TOF250 is a single-point ranging radar based on TOF principle, using 940nm infrared light source, providing accurate and repeatable
long-distance measurement for high-speed autofocus (AF). The innovative TOF time-of-flight technology makes this sensor The performance is independent of the reflectivity of the target object. TOF (time-of-flight) measurement technology is realized by SPAD (single photon avalanche diode) of low-cost CMOS process, which makes the measurement result accurate and has higher anti-interference to ambient light sex. Support UART and I2C communication.

I2C communication protocol

insert image description here

I2C register address

insert image description here
Note: The host should delay at least 30uS to prepare data for the module after sending the register address, otherwise I2C will be abnormal.

TOF250 pin description and STM32 wiring

insert image description here
insert image description here

Wiring with STM32

TOF250 STM32
5V 5V
GND GND
SDA PB7
SCL PB6

illustrate:

This test only uses the analog IIC, to facilitate the understanding of
the USB printing results, the TX of the serial port 1 is required.

program

main.c

int main(void)
{
    
    
    //unsigned char y;
    __I2C_Status_TypeDef i2CStatus;
//    u16 len;                           //检测每次接收到的长度,方便字符串转整数的时候进行处理
    User_I2C_Init(); //IIC初始化
    delay_init();
    // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级

    /**************************************************************
    **TOF250串口通信协议:
    **波特率 : 9600
    **数据位 : 8
    **校验   : 无
    **停止位 : 1
    ****************************************************************/
    uart_init(9600);	 //串口初始化为115200
    while(1)
    {
    
    
        i2CStatus = I2C_RecvBytes(TOF250_ID,TOF250_DIST_H, &RxData[0], 2,100);    //通过IIC读取TOF250距离值信息
        if(i2CStatus == I2C_OK) {
    
    
            Tof250_dist = RxData[1] + RxData[0]*256;
            printf("%u cm\n",Tof250_dist);
        }
        delay_ms(100);                                      //延时100ms,TOF250默认更新速度10HZ
    }
}

myiic.h

#define USER_I2C_SCL_PIN  GPIO_Pin_6
#define USER_I2C_SDA_PIN  GPIO_Pin_7
#define USER_I2C_GPIO     GPIOB

//IO方向设置
 
#define SDA_IN()  {
      
      GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)8<<28;}
#define SDA_OUT() {
      
      GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)3<<28;}

//IO操作函数	 
#define IIC_SCL    PBout(6) //SCL
#define IIC_SDA    PBout(7) //SDA	 
#define READ_SDA   PBin(7)  //输入SDA 

Experimental results

insert image description here
5% accuracy

Summarize

If you need a specific routine, you can leave a comment and get it by email. The official CSDN of TOF250 is: https://blog.csdn.net/HCJ_Application/article/details/124058266
The program is a reference to the official routine

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/130262868