Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
nibu
Level 1
Level 1
First like given First reply posted First question asked

Hi,

I'm building a calorimeter right now for a project at my university and for that I need to work with a microcontroller and a thermal sensor. I bought the DS18B20 and my prof gave me the XMC4700 telling me it's really easy to work with this microcontroller. The only problem is I have zero knowledge reagarding proramming things and it was already really hard to get used to DAVE but now I just can't get the DS18B20 to work. 

My question, does someone have an example code for the DS18B20 that works with the XMC4700.

Thanks in advance,

nibu

0 Likes
1 Solution
JaydenCai
Level 4
Level 4
25 replies posted First solution authored 25 sign-ins

Here's an example code for reading temperature data from the DS18B20 using the XMC4700:

#include <xmc_gpio.h>
#include <xmc_usic.h>
#include <xmc_uart.h>
#include <xmc_scu.h>
#include <stdint.h>

#define DS18B20_PORT    XMC_GPIO_PORT0
#define DS18B20_PIN     0

/* Initialization functions */
void ds18b20_reset(void)
{
    XMC_GPIO_SetOutputLow(DS18B20_PORT, DS18B20_PIN);
    XMC_GPIO_SetMode(DS18B20_PORT, DS18B20_PIN, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
    XMC_USIC_CH_Init(XMC_UART1_CH0);
    XMC_USIC_CH_SetInterruptNodePointer(XMC_UART1_CH0, XMC_USIC_CH_INTERRUPT_NODE_POINTER_RECEIVE, 0);
}

/* Send a 1-wire command to the DS18B20 */
void ds18b20_send_bit(uint8_t bit)
{
    if (bit) {
        XMC_GPIO_SetOutputHigh(DS18B20_PORT, DS18B20_PIN);
    } else {
        XMC_GPIO_SetOutputLow(DS18B20_PORT, DS18B20_PIN);
    }
    XMC_GPIO_SetMode(DS18B20_PORT, DS18B20_PIN, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
    XMC_USIC_CH_EnableEvent(XMC_UART1_CH0, XMC_USIC_CH_EVENT_STANDARD_RECEIVE_START);
}

/* Read a bit from the DS18B20 */
uint8_t ds18b20_read_bit(void)
{
    uint8_t bit = 0;
    XMC_GPIO_SetOutputLow(DS18B20_PORT, DS18B20_PIN);
    XMC_GPIO_SetMode(DS18B20_PORT, DS18B20_PIN, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
    XMC_USIC_CH_EnableEvent(XMC_UART1_CH0, XMC_USIC_CH_EVENT_STANDARD_RECEIVE_START);
    /* Wait for the signal from the DS18B20 */
    while (!XMC_USIC_CH_GetReceiveFlagStatus(XMC_UART1_CH0, XMC_USIC_CH_STATUS_FLAG_STANDARD_RECEIVE_INDICATION));
    if (XMC_USIC_CH_GetReceivedData(XMC_UART1_CH0) & (1 << XMC_UART_CH_DATA_WIDTH - 1)) {
        bit = 1;
    }
    return bit;
}

/* Send a byte to the DS18B20 */
void ds18b20_send_byte(uint8_t byte)
{
    uint8_t i;
    for (i = 0; i < 8; i++) {
        ds18b20_send_bit(byte & 0x01);
        byte >>= 1;
    }
}

/* Read a byte from the DS18B20 */
uint8_t ds18b20_read_byte(void)
{
    uint8_t byte = 0;
    uint8_t i;
    for (i = 0; i < 8; i++) {
        byte >>= 1;
        byte |= ds18b20_read_bit() << 7;
    }
    return byte;
}

/* Read the temperature from the DS18B20 */
float ds18b20_read_temperature(void)
{
    float temperature = 0.0f;
    uint8_t data[9];
    ds18b20_reset();
    /* Skip ROM command */
    ds18b20_send_byte(0xCC);
    /* Start conversion command */
    ds18b20_send_byte(0x44);
    /* Wait for conversion to finish */
    while (!ds18b20_read_bit());
    ds18b20_reset();
    /* Skip ROM command */
    ds18b20_send_byte(0xCC);
    /* Read scratchpad command */
    ds18b20_send_byte(0xBE);
    /* Read the scratchpad data */
    uint8_t i;
    for (i = 0; i < 9; i++) {
        data[i] = ds18b20_read_byte();
    }
    /* Calculate the temperature */
    int16_t raw_temp = ((int16_t)data[1] << 😎 | data[0];
    temperature = (float)raw_temp / 16.0f;
    return temperature;
}

int main(void)
{
    float temp;

    /* Initialize the DS18B20 */
    ds18b20_reset();

    /* Read the temperature from the DS18B20 */
    temp = ds18b20_read_temperature();

return 0;
}

This code initializes the DS18B20 sensor and reads the temperature data from the sensor. Please note that you may need to make some adjustments to this code based on your specific requirements and hardware setup.

I hope this helps.

View solution in original post

5 Replies
Aashita_R
Moderator
Moderator
Moderator
50 likes received 100 solutions authored 250 replies posted

Hi @nibu ,

You can refer to DS18B20 related libraries in the third party site : https://www.analog.com/media/en/technical-documentation/data-sheets/ds18b20.pdf for getting on this. However, we can help you for XMC 4700 programming. Here you can find various code examples programmed over XMC 4700 : https://www.infineon.com/cms/en/product/promopages/aim-mc/dave_downloads.html 

Here is XMC 4700 landing page where you can find all the user manuals related to the same : https://www.infineon.com/cms/en/product/microcontroller/32-bit-industrial-microcontroller-based-on-a...

Please go through above and let us know in case of any further query. We would be happy to help!

Best Regards,

Aashita

 

JaydenCai
Level 4
Level 4
25 replies posted First solution authored 25 sign-ins

Here's an example code for reading temperature data from the DS18B20 using the XMC4700:

#include <xmc_gpio.h>
#include <xmc_usic.h>
#include <xmc_uart.h>
#include <xmc_scu.h>
#include <stdint.h>

#define DS18B20_PORT    XMC_GPIO_PORT0
#define DS18B20_PIN     0

/* Initialization functions */
void ds18b20_reset(void)
{
    XMC_GPIO_SetOutputLow(DS18B20_PORT, DS18B20_PIN);
    XMC_GPIO_SetMode(DS18B20_PORT, DS18B20_PIN, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
    XMC_USIC_CH_Init(XMC_UART1_CH0);
    XMC_USIC_CH_SetInterruptNodePointer(XMC_UART1_CH0, XMC_USIC_CH_INTERRUPT_NODE_POINTER_RECEIVE, 0);
}

/* Send a 1-wire command to the DS18B20 */
void ds18b20_send_bit(uint8_t bit)
{
    if (bit) {
        XMC_GPIO_SetOutputHigh(DS18B20_PORT, DS18B20_PIN);
    } else {
        XMC_GPIO_SetOutputLow(DS18B20_PORT, DS18B20_PIN);
    }
    XMC_GPIO_SetMode(DS18B20_PORT, DS18B20_PIN, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
    XMC_USIC_CH_EnableEvent(XMC_UART1_CH0, XMC_USIC_CH_EVENT_STANDARD_RECEIVE_START);
}

/* Read a bit from the DS18B20 */
uint8_t ds18b20_read_bit(void)
{
    uint8_t bit = 0;
    XMC_GPIO_SetOutputLow(DS18B20_PORT, DS18B20_PIN);
    XMC_GPIO_SetMode(DS18B20_PORT, DS18B20_PIN, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
    XMC_USIC_CH_EnableEvent(XMC_UART1_CH0, XMC_USIC_CH_EVENT_STANDARD_RECEIVE_START);
    /* Wait for the signal from the DS18B20 */
    while (!XMC_USIC_CH_GetReceiveFlagStatus(XMC_UART1_CH0, XMC_USIC_CH_STATUS_FLAG_STANDARD_RECEIVE_INDICATION));
    if (XMC_USIC_CH_GetReceivedData(XMC_UART1_CH0) & (1 << XMC_UART_CH_DATA_WIDTH - 1)) {
        bit = 1;
    }
    return bit;
}

/* Send a byte to the DS18B20 */
void ds18b20_send_byte(uint8_t byte)
{
    uint8_t i;
    for (i = 0; i < 8; i++) {
        ds18b20_send_bit(byte & 0x01);
        byte >>= 1;
    }
}

/* Read a byte from the DS18B20 */
uint8_t ds18b20_read_byte(void)
{
    uint8_t byte = 0;
    uint8_t i;
    for (i = 0; i < 8; i++) {
        byte >>= 1;
        byte |= ds18b20_read_bit() << 7;
    }
    return byte;
}

/* Read the temperature from the DS18B20 */
float ds18b20_read_temperature(void)
{
    float temperature = 0.0f;
    uint8_t data[9];
    ds18b20_reset();
    /* Skip ROM command */
    ds18b20_send_byte(0xCC);
    /* Start conversion command */
    ds18b20_send_byte(0x44);
    /* Wait for conversion to finish */
    while (!ds18b20_read_bit());
    ds18b20_reset();
    /* Skip ROM command */
    ds18b20_send_byte(0xCC);
    /* Read scratchpad command */
    ds18b20_send_byte(0xBE);
    /* Read the scratchpad data */
    uint8_t i;
    for (i = 0; i < 9; i++) {
        data[i] = ds18b20_read_byte();
    }
    /* Calculate the temperature */
    int16_t raw_temp = ((int16_t)data[1] << 😎 | data[0];
    temperature = (float)raw_temp / 16.0f;
    return temperature;
}

int main(void)
{
    float temp;

    /* Initialize the DS18B20 */
    ds18b20_reset();

    /* Read the temperature from the DS18B20 */
    temp = ds18b20_read_temperature();

return 0;
}

This code initializes the DS18B20 sensor and reads the temperature data from the sensor. Please note that you may need to make some adjustments to this code based on your specific requirements and hardware setup.

I hope this helps.

nibu
Level 1
Level 1
First like given First reply posted First question asked

Hey @JaydenCai ,

Thank you very much for the code. I have two more questions, do I put the main part of the code in the while-loop of the microcontroller and also I have an syntax-error in the following line 

 int16_t raw_temp = ((int16_t)data[1] <<  | data[0];

 I think it's missing a bracket but I'm not sure where to put it.

 

Thank you, nibu

 

0 Likes
JaydenCai
Level 4
Level 4
25 replies posted First solution authored 25 sign-ins

The main part of the code can be placed in a while loop to continuously read the temperature value from the DS18B20 sensor.

 

int16_t raw_temp = ((int16_t)data[1] << 😎 | data[0];

 

0 Likes

int16_t raw_temp = ((int16_t)data[1] << 8) | data[0];

0 Likes