查看: 3780|回复: 16

AS3935雷电传感器、TCS3471x 颜色逛传感器 代码 library

[复制链接]

该用户从未签到

发表于 2015-5-4 17:24:07 | 显示全部楼层 |阅读模式
分享到:
摘自国外某论坛~
Thought I might share libraries I made for these chips, AS3935 lightning detector chip (yes, lightning) and TCS34711, TCS34713, TCS34715 and TCS34717 color and light sensor chips, by coincidence both by www.ams.com.
楼主自己做的libraries ,有 AS3935 富兰克林传感器,还有各种光电才传感器,颜色传感器,包括TCS34711, TCS34713, TCS34715, TCS34717。


AS3935 is fun little chip that detects lightnings up to 40km afar, since it uses ferrite rod antenna it is somewhat directional, but anyway, it does what it says, detects lightnings. Library using SPI communications -link.
I made this library so that it does not depend on specific implementation of SPI, so one can use bitbanged SPI, hardware SPI or whatever SPI.

AS3935是个非常小非常有趣的芯片,它可以检测到远达 40km 的雷电,因为它使用了 ferrite rod antenna(磁棒天线),这个有点定向,不过,它的确做到了它所描述的。采用SPI通信库(链接指向github,国内打不开的,不过我将程序打包了,就在这段下面),我制作了这个lib,使之不依赖与SPI的特定应用,所以任何人都可以使用 bitbanged SPI,SPI硬件SPI或什么的。



TCS3471 series of color/light sensors are highly sensitive and have 16bit resolution, they use i2c interface. Library I wrote - link.
This library is written so that library code does not access Wire library directly, but rather requires user sketch to provide functions to send and receive data over i2c bus.
I did that for a couple of reasons, one reason is that TCS3471 series of chips have fixed i2c address, another reason, I wanted to make the code usable on most of Arduinos and Arduino compatible platforms and the third reason, this way user can use i2c multiplexer to address multiple chips and library requires no changes to accommodate for that.
According to TCS3471 datasheet, these chips are pin and register compatible with all TCS3x7x chips, but I have tested the library only with TCS34717.

