VL53L0X laser ranging module MCU driver

Publisher:Yuexiang888Latest update time:2019-12-23 Source: 51heiKeywords:VL53L0X Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I made a laser distance measurement VL53L0X program myself, and I want to share it with you. There is no display device in this program, because I don't know what you want to use for display, so please modify and add it according to your personal situation, but the program in the attachment is the program after I added OLED,
please use it as needed.

The microcontroller source program is as follows:

#include

#include "intrins.h"

#define uchar unsigned char

#define uint unsigned int


#define VL53L0X_REG_IDENTIFICATION_MODEL_ID         0xc0

#define VL53L0X_REG_IDENTIFICATION_REVISION_ID      0xc2

#define VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD   0x50

#define VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD 0x70

#define VL53L0X_REG_SYSRANGE_START                  0x00

#define VL53L0X_REG_RESULT_INTERRUPT_STATUS         0x13

#define VL53L0X_REG_RESULT_RANGE_STATUS             0x14

#define address 0x52


sbit I2C_SCL = P1^3;

sbit I2C_SDA = P1^2;



void I2C_Delay10us();

void I2C_Start();

void I2C_Stop();

fly I2C_SendByte(fly dat);

fly I2C_ReadByte();

//void I2C_SendACK(bit ack);

void vl53l0x_send(fly reg,fly dat);

fly vl53l0x_read(fly reg);

short makeuint16(int lsb, int msb);


fly gbuf[16];

int acnt ;

uint scnt;

uint dist ;

void DeviceRangeStatusInternal;


/*******************************************************************************

* Function name: Delay1ms() //@11.0592MHz

* Function: Delay

* Input: None

* Output: None

*******************************************************************************/

void Delay1ms()                //@11.0592MHz

{

    unsigned char i, j;


    _nop_();

    i = 11;

    j = 190;

    do

    {

        while (--j);

    } while (--i);

}

/*******************************************************************************

* Function name: delay_ms(unsigned int time)

* Function: Delay

* Input: time

* Output: None

*******************************************************************************/

void delay_ms(unsigned int time)

{

    unsigned char

    i;

    for(i=1; i<=time; i++)

        Delay1ms();

}

/*******************************************************************************

* Function name: Delay1us()

* Function: Delay

* Input: None

* Output: None

*******************************************************************************/

void I2C_Delay10us()

{

    unsigned char i;


    _nop_();

    _nop_();

    _nop_();

    i = 24;

    while (--i);

}

/*******************************************************************************

* Function name: I2C_Start()

* Function: Start signal: When the I2C_SCL clock signal is at a high level, the I2C_SDA signal generates a falling edge

* Input: None

* Output: None

* Note: After the start, I2C_SDA and I2C_SCL are both 0

*******************************************************************************/

void I2C_Start()

{

    I2C_SDA = 1;

    I2C_Delay10us();

    I2C_SCL = 1;

    I2C_Delay10us(); //The setup time is I2C_SDA hold time>4.7us

    I2C_SDA = 0;

    I2C_Delay10us(); //Hold time is >4us

    I2C_SCL = 0;

    I2C_Delay10us();

}

/*******************************************************************************

* Function name: I2C_Stop()

* Function: Termination signal: The I2C_SDA signal generates a rising edge during the high level of the I2C_SCL clock signal

* Input: None

* Output: None

* Note: After the end, keep I2C_SDA and I2C_SCL both 1; indicating that the bus is idle

*******************************************************************************/

void I2C_Stop()

{

    I2C_SDA = 0;

    I2C_Delay10us();

    I2C_SCL = 1;

    I2C_Delay10us(); //Establishment time is greater than 4.7us

    I2C_SDA = 1;

    I2C_Delay10us();

}

/*******************************************************************************

* Data type: I2cSendByte(uchar num)

* Function: Send a byte via I2C. During the high level of the I2C_SCL clock signal,

* * Keep the sending signal I2C_SDA stable

* Input: num, ack

* Output: 0 or 1. Return 1 if the message is sent successfully, and return 0 if the message fails to send.

* Note: After sending a byte, I2C_SCL=0, if a response is required, the response is set to 1, otherwise it is 0

*******************************************************************************/

fly I2C_SendByte(fly dat)

