Dear Community;
I am trying to port this code that was made for an nrf52 to an nRF9160. I have a 4.99K pull-up resistor between the data line and VCC.
This is my ds18b20.c library file after modifications made for nRF9160:
/**
* Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <drivers/gpio.h>
#include "ds18b20.h"
#define DS_PIN 1
// Commands
#define STARTCONVO 0x44
#define READSCRATCH 0xBE
#define WRITESCRATCH 0x4E
// Scratchpad locations
#define TEMP_LSB 0
#define TEMP_MSB 1
// Device resolution
#define TEMP_9_BIT 0x1F // 9 bit
#define TEMP_10_BIT 0x3F // 10 bit
#define TEMP_11_BIT 0x5F // 11 bit
#define TEMP_12_BIT 0x7F // 12 bit
typedef uint8_t ScratchPad[9];
static const struct device *gpio_dev;
/**@brief Function for sending one bit to bus.
*/
void ds18b20_send(char bit)
{
//nrf_gpio_cfg_output(DS_PIN);
//nrf_gpio_pin_clear(DS_PIN);
//nrf_delay_us(5);
gpio_pin_configure(gpio_dev, DS_PIN, GPIO_OUTPUT_LOW);
gpio_pin_set(gpio_dev, DS_PIN, 0);
k_sleep(K_USEC(5));
if(bit==1)
{
//nrf_gpio_pin_set(DS_PIN);
gpio_pin_set(gpio_dev, DS_PIN, 1);
}
//nrf_delay_us(80);
//nrf_gpio_pin_set(DS_PIN);
k_sleep(K_USEC(80));
gpio_pin_set(gpio_dev, DS_PIN, 1);
}
/**@brief Function for reading one bit from bus.
*/
unsigned char ds18b20_read(void)
{
unsigned char presence=0;
//nrf_gpio_cfg_output(DS_PIN);
//nrf_gpio_pin_clear(DS_PIN);
//nrf_delay_us(2);
//nrf_gpio_pin_set(DS_PIN);;
//nrf_delay_us(15);
//nrf_gpio_cfg_input(DS_PIN,NRF_GPIO_PIN_NOPULL);
gpio_pin_configure(gpio_dev, DS_PIN, GPIO_OUTPUT_LOW);
gpio_pin_set(gpio_dev, DS_PIN, 0);
k_sleep(K_USEC(2));
gpio_pin_set(gpio_dev, DS_PIN, 1);
k_sleep(K_USEC(15));
gpio_pin_configure(gpio_dev, DS_PIN, GPIO_INPUT);
//if(nrf_gpio_pin_read(DS_PIN))
if (gpio_pin_get_raw(gpio_dev, DS_PIN))
{
presence = 1;
}
else
{
presence = 0;
}
printk("\npresence = %d", presence);
return presence;
}
/**@brief Function for sending one byte to bus.
*/
void ds18b20_send_byte(char data)
{
unsigned char i;
unsigned char x;
for(i=0;i<8;i++)
{
x = data>>i;
x &= 0x01;
ds18b20_send(x);
}
//nrf_delay_us(100);
k_sleep(K_USEC(100));
}
/**@brief Function for reading one byte from bus.
*/
unsigned char ds18b20_read_byte(void)
{
unsigned char i;
unsigned char data = 0;
for (i=0;i<8;i++)
{
if(ds18b20_read()) data|=0x01<<i;
//nrf_delay_us(15);
k_sleep(K_USEC(15));
}
return(data);
}
/**@brief Function for sending reset pulse.
*/
unsigned char ds18b20_reset(void)
{
unsigned char presence;
//nrf_gpio_cfg_output(DS_PIN);
//nrf_gpio_pin_clear(DS_PIN);
//nrf_delay_us(500);
//nrf_gpio_pin_set(DS_PIN);
//nrf_gpio_cfg_input(DS_PIN,NRF_GPIO_PIN_NOPULL); // usikkert p� pull her. m� sjekkes
//nrf_delay_us(30);
gpio_pin_configure(gpio_dev, DS_PIN, GPIO_OUTPUT_LOW);
gpio_pin_set(gpio_dev, DS_PIN, 0);
k_sleep(K_USEC(500));
gpio_pin_set(gpio_dev, DS_PIN, 1);
gpio_pin_configure(gpio_dev, DS_PIN, GPIO_INPUT);
k_sleep(K_USEC(30));
if(gpio_pin_get_raw(gpio_dev, DS_PIN) == 0)
{
presence = 1;
}
else
{
presence = 0;
}
//nrf_delay_us(470);
k_sleep(K_USEC(470));
if(gpio_pin_get_raw(gpio_dev, DS_PIN) == 1)
{
presence = 1;
}
else
{
presence = 0;
}
return presence;
}
/**@brief Function for reading temperature.
*/
float ds18b20_get_temp(void)
{
unsigned char check;
char temp1=0, temp2=0;
check=ds18b20_reset();
if(check)
{
ds18b20_send_byte(0xCC); //Skip ROM.
ds18b20_send_byte(0x44); //Start temp conversion.
//nrf_delay_ms(600UL);
k_sleep(K_MSEC(600UL));
check=ds18b20_reset();
ds18b20_send_byte(0xCC); //Skip ROM.
ds18b20_send_byte(0xBE); //Read temp from Scratchpad.
temp1=ds18b20_read_byte();
temp2=ds18b20_read_byte();
check=ds18b20_reset();
float temp=0;
temp=(float)(temp1+(temp2*256))/16;
return temp;
}
return 0;
}
/**@brief Function for reading bit.
*/
uint8_t OneWire_read_bit(void)
{
uint8_t r;
//nrf_gpio_cfg_output(DS_PIN);
//nrf_gpio_pin_clear(DS_PIN);
//nrf_delay_us(3);
//nrf_gpio_cfg_input(DS_PIN,NRF_GPIO_PIN_NOPULL);
//nrf_delay_us(10);
//r =nrf_gpio_pin_read(DS_PIN);
//nrf_delay_us(53);
gpio_pin_configure(gpio_dev, DS_PIN, GPIO_OUTPUT_LOW);
gpio_pin_set(gpio_dev, DS_PIN, 0);
k_sleep(K_USEC(3));
gpio_pin_configure(gpio_dev, DS_PIN, GPIO_INPUT);
k_sleep(K_USEC(10));
r = gpio_pin_get_raw(gpio_dev, DS_PIN);
printk("\nr = %d", r);
k_sleep(K_USEC(53));
return r;
}
/**@brief Function for reading.
*/
uint8_t OneWire_read()
{
uint8_t bitMask;
uint8_t r = 0;
for (bitMask = 0x01; bitMask; bitMask <<= 1) {
if ( OneWire_read_bit()) r |= bitMask;
}
return r;
}
/**@brief Function for reading scratchpad value
*/
void ds18b20_readScratchPad(uint8_t *scratchPad, uint8_t fields)
{
ds18b20_reset();
ds18b20_send_byte(0xCC);
ds18b20_send_byte(READSCRATCH);
for(uint8_t i=0; i < fields; i++)
{
scratchPad[i] = OneWire_read();
}
ds18b20_reset();
}
/**@brief Function for request temperature reading
*/
void ds18b20_requestTemperatures(void)
{
ds18b20_reset();
ds18b20_send_byte(0xCC);
ds18b20_send_byte(STARTCONVO);
}
/**@brief Function for reading temperature method 2
*/
float ds18b20_get_temp_method_2(void)
{
ds18b20_requestTemperatures();
unsigned char check;
ScratchPad scratchPad;
ds18b20_readScratchPad(scratchPad, 2);
int16_t rawTemperature = (((int16_t)scratchPad[TEMP_MSB]) << 8) | scratchPad[TEMP_LSB];
float temp = 0.0625 * rawTemperature;
return temp;
}
/**@brief Function for setting temperature resolution
*/
void ds18b20_setResolution(uint8_t resolution)
{
ds18b20_reset();
ds18b20_send_byte(0xCC);
ds18b20_send_byte(WRITESCRATCH);
// two dummy values for LOW & HIGH ALARM
ds18b20_send_byte(0);
ds18b20_send_byte(100);
switch (resolution)
{
case 12:
ds18b20_send_byte(TEMP_12_BIT);
break;
case 11:
ds18b20_send_byte(TEMP_11_BIT);
break;
case 10:
ds18b20_send_byte(TEMP_10_BIT);
break;
case 9:
default:
ds18b20_send_byte(TEMP_9_BIT);
break;
}
ds18b20_reset();
}
/*Funcion para inicializar GPIO*/
void Init_GPIO(){
gpio_dev = device_get_binding("GPIO_0");
if (!gpio_dev) {
printk("Error getting GPIO_0: failed.\n");
}
}
When I run the code I am always getting -0.062 ºC, which means that all 16 data bits are "1". 1111 1111 1111 1111 --> -0.062 ºC.
If I print every receive bit I can confirm that they are "1", but they shouldn't be all "1". I think it has to do with how I configure the GPIOs as input, I don't know if
nrf_gpio_cfg_input(DS_PIN,NRF_GPIO_PIN_NOPULL); and
gpio_pin_configure(gpio_dev, DS_PIN, GPIO_INPUT); are equivalent.
I have also tried to change sensor, but I get same -0.062 ºC. I have seen other people getting the -0.062 ºC value in this forum, but I can not found a solution, maybe someone can help.
Thanks in advance.