TCS3471 系列 颜色/光传感器 高度敏感、有16位分辨率,使用I2C接口。楼主所写的Library在本段下面,我写这个 library,可以使 library code 不直接访问 Wire library,而是要求用户草图提供功能来发送和接收数据的I2C总线。(不懂不懂
我这样做有几个原因:一个原因是,TCS3471 系列芯片有固定 I2C 地址,另一个原因,我想让代码可用在大多数 Arduinos上,以及 Arduino 兼容的平台,第三个原因是,这样用户可以使用 I2C 多路复用器来处理多个芯片,而不需要改变 library 来适应。
根据 TCS3471 的数据表,这些芯片的引脚和寄存器都和 TCS3x7x 系列芯片兼容,但是我在测试 library 的时候仅仅测试了TCS34717。





回复

使用道具 举报

该用户从未签到

 楼主| 发表于 2015-5-4 17:33:04 | 显示全部楼层
有位网友提了些意见:
had a quick look at the lightning lib, decent piece of work

two remarks
- you can extract writeSPI as it is used a few times.

  1. // private
  2. void _writeSPI(byte val1, byte val2)
  3. {
  4.   digitalWrite(_CSPin,LOW);
  5.   SPITransferFunc(val1);
  6.   SPITransferFunc(val2);
  7.   digitalWrite(_CSPin,HIGH);
  8. }
复制代码

and in the constructor
  1. AS3935::AS3935(byte (*SPItransfer)(byte),int csPin, int irq)
  2. {
  3.         SPITransferFunc = SPItransfer;
  4.         _CSPin = csPin;
  5.         _IRQPin = irq;

  6.         digitalWrite(_CSPin,HIGH);
  7.         pinMode(_CSPin,OUTPUT);
  8.         pinMode(_IRQPin,INPUT);
  9. }
复制代码
swapping the lines this way will prevent a possible (very) short LOW on the _CSPin . It was mentioned in another discussion not so long ago.

楼主回复说:
first issue, well, not sure it would do any good or bad to the size of binary (although, i might check that), otherwise there is no reason to change, as nobody looks at the code of libraries most of the time
second, duly noted and pushed to git, thank you.


回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2015-5-4 17:39:56 | 显示全部楼层
对于楼主的回复:
first issue, well, not sure it would do any good or bad to the size of binary (although, i might check that) otherwise there is no reason to change
这位网友说:
Agree, but especially on the UNO all bytes saved (even a few) add up and frees memory for other functionality.
Please let me know the diff in size (esp for the color lib)

对于楼主的回复:
as nobody looks at the code of libraries most of the time
这位网友说:
I do if the sensors / topic interests me
All that said, I must say you delivered well written code and again thanks for sharing.




那位网友又说到:
The TCS3471 lib looks also like some decent work, but I have a more serious remark here.

In the color library I would remove detect() and all tests to _detected. The _detected constructs makes the library definitely more robust I know.
But more importantly (imho) you cannot have two of these sensors on the bus any more. Most libs give the address as parameter to the constructor.

The footprint of the lib would be smaller (to be tested how much) too.

Still good work!

楼主回复说:
The library was written with this board in mind - 无效链接,which has TXS0104 level converter chip on it, outputs of which can be put in high impedance state, so in theory (not tested yet), one could attach more than one chip to i2c, make multiple instances of TCS3471 object and take care of addressing specific instance in sketch, or one could use i2c multiplexer like TCA9548A and again use multiple instances of TCS3471. Besides, each instance tries to determine correct address on it's own, since different versions of chip can have either address 0x29 or 0x39. If Arduino environment follows regular c++ rules, which I hope it does, since compiler is based on gcc, each instance will have it's own copy of member variables, since none of them is declared as static.
Nevertheless, I might consider reviewing the code if there is enough interest in this, besides this is version 1. Much more footprint could be saved if I got rid of float arguments to some functions, but I decided to leave those in for ease of use


对于楼主的回复:Besides, each instance tries to determine correct address on it's own, since different versions of chip can have either address 0x29 or 0x39.
网友回复到:
But I foresee this scenario:

object1 will test address1 first and find it on the I2C bus => happy
object2 will test address1 first and find it on the I2C bus => happy (oops)

I saw the floating point and yes you can save quite a bit there.
You could use an uint16_t and set the timing in milliseconds to get the 1/10 of second behavior identical.
That would allow for periods up to 65 seconds (if it indicates millis).

If you use an uint16_t for 1/10 of second you can even do longer timings.

Eager to see the size improvements you will get!

楼主说:
User sketch has to take care of defining different sets of communication functions for each of instances, that way one can ensure that right chip is addressed in case of multiplexer or right chip is enabled in case of level shifter.
I left those floats in because milliseconds are less confusing than 10x milliseconds. Allowed values are limited by chip itself, for one of timing functions it is 2.4ms-614.4ms, but for another one it is 2.4-7372.8ms, which translates to uint32_t if 10x the value is used. Anyway, if that becomes a problem, I will re-consider these things.



回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2015-5-4 17:42:00 | 显示全部楼层
另一位网友:
Still trying to compile this code for my Arduino Mega 256.  Just downloaded the chip library and new copy of Arduino.  
  1. AS3935\AS3935.cpp.o: In function `AS3935::_ffsz(unsigned char)':
  2. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:49: multiple definition of `AS3935::_ffsz(unsigned char)'
  3. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:49: first defined here
  4. c:/users/office/desktop/arduino-1.0.4/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
  5. AS3935\AS3935.cpp.o: In function `AS3935::_SPITransfer2(unsigned char, unsigned char)':
  6. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:32: multiple definition of `AS3935::_SPITransfer2(unsigned char, unsigned char)'
  7. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:32: first defined here
  8. AS3935\AS3935.cpp.o: In function `AS3935::reset()':
  9. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:77: multiple definition of `AS3935::reset()'
  10. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:77: first defined here
  11. AS3935\AS3935.cpp.o: In function `AS3935::_rawRegisterRead(unsigned char)':
  12. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:43: multiple definition of `AS3935::_rawRegisterRead(unsigned char)'
  13. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:43: first defined here
  14. AS3935\AS3935.cpp.o: In function `AS3935::registerRead(unsigned char, unsigned char)':
  15. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:66: multiple definition of `AS3935::registerRead(unsigned char, unsigned char)'
  16. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:66: first defined here
  17. AS3935\AS3935.cpp.o: In function `AS3935::getWatchdogThreshold()':
  18. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:209: multiple definition of `AS3935::getWatchdogThreshold()'
  19. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:209: first defined here
  20. AS3935\AS3935.cpp.o: In function `AS3935::getSpikeRejection()':
  21. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:198: multiple definition of `AS3935::getSpikeRejection()'
  22. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:198: first defined here
  23. AS3935\AS3935.cpp.o: In function `AS3935::getNoiseFloor()':
  24. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:187: multiple definition of `AS3935::getNoiseFloor()'
  25. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:187: first defined here
  26. AS3935\AS3935.cpp.o: In function `AS3935::lightningDistanceKm()':
  27. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:172: multiple definition of `AS3935::lightningDistanceKm()'
  28. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:172: first defined here
  29. AS3935\AS3935.cpp.o: In function `AS3935::getMinimumLightnings()':
  30. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:161: multiple definition of `AS3935::getMinimumLightnings()'
  31. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:161: first defined here
  32. AS3935\AS3935.cpp.o: In function `AS3935::interruptSource()':
  33. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:146: multiple definition of `AS3935::interruptSource()'
  34. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:146: first defined here
  35. AS3935\AS3935.cpp.o: In function `AS3935::registerWrite(unsigned char, unsigned char, unsigned char)':
  36. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:55: multiple definition of `AS3935::registerWrite(unsigned char, unsigned char, unsigned char)'
  37. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:55: first defined here
  38. AS3935\AS3935.cpp.o: In function `AS3935::clearStats()':
  39. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:218: multiple definition of `AS3935::clearStats()'
  40. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:218: first defined here
  41. AS3935\AS3935.cpp.o: In function `AS3935::setWatchdogThreshold(int)':
  42. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:212: multiple definition of `AS3935::setWatchdogThreshold(int)'
  43. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:212: first defined here
  44. AS3935\AS3935.cpp.o: In function `AS3935::setSpikeRejection(int)':
  45. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:201: multiple definition of `AS3935::setSpikeRejection(int)'
  46. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:201: first defined here
  47. AS3935\AS3935.cpp.o: In function `AS3935::setNoiseFloor(int)':
  48. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:190: multiple definition of `AS3935::setNoiseFloor(int)'
  49. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:190: first defined here
  50. AS3935\AS3935.cpp.o: In function `AS3935::setOutdoors()':
  51. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:182: multiple definition of `AS3935::setOutdoors()'
  52. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:182: first defined here
  53. AS3935\AS3935.cpp.o: In function `AS3935::setIndoors()':
  54. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:177: multiple definition of `AS3935::setIndoors()'
  55. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:177: first defined here
  56. AS3935\AS3935.cpp.o: In function `AS3935::setMinimumLightnings(int)':
  57. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:164: multiple definition of `AS3935::setMinimumLightnings(int)'
  58. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:164: first defined here
  59. AS3935\AS3935.cpp.o: In function `AS3935::enableDisturbers()':
  60. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:156: multiple definition of `AS3935::enableDisturbers()'
  61. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:156: first defined here
  62. AS3935\AS3935.cpp.o: In function `AS3935::disableDisturbers()':
  63. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:151: multiple definition of `AS3935::disableDisturbers()'
  64. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:151: first defined here
  65. AS3935\AS3935.cpp.o: In function `AS3935::powerDown()':
  66. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:134: multiple definition of `AS3935::powerDown()'
  67. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:134: first defined here
  68. AS3935\AS3935.cpp.o: In function `AS3935::powerUp()':
  69. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:137: multiple definition of `AS3935::powerUp()'
  70. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:137: first defined here
  71. AS3935\AS3935.cpp.o: In function `AS3935::calibrate()':
  72. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:81: multiple definition of `AS3935::calibrate()'
  73. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:81: first defined here
  74. AS3935\AS3935.cpp.o: In function `AS3935':
  75. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:22: multiple definition of `AS3935::AS3935(unsigned char (*)(unsigned char), int, int)'
  76. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:22: first defined here
  77. AS3935\AS3935.cpp.o: In function `AS3935':
  78. C:\Users\office\Documents\Arduino\libraries\AS3935/AS3935.cpp:22: multiple definition of `AS3935::AS3935(unsigned char (*)(unsigned char), int, int)'
  79. AS3935.cpp.o:C:\Users\office\AppData\Local\Temp\build4348146508195175771.tmp/AS3935.cpp:22: first defined here
复制代码
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2015-5-4 17:43:02 | 显示全部楼层
楼上的这位网友:
Just an update, I have tried using Arduino 1.0.1, 1.0.3, and 1.0.4 with the same error.  I am running Windows 7, and have rebooted.  I have also re-downloaded the Lightning sensor library, but it still wont compile.  Anyone have any thoughts?  I would love to get this code on my Megs256!

Solved.  I downloaded the Library a 3rd time from Git, and it compiled just fine.  Not sure what was going on, but something kept getting corrupted.
回复 支持 反对

使用道具 举报

  • TA的每日心情
    开心
    2017-4-1 08:38
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2015-5-5 11:14:20 | 显示全部楼层
    辛苦~~~翻译辛苦了~~
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-15 14:34
  • 签到天数: 651 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2015-5-5 11:40:24 | 显示全部楼层
    这么下去搂住就是学冠中西的专家了,如果有机会实际设计一下,不得了。
    对于不大上外网的童鞋很有帮助。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

     楼主| 发表于 2015-5-5 13:07:02 | 显示全部楼层
    gaon2 发表于 2015-5-5 11:40
    这么下去搂住就是学冠中西的专家了,如果有机会实际设计一下,不得了。
    对于不大上外网的童鞋很有帮助。 ...

    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2020-8-17 11:16
  • 签到天数: 131 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2015-5-6 08:12:03 | 显示全部楼层
    收藏,未来项目该方案可能要用到这个芯片。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2020-8-17 11:16
  • 签到天数: 131 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2015-5-6 08:16:19 | 显示全部楼层
    TCS3471x 颜色传感器有没有赠送开发板啊?
    我现在的一个项目中要用到9个TCS3200,  SOIC8封装,面积大了。
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-11-22 20:44 , Processed in 0.223666 second(s), 35 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.