user1799537
user1799537

Reputation:

How to read temp from DS18B20 using UART board

I bought board like this board now i want to connect temperature sensor to this board. How to read temperature from sensor in c or c++? I tried to write some code but it won't work. I connect DS18B20 data cabel directly to TXD and RXD pins.

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <cstring>
#include <inttypes.h>
#include <errno.h>

int
set_interface_attribs (int fd, int speed, int parity)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0)
    {
            std::cout<<"error "<<errno<<" from tcgetattr";
            return -1;
    }

    cfsetospeed (&tty, speed);
    cfsetispeed (&tty, speed);

    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
    // disable IGNBRK for mismatched speed tests; otherwise receive break
    // as \000 chars
    tty.c_iflag &= ~IGNBRK;         // ignore break signal
    tty.c_lflag = 0;                // no signaling chars, no echo,
                                    // no canonical processing
    tty.c_oflag = 0;                // no remapping, no delays
    tty.c_cc[VMIN]  = 0;            // read doesn't block
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

    tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                    // enable reading
    tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
    tty.c_cflag |= parity;
    tty.c_cflag &= ~CSTOPB;
    tty.c_cflag &= ~CRTSCTS;

    if (tcsetattr (fd, TCSANOW, &tty) != 0)
    {
        std::cout<<"error "<<errno<<" from tcsetattr";
            return -1;
    }
    return 0;
}

void
set_blocking (int fd, int should_block)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0)
    {
        std::cout<<"error "<<errno<<" from tggetattr";
            return;
    }
    tty.c_cc[VMIN]  = should_block ? 1 : 0;
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    if (tcsetattr (fd, TCSANOW, &tty) != 0)
            std::cout<<"error "<<errno<<" setting term attributes";
}

int main()
{
        char *portname = "/dev/ttyUSB0";
        int tty_fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
        if (tty_fd < 0)
        {
            std::cout<<"error "<<errno<<" opening "<<portname<<": "<< strerror (errno);
                return -1;
        }

        set_interface_attribs (tty_fd, B9600, 0);  // set speed to 115,200 bps, 8n1 (no parity)
        set_blocking (tty_fd, true);

        unsigned char c = 0xCC;
        if(!write(tty_fd, &c, sizeof(c)))
            std::cout<<"Write error";

        sleep(2);

        unsigned char buffer[8];
        int size;
        if((size = read(tty_fd, &buffer, 8)) < 0)
                std::cout<<"Error";
        else
            std::cout<<"CC("<<size<<")='"<<buffer<<"'";

        std::cout<<"\n";

        c = 0x44;
        if(!write(tty_fd, &c, sizeof(c)))
            std::cout<<"Write error2";

        c = 0xBE;
        if(!write(tty_fd, &c, sizeof(c)))
            std::cout<<"Write error2";

        sleep(2);

        if((size = read(tty_fd, &buffer, 8)) < 0)
                std::cout<<"Error";
        else
            std::cout<<"BE("<<size<<")='"<<buffer<<"'";

        std::cout<<"\n######################\n";

        close(tty_fd);
}

I got:

CC(1)='Č@'
BE(2)='@ž@'
######################
CC(1)='Č@'
BE(2)='@ž@'
######################
CC(1)='Č@'
BE(2)='@ž@'
######################

Can you help me?

Upvotes: 0

Views: 3580

Answers (3)

Francesco Lavra
Francesco Lavra

Reputation: 889

As pointed out by user3804701, it's indeed possible to communicate with a 1-Wire device using a UART interface, and the application note at https://www.maximintegrated.com/en/app-notes/index.mvp/id/214 contains all the information needed to make it work. But the OP's code needs several fixes:

  • Each transaction with DS18B20 is made of 3 steps: initialization (also called reset), ROM command and function command, optionally followed by a data exchange
  • The initialization or reset step is performed by configuring the UART with 9600 bps baud rate, transmitting 0xF0, and receiving a dummy byte; then, the baud rate must be set to 115200 bps to perform the next steps
  • Following the reset step, data is sent to DS18B20 by writing to the UART a 0xFF byte for each data bit set to 1, and a 0x00 byte for each bit set to 0, starting from the least significant bit; for example, to send 0xAB (i.e. 10101011), one would write to the UART the sequence (FF FF 00 FF 00 FF 00 FF); for each byte written to the UART, there is a "return byte" that needs to be read from the UART and discarded
  • Data is received from DS18B20 by sending the 0xFF byte following the rules in the previous bullet point, but instead of discarding the "return bytes" reading from the UART a byte for each data bit, starting from the least significant bit: a 0xFF value means that the bit value is 1, otherwise the bit value is 0; for example, the sequence (00 FF FF 00 FF 00 00 FF) read from the UART means that DS18B20 sent 0x96 (i.e. 10010110)
  • If there is just one DS18B20 connected to the 1-Wire bus, all transactions can use "skip ROM" (byte value 0xCC) as ROM command
  • The first transaction to be performed is the one that triggers a temperature conversion from DS18B20, with function command 0x44
  • After waiting for the conversion to complete (which can take up to 750 ms), the host can perform a second transaction to read the DS18B20 scratchpad memory (function command 0xBE); the scratchpad memory is 9 bytes long and contains among other things the temperature value

So in summary the steps needed to get a temperature sample from DS18B20 are: reset, write 0xCC, write 0x44, wait for conversion, reset, write 0xCC, write 0xBE, read 9 bytes.

Example code that implements this stuff is available at https://github.com/dword1511/onewire-over-uart.

Upvotes: 1

user3804701
user3804701

Reputation: 21

You can hack the UART to communicate with 1 wire protocol. Connect Rx to Tx and add 4.7 pull-up resistor See the application note from maxim:

http://www.maximintegrated.com/en/app-notes/index.mvp/id/214

Upvotes: 1

user1619508
user1619508

Reputation:

You can't do this with any software. The DS18B20 is electrically incompatible with the board you have. The sensor uses a 1-wire, open-collector communication scheme that is completely different from the serial protocol normally used with this board. With great difficulty you might be able to do some bit-banging of the RTS/CTS signals but you need circuitry to combine them into a bidirectional open-collector signal.

Upvotes: 3

Related Questions