VL53l0x激光测距--单片机程序
还有一个是ST 的
- #include "stm32f10x.h" //stm32头文件
- #include "usart.h"
- #include "sys.h"
- #include "delay.h"
- #include "led.h"
- #include "vl53l0x.h"
- #include "vl53l0x_it.h"
- /******************************************************************************/
- extern VL53L0X_Dev_t vl53l0x_dev;//设备I2C数据参数
- extern u8 alarm_flag;
- /******************************************************************************/
- int main(void)
- {
- u32 i;
- u8 mode;
- VL53L0X_RangingMeasurementData_t RangingMeasurementData;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//分组2
- uart_init(115200); //串口1
- delay_init();
- LED_Init();
- delay_ms(200);
-
- if(vl53l0x_init(&vl53l0x_dev))//vl53l0x初始化
- printf("VL53L0X_init Error!!!\r\n");
- else
- printf("VL53L0X_init OK\r\n");
-
- mode=3; //高速模式,写错会导致测量速度慢
- vl53l0x_interrupt_start(&vl53l0x_dev,mode);
-
- while(1)
- {
- //VL53L0X_ClearInterruptMask(dev,0);//清除VL53L0X中断标志位
- //status = VL53L0X_StopMeasurement(dev); //停止测量
-
- if(alarm_flag==1)//触发中断
- {
- alarm_flag=0;
- VL53L0X_GetRangingMeasurementData(&vl53l0x_dev,&RangingMeasurementData);//获取测量距离,并且显示距离
- printf("d: %3d mm\r\n",RangingMeasurementData.RangeMilliMeter);
- VL53L0X_ClearInterruptMask(&vl53l0x_dev,0);//清除VL53L0X中断标志位
- }
- i++;
- if(i>=0x1fffff)
- {
- i=0;
- LED0=!LED0;
- }
- }
- }
- /***************************************************************************/
- /***************************************************************************/
复制代码
单片机源程序如下:
- /*****************************
- 更新日期2017年2月28日
- 模拟串口参考stc官方程序
- 来自arduino实例
- 使用11.0592MHz内部RC;
- VL53L0X:
- SCL-->P3.3
- SDA-->P3.2
- 串口:
- P3.0 RXD
- P3.1 TXD
- *******************************/
- #include"stc.h"
- #include"i2c.h"
- #include"VL53L0X.h"
- #include "intrins.h"
- #define uchar unsigned char
- #define uint unsigned int
- /***********全局变量定义**************/
- bit flag;
- uchar val1;
- short val_s;
- uchar gbuf[16];
- uint acnt ;
- uint scnt;
- uint dist ;
- uchar DeviceRangeStatusInternal;
- #define BAUD 0xFE80 // 9600bps @ 11.0592MHz
- //sfr AUXR = 0x8E;
- sbit RXB = P3^0; //define UART TX/RX port
- sbit TXB = P3^1;
- typedef bit BOOL;
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- BYTE TBUF,RBUF;
- BYTE TDAT,RDAT;
- BYTE TCNT,RCNT;
- BYTE TBIT,RBIT;
- BOOL TING,RING;
- BOOL TEND,REND;
- void UART_INIT();
- void delay()
- {
- unsigned char i;
- for(i=0; i<100; i++);
- }
- void Delay1ms() //@11.0592MHz
- {
- unsigned char i, j;
- _nop_();
- i = 11;
- j = 190;
- do
- {
- while (--j);
- } while (--i);
- }
- void delay_ms(unsigned int time)
- {
- unsigned char
- i;
- for(i=1; i<=time; i++)
- Delay1ms();
- }
- void uartsend_int16(short val)
- {
- uchar val1;
- val1=val/10000;
- while(!TEND);
- TEND=0;
- TBUF='0'+val1;
- TING=1;
- val1=val%10000/1000;
- while(!TEND);
- TEND=0;
- TBUF='0'+val1;
- TING=1;
- val1=val%1000/100;
- while(!TEND);
- TEND=0;
- TBUF='0'+val1;
- TING=1;
- val1=val%100/10;
- while(!TEND);
- TEND=0;
- TBUF='0'+val1;
- TING=1;
- val1=val%10/1;
- while(!TEND);
- TEND=0;
- TBUF='0'+val1;
- TING=1;
- }
- void uartprintf(char *s)
- {
- int i=0;
- while(s[i]!='\0')
- {
- while(!TEND);
- TEND=0;
- TBUF=s[i];
- TING=1;
- i++;
- }
- }
- void uartprintenter()
- {
- while(!TEND);
- TEND=0;
- TBUF=0x0d;
- TING=1;
- while(!TEND);
- TEND=0;
- TBUF=0x0a;
- TING=1;
- }
- void main()
- {
- uchar val = 0;
- int cnt = 0;
- TMOD = 0x00; //timer0 in 16-bit auto reload mode
- AUXR = 0x80; //timer0 working at 1T mode
- TL0 = BAUD;
- TH0 = BAUD>>8; //initial timer0 and set reload value
- TR0 = 1; //tiemr0 start running
- ET0 = 1; //enable timer0 interrupt
- PT0 = 1; //improve timer0 interrupt priority
- EA = 1; //open global interrupt switch
- UART_INIT();
- uartprintf("start");
- while (1)
- {
- vl53l0x_send(VL53L0X_REG_SYSRANGE_START, 0x01);
- while (cnt < 100) { // 1 second waiting time max
- delay_ms(10);
- val = vl53l0x_read(VL53L0X_REG_RESULT_RANGE_STATUS);
- if (val & 0x01) break;
- cnt++;
- }
- if (val & 0x01) uartprintf("ready");
- else uartprintf("not ready");
- uartprintenter();
- //vl53l0x_read_block(0x14, 12);
- 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)
- {
- uartprintf("distance:");
- uartsend_int16(dist);
- uartprintf("mm");
- uartprintenter();
- }
- else
- {
- uartprintf("error");
- uartprintenter();
- }
- delay_ms(100);
- }
- }
- void tm0() interrupt 1 using 1
- {
- if (RING)
- {
- if (--RCNT == 0)
- {
- RCNT = 3; //reset send baudrate counter
- if (--RBIT == 0)
- {
- RBUF = RDAT; //save the data to RBUF
- RING = 0; //stop receive
- REND = 1; //set receive completed flag
- }
- else
- {
- RDAT >>= 1;
- if (RXB) RDAT |= 0x80; //shift RX data to RX buffer
- }
- }
- }
- else if (!RXB)
- {
- RING = 1; //set start receive flag
- RCNT = 4; //initial receive baudrate counter
- RBIT = 9; //initial receive bit number (8 data bits + 1 stop bit)
- }
- if (--TCNT == 0)
- {
- TCNT = 3; //reset send baudrate counter
- if (TING) //judge whether sending
- {
- if (TBIT == 0)
- {
- TXB = 0; //send start bit
- TDAT = TBUF; //load data from TBUF to TDAT
- TBIT = 9; //initial send bit number (8 data bits + 1 stop bit)
- }
- else
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei下载(如有错误,烦请大神批评指导):
STM32:
VL53L0X_interrupt.rar
(358.49 KB, 下载次数: 432)
51:
VL53l0x激光测距.rar
(62.09 KB, 下载次数: 566)
|