Пробовал перенести код из Вашей библиотеки на ардуину для поиска багов и понимания процесса
#define SKIP_ROM_CMD 0xcc
#define START_CONV_CMD 0x44
#define READ_SCR_CMD 0xbe
#define WRITE_SCR_CMD 0x4e
#define REG_TH 0x00
#define REG_TL 0xff
#define REG_CONFIG 0x3f // precision 10bit
#define DS_NO_ERROR 0
#define DS_NOT_FOUND 1
#define DS_TIMEOUT 2
#define DS_CRC_ERROR 3
#define DSPIN 10
uint8_t i, data[9], crc = 0;
uint8_t ret=0;
int temp=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
ret = ds18b20_read();
Serial.print(" Temp = ");
//Serial.print(present, HEX);
Serial.println(temp);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
//data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
}
static uint8_t OneWireReset(void)
{
uint8_t r = 1;
pinMode(DSPIN,OUTPUT);
digitalWrite(DSPIN, LOW);
delayMicroseconds(480);
pinMode(DSPIN,INPUT);
delayMicroseconds(80);
r = !digitalRead(DSPIN);
delayMicroseconds(400);
return r;
}
static void OneWireOutByte(uint8_t d)
{
uint8_t n;
noInterrupts();
for(n = 8; n > 0; n--) {
if (d & 0x01) {
pinMode(DSPIN,OUTPUT);
digitalWrite(DSPIN, LOW);
delayMicroseconds(2);
pinMode(DSPIN,INPUT);
delayMicroseconds(59);
}
else {
pinMode(DSPIN,OUTPUT);
digitalWrite(DSPIN, LOW);
delayMicroseconds(59);
pinMode(DSPIN,INPUT);
delayMicroseconds(2);
}
d = d >> 1;
}
interrupts();
}
static uint8_t OneWireInByte(void)
{
uint8_t d = 0, n, b = 0;
noInterrupts();
for (n = 0; n < 8; n++) {
pinMode(DSPIN,OUTPUT);
digitalWrite(DSPIN, LOW);
delayMicroseconds(2);
pinMode(DSPIN,INPUT);
delayMicroseconds(10);
b = digitalRead(DSPIN);
delayMicroseconds(50);
d = (d >> 1) | (b << 7);
}
interrupts();
return d;
}
// read DS18B20 tempearature in 1/10 C
uint8_t ds18b20_read(void)
{
// uint8_t i, data[9], crc = 0;
uint16_t trow, convtm;
float decimal;
if (!OneWireReset()) {
return DS_NOT_FOUND;
}
// 0xCC skip ROM
OneWireOutByte(SKIP_ROM_CMD);
// setting precision 10bit
OneWireOutByte(WRITE_SCR_CMD);
OneWireOutByte(REG_TH);
OneWireOutByte(REG_TL);
OneWireOutByte(REG_CONFIG);
if (!OneWireReset()) {
return DS_NOT_FOUND;
}
// 0xCC skip ROM
OneWireOutByte(SKIP_ROM_CMD);
// 0x44 start conversion
OneWireOutByte(START_CONV_CMD);
/*
convtm = 400; // ~200ms
while (!digitalRead(DSPIN) && --convtm > 0);
// conversion timeout
if (convtm == 0) {
return DS_TIMEOUT;
}
*/
delay(1000);
if (!OneWireReset()) {
return DS_NOT_FOUND;
}
OneWireOutByte(SKIP_ROM_CMD);
// 0xbe get temperature from ram
OneWireOutByte(READ_SCR_CMD);
for (i = 0; i < 9; i++) {
/* Read byte by byte */
data[i] = OneWireInByte();
}
//if (CRC8(data, 8) != data[8]) {
// return DS_CRC_ERROR;
// }
/* First two bytes of scratchpad are temperature values */
trow = ((int16_t) data[1] << 8) | data[0];
/* Check if temperature is negative */
i = 0;
if (trow & 0x8000) {
/* Two's complement, temperature is negative */
trow = ~trow + 1;
i = 1;
}
decimal = (float)((int16_t) trow >> 2) / 4; // 10bit
if (i) {
decimal = 0 - decimal;
}
temp = (int) (decimal * 10);
return DS_NO_ERROR;
}
Единственно пришлось поставить задержку после команды преобразования, вместо цикла, а то читались FF и этот код заработал, даже с таймингами по 50 мкс пробовал. Значит код в принципе рабочий, может уважаемые Гуру ответят, почему вот это код не работает на NRF24LE1, вернее работает только на одном датчике из моих десяти?
#define DSPIN GPIO_PIN_ID_P0_3
#include <stdint.h>
#include "gpio.h"
#include "delay.h"
#include "interrupt.h"
#include "crc8.h"
#include "ds18b20.h"
static uint8_t OneWireReset(void)
{
uint8_t r = 1;
gpio_pin_configure(DSPIN,
GPIO_PIN_CONFIG_OPTION_DIR_OUTPUT
| GPIO_PIN_CONFIG_OPTION_OUTPUT_VAL_CLEAR
);
delay_us(480);
gpio_pin_configure(DSPIN,
GPIO_PIN_CONFIG_OPTION_DIR_INPUT
);
delay_us(80);
r = !gpio_pin_val_read(DSPIN);
delay_us(400);
return r;
}
static void OneWireOutByte(uint8_t d)
{
uint8_t n;
interrupt_control_global_disable();
for(n = 8; n > 0; n--) {
if (d & 0x01) {
gpio_pin_configure(DSPIN,
GPIO_PIN_CONFIG_OPTION_DIR_OUTPUT
| GPIO_PIN_CONFIG_OPTION_OUTPUT_VAL_CLEAR
);
delay_us(1);
gpio_pin_configure(DSPIN,
GPIO_PIN_CONFIG_OPTION_DIR_INPUT
);
delay_us(49);
}
else {
gpio_pin_configure(DSPIN,
GPIO_PIN_CONFIG_OPTION_DIR_OUTPUT
| GPIO_PIN_CONFIG_OPTION_OUTPUT_VAL_CLEAR
);
delay_us(49);
gpio_pin_configure(DSPIN,
GPIO_PIN_CONFIG_OPTION_DIR_INPUT
);
delay_us(1);
}
d = d >> 1;
}
interrupt_control_global_enable();
}
static uint8_t OneWireInByte(void)
{
uint8_t d = 0, n, b = 0;
interrupt_control_global_disable();
for (n = 0; n < 8; n++) {
gpio_pin_configure(DSPIN,
GPIO_PIN_CONFIG_OPTION_DIR_OUTPUT
| GPIO_PIN_CONFIG_OPTION_OUTPUT_VAL_CLEAR
);
delay_us(1);
gpio_pin_configure(DSPIN,
GPIO_PIN_CONFIG_OPTION_DIR_INPUT
);
delay_us(2);
b = gpio_pin_val_read(DSPIN);
delay_us(50);
d = (d >> 1) | (b << 7);
}
interrupt_control_global_enable();
return d;
}
// read DS18B20 tempearature in 1/10 C
dserror_t ds18b20_read(int *temp)
{
uint8_t i, data[9], crc = 0;
uint16_t trow, convtm;
float decimal;
if (!OneWireReset()) {
return DS_NOT_FOUND;
}
// 0xCC skip ROM
OneWireOutByte(SKIP_ROM_CMD);
// setting precision 10bit
OneWireOutByte(WRITE_SCR_CMD);
OneWireOutByte(REG_TH);
OneWireOutByte(REG_TL);
OneWireOutByte(REG_CONFIG);
if (!OneWireReset()) {
return DS_NOT_FOUND;
}
// 0xCC skip ROM
OneWireOutByte(SKIP_ROM_CMD);
// 0x44 start conversion
OneWireOutByte(START_CONV_CMD);
// wait while temperature value not ready
//convtm = 400; // ~200ms
//while (!gpio_pin_val_read(DSPIN) && --convtm > 0);
// conversion timeout
delay_ms(1000);
if (convtm == 0) {
return DS_TIMEOUT;
}
if (!OneWireReset()) {
return DS_NOT_FOUND;
}
OneWireOutByte(SKIP_ROM_CMD);
// 0xbe get temperature from ram
OneWireOutByte(READ_SCR_CMD);
for (i = 0; i < 9; i++) {
/* Read byte by byte */
data[i] = OneWireInByte();
}
if (CRC8(data, 8) != data[8]) {
return DS_CRC_ERROR;
}
/* First two bytes of scratchpad are temperature values */
trow = ((int16_t) data[1] << 8) | data[0];
/* Check if temperature is negative */
i = 0;
if (trow & 0x8000) {
/* Two's complement, temperature is negative */
trow = ~trow + 1;
i = 1;
}
decimal = (float)((int16_t) trow >> 2) / 4; // 10bit
if (i) {
decimal = 0 - decimal;
}
*temp = (int) (decimal * 10);
return DS_NO_ERROR;
}