s8 get_temperature_3wire(){ u8 buf[2]; if(OW_Send(OW_SEND_RESET, (u8 *)"\xcc\x44", 2, 0, 0, OW_NO_READ) == OW_NO_DEVICE) {return 0;} #if DS18B20_RESOLUTION == DS18B20_9BIT delay_for(100); #endif #if DS18B20_RESOLUTION == DS18B20_10BIT delay_for(200); #endif #if DS18B20_RESOLUTION == DS18B20_11BIT delay_for(400); #endif #if DS18B20_RESOLUTION == DS18B20_12BIT delay_for(750); #endif OW_Send(OW_SEND_RESET, (u8 *)"\xcc\xbe\xff\xff", 4, (u8 *)buf, 2, 2); buf[0] >>= 4; // убираем дробную часть buf[1] <<= 4; // убираем лишние знаки return(buf[1] | buf[0]); // объединяем 2 байта -> возврат }
//не отлажено на железе!!! void get_temperature_2wire(int *hb, int *lb){ //команда всем сенсорам - измерять if(OW_Send(OW_SEND_RESET, (u8 *)"\xcc\x44", 2, 0, 0, OW_NO_READ)==OW_NO_DEVICE) {return;} //PIN_ON(LED_GREEN); // назначаем функцию двухтактного выхода - подаем "питание" на шину OW_out_set_as_Power_pin(); // выдерживаем время измерения (например 750 мс для 12-битного измерения) #if DS18B20_RESOLUTION == DS18B20_9BIT delay_for(100); #endif #if DS18B20_RESOLUTION == DS18B20_10BIT delay_for(200); #endif #if DS18B20_RESOLUTION == DS18B20_11BIT delay_for(400); #endif #if DS18B20_RESOLUTION == DS18B20_12BIT delay_for(750); #endif // восстанавливаем функцию передатчика UART OW_out_set_as_TX_pin(); u8 buf[2]; OW_Send(OW_SEND_RESET, (u8 *)"\xcc\xbe\xff\xff", 4, (u8 *)buf, 2, 2); #if 0 buf[0] >>= 4; // убираем дробную часть buf[1] <<= 4; // убираем лишние знаки return(buf[1] | buf[0]); // объединяем 2 байта -> возврат #endif get_temperature_value(buf, *hb, *lb); }
int main(int argc, char *argv[]) { FILE *tfile, *sfile; const char *sname = NULL, *tname = NULL; double divi = 1, maxdelay = 0; int c, diviopt = FALSE, maxdelayopt = FALSE, idx; unsigned long line; int ch; static const struct option longopts[] = { { "timing", required_argument, 0, 't' }, { "typescript", required_argument, 0, 's' }, { "divisor", required_argument, 0, 'd' }, { "maxdelay", required_argument, 0, 'm' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; /* Because we use space as a separator, we can't afford to use any * locale which tolerates a space in a number. In any case, script.c * sets the LC_NUMERIC locale to C, anyway. */ setlocale(LC_ALL, ""); setlocale(LC_NUMERIC, "C"); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((ch = getopt_long(argc, argv, "t:s:d:m:Vh", longopts, NULL)) != -1) switch(ch) { case 't': tname = optarg; break; case 's': sname = optarg; break; case 'd': diviopt = TRUE; divi = getnum(optarg); break; case 'm': maxdelayopt = TRUE; maxdelay = getnum(optarg); break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); default: usage(stderr); } argc -= optind; argv += optind; idx = 0; if ((argc < 1 && !tname) || argc > 3) { warnx(_("wrong number of arguments")); usage(stderr); } if (!tname) tname = argv[idx++]; if (!sname) sname = idx < argc ? argv[idx++] : "typescript"; if (!diviopt) divi = idx < argc ? getnum(argv[idx]) : 1; if (maxdelay < 0) maxdelay = 0; tfile = fopen(tname, "r"); if (!tfile) err(EXIT_FAILURE, _("cannot open %s"), tname); sfile = fopen(sname, "r"); if (!sfile) err(EXIT_FAILURE, _("cannot open %s"), sname); /* ignore the first typescript line */ while((c = fgetc(sfile)) != EOF && c != '\n'); for(line = 0; ; line++) { double delay; size_t blk; char nl; if (fscanf(tfile, "%lf %zu%c\n", &delay, &blk, &nl) != 3 || nl != '\n') { if (feof(tfile)) break; if (ferror(tfile)) err(EXIT_FAILURE, _("failed to read timing file %s"), tname); errx(EXIT_FAILURE, _("timings file %s: %lu: unexpected format"), tname, line); } delay /= divi; if (maxdelayopt && delay > maxdelay) delay = maxdelay; if (delay > SCRIPT_MIN_DELAY) delay_for(delay); emit(sfile, sname, blk); } fclose(sfile); fclose(tfile); printf("\n"); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { FILE *tfile, *sfile; const char *sname, *tname; double divi; int c; unsigned long line; size_t oldblk = 0; /* Because we use space as a separator, we can't afford to use any * locale which tolerates a space in a number. In any case, script.c * sets the LC_NUMERIC locale to C, anyway. */ setlocale(LC_ALL, ""); setlocale(LC_NUMERIC, "C"); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); if (argc < 2 && argc > 4) usage(EXIT_FAILURE); tname = argv[1]; sname = argc > 2 ? argv[2] : "typescript"; divi = argc == 4 ? getnum(argv[3]) : 1; tfile = fopen(tname, "r"); if (!tfile) err(EXIT_FAILURE, _("cannot open timing file %s"), tname); sfile = fopen(sname, "r"); if (!sfile) err(EXIT_FAILURE, _("cannot open typescript file %s"), sname); /* ignore the first typescript line */ while((c = fgetc(sfile)) != EOF && c != '\n'); for(line = 0; ; line++) { double delay; size_t blk; char nl; if ((fscanf(tfile, "%lf %zd%[\n]\n", &delay, &blk, &nl) != 3) || (nl != '\n')) { if (feof(tfile)) break; if (ferror(tfile)) err(EXIT_FAILURE, _("failed to read timing file %s"), tname); errx(EXIT_FAILURE, _("timings file %s: %lu: unexpected format"), tname, line); } delay /= divi; if (delay > SCRIPT_MIN_DELAY) delay_for(delay); if (oldblk) emit(sfile, sname, oldblk); oldblk = blk; } fclose(sfile); fclose(tfile); exit(EXIT_SUCCESS); }