As an example of the
operation of the 1-wire function of the CGMMSTICK1, this example
interfaces to a DS1822 temperature device.
From the DS1822
datasheet:
The
DS1822 digital thermometer provides 9- to 12-bit centigrade
temperature measurements and has an alarm function with NV
user-programmable upper and lower trigger points. The DS1822
communicates over a 1-Wire bus that by definition requires only one
data line (and ground) for communication with a central
microprocessor. It has an operating temperature range of –55°C to
+125°C and is accurate to ±2.0°C over the range of –10°C to
+85°C.
Unique
1-Wire® interface requires only one port pin for communication.
Each
device has a unique 64-bit serial code stored in an on-board ROM.
The
hardware couldn't be simpler.
The DS1822 pin 1 was
connected to ground, pin 3 connected to 5V, and the center pin, pin
2, connected to the CGMMSTICK I/O pin 20. The center pin is the data
pin of the 1-wire device. I/O pin 20 has a 5k ohm (two 10k in
parallel) pullup.
From the software
perspective reading from a 1-wire device isn't as simple as just
sending a read command. You have to write to the device first to tell
it you want to read.
Furthermore it takes a
moment for the device to start and complete the temperature
measurement process.
First the 'bus' should
be reset. In this case the 1-wire bus is I/O Pin 20 and only contains
the single DS1822 device.
' Reset the 1w bus, DS1822
device
OWRESET 20
The reset command
selects the pin (bus) to reset and optionally can receive an
indication that there are 1 or more devices present on the bus. This
puts a reset pulse on the bus.
All devices have a
serial number, so that if there are multiple devices on a single I/O
Pin (bus), any individual device can be addressed for access.
In this example there
is only a single device, so rather than talking to it by addressing
it specifically, a command is used that skips the need for a serial
number. This is called “Skip ROM” in the 1-wire terminology.
From the data sheet:
SKIP
ROM [CCh]
The
master can use this command to address all devices on the bus
simultaneously without sending out any ROM code information. For
example, the master can make all DS1822s on the bus perform
simultaneous temperature conversions by issuing a Skip ROM command
followed by a Convert T [44h] command.
Note
that the Read Scratchpad [BEh] command can follow the Skip ROM
command only if there is a single slave device on the bus. In this
case time is saved by allowing the master to read from the slave
without sending the device’s 64-bit ROM code. A Skip ROM command
followed by a Read Scratchpad command will cause a data collision on
the bus if there is more than one slave since multiple devices will
attempt to transmit data simultaneously.
The DS1822 will be told
to start a temperature conversion.
' Start temperature conversion
' Pin 20, send reset first,
send two bytes
' &hCC - Skip ROM
' &h44 - Start conversion
OWWRITE 20, 1, 2, &hCC, &h44
The OWWRITE command
selects a pin/bus to write to. A reset pulse again is sent to that
bus and then two bytes are sent. &hCC is the command byte to skip
the serial number, and &h44 tells the DS1822 to start the
temperature conversion process.
The conversion process
takes a little while to complete, so the program pauses to allow that
conversion to finish.
' Wait a second for conversion
process to finish
PAUSE 100
After that pause, a
command is sent that tells the DS1822 that the next communication
will be a read.
' Read data/temperature
command
' Pin 20, send reset first,
send two bytes
' &hCC - Skip ROM
' &hBE - read data command
OWWRITE 20, 1, 2, &hCC, &hBE
This again resets the
bus and uses “Skip ROM” since this example uses only a single
1-wire device. &hBE is the command to read.
The read command is
next to read the temperature value.
' Read data/temperature from
DS1822
' Pin 20, send reset after,
read two bytes
OWREAD 20, 2, 2, LowTemp,
HighTemp
Note that the bus is
NOT reset at the start of the read command. The DS1822 expects the
read to follow the command to read without a reset between the
commands.
OWREAD reads the
selects bus line. The options to the command allow for a selected
number of bytes to be read. In this case we need to read only the
first two bytes of the multi-byte register, the registers that
contain the temperature data. A bus reset happens after the OWREAD
command.
Math is performed to
combine the two bytes that were read from the temperature device
according to the DS1822 data sheet.
' Combine and adjust according
to data sheet
Cel = ((HighTemp And &b111) *
256 + LowTemp) / 16
The resulting Celsius
temperature is printed, then converted to Fahrenheit and that is also
printed.
' Print Celsius and
convert/print Fahrenheit
Print "Celsius" Cel
Print "Fahrenheit" ( 9
/ 5) * Cel + 32;
Purchase a CGMMSTICK1 Download of Maximite Integrated Development Environment: MMIDE
No comments:
Post a Comment