Using 1-Wire device with Intel Galileo
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的更多相关文章
- Intel Galileo驱动单总线设备(DHT11\DHT22)(转)
Intel Galileo一代的IO翻转速度不够,无法直接驱动单总线设备,二代听说改进了,但没有库,于是国外开发者想出了另一种法子,转过来给大家学习下.如果后面有时间,再来翻译.原文地址:http:/ ...
- Intel Galileo development documentation
Intel Galileo development Documentation Author:Liutianchen 1552227, Department of Computer Science,E ...
- x86 构架的 Arduino 开发板Intel Galileo
RobotPeak是上海的一家硬件创业团队,团队致力于民用机器人平台系统.机器人操作系统(ROS)以及相关设备的设计研发,并尝试将日新月异的机器人技术融入人们的日常生活与娱乐当中.同时,RobotPe ...
- x86 版的 Arduino Intel Galileo 开发板的体验、分析和应用
1.前言 在今年(2013)罗马举办的首届欧洲 Make Faire 上,Intel 向对外发布了采用 x86 构架的 Arduino 开发板:Intel Galileo.这无疑是一个开源硬件领域的重 ...
- Intel Galileo Debian Image Prequits
Intel Galileo开发板 Debian镜像 在原发布者的基础上进行了更新,附带开发入门套件,打包内容: -intel_galileo_debian_xfce4镜像 -约3GB -putty - ...
- Windows on Device 项目实践 5 - 姿态控制灯制作
在前面几篇文章中,我们学习了如何利用Intel Galileo开发板和Windows on Device来设计并完成PWM调光灯.感光灯.火焰报警器和智能风扇的制作,涉及到了火焰传感器.DC直流电机. ...
- Windows on Device 项目实践 4 - 智能风扇制作
在前面的文章中,我们已经学习并且利用Intel Galileo开发板和Windows on Device制作了火焰报警器.感光灯和PWM调光灯.在这个项目中,我们来利用温度传感器和直流电机,完成一个简 ...
- Windows on Device 项目实践 3 - 火焰报警器制作
在前两篇<Windows on Device 项目实践 1 - PWM调光灯制作>和<Windows on Device 项目实践 2 - 感光灯制作>中,我们学习了如何利用I ...
- Windows on Device 项目实践 2 - 感光灯制作
在上一篇<Windows on Device 项目实践 1 - PWM调光灯制作>中,我们学习了如何利用Intel Galileo开发板和Windows on Device来设计并完成一个 ...
随机推荐
- Graph 卷积神经网络:概述、样例及最新进展
http://www.52ml.net/20031.html [新智元导读]Graph Convolutional Network(GCN)是直接作用于图的卷积神经网络,GCN 允许对结构化数据进行端 ...
- Mahout源码目录说明
http://www.cnblogs.com/dlts26/archive/2011/08/23/2150230.html mahout项目是由多个子项目组成的,各子项目分别位于源码的不同目录下,下面 ...
- 10个在UNIX或Linux终端上快速工作的建议
你有没有惊讶地看到有人在Unix/ Linux中工作得非常快,噼里啪啦的敲键盘,快速的启动命令,飞快地执行命令? 在本文中,我共享了一些在Linux中快速.高效工作所遵循的Unix/ Linux命令实 ...
- CMenu and Dialog-based applications
[问] Is it possible to put a menu in a dialog based application? How? [答] Yes, it is possible to add ...
- system函数的应用一例
system函数的应用一例
- 具体解说Android的图片下载框架UniversialImageLoader之磁盘缓存(一)
沉浸在Android的开发世界中有一些年头的猴子们,预计都可以深深的体会到Android中的图片下载.展示.缓存一直是心中抹不去的痛.鄙人亦是如此.Ok,闲话不说.为了督促自己的学习.以下就逐一的挖掘 ...
- mybatis学习资源
官网:http://mybatis.org/index.html 代码:https://code.google.com/p/mybatisnet/ wiki:http://zh.wikipedia.o ...
- Swift语言精要 - 浅谈代理模式(Delegate)
在iOS编程中,我们经常谈到代理代理,也就是delegate,那么什么是代理呢? 我们来看一下cocoa对它的描述: Delegation is Cocoa’s term for passing of ...
- 浅谈关于QT中Webkit内核浏览器
关于QT中Webkit内核浏览器是本文要介绍的内容,主要是来学习QT中webkit中浏览器的使用.提起WebKit,大家自然而然地想到浏览器.作为浏览器内部的主要构件,WebKit的主要工作是渲染.给 ...
- java相关知识集锦
java语言基础知识: Java8 Stream语法详解 不用循环 java 8系列之Stream的基本语法详解 java8 stream filter等功能代替for Java中try catch ...