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(); } }
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
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- I'm new to PCB, please give me some guidance, thank you
- Does lighttpd support PHP?
- How to keep the IO port in BlueNRG-1 sleep mode
- 2011 eNewsletter
- Which hot air gun and soldering station is worth having?
- Wi-Fi filter design tips (recommended for collection)
- One picture to understand: Manned spacecraft are booming in various countries, who is leading the way?
- TI Designs – Precision file question about Bode plots
- IoT Project Collection 2: Which one would you like to build?
- Award-winning live broadcast: Application of Weidmuller products in the semiconductor industry is in progress!