久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1510|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

基于arduino的VL53L0X驅(qū)動(dòng)做的C51適配VL53L0X驅(qū)動(dòng)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
1.簡(jiǎn)介

在研究了各位大神的代碼后,結(jié)合購(gòu)買模塊時(shí)商家提供的arduino的VL53L0X驅(qū)動(dòng),我修改ardiuno的驅(qū)動(dòng)寫出了兼容C51的VL53L0X驅(qū)動(dòng)。我的測(cè)試的單片機(jī)是STC8H3K64S4,IIC通信使用的時(shí)STC硬件的IIC。大家只需要把我的IIC驅(qū)動(dòng)和顯示代碼修改成自己的,最后添加一個(gè)定時(shí)1ms的定時(shí)器作為VL53L0X的時(shí)基,就可以直接拿來(lái)用了。
這是本人第一次發(fā)帖,做的工作也比較簡(jiǎn)單,如果有什么做的不好的還請(qǐng)諒解。

參考的網(wǎng)站:
http://www.zg4o1577.cn/bbs/dpj-167298-1.html

使用上面網(wǎng)站的代碼寫出來(lái)的程序,這個(gè)代碼只有最基本的測(cè)距功能,測(cè)距誤差大概在±5%。對(duì)我剛剛接觸VL53L0X時(shí)啟發(fā)很大。
在使用商家提供的arduino的VL53L0X驅(qū)動(dòng)后,由于設(shè)置了VL53L0X的相關(guān)寄存器,精度可以達(dá)到±1%。當(dāng)然超過(guò)模塊的1.2m的測(cè)距范圍精度還是會(huì)差很多。


2.上代碼

主函數(shù) main.c
人機(jī)交互的相關(guān)代碼替換成自己的就行了
  1. #include"inc/VL53L0X.h"
復(fù)制代碼

VL53L0X的驅(qū)動(dòng) VL53L0X.c

代碼太多了只顯示一部分,詳情看附件,大家用的時(shí)候記得添加時(shí)基就行了。

  1. // Most of the functionality of this library is based on the VL53L0X API
  2. // provided by ST (STSW-IMG005), and some of the explanatory comments are quoted
  3. // or paraphrased from the API source code, API user manual (UM2039), and the
  4. // VL53L0X datasheet.

  5. #include "inc/VL53L0X.h"
  6. #include "inc/VL53L0X_IIC.h"
  7. #include "NodeHandler.h"
  8. #include <string.h>


  9. unsigned char stop_variable;
  10. unsigned long measurement_timing_budget_us;
  11. unsigned int io_timeout = 0, timeout_start_ms, t2;
  12. bit did_timeout;


  13. //===================================
  14. //你自己的定時(shí)器時(shí)基定時(shí)1ms
  15. void Time2() interrupt 12
  16. {
  17.         t2++;
  18. }
  19. //==================================================


  20. // Defines /////////////////////////////////////////////////////////////////////

  21. // The Arduino two-wire interface uses a 7-bit number for the address,
  22. // and sets the last bit correctly based on reads and writes
  23. #define ADDRESS_DEFAULT 0x52

  24. // Record the current time to check an upcoming timeout against
  25. #define startTimeout() (timeout_start_ms = t2)

  26. // Check if timeout is enabled (set to nonzero value) and has expired
  27. #define checkTimeoutExpired() (io_timeout > 0 && ((unsigned  int)t2 - timeout_start_ms) > io_timeout)

  28. // Decode VCSEL (vertical cavity surface emitting laser) pulse period in PCLKs
  29. // from register value
  30. // based on VL53L0X_decode_vcsel_period()
  31. #define decodeVcselPeriod(reg_val)      (((reg_val) + 1) << 1)

  32. // Encode VCSEL pulse period register value from period in PCLKs
  33. // based on VL53L0X_encode_vcsel_period()
  34. #define encodeVcselPeriod(period_pclks) (((period_pclks) >> 1) - 1)
復(fù)制代碼

