SBOA542 November 2022 TMP1826 , TMP1827
The application example demonstrates a simple operation of the host sending a command to read the temperature from the TMP1826 device. As the application example shows, the basic units of bus communication are mcu_txOneWireReset, mcu_txOneWireByte, and mcu_rxOneWireByte.
// Send Bus Reset
mcu_txOneWireReset();
// Send SKIP ADDRESS command
mcu_txOneWireByte(0xCC);
// Send TEMP CONVERT function
mcu_txOneWireByte(0x44);
// Wait for tCONV = 5.5 ms
DelayMS(6);
// Send Bus Reset
mcu_txOneWireReset();
// Send SKIP ADDRESS command
mcu_txOneWireByte(0xCC);
// Send READ SCRATCHPAD-1 function
mcu_txOneWireByte(0xBE);
// Read two bytes of temperature data
TempLSB = mcu_rxOneWireByte();
TempMSB = mcu_rxOneWireByte();
When using GPIO for communicating with the TMP1826, the host MCU requires a delay counter to implement the communication. The following pseudocode shows how to implement bus reset and response using GPIO with the input or output function and a delay counter.
void mcu_txOneWireReset()
{
// Send Bus Reset
GPIOWrite(GPIO.OUT,0);
// Wait for tRSTL = 48 us
DelayUS(48);
// Release Bus Reset
GPIOWrite(GPIO.OUT,1);
// Wait for tPDH = 8 us
DelayUS(8);
// Wait for Bus Reset Response
while(GPIORead(GPIO.IN) != 0);
while(GPIORead(GPIO.IN) != 1);
}
Figure 2-3 shows how the host drives the GPIO.OUT pin low for tRSTL (indicated by marker pair 0) and then drives the pin high afterwards. The TMP1826 then waits for tPDH and drives the SDQ pin low for tPDL (indicated by marker pair 1). The host checks on the pin GPIO.IN for the response from TMP1826.
Once the bus reset is sent and the response received, the host then sends the address command and function command to the device. As all commands and data transfers are at the byte boundary, the following pseudocode illustrates how to send the bytes and bits to the device.
void mcu_txOneWireByte(uint8_t bytevalue)
{
uint8_t count;
// Shift bits LSB to MSB
for(count=0; count<8; count++)
{
mcu_txOneWireBit((bytevalue >> count) & 0x01);
}
}
void mcu_txOneWireBit(uint8_t bitvalue)
{
// If logic '0' then set GPIO to 0 and wait for tWR0L
if(bitvalue == 0x00)
{
GPIOWrite(GPIO.OUT,0);
// Wait for tWR0L = 10 us
DelayUS(10);
}
// If logic '1' then set GPIO to 0 and wait for tWR1L
else if(bitvalue == 0x01)
{
GPIOWrite(GPIO.OUT,0);
// Wait for tWR1L = 2 us
DelayUS(2);
}
// Release the GPIO
GPIOWrite(GPIO.OUT,1);
// Wait for tREC = 2 us
DelayUS(2);
}
Figure 2-4 shows the write byte transfer of address command 0hCC (SKIPADDR) and function command 0h44 (CONVERTTEMP) for temperature conversion. During the write operation, the GPIO.OUT is set low in accordance with tWR0L and tWR1L to indicate logic '0' and logic '1', respectively. As the device is always in receive state, the device does not respond on the SDQ pin and hence the GPIO.IN pin mirrors the GPIO.OUT.
The final set of pseudocode (following) shows how to implement a read from the device after sending an address or function command which the device can respond to. As with data transfer to the device, whenever data is read back from the device, the read is done at byte boundary. The following function shows how to implement both the byte and bit read functions.
uint8_t mcu_rxOneWireByte(void)
{
uint8_t count;
uint8_t rxbyte = 0x00;
// Shift bits LSB to MSB
for(count=0; count<8; count++)
{
rxbyte |= (mcu_rxOneWireBit() << count);
}
}
uint8_t mcu_rxOneWireBit(void)
{
uint8_t rxbit;
// Send the falling edge to begin the read
GPIOWrite(GPIO.OUT,0);
// Wait for tRL = 2 us and release the bus
DelayUS(2);
GPIOWrite(GPIO.OUT,1);
// Wait for tRC and sample the state of the bus
DelayUS(1);
rxbit = GPIORead(GPIO.IN);
// Wait for remainder of the slot time (tSLOT-tMSW)
DelayUS(8);
return(rxbit);
}
Figure 2-5 shows the write byte of the function command 0hBE (READ SCRATCHPAD-1) and read byte that follows the write byte. The host during the read operation drives GPIO.OUT low for tRL. As indicated by the marker pair 2, if the device has a logic '1' to send to the host, the device does not drive the SDQ pin. When the device has a logic '0' to send, the device drives the SDQ pin low as shown by the marker pair 3. The host must sample the GPIO.IN at tMSW to check if the device is sending a logic '1' or logic '0'.