{

    uchar a = 0, b = 0; // Maximum value is 255, one machine cycle is 1us, and the maximum delay is 255us.


    for(a=0; a<8; a++) //To send 8 bits, starting from the highest bit

    {

        dat <<= 1;

        I2C_SDA=CY;

        I2C_Delay10us();

        I2C_SCL = 1;

        I2C_Delay10us(); //Establishment time>4.7us

        I2C_SCL = 0;

        I2C_Delay10us(); //Time is greater than 4us

    }


    //I2C_SDA = 1;

    //I2C_Delay10us();

    I2C_SCL = 1;

    I2C_Delay10us();

    CY=I2C_SDA;

    I2C_SCL = 0;

    I2C_Delay10us();

    return CY;

}

/*******************************************************************************

* Function name: I2cReadByte()

* Function: Read a byte using I2C

* Input: None

* Output: dat

* Note: After receiving a byte, I2C_SCL=0

*******************************************************************************/

fly I2C_ReadByte()

{

    fly a = 0, dat = 0;

    I2C_SDA = 1; //I2C_SCL is 0 after starting and sending a byte

    I2C_Delay10us();

    for(a=0; a<8; a++) //Receive 8 bytes

    {

        I2C_SCL = 1;

        I2C_Delay10us();

        dat <<= 1;

        dat |= I2C_SDA;

        I2C_Delay10us();

        I2C_SCL = 0;

        I2C_Delay10us();

    }

    return that;

}

/*******************************************************************************

* Function name: vl53l0x_send(uchar reg,uchar dat)

* Function: Use I2C to send commands to vl530xl

* Input: reg,dat

* Output: None

*******************************************************************************/

void vl53l0x_send(fly reg,fly dat)

{

    I2C_Start();

    I2C_SendByte(address);

    I2C_SendByte(right);

    I2C_SendByte(that);

    I2C_Stop();

}

/*******************************************************************************

* Function name: vl53l0x_read(uchar reg)

* Function: Use I2C to read a byte from vl530x0l

* Input: reg

* Output: num

*******************************************************************************/

fly vl53l0x_read(fly reg)

{

    unsigned char num;

    I2C_Start();

    I2C_SendByte(address); //Send write device address

    I2C_SendByte(reg); //Send the address to be read

                I2C_Stop();

    I2C_Start();

    I2C_SendByte(address+1); //Send the read device address

    num=I2C_ReadByte(); //Read data

    I2C_Stop();

    return num;

}

/*******************************************************************************

* Function name: makeuint16(int lsb, int msb)

* Function: Convert the read data into an array

* Input: lsb,msb

* Output: dat

*******************************************************************************/

short makeuint16(int lsb, int msb) 

{

    return ((msb & 0xFF) << 8) | (lsb & 0xFF);

}

/*******************************************************************************

* Function name: date()

* Function: Read and convert data, process data

* Input: None

* Output: dist

*******************************************************************************/

unsigned char date()

{

        float val = 0;        

// fly val;

        int cnt = 0;

                        vl53l0x_send(VL53L0X_REG_SYSRANGE_START, 0x01);

                while (cnt < 100) ///Check whether the data is transmitted normally

                { 

      delay_ms(10);

      val = vl53l0x_read(VL53L0X_REG_RESULT_RANGE_STATUS);

      if (val & 0x01) break;

      cnt++;

    }

                 gbuf[0]=vl53l0x_read(0x14);

                 gbuf[7]=vl53l0x_read(0x14+7);

                 gbuf[6]=vl53l0x_read(0x14+6);

                 gbuf[9]=vl53l0x_read(0x14+9);

                 gbuf[8]=vl53l0x_read(0x14+8);

                 gbuf[11]=vl53l0x_read(0x14+11);

                 gbuf[10]=vl53l0x_read(0x14+10);

     acnt = makeuint16(gbuf[7], gbuf[6]);

     scnt = makeuint16(gbuf[9], gbuf[8]);                 

     dist = makeuint16(gbuf[11], gbuf[10]);                 

//                 DeviceRangeStatusInternal = ((gbuf[0] & 0x78) >> 3);

//                         if(DeviceRangeStatusInternal==11&&dist>20&&dist<1200)

//                        {

// valley=dist/10000;

// OLED_dan(8*0,0,vall+16);

// valley=dist%10000/1000;

// OLED_dan(8*1,0,vall+16);

// valley=dist%1000/100;

// OLED_dan(8*2,0,vall+16);

// valley=dist%100/10;

// OLED_dan(8*3,0,vall+16);

// valley=dist%10/1;

// OLED_dan(8*4,0,vall+16);

//                        } 

                        return dist; //The array is five bits

}

void main()

{


        while(1)

        {

    date();

        }

}

Keywords:VL53L0X Reference address:VL53L0X laser ranging module MCU driver

Previous article:MCU code for fully automatic washing machine controller design
Next article:Simulation and program source code of single chip DC motor speed regulation and display

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号