Open ksy5662 opened 6 years ago
Thank you very much for the advice you provided, we will fully consider it
@ksy5662 Hi. Did you find how to connect DS18B20 to RAK811? I broke my mind trying to do it. The main problem is to get needed delay in microseconds. Standard delay is in milliseconds. Please somebody help me.
Hello @vkiselyov
Well this one wire sensors are a pain in the a.... Unfortunately, I got no none at hand ATM to test/provide you with an example. But for the delay, I remember I used a timer.
Regards, Edi
Hi @eiten. As far as I know. RAK 811(STM32 inside) doesn't have microseconds a timer. I found only milliseconds a timer. In oscilloscope diagram I can see that gpio switch between INPUT and OUTPUT mode takes a long time, and is doesn't have fixed time. So I can't control delay in OneWire protocol.
Maybe @RAKWireless can give me some advice how to do that.
For details. I use CoIDE classA example project as a base. When I connect DS18B20 to Arduino UNO I can see in oscilloscope diagram very accurate time intervals. One of the problems is very long switch between high and low levels if GpioWrite method is used. I start to use BSRR register and time for switch is reduced. Now I have no idea how to reduce time for switch between INPUT and OUTPUT mode.
Next week, I should get a new RAK811 tracker board (I don't want to rip my installations out of the cars/bikes). I will test and report back. Would you like to give me your code?
`/* Copyright (c) 2007, Jim Studt (original old version - many contributors since)
The latest version of this library may be found at: http://www.pjrc.com/teensy/td_libs_OneWire.html
OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since January 2010.
DO NOT EMAIL for technical support, especially not for ESP chips! All project support questions must be posted on public forums relevant to the board or chips used. If using Arduino, post on Arduino's forum. If using ESP, post on the ESP community forums. There is ABSOLUTELY NO TECH SUPPORT BY PRIVATE EMAIL!
Github's issue tracker for OneWire should be used only to report specific bugs. DO NOT request project support via Github. All project and tech support questions must be posted on forums, not github issues. If you experience a problem and you are not absolutely sure it's an issue with the library, ask on a forum first. Only use github to report issues after experts have confirmed the issue is with OneWire rather than your project.
Back in 2010, OneWire was in need of many bug fixes, but had been abandoned the original author (Jim Studt). None of the known contributors were interested in maintaining OneWire. Paul typically works on OneWire every 6 to 12 months. Patches usually wait that long. If anyone is interested in more actively maintaining OneWire, please contact Paul (this is pretty much the only reason to use private email about OneWire).
OneWire is now very mature code. No changes other than adding definitions for newer hardware support are anticipated.
Version 2.3: Unknown chip fallback mode, Roger Clark Teensy-LC compatibility, Paul Stoffregen Search bug fix, Love Nystrom
Version 2.2: Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030 Fix DS18B20 example negative temperature Fix DS18B20 example's low res modes, Ken Butcher Improve reset timing, Mark Tillotson Add const qualifiers, Bertrik Sikken Add initial value input to crc16, Bertrik Sikken Add target_search() function, Scott Roberts
Version 2.1: Arduino 1.0 compatibility, Paul Stoffregen Improve temperature example, Paul Stoffregen DS250x_PROM example, Guillermo Lovato PIC32 (chipKit) compatibility, Jason Dangel, dangel.jason AT gmail.com Improvements from Glenn Trewitt:
Version 2.0: Modifications by Paul Stoffregen, January 2010: http://www.pjrc.com/teensy/td_libs_OneWire.html Search fix from Robin James http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 Use direct optimized I/O in all cases Disable interrupts during timing critical sections (this solves many random communication errors) Disable interrupts during read-modify-write I/O Reduce RAM consumption by eliminating unnecessary variables and trimming many to 8 bits Optimize both crc8 - table version moved to flash
Modified to work with larger numbers of devices - avoids loop. Tested in Arduino 11 alpha with 12 sensors. 26 Sept 2008 -- Robin James http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27
Updated to work with arduino-0008 and to include skip() as of 2007/07/06. --RJL20
Modified to calculate the 8-bit CRC directly, avoiding the need for the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010 -- Tom Pollard, Jan 23, 2008
Jim Studt's original library was modified by Josh Larios.
Tom Pollard, pollard@alum.mit.edu, contributed around May 20, 2008
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Much of the code was inspired by Derek Yerger's code, though I don't think much of that remains. In any event that was.. (copyleft) 2006 by Derek Yerger - Free to distribute freely.
The CRC code was excerpted and inspired by the Dallas Semiconductor sample code bearing this copyright. //--------------------------------------------------------------------------- // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // // Except as contained in this notice, the name of Dallas Semiconductor // shall not be used except as stated in the Dallas Semiconductor // Branding Policy. //-------------------------------------------------------------------------- */
void directModeInput(Gpio_t *obj, PinNames pin ) { //printf("directModeInput\r\n"); //GpioInit(obj, pin, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0); GpioMcuInit(obj, pin, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
/*GPIO_InitTypeDef gpinit;
gpinit.Mode = GPIO_MODE_INPUT;
gpinit.Pull = GPIO_NOPULL;
gpinit.Speed = GPIO_SPEED_FREQ_HIGH;
gpinit.Pin = obj->pinIndex;
HAL_GPIO_Init(obj->port,&gpinit);*/
}
void directModeOutput(Gpio_t *obj, PinNames pin) { //printf("directModeOutput\r\n"); //GpioInit(obj, pin, PIN_OUTPUT, PIN_OPEN_DRAIN, PIN_NO_PULL, 0); //GpioInit(obj, pin, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0); GpioMcuInit(obj, pin, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
/*GPIO_InitTypeDef gpinit;
gpinit.Mode = GPIO_MODE_OUTPUT_OD;
gpinit.Pull = GPIO_NOPULL;
gpinit.Speed = GPIO_SPEED_FREQ_HIGH;
gpinit.Pin = pin;
HAL_GPIO_Init(obj->port,&gpinit);*/
}
void directWriteLow(Gpio_t *obj) { //GPIO_REG(GPIO_OUTPUT_VAL) &= ~mask; //printf("directWriteLow\r\n"); GpioWrite(obj,0x00); }
void directWriteHigh(Gpio_t *obj) { //printf("directWriteHigh\r\n"); GpioWrite(obj,0x01); }
uint8_t directRead(Gpio_t *obj) { //return (GpioRead(obj) & 0x01); return (HAL_GPIO_ReadPin( obj->port, obj->pinIndex ) & 0x01); }
void delayUS(uint32_t us) { /volatile uint32_t counter = us; while(counter--);/
}
void ow_begin(Gpio_t obj,PinNames pin) { //pinMode(pin, INPUT); obj_int = obj; pin_int = pin; GpioInit(obj, pin, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0); /bitmask = PIN_TO_BITMASK(pin); baseReg = PIN_TO_BASEREG(pin);/ /#if ONEWIRE_SEARCH reset_search();
}
// Perform the onewire reset function. We will wait up to 250uS for // the bus to come high, if it doesn't then it is broken or shorted // and we return a 0; // // Returns 1 if a device asserted a presence pulse, 0 otherwise. // uint8_t ow_reset(void) {
uint8_t r;
uint8_t retries = 125;
GPIO_TypeDef* gt = obj_int->port;
GpioMcuInit(obj_int, pin_int, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
// wait until the wire is high... just in case
do {
if (--retries == 0) return 0;
DELAYN(4);//2мкС
} while ( !directRead(obj_int));
//noInterrupts();
gt->BSRR = (uint32_t)obj_int->pinIndex << 16 ;
GpioMcuInit(obj_int, pin_int, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
//interrupts();
DELAYN(206);
//noInterrupts();
GpioMcuInit(obj_int, pin_int, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
DELAYN(24);
r = !directRead(obj_int);
//interrupts();
DELAYN(180);
return r;
}
// // Write a bit. Port and bit is used to cut lookup time and provide // more certain timing. // void ow_write_bit(uint8_t v) { GPIO_TypeDef* gt = obj_int->port; if (v & 1) { BoardDisableIrq(); //directModeOutput(obj_int, pin_int); gt->BSRR = (uint32_t)obj_int->pinIndex << 16 ; DELAYN(8);//10мкС NP();NP();NP();NP();NP();NP();NP();NP();NP(); //NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP(); //directWriteHigh(obj_int); gt->BSRR = obj_int->pinIndex; BoardEnableIrq(); DELAYN(11);//55мкС //NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP(); //NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP(); } else { //BoardDisableIrq(); //directModeOutput(obj_int, pin_int); gt->BSRR = (uint32_t)obj_int->pinIndex << 16 ; DELAYN(22);//65мкС //NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP();NP(); gt->BSRR = obj_int->pinIndex; //NP(); //BoardEnableIrq(); //DELAYN(1); //5мкС } }
// // Read a bit. Port and bit is used to cut lookup time and provide // more certain timing. // uint8_t ow_read_bit(void) { GPIO_TypeDef* gt = obj_int->port; uint8_t r;
BoardDisableIrq();
GpioMcuInit(obj_int, pin_int, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
//gt->BSRR = (uint32_t)obj_int->pinIndex << 16 ;
//DELAYN(1); //3мкС
GpioMcuInit(obj_int, pin_int, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
DELAYN(1);//10-16мкС
r = directRead(obj_int);
BoardEnableIrq();
DELAYN(11);//53мкС
return r;
}
// // Write a byte. The writing code uses the active drivers to raise the // pin high, if you need power after the write (e.g. DS18S20 in // parasite power mode) then set 'power' to 1, otherwise the pin will // go tri-state at the end of the write to avoid heating in a short or // other mishap. // void ow_write(uint8_t v, uint8_t power / = 0 /) { uint8_t bitMask;
GPIO_TypeDef* gt = obj_int->port;
GpioMcuInit(obj_int, pin_int, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
for (bitMask = 0x01; bitMask; bitMask <<= 1) {
ow_write_bit( (bitMask & v)?1:0);
}
if ( !power) {
BoardDisableIrq();
//noInterrupts();
GpioMcuInit(obj_int, pin_int, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
//DIRECT_MODE_INPUT(baseReg, bitmask);
gt->BSRR = (uint32_t)obj_int->pinIndex << 16 ;
//directWriteLow(obj_int);
BoardEnableIrq();
//interrupts();
}
}
void ow_write_bytes(const uint8_t buf, uint16_t count, uint8_t power / = 0 /) { GPIO_TypeDef gt = obj_int->port; for (uint16_t i = 0 ; i < count ; i++) ow_write(buf[i],0); if (!power) { BoardDisableIrq(); //noInterrupts(); GpioMcuInit(obj_int, pin_int, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0);
gt->BSRR = (uint32_t)obj_int->pinIndex << 16 ;
//directWriteLow(obj_int);
BoardEnableIrq();
//interrupts();
} }
// // Read a byte // uint8_t ow_read() { uint8_t bitMask; uint8_t r = 0;
for (bitMask = 0x01; bitMask; bitMask <<= 1) {
if ( ow_read_bit()) r |= bitMask;
}
return r;
}
void ow_read_bytes(uint8_t *buf, uint16_t count) { for (uint16_t i = 0 ; i < count ; i++) buf[i] = ow_read(); }
// // Do a ROM select // void ow_select(const uint8_t rom[8]) { uint8_t i;
ow_write(0x55,0); // Choose ROM
for (i = 0; i < 8; i++) ow_write(rom[i],0);
}
// // Do a ROM skip // void ow_skip() { ow_write(0xCC,0); // Skip ROM }
void ow_depower() { BoardDisableIrq(); //noInterrupts(); GpioMcuInit(obj_int, pin_int, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0); //DIRECT_MODE_INPUT(baseReg, bitmask); BoardEnableIrq(); //interrupts(); }
void ow_reset_search() { // reset the search state LastDiscrepancy = 0; LastDeviceFlag = 0; LastFamilyDiscrepancy = 0; for(int i = 7; ; i--) { ROM_NO[i] = 0; if ( i == 0) break; } }
uint8_t ow_search(uint8_t newAddr, uint8_t search_mode / = true */) { uint8_t id_bit_number; uint8_t last_zero, rom_byte_number; uint8_t search_result; uint8_t id_bit, cmp_id_bit;
unsigned char rom_byte_mask, search_direction;
// initialize for search id_bit_number = 1; last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; search_result = 0;
// if the last call was not the last one if (!LastDeviceFlag) { // 1-Wire reset if (!ow_reset()) { // reset the search LastDiscrepancy = 0; LastDeviceFlag = 0; LastFamilyDiscrepancy = 0; return 0; }
// issue the search command
if (search_mode == 1) {
ow_write(0xF0,0); // NORMAL SEARCH
} else {
ow_write(0xEC,0); // CONDITIONAL SEARCH
}
// loop to do the search
do
{
// read a bit and its complement
id_bit = ow_read_bit();
cmp_id_bit = ow_read_bit();
// check for no devices on 1-wire
if ((id_bit == 1) && (cmp_id_bit == 1)) {
printf("check for no devices on 1-wire\r\n");
break;
} else {
// all devices coupled have 0 or 1
if (id_bit != cmp_id_bit) {
search_direction = id_bit; // bit write value for search
} else {
// if this discrepancy if before the Last Discrepancy
// on a previous next then pick the same as last time
if (id_bit_number < LastDiscrepancy) {
search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
} else {
// if equal to last pick 1, if not then pick 0
search_direction = (id_bit_number == LastDiscrepancy);
}
// if 0 was picked then record its position in LastZero
if (search_direction == 0) {
last_zero = id_bit_number;
// check for Last discrepancy in family
if (last_zero < 9)
LastFamilyDiscrepancy = last_zero;
}
}
// set or clear the bit in the ROM byte rom_byte_number
// with mask rom_byte_mask
if (search_direction == 1)
ROM_NO[rom_byte_number] |= rom_byte_mask;
else
ROM_NO[rom_byte_number] &= ~rom_byte_mask;
// serial number search direction write bit
ow_write_bit(search_direction);
// increment the byte counter id_bit_number
// and shift the mask rom_byte_mask
id_bit_number++;
rom_byte_mask <<= 1;
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
if (rom_byte_mask == 0) {
rom_byte_number++;
rom_byte_mask = 1;
}
}
}
while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
// if the search was successful then
if (!(id_bit_number < 65)) {
// search successful so set LastDiscrepancy,LastDeviceFlag,search_result
LastDiscrepancy = last_zero;
// check for last device
if (LastDiscrepancy == 0) {
LastDeviceFlag = 1;
}
search_result = 1;
}
}
// if no device found then reset counters so next 'search' will be like a first if (!search_result || !ROM_NO[0]) { LastDiscrepancy = 0; LastDeviceFlag = 0; LastFamilyDiscrepancy = 0; search_result = 0; } else { for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i]; } return search_result; } `
`#ifndef OneWire_h
// You can exclude certain features from OneWire. In theory, this // might save some space. In practice, the compiler automatically // removes unused code (technically, the linker, using -fdata-sections // and -ffunction-sections when compiling, and Wl,--gc-sections // when linking), so most of these will not result in any code size // reduction. Well, unless you try to use the missing features // and redesign your program to not need them! ONEWIRE_CRC8_TABLE // is the exception, because it selects a fast but large algorithm // or a small but slow algorithm.
// you can exclude onewire_search by defining that to 0
// You can exclude CRC checks altogether by defining this to 0
// Select the table-lookup method of computing the 8-bit CRC // by setting this to 1. The lookup table enlarges code size by // about 250 bytes. It does NOT consume RAM (but did in very // old versions of OneWire). If you disable this, a slower // but very compact algorithm is used.
// You can allow 16-bit CRC checks by defining this to 1 // (Note that ONEWIRE_CRC must also be 1.)
// Board-specific macros for direct GPIO
Gpio_t *obj_int; PinNames pin_int;
unsigned char ROM_NO[8]; uint8_t LastDiscrepancy; uint8_t LastFamilyDiscrepancy; uint8_t LastDeviceFlag;
void ow_begin(Gpio_t *obj,PinNames pin);
// Perform a 1-Wire reset cycle. Returns 1 if a device responds
// with a presence pulse. Returns 0 if there is no device or the
// bus is shorted or otherwise held low for more than 250uS
uint8_t ow_reset(void);
// Issue a 1-Wire rom select command, you do the reset first.
void ow_select(const uint8_t rom[8]);
// Issue a 1-Wire rom skip command, to address all on bus.
void ow_skip(void);
// Write a byte. If 'power' is one then the wire is held high at
// the end for parasitically powered devices. You are responsible
// for eventually depowering it by calling depower() or doing
// another read or write.
void ow_write(uint8_t v, uint8_t power);
void ow_write_bytes(const uint8_t *buf, uint16_t count, uint8_t power);
// Read a byte.
uint8_t ow_read(void);
void ow_read_bytes(uint8_t *buf, uint16_t count);
// Write a bit. The bus is always left powered at the end, see
// note in write() about that.
void ow_write_bit(uint8_t v);
// Read a bit.
uint8_t ow_read_bit(void);
// Stop forcing power onto the bus. You only need to do this if
// you used the 'power' flag to write() or used a write_bit() call
// and aren't about to do another read or write. You would rather
// not leave this powered if you don't have to, just in case
// someone shorts your bus.
void ow_depower(void);
void ow_reset_search();
uint8_t ow_search(uint8_t newAddr, uint8_t search_mode / = true */);
// Prevent this name from leaking into Arduino sketches
`
`#ifndef DELAY_US_H
//5 - 3мкС //6 - 5-6мкС //8 - 3мкС //9 - 27мкС //10 - 30-31мкС //11 - 16мкС //12 - 36-37мкС //13 - 39мкС //14 - 42мкС //15 - 22мкС //16 - 47мкС //17 - 50-51мкС //18 - 53-54мкС //19 - 56-57мкС //20 - 59-60мкС //21 - 62-63мкС //22 - 65-66мкС //23 - 67-68 //24 - 70 //170 - 380 //177 - 380 //178 - 409 //179 - 386 //180 - 415 //206 - 480
{\
__asm__ volatile("NOP"); \
}
`
I'll look over it as soon as I get the HW
this code has many different ways of solutions in comment. I tried several methods to decide my problem, but they didn't work
Some news about it ?
I need some library for OneWire and DS18B20 which is supported in Arduino.
Here are some information
https://www.pjrc.com/teensy/td_libs_OneWire.html
https://github.com/milesburton/Arduino-Temperature-Control-Library
Any Plan for supporting Arduino IDE?? Seems STmicro can be supported by Arduino.