IIC驅(qū)動(dòng)
有人喜歡模擬IIC,有人喜歡硬件IIC,他們各有各的優(yōu)點(diǎn),大家按照自己的喜好替換調(diào)我的IIC驅(qū)動(dòng)就行了
  1. #define VL53L0X_address                         0x52

  2. //IIC寫8位數(shù)據(jù)
  3. void VL53L0X_Write(unsigned char address, unsigned char value)
  4. {
  5.         P_SW2 = 0xB0;
  6.         Start();
  7.         SendData(VL53L0X_address);
  8.         RecvACK();
  9.         SendData(address);
  10.         RecvACK();
  11.         SendData(value);
  12.         RecvACK();
  13.         Stop();
  14.         Delay();
  15.         P_SW2 = 0x00;
  16. }

  17. //IIC寫16位數(shù)據(jù)
  18. void VL53L0X_Write16Bit(unsigned char address, unsigned int value)
  19. {
  20.         P_SW2 = 0xB0;
  21.         VL53L0X_Write(address++, (value >> 8) & 0xFF);
  22.         VL53L0X_Write(address++, value & 0xFF);
  23.         P_SW2 = 0x00;
  24. }

  25. //IIC寫32位數(shù)據(jù)
  26. //void VL53L0X_Write32Bit(unsigned char address, unsigned long value)
  27. //{
  28. //        Start();
  29. //        SendData(VL53L0X_address);
  30. //        RecvACK();
  31. //        SendData(address);
  32. //        RecvACK();
  33. //        SendData((value >> 24) & 0xFF);
  34. //        RecvACK();
  35. //        SendData((value >> 16) & 0xFF);
  36. //        RecvACK();
  37. //        SendData((value >> 8) & 0xFF);
  38. //        RecvACK();
  39. //        SendData(value & 0xFF);
  40. //        RecvACK();
  41. //        Stop();
  42. //         Delay();
  43. //}

  44. //寫多位數(shù)據(jù)
  45. void VL53L0X_WriteMulti(unsigned char address, unsigned char const * src, unsigned char count)
  46. {
  47.         P_SW2 = 0xB0;        
  48.         while(count--)
  49.         {
  50.                 VL53L0X_Write(address++, *(src++));
  51.         }
  52.         P_SW2 = 0x00;
  53. }

  54. //IIC讀8位數(shù)據(jù)
  55. unsigned char VL53L0X_Read(unsigned char address)
  56. {
  57.         unsigned char receive;
  58.         P_SW2 = 0xB0;
  59.         Start();
  60.         SendData(VL53L0X_address);
  61.         RecvACK();
  62.         SendData(address);
  63.         RecvACK();
  64.         
  65.         Start();
  66.         SendData(VL53L0X_address + 1);
  67.         RecvACK();
  68.         receive = RecvData();
  69.         SendNAK();
  70.         Stop();
  71.         Delay();
  72.         P_SW2 = 0x00;
  73.         
  74.         return receive;
  75. }

  76. //IIC讀16位數(shù)據(jù)
  77. unsigned int VL53L0X_Read16Bit(unsigned char address)
  78. {
  79.         unsigned int receive;
  80.         P_SW2 = 0xB0;
  81.         receive = VL53L0X_Read(address++) << 8;
  82.         receive |= VL53L0X_Read(address);
  83.         P_SW2 = 0x00;
  84.         
  85.         return receive;
  86. }

  87. //IIC讀多位數(shù)據(jù)
  88. void VL53L0X_ReadMulti(unsigned char address, unsigned char * dst, unsigned char count)
  89. {
  90.         while(count--)
  91.         {
  92.                 *(dst++) = VL53L0X_Read(address++);
  93.         }
  94.         P_SW2 = 0x00;
  95. }
復(fù)制代碼

3.其他
說(shuō)一個(gè)我的IIC編程遇到的問(wèn)題,我在IIC連續(xù)讀SDA的數(shù)據(jù)的時(shí)候會(huì)存在有時(shí)讀到的數(shù)據(jù)不穩(wěn)定的情況
連續(xù)讀的時(shí)候,如下讀兩個(gè)字節(jié)
  1. //IIC讀16位數(shù)據(jù)
  2. unsigned int VL53L0X_Read16Bit(unsigned char address)
  3. {
  4.         unsigned int receive;
  5.         IICStart();
  6.         IICSendData(VL53L0X_address);
  7.         IICWaitACK();
  8.         IICSendData(address);
  9.         IICWaitACK();
  10.         IICStop();
  11.         
  12.         IICStart();
  13.         IICSendData(VL53L0X_address + 1);
  14.         IICWaitACK();
復(fù)制代碼
VL53L0X的內(nèi)部寄存器索引會(huì)在讀完一個(gè)寄存器后自加1,索引加1后讀下一個(gè)寄存器的數(shù)據(jù)。
那么如果這個(gè)自加1的索引自加的速度跟不上你IIC讀取的速度呢。
這樣IIC讀取的數(shù)據(jù)就會(huì)不可靠。
連續(xù)讀兩個(gè)字節(jié),我修改成連續(xù)兩次讀一個(gè)字節(jié)后,數(shù)據(jù)的讀取就穩(wěn)定許多了

  1. //IIC讀16位數(shù)據(jù)
  2. unsigned int VL53L0X_Read16Bit(unsigned char address)
  3. {
  4.         unsigned int receive;
  5.         P_SW2 = 0xB0;
  6.         receive = VL53L0X_Read(address++) << 8;     //兩個(gè)字節(jié)分兩次讀取
  7.         receive |= VL53L0X_Read(address);
  8.         P_SW2 = 0x00;
  9.         
  10.         return receive;
  11. }
復(fù)制代碼
或者大家有更好的見(jiàn)解也可以告訴我,畢竟我也是剛剛開始研究IIC,對(duì)IIC的認(rèn)識(shí)也可能有錯(cuò)誤(說(shuō)不定我的程序就是建立在BUG上的

4.結(jié)果





5.工程源碼及附件
MRC048A-GY-VL53L0XV2 資料.rar (696.82 KB, 下載次數(shù): 5)

VL53L0X -硬件IIC(發(fā)帖).rar (210.5 KB, 下載次數(shù): 5)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产一区二区三区在线视频 | 一区二区三区高清 | 亚洲另类自拍 | 成在线人视频免费视频 | 国产精品成人一区二区 | 日本一区二区三区免费观看 | 成人精品久久 | 男人天堂国产 | 久久精品男人的天堂 | 亚洲欧洲一区 | 美女国内精品自产拍在线播放 | 在线视频中文字幕 | 久久久久精 | 女人毛片a毛片久久人人 | 一区二区三区免费在线观看 | 一区二区高清不卡 | 国产在线精品一区二区三区 | 蜜桃在线视频 | 一区二区三区亚洲精品国 | 精品乱人伦一区二区三区 | 日韩免费高清视频 | 成人欧美一区二区三区黑人孕妇 | 国产在线看片 | 久久精品国产99国产精品 | 欧美日韩视频在线第一区 | 免费久久久久久 | 日韩在线观看网站 | 91观看 | 伊人伊人伊人 | 亚洲啊v在线 | 91麻豆精品国产91久久久久久 | 992人人草 | 国产精品91久久久久久 | 天天操精品视频 | 国产精品福利网 | 麻豆一区二区三区 | 精品国产乱码久久久久久a丨 | 欧美日韩精品综合 | 久久亚洲一区二区三 | 女女百合av大片一区二区三区九县 | 欧美精品一二三 |