Other Parts Discussed in Thread: TM4C129XNCZAD, TM4C1294NCPDT
Hello, I'm using a TM4C1294 launchpad and is trying to communicate with the DS18B20+ temperature sensor (this is practically the same as the DS18B20). I'm using the UART modules that the microcontroller provides to interface with the 1-wire protocol that the sensor uses. The circuitry can be found in Figure 2b on this link:
Here is the code:
//*****************************************************************************
//
// This functions checks the temperature inside the compost container.
//
//*****************************************************************************
uint32_t checkTemperature(uint32_t g_ui32SysClock)
{
int32_t temperature_value_1 = 0;
int32_t temperature_value_2 = 0;
int32_t temperature_value_3 = 0;
int32_t decimal_value_1 = 0;
int32_t decimal_value_2 = 0;
int32_t decimal_value_3 = 0;
int32_t detect_1 = 0;
int32_t detect_2 = 0;
int32_t detect_3 = 0;
int32_t i = 0;
int32_t data = 0;
char temp_var_1[4] = {};
char temp_var_2[4] = {};
char temp_var_3[4] = {};
bool temp_1_is_negative = false;
bool temp_2_is_negative = false;
bool temp_3_is_negative = false;
//
// Initialize the UARTs for reset and presence detection.
//
configTempSensUART(g_ui32SysClock, 9600);
//
// Check if devices are present and ready.
//
while (1)
{
//
// Transmit reset pulse to 1-wire devices.
//
transmitTempSensPulse(0xF0);
//
// Get the values in the receive FIFO.
//
while ((detect_1 = MAP_UARTCharGetNonBlocking(UART5_BASE)) == -1);
while ((detect_2 = MAP_UARTCharGetNonBlocking(UART6_BASE)) == -1);
while ((detect_3 = MAP_UARTCharGetNonBlocking(UART7_BASE)) == -1);
//
// If values are not equal to 0xF0, then devices are detected.
//
if ((detect_1 != 0xF0) && (detect_2 != 0xF0) && (detect_3 != 0xF0))
{
break;
}
//
// Display message.
//
UARTprintf("Temperature sensors not detected. Make sure all three "
"sensors are connected.\n");
}
//
// Re-initialize the UARTs for sending commands and reading.
//
configTempSensUART(g_ui32SysClock, 115200);
//
// Transmit command 0xCC to skip ROM command.
//
transmitTempSensCommand(0xCC);
//
// Transmit command 0x44 to initiate temperature conversion.
//
transmitTempSensCommand(0x44);
MAP_SysCtlDelay(g_ui32SysClock / 3 / 1000 * 750); // 750 ms delay
//
// Re-initialize device access.
//
configTempSensUART(g_ui32SysClock, 9600);
transmitTempSensPulse(0xF0);
configTempSensUART(g_ui32SysClock, 115200);
transmitTempSensCommand(0xCC);
//
// Transmit command 0xBE to get temperature data.
//
transmitTempSensCommand(0xBE);
//
// Configuring the UART disables and enables the UART. Here it is being
// used to clear the FIFO before reading.
//
configTempSensUART(g_ui32SysClock, 115200);
//
// Tell device to send value back. Total of 2 bytes, but stored over 4
// indexes, with 4 bits per index.
//
for (i = 15; i >= 0; --i)
{
//
// Send a read pulse to device, then clear the FIFO.
//
transmitTempSensPulse(0xFF);
//
// Get the temperature value from sensor 1.
//
while ((data = MAP_UARTCharGetNonBlocking(UART5_BASE)) == -1);
if (data == 0xFF)
{
temp_var_1[i/4] >>= 1;
temp_var_1[i/4] |= 0x08;
}
else
{
temp_var_1[i/4] >>= 1;
}
//
// Get the temperature value from sensor 2.
//
while ((data = MAP_UARTCharGetNonBlocking(UART6_BASE)) == -1);
if (data == 0xFF)
{
temp_var_2[i/4] >>= 1;
temp_var_2[i/4] |= 0x08;
}
else
{
temp_var_2[i/4] >>= 1;
}
//
// Get the temperature value from sensor 3.
//
while ((data = MAP_UARTCharGetNonBlocking(UART7_BASE)) == -1);
if (data == 0xFF)
{
temp_var_3[i/4] >>= 1;
temp_var_3[i/4] |= 0x08;
}
else
{
temp_var_3[i/4] >>= 1;
}
}
//
// Stop reading data.
//
configTempSensUART(g_ui32SysClock, 9600);
transmitTempSensPulse(0xF0);
//
// Check if temperatures are in the negatives.
//
if (temp_var_1[0] == 0x0F)
{
temp_1_is_negative = true;
}
if (temp_var_2[0] == 0x0F)
{
temp_2_is_negative = true;
}
if (temp_var_3[0] == 0x0F)
{
temp_3_is_negative = true;
}
//
// Convert data to Celsius.
//
temperature_value_1 = (temp_var_1[1] << 4) | temp_var_1[2];
temperature_value_2 = (temp_var_2[1] << 4) | temp_var_2[2];
temperature_value_3 = (temp_var_3[1] << 4) | temp_var_3[2];
decimal_value_1 = temp_var_1[3] * 625;
decimal_value_2 = temp_var_2[3] * 625;
decimal_value_3 = temp_var_3[3] * 625;
while (decimal_value_1 % 10 == 0)
{
decimal_value_1 /= 10;
}
while (decimal_value_2 % 10 == 0)
{
decimal_value_2 /= 10;
}
while (decimal_value_3 % 10 == 0)
{
decimal_value_3 /= 10;
}
//
// Print temperature values.
//
if (temp_1_is_negative)
{
UARTprintf("temperature_value_1 = -%d.%d %cC\n", temperature_value_1,
decimal_value_1, 248);
}
else
{
UARTprintf("temperature_value_1 = %d.%d %cC\n", temperature_value_1,
decimal_value_1, 248);
}
if (temp_2_is_negative)
{
UARTprintf("temperature_value_2 = -%d.%d %cC\n", temperature_value_2,
decimal_value_2, 248);
}
else
{
UARTprintf("temperature_value_2 = %d.%d %cC\n", temperature_value_2,
decimal_value_2, 248);
}
if (temp_3_is_negative)
{
UARTprintf("temperature_value_3 = -%d.%d %cC\n\n", temperature_value_3,
decimal_value_3, 248);
}
else
{
UARTprintf("temperature_value_3 = %d.%d %cC\n\n", temperature_value_3,
decimal_value_3, 248);
}
return STATUS_NORMAL;
}
//*****************************************************************************
//
// Configure UART modules used by temperature sensors to specified baud rate.
//
//*****************************************************************************
void configTempSensUART(uint32_t g_ui32SysClock, uint32_t ui32Baud)
{
//
// Initialize the UARTs. Set the baud rate, number of data bits, turn off
// parity, number of stop bits, and stick mode.
//
MAP_UARTConfigSetExpClk(UART5_BASE, g_ui32SysClock, ui32Baud,
UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE);
MAP_UARTConfigSetExpClk(UART6_BASE, g_ui32SysClock, ui32Baud,
UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE);
MAP_UARTConfigSetExpClk(UART7_BASE, g_ui32SysClock, ui32Baud,
UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE);
}
//*****************************************************************************
//
// Send specified command to UART modules used by temperature sensors.
//
//*****************************************************************************
void transmitTempSensCommand(char command)
{
uint32_t i = 0;
//
// Transmit command LSB first.
//
for (i = 0; i < 8; ++i)
{
if ((command >> i) & 0x01)
{
while (!MAP_UARTCharPutNonBlocking(UART5_BASE, 0xFF));
while (!MAP_UARTCharPutNonBlocking(UART6_BASE, 0xFF));
while (!MAP_UARTCharPutNonBlocking(UART7_BASE, 0xFF));
}
else
{
while (!MAP_UARTCharPutNonBlocking(UART5_BASE, 0x00));
while (!MAP_UARTCharPutNonBlocking(UART6_BASE, 0x00));
while (!MAP_UARTCharPutNonBlocking(UART7_BASE, 0x00));
}
}
}
//*****************************************************************************
//
// Send specified pulse to UART modules used by temperature sensors.
//
//*****************************************************************************
void transmitTempSensPulse(char pulse)
{
while (!MAP_UARTCharPutNonBlocking(UART5_BASE, pulse));
while (!MAP_UARTCharPutNonBlocking(UART6_BASE, pulse));
while (!MAP_UARTCharPutNonBlocking(UART7_BASE, pulse));
//
// Check for values. Loop until values are placed in the receive FIFO.
//
while (!MAP_UARTCharsAvail(UART5_BASE) &&
!MAP_UARTCharsAvail(UART6_BASE) &&
!MAP_UARTCharsAvail(UART7_BASE));
}
Right now, the 3 sensors are returning the same 2 bytes 07FF every time, which translate to +127.9365 degree Celsius based on the datasheet. I'm not sure where it is getting this number, or why it's the same number every time.
Here is the link to the datasheet for reference:
