首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带SoftWire (I2C仿真器)库的多个tcs34725 arduino颜色传感器

带SoftWire (I2C仿真器)库的多个tcs34725 arduino颜色传感器
EN

Stack Overflow用户
提问于 2017-10-09 23:52:44
回答 1查看 2.4K关注 0票数 3

我想在我的Arduino mega上安装2个TCS34725颜色传感器。传感器使用I2c通信,因此我不能将它们放在相同的I2C引脚上,因为它们具有相同的地址。我提出的解决方案是使用一个模拟器线路库"SoftWire"(https://github.com/felias-fogg/SoftI2CMaster),它可以模拟任何2个引脚作为SDA和SCL。这个库的用法完全相同,只是我必须创建一个实例并包含avr/io.h才能启动它:

代码语言:javascript
复制
#include <SoftWire.h>
#include <avr/io.h>
SoftWire Wire = SoftWire();

对我来说,现在的问题是调整TCS34725库以使其与SoftWire一起工作,下面的过程就是我所做的,但它不起作用。下面是原始库的头文件的第一部分:

代码语言:javascript
复制
#ifndef _TCS34725_H_
#define _TCS34725_H_

#if ARDUINO >= 100
  #include <Arduino.h>
#else
  #include <WProgram.h>
#endif

#include <Wire.h>

这是访问完整库的链接:https://github.com/adafruit/Adafruit_TCS34725好的,所以我要做的就是简单地更改

代码语言:javascript
复制
#include<Wire.h> 

代码语言:javascript
复制
#include<SoftWire.h>
#include<avr/io.h>
#define SDA_PORT PORTC
#define SDA_PIN 4
#define SCL_PORT PORTC
#define SCL_PIN 5

我做的另一件事是将Wire实例添加到头文件内的私有变量中:

代码语言:javascript
复制
private:
boolean _tcs34725Initialised;
tcs34725Gain_t _tcs34725Gain;
tcs34725IntegrationTime_t _tcs34725IntegrationTime;
>>>SoftWire Wire;<<<
void     disable(void);

我做的最后一件事是添加:

代码语言:javascript
复制
SoftWire Wire = SoftWire();

在库的cpp文件中包含之后。它看起来就像这样:

代码语言:javascript
复制
#ifdef __AVR
  #include <avr/pgmspace.h>
#elif defined(ESP8266)
  #include <pgmspace.h>
#endif
#include <stdlib.h>
#include <math.h>

#include "Adafruit_TCS34725.h"
SoftWire Wire = SoftWire();

我保存了代码并在草图上运行,代码如下:

代码语言:javascript
复制
#include "SoftWire.h"
#include "avr/io.h"
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup() {
  Serial.begin(9600);
  Serial.println("Color View Test!");

  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }
}

void loop() {
  uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);      // turn on LED

  delay(60);  // takes 50ms to read 

  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);  // turn off LED

  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.print(blue);
}

这个草图在原始的WIRE.h上工作得很好,但现在它不能在SoftWire.h上工作。我是arduino和图书馆的新手。

EN

回答 1

Stack Overflow用户

发布于 2017-12-25 08:58:30

问题解决了!找到一个库,用于放置2个或更多使用软i2c的tcs34725传感器。这里有个链接:Adafruit_TCS34725_SoftI2C-master

感谢这段代码的开发者!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46650433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档