Finished HDC1000 chip program

This commit is contained in:
LazyAssassin445
2017-02-26 15:31:33 +00:00
parent d3d78d0270
commit 89a98e9393
6 changed files with 177 additions and 58 deletions

Binary file not shown.

View File

@@ -3,10 +3,7 @@
import time
from pprint import pprint
try:
import smbus2 as smbus
except ImportError:
import smbus
import i2c
I2CADDR = 0x40
@@ -14,7 +11,7 @@ TEMP_REG = 0x00
HUMID_REG = 0x01
CONFIG_REG = 0x02
CONFIG_RST = (1 << 15)
CONFIG_RESET = (1 << 15)
CONFIG_HEAT = (1 << 13)
CONFIG_MODE_SINGLE = 0
CONFIG_MODE_BOTH = (1 << 12)
@@ -31,74 +28,55 @@ SERIAL3 = 0xFD
MANUFID = 0xFE
DEVICEID = 0xFF
try:
bus = smbus.SMBus(1)
except:
print("No I2C bus found.")
bus = i2c.IIC(I2CADDR, 1)
def reset():
def reset(extra = 0):
config = (
CONFIG_RST |
CONFIG_MODE_BOTH |
CONFIG_RESET |
CONFIG_MODE_SINGLE |
CONFIG_TRES_14 |
CONFIG_HRES_14
CONFIG_HRES_14 |
extra
)
bus.write_word_data(I2CADDR, CONFIG_REG, config)
bus.i2c([CONFIG_REG, config >> 8], 0)
def readSensors():
bus.write_byte(I2CADDR, TEMP_REG)
def temperature():
# Request temperature measurement
bus.i2c([TEMP_REG], 0)
time.sleep(0.05)
time.sleep(0.2)
temp = bus.read_byte(I2CADDR)
temp << 8
temp |= bus.read_byte(I2CADDR)
data = bus.read(2)
temp = (data[0] << 8) | data[1]
#print(hex(temp))
return (temp / 65536.0) * 165.0 - 40
hum = bus.read_byte(I2CADDR)
hum << 8
hum |= bus.read_byte(I2CADDR)
def humidity():
# Request humidity measurement
bus.i2c([HUMID_REG], 0)
print(hex(temp))
print(hex(hum))
time.sleep(0.2)
#float temp = (read32(HDC1000_TEMP, 20) >> 16);
#temp /= 65536;
#temp *= 165;
#temp -= 40;
#float hum = (read32(HDC1000_TEMP, 20) & 0xFFFF);
#hum /= 65536;
#hum *= 100;
#return hum;
data = bus.read(2)
hum = (data[0] << 8) | data[1]
#print(hex(hum))
return (hum / 65536.0) * 100.0
def drySensor():
origconfig = bus.read_word_data(I2CADDR, CONFIG_REG)
newconfig = (
CONFIG_RST |
CONFIG_HEAT |
CONFIG_MODE_BOTH |
CONFIG_TRES_14 |
CONFIG_HRES_14
)
# Turn on the heater
reset(CONFIG_HEAT)
bus.write_word_data(I2CADDR, CONFIG_REG, newconfig)
# Take 1000 reading as fast as possible
# (the heater is only activated when performing a reading)
for x in range(10000):
try:
temperature()
time.sleep(0.1)
except: pass
sleep(0.1)
# Take 1000 readings
for x in range(0, 1000):
triggerMeasurements()
sleep(0.1)
sleep(0.1)
origconfig |= CONFIG_RST;
bus.write_word_data(I2CADDR, CONFIG_REG, origconfig)
# Turn off the heater
reset()
def init():
reset()
manuf = bus.read_word_data(I2CADDR, MANUFID)
dev = bus.read_word_data(I2CADDR, DEVICEID)
print("Manufacturer:", manuf, "Device:", dev)

62
modules/i2c.py Normal file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/env python
# Contributed by Raju and updated by Jim
#
# Use:
# import i2c
# device = i2c.IIC(<device i2c address 7 bit>,<Bus number>)
# example
# dev = i2c.IIC(0x32,1)
# dev.i2c([10,1,0,1],1) # sends bytes 10,1,0,1 and returns 1 byte
# dev.close()
#
import io
import fcntl
# i2c.py
# 2016-03-14
# Public Domain
I2C_SLAVE=0x0703
# Based on 'notSMB' for an easier way to access the i2c bus using just one
# function. The main difference between this and notSMB is that the bus
# here will be dedicated to 1 device address
class IIC:
def __init__(self, device, bus):
self.fr = io.open("/dev/i2c-"+str(bus), "rb", buffering=0)
self.fw = io.open("/dev/i2c-"+str(bus), "wb", buffering=0)
# set device address
fcntl.ioctl(self.fr, I2C_SLAVE, device)
fcntl.ioctl(self.fw, I2C_SLAVE, device)
def write(self, data):
if type(data) is list:
data = bytes(data)
self.fw.write(data)
def read(self, count):
s = ''
l = []
s = self.fr.read(count)
return s
#if len(s) != 0:
# for n in s:
# l.append(rd(n))
# return l
def close(self):
self.fw.close()
self.fr.close()
def i2c(self,listin,nout):
self.write(bytearray(listin))
rv = 0
if nout != 0:
rv = self.read(nout)
return rv

57
modules/testhdc1000.py Normal file
View File

@@ -0,0 +1,57 @@
#stributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# HDC1000
# This code is designed to work with the HDC1000_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Temperature?sku=HDC1000_I2CS#tabs-0-product_tabset-2
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# HDC1000 address, 0x40(64)
# Select configuration register, 0x02(02)
# 0x30(48) Temperature, Humidity enabled, Resolultion = 14-bits, Heater on
bus.write_byte_data(0x40, 0x02, 0x30)
# HDC1000 address, 0x40(64)
# Send temp measurement command, 0x00(00)
bus.write_byte(0x40, 0x00)
time.sleep(0.5)
# HDC1000 address, 0x40(64)
# Read data back, 2 bytes
# temp MSB, temp LSB
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
# Convert the data
temp = (data0 * 256) + data1
print(hex(temp))
cTemp = (temp / 65536.0) * 165.0 - 40
fTemp = cTemp * 1.8 + 32
# HDC1000 address, 0x40(64)
# Send humidity measurement command, 0x01(01)
bus.write_byte(0x40, 0x01)
time.sleep(0.5)
# HDC1000 address, 0x40(64)
# Read data back, 2 bytes
# humidity MSB, humidity LSB
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
# Convert the data
humidity = (data0 * 256) + data1
print(hex(humidity))
humidity = (humidity / 65536.0) * 100.0
# Output data to screen
print "Relative Humidity : %.2f %%" %humidity
print "Temperature in Celsius : %.2f C" %cTemp
print "Temperature in Fahrenheit : %.2f F" %fTemp

11
testprograms/camera/oneshot Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/python3
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(5)
camera.capture('/home/pi/camera/pics/image.jpg')
camera.stop_preview()

11
testprograms/camera/preview Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/python3
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(10)
camera.stop_preview()