Using 1-Wire device with Intel Galileo

Many people have had trouble with getting 1-Wire devices to work with the Galileo and Galileo Gen2 boards.

The main reason is that the Galileo and Galileo Gen2 uses IO expanders for many of  its GPIOs. Even with the pins Arduno header pins that have native SoC IO speeds, it is not possible because the GPIOs that control the muxing of those pins uses pins from the IO expanders.

Galileo Muxing For Pin2

Galileo Cypress IO Expander

If we look at the two images taken form the Galileo schematic, IO which is connected to the Arduino Digital 2 pin, is controlled by a mux pin IO2_MUX. This pin is then connected to the Cypress IO expander.

The end result is there is significant latency when switching pin direction using pinMode() becuase it requires I2C transactions with the IO expanders.

I will show a way to use 1-Wire device with the Galileo and Galileo Gen2 boards.

The trick is to use 2 pins instead of 1. For the Galileo, pins 2 and 3 must be used since they are the only pins fast enough to achieve this. For the Galileo Gen2, any pins except pins 7 and 8 can be used.

For this tutorial, I will use a DHT11 sensor which a very cheap and popular 1-Wire humidity and temperature sensor.

The Proper Way

The proper way of doing this is to use a tri-state buffer.

This works because, when pin 3 is pulled HIGH, the tri-state buffer prevents the HIGH signal from being passed to the other side. However, the 1-Wire device still sees a high signal because of the pull-up resistor.
When pin 3 is pulled LOW, the signal passes through the tri-state buffer and a LOW signal is detected by the 1-Wire device.

The Easy Way

For the easy way the only extra hardware needed is a diode such as the 1N4148 signal diode. This essential works the same way as the tri-state buffer method where only a LOW signal passes through because current only passes in one direction through a diode.

Since we are now using two pins, we will also need to make changes in the libraries used. As an example, I modified the DHT-11 library from Adafruit.

 #ifndef DHT_H
