Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2014-12-22 | Zeroday | Zeroday | ow.c |
This module provides functions to work with the 1-Wire device communications bus system.
Computes the 1-Wire CRC16 and compare it against the received CRC.
ow.check_crc16(buf, inverted_crc0, inverted_crc1[, crc])
buf
string value, data to be calculated check sum in stringinverted_crc0
LSB of received CRCinverted_crc1
MSB of received CRCcrc
CRC starting value (optional)
true if the CRC matches, false otherwise
Computes a Dallas Semiconductor 16 bit CRC. This is required to check the integrity of data received from many 1-Wire devices. Note that the CRC computed here is not what you'll get from the 1-Wire network, for two reasons:
- The CRC is transmitted bitwise inverted.
- Depending on the endian-ness of your processor, the binary representation of the two-byte return value may have a different byte order than the two bytes you get from 1-Wire.
ow.crc16(buf[, crc])
buf
string value, data to be calculated check sum in stringcrc
CRC starting value (optional)
the CRC16 as defined by Dallas Semiconductor
Computes a Dallas Semiconductor 8 bit CRC, these are used in the ROM and scratchpad registers.
ow.crc8(buf)
buf
string value, data to be calculated check sum in string
CRC result as byte
Stops forcing power onto the bus. You only need to do this if you used the 'power' flag to ow.write()
or used a ow.write_bytes()
and aren't about to do another read or write.
ow.depower(pin)
pin
1~12, I/O index
nil
####See also
Reads a byte.
####Syntax
ow.read(pin)
pin
1~12, I/O index
byte read from slave device
Reads multi bytes.
ow.read_bytes(pin, size)
pin
1~12, I/O indexsize
number of bytes to be read from slave device (up to 256)
string
bytes read from slave device
Performs a 1-Wire reset cycle.
ow.reset(pin)
pin
1~12, I/O index
1
if a device responds with a presence pulse0
if there is no device or the bus is shorted or otherwise held low for more than 250 µS
Clears the search state so that it will start from the beginning again.
ow.reset_search(pin)
pin
1~12, I/O index
nil
Looks for the next device.
ow.search(pin)
pin
1~12, I/O index
rom_code
string with length of 8 upon success. It contains the rom code of slave device. Returns nil
if search was unsuccessful.
Issues a 1-Wire rom select command. Make sure you do the ow.reset(pin)
first.
ow.select(pin, rom)
pin
1~12, I/O indexrom
string value, len 8, rom code of the slave device
nil
-- 18b20 Example
pin = 9
ow.setup(pin)
count = 0
repeat
count = count + 1
addr = ow.reset_search(pin)
addr = ow.search(pin)
tmr.wdclr()
until (addr ~= nil) or (count > 100)
if addr == nil then
print("No more addresses.")
else
print(addr:byte(1,8))
crc = ow.crc8(string.sub(addr,1,7))
if crc == addr:byte(8) then
if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then
print("Device is a DS18S20 family device.")
repeat
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
tmr.delay(1000000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE,1)
print("P="..present)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
print("CRC="..crc)
if crc == data:byte(9) then
t = (data:byte(1) + data:byte(2) * 256) * 625
t1 = t / 10000
t2 = t % 10000
print("Temperature="..t1.."."..t2.."Centigrade")
end
tmr.wdclr()
until false
else
print("Device family is not recognized.")
end
else
print("CRC is not valid!")
end
end
####See also ow.reset()
Sets a pin in onewire mode.
ow.setup(pin)
pin
1~12, I/O index
nil
Issues a 1-Wire rom skip command, to address all on bus.
ow.skip(pin)
pin
1~12, I/O index
nil
Sets up the search to find the device type family_code
. The search itself has to be initiated with a subsequent call to ow.search()
.
ow.target_search(pin, family_code)
pin
1~12, I/O indexfamily_code
byte for family code
nil
####See also ow.search()
Writes a byte. If power
is 1 then the wire is held high at the end for parasitically powered devices. You are responsible for eventually depowering it by calling ow.depower()
or doing another read or write.
ow.write(pin, v, power)
pin
1~12, I/O indexv
byte to be written to slave devicepower
1 for wire being held high for parasitically powered devices
nil
####See also ow.depower()
Writes multi bytes. If power
is 1 then the wire is held high at the end for parasitically powered devices. You are responsible for eventually depowering it by calling ow.depower()
or doing another read or write.
ow.write_bytes(pin, buf, power)
pin
1~12, IO indexbuf
string to be written to slave devicepower
1 for wire being held high for parasitically powered devices
nil
####See also ow.depower()