ADVENTURES IN PROPELLER PROGRAMMING
Post comments on this article and find any associated files and/or downloads at
www.nutsvolts.com/index.php?/magazine/article/december2015_SpinZone.
is public in case communications are lost with the device.
Generally speaking, communications with the SHT11
are similar to I2C in that we’re using an open-drain
connection on the data line, we transmit eight bits at a
time, and the receiving end will acknowledge (pull the
data line low) on the ninth clock pulse. All
communications begin with a transmission start sequence
(see right side of Figure 3), followed by write and read
transactions. As we need it to initiate all transactions, the
■ FIGURE 3. SHT11 Connection Reset and
Transmission Start.
pub trans_start
dira[dio] := 0
outa[sck] := 1
dira[dio] := 1
outa[sck] := 0
outa[sck] := 1
dira[dio] := 0
outa[sck] := 0
The trans_start() method begins by releasing the DIO
pin to the module’s pull-up, taking the line high as
required. The clock line goes high, followed by writing 1
to the dira[dio]. As the outa bit for the DIO pin was
initialized to 0, this action will cause the DIO pin to
become an output and low, pulling the DIO line low as
required by the sequence. The first clock is finished, the
second started, then the DIO line is released to the pull-up
again by writing 0 to dira[dio]. This sequence
illustrates using open-drain output on the DIO pin while
the SCK (clock) pin is driven.
Now, we can drop into the actual transmission loop
that will run eight times; once per bit. The first line rotates
b by one bit; what this does is shift all bits left by one,
then write what was in bit31 to bit0 (that last part is the
difference between a shift and a rotate). By rotating left,
we are accessing the bits from the MSB side of the value.
Now, we can write that to the dira[dio] bit to control
the DIO line. A 1 will cause the line to be pulled low; a 0
will allow the line to be released to the pull-up. When
DIO is set up, the bit is clocked out by taking the SCK pin
high, then back low.
After the byte is sent, we must release the DIO pin so
that the SHT11 can create the ACK/NAK bit. With the
Communications with the module are accomplished
with the wr_byte() and rd_byte() methods. Let’s start with
wr_byte() as it can be tricky to understand at first:
DIO pin set to input mode, we take the clock pin back
high and then read the state of the DIO pin. A good
transmission will result in the DIO pin being pulled low by
the SHT11 during this ninth clock pulse. The state of the
pub wr_byte(b) | ackbit
ACK/NAK bit is returned to the caller. As you’d expect,
the rd_byte() method reverses the process:
return ackbit
pub rd_byte(ackbit) | b
return b
We start by setting the DIO pin to input mode and
clearing the result variable b. Inside the loop (which runs
eight times), we take the clock high, sample the DIO pin
into the LSB side of the result, then take the clock back
low. Note that the first part of the sample line shifts the