#define DHT_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif /* DHT library MIT license
written by Adafruit Industries Modified by Dino Tinitigan (dino.tinitigan@intel.com to work on Intel Galileo boards
*/ // how many timing transitions we need to keep track of. 2 * number bits + extra
#define MAXTIMINGS 85 #define DHT11 11
#define DHT22 22
#define DHT21 21
#define AM2301 21 class DHT {
private:
uint8_t data[];
uint8_t _inpin, _outpin, _type, _count;
unsigned long _lastreadtime;
boolean firstreading;
int pulseLength(int pin);
void delayMicrosGal(unsigned long usec);
int bitsToByte(int bits[]); public:
DHT(uint8_t inPin, uint8_t outPin,uint8_t type, uint8_t count=);
void begin(void);
float readTemperature(bool S=false);
float convertCtoF(float);
float computeHeatIndex(float tempFahrenheit, float percentHumidity);
float readHumidity(void);
boolean read(void); };
#endif
 /* DHT library 

 MIT license
written by Adafruit Industries Modified by Dino Tinitigan (dino.tinitigan@intel.com to work on Intel Galileo boards
*/ #include "DHT.h" DHT::DHT(uint8_t inPin, uint8_t outPin, uint8_t type, uint8_t count) {
_inpin = inPin;
_outpin = outPin;
_type = type;
_count = count;
firstreading = true;
} void DHT::begin(void) {
// set up the pins!
pinMode(_inpin, INPUT);
pinMode(_outpin, INPUT);
digitalWrite(_outpin, HIGH);
_lastreadtime = ;
} //boolean S == Scale. True == Farenheit; False == Celcius
float DHT::readTemperature(bool S) {
float f; if (read()) {
switch (_type) {
case DHT11:
f = data[];
if(S)
f = convertCtoF(f); return f;
case DHT22:
case DHT21:
f = data[] & 0x7F;
f *= ;
f += data[];
f /= ;
if (data[] & 0x80)
f *= -;
if(S)
f = convertCtoF(f); return f;
}
}
return NAN;
} float DHT::convertCtoF(float c) {
return c * / + ;
} float DHT::readHumidity(void) {
float f;
if (read()) {
switch (_type) {
case DHT11:
f = data[];
return f;
case DHT22:
case DHT21:
f = data[];
f *= ;
f += data[];
f /= ;
return f;
}
}
return NAN;
} float DHT::computeHeatIndex(float tempFahrenheit, float percentHumidity) {
// Adapted from equation at: https://github.com/adafruit/DHT-sensor-library/issues/9 and
// Wikipedia: http://en.wikipedia.org/wiki/Heat_index
return -42.379 +
2.04901523 * tempFahrenheit +
10.14333127 * percentHumidity +
-0.22475541 * tempFahrenheit*percentHumidity +
-0.00683783 * pow(tempFahrenheit, ) +
-0.05481717 * pow(percentHumidity, ) +
0.00122874 * pow(tempFahrenheit, ) * percentHumidity +
0.00085282 * tempFahrenheit*pow(percentHumidity, ) +
-0.00000199 * pow(tempFahrenheit, ) * pow(percentHumidity, );
} boolean DHT::read(void) {
uint8_t laststate = HIGH;
uint8_t counter = ;
uint8_t j = , i;
unsigned long currenttime; int bitContainer[]; // Check if sensor was read less than two seconds ago and return early
// to use last reading.
currenttime = millis();
if (currenttime < _lastreadtime) {
// ie there was a rollover
_lastreadtime = ;
}
if (!firstreading && ((currenttime - _lastreadtime) < )) {
return true; // return last correct measurement
//delay(2000 - (currenttime - _lastreadtime));
}
firstreading = false;
/*
Serial.print("Currtime: "); Serial.print(currenttime);
Serial.print(" Lasttime: "); Serial.print(_lastreadtime);
*/
_lastreadtime = millis(); data[] = data[] = data[] = data[] = data[] = ; // pull the pin high and wait 250 milliseconds
pinMode(_outpin, OUTPUT_FAST);
pinMode(_inpin, INPUT_FAST);
digitalWrite(_outpin, HIGH);
delay(); // now pull it low for ~20 milliseconds
noInterrupts();
digitalWrite(_outpin, LOW);
delay();
digitalWrite(_outpin, HIGH);
delayMicrosGal(); //read the 40 bits
delayMicrosGal();
for(int bytes = ; bytes < ; bytes++)
{
for(int i = ; i < ; i++)
{
int pulse = pulseLength(_inpin);
if(pulse > )
{
bitContainer[i] = ;
}
else
{
bitContainer[i] = ;
}
}
data[bytes] = bitsToByte(bitContainer);
} interrupts(); //Serial.println(j, DEC);
/**
Serial.print(data[0], HEX); Serial.print(", ");
Serial.print(data[1], HEX); Serial.print(", ");
Serial.print(data[2], HEX); Serial.print(", ");
Serial.print(data[3], HEX); Serial.print(", ");
Serial.print(data[4], HEX); Serial.print(" =? ");
Serial.println(data[0] + data[1] + data[2] + data[3], HEX);
**/
/**
Serial.println(data[0]);
Serial.println(data[1]);
Serial.println(data[2]);
Serial.println(data[3]);
Serial.println(data[4]);
**/
// check we read 40 bits and that the checksum matches if((data[] == ((data[] + data[] + data[] + data[]) & 0xFF)))
{
return true;
} return false; } void DHT::delayMicrosGal(unsigned long usec)
{
unsigned long a = micros();
unsigned long b = a;
while((b-a) < usec)
{
b = micros();
}
} int DHT::pulseLength(int pin)
{
unsigned long a = micros();
unsigned long b = a;
unsigned long c = a;
int timeout = ;
int fastPin = ;
int highValue = ; if(PLATFORM_NAME == "GalileoGen2")
{
switch(pin)
{
case :
fastPin = GPIO_FAST_ID_QUARK_SC(0x08);
highValue = 0x08;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_SC(0x10);
highValue = 0x10;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_SC(0x20);
highValue = 0x20;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_SC(0x40);
highValue = 0x40;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_NC_RW(0x10);
highValue = 0x10;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_NC_CW(0x01);
highValue = 0x01;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_NC_CW(0x02);
highValue = 0x02;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_NC_RW(0x04);
highValue = 0x04;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_SC(0x04);
highValue = 0x04;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_NC_RW(0x08);
highValue = 0x08;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_SC(0x80);
highValue = 0x80;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_NC_RW(0x20);
highValue = 0x20;
break;
default:
highValue = ;
break;
}
}
else
{
switch(_inpin)
{
case :
fastPin = GPIO_FAST_ID_QUARK_SC(0x40);
highValue = 0x40;
break;
case :
fastPin = GPIO_FAST_ID_QUARK_SC(0x80);
highValue = 0x80;
break;
default:
highValue = ;
break;
}
}
a = micros();
while(fastGpioDigitalRead(fastPin) == )
{
b= micros();
if((b - a) >= timeout)
{
break;
}
}
a = micros();
while(fastGpioDigitalRead(fastPin) == highValue)
{
b= micros();
if((b - a) >= timeout)
{
break;
}
}
return (b - a);
return ;
} int DHT::bitsToByte(int bits[])
{
int data = ;
for(int i = ; i < ; i++)
{
if (bits[i])
{
data |= (int)( << ( - i));
}
}
return data;
}
 // Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain #include "DHT.h" #define DHTIN 2 // what pin we're connected to
#define DHTOUT 3 // Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301) // Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor DHT dht(DHTIN,DHTOUT, DHTTYPE); void setup() {
Serial.begin();
Serial.println("DHTxx test!"); dht.begin();
} void loop() {
// Wait a few seconds between measurements.
delay(); // Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
} // Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h); Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.println(" *F");
}

原文地址:http://bigdinotech.com/tutorials/galileo-tutorials/using-1-wire-device-with-intel-galileo/

Using 1-Wire device with Intel Galileo的更多相关文章

  1. Intel Galileo驱动单总线设备(DHT11\DHT22)(转)

    Intel Galileo一代的IO翻转速度不够,无法直接驱动单总线设备,二代听说改进了,但没有库,于是国外开发者想出了另一种法子,转过来给大家学习下.如果后面有时间,再来翻译.原文地址:http:/ ...

  2. Intel Galileo development documentation

    Intel Galileo development Documentation Author:Liutianchen 1552227, Department of Computer Science,E ...

  3. x86 构架的 Arduino 开发板Intel Galileo

    RobotPeak是上海的一家硬件创业团队,团队致力于民用机器人平台系统.机器人操作系统(ROS)以及相关设备的设计研发,并尝试将日新月异的机器人技术融入人们的日常生活与娱乐当中.同时,RobotPe ...

  4. x86 版的 Arduino Intel Galileo 开发板的体验、分析和应用

    1.前言 在今年(2013)罗马举办的首届欧洲 Make Faire 上,Intel 向对外发布了采用 x86 构架的 Arduino 开发板:Intel Galileo.这无疑是一个开源硬件领域的重 ...

  5. Intel Galileo Debian Image Prequits

    Intel Galileo开发板 Debian镜像 在原发布者的基础上进行了更新,附带开发入门套件,打包内容: -intel_galileo_debian_xfce4镜像 -约3GB -putty - ...

  6. Windows on Device 项目实践 5 - 姿态控制灯制作

    在前面几篇文章中,我们学习了如何利用Intel Galileo开发板和Windows on Device来设计并完成PWM调光灯.感光灯.火焰报警器和智能风扇的制作,涉及到了火焰传感器.DC直流电机. ...

  7. Windows on Device 项目实践 4 - 智能风扇制作

    在前面的文章中,我们已经学习并且利用Intel Galileo开发板和Windows on Device制作了火焰报警器.感光灯和PWM调光灯.在这个项目中,我们来利用温度传感器和直流电机,完成一个简 ...

  8. Windows on Device 项目实践 3 - 火焰报警器制作

    在前两篇<Windows on Device 项目实践 1 - PWM调光灯制作>和<Windows on Device 项目实践 2 - 感光灯制作>中,我们学习了如何利用I ...

  9. Windows on Device 项目实践 2 - 感光灯制作

    在上一篇<Windows on Device 项目实践 1 - PWM调光灯制作>中,我们学习了如何利用Intel Galileo开发板和Windows on Device来设计并完成一个 ...

随机推荐

  1. scikit-learn——快速入门 - daniel-D(转)

    ML sklearn快速入门 申明:该系列博客是学习 sklearn 的笔记,内容将涵盖大部分机器学习的方法.本人微博@迅猛龙Daniel,能力有限,存在任何问题,希望共同交流.该博客采用马克飞象专业 ...

  2. Linq-查询上一条下一条

    //下一条 int pollid = poll.Where(f => f.PollID < CurrentId).OrderByDescending(o => o.PollID).F ...

  3. Spring(十四):使用FactoryBean配置Bean

    FactoryBean简介: 1)Spring中Bean包含两种一种是普通Bean,另外一种是FactoryBean.它们都受IOC容器管理,但是也有不同之处. 2)普通Bean与FactoryBea ...

  4. Eclipse 2017最佳20个插件

    https://www.infoworld.com/article/2606814/development-tools/development-tools-12-eclipse-plug-ins-ev ...

  5. PHP构造函数的重载

    由于PHP是弱类型语言,因此函数的输入参数类型无法确定(可以使用类型暗示,但是类型暗示无法用在诸如整型,字符串之类的标量类型上),并且对于一个函数,比如只定义了3个输入参数,PHP却运行调用的时候输入 ...

  6. [Docker] Getting Started with Container Networks

    It is possible to group containers into a network and we can create multi networks so that container ...

  7. Android开发——Android M(6.0) 权限解决方案

    Android开发--Android M(6.0) 权限解决方案 自从Android M(6.0)发布以来,权限管理相比以前有了很大的改变,很多程序员发现之前运行的好好的Android应用在Andro ...

  8. Internet传输协议-TCP

    http://phei.eefocus.com/book/08-07/473781276058574.html http://www.eefocus.com/communication/210643 ...

  9. Android学习笔记四:activity的四种启动模式

    Activity有四种启动模式:standard,singleTop,singleTask,singleInstance. 1.standard:标准启动模式 默认模式,这个模式下启动的Activit ...

  10. PgSQL · 源码分析· pg_dump分析

    PostgreSQL本身提供了逻辑导出工具pg_dumpall和pg_dump,其中pg_dumpall导出所有的数据库,pg_dump导出单个数据库,两个工具的用法和参数不再详细介绍,本文从代码层面 ...