📄 onewire.c
字号:
/* myTemp is used to connect PalmOS PDAs to Dallas Microlan(tm) Devices It currently supports DS1820/DS18S20 Digital Thermometers Copyright (C) 2000 Christof Klaiber This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Christof Klaiber <christof.klaiber@gmx.de> Lachnerstr. 22 76131 Karlsruhe Germany*/#include <PalmOS.h>#include <SerialMgrOld.h>#include "onewire.h"/* local functions*/Err OWTouchBits(UInt16, UInt8, UInt8*);Err OWTouchBit(UInt16, UInt8*);Err OWTouchReset(UInt16 serialRefNum, UInt8* devicePresent) { SerSettingsType term; Err err; UInt8 serBuf; Int32 recTimeout = ((SysTicksPerSecond() / kRecTimeoutScale) == 0) ? 1 : (SysTicksPerSecond() / kRecTimeoutScale);/* default: no device present*/ *devicePresent = 0; SerReceiveFlush(serialRefNum, 0); err = SerSendFlush(serialRefNum); if (err) { return err; } /* Change Serial Settings to 9600 Baud Reset Pulse needs to be that long */ term.baudRate = 9600; term.flags = serDefaultSettings; err = SerSetSettings(serialRefNum, &term); if (err) { return err; } /* Send the Reset Pulse */ serBuf = 0xf0;/* returns the number of bytes transmitted, discarded*/ SerSend(serialRefNum, &serBuf, 1, &err); if (err) { return err; } err = SerSendWait(serialRefNum, -1); if (err) { return err; } /* wait for an possible answer to detect devices on the bus we get a serErrTimeOut if there is nothing attached to the bus*/ err = SerReceiveWait(serialRefNum, 1, recTimeout); if ((err != serErrTimeOut) && (err != 0)) { return err; } /* Get the answer if any returned bytes are discarded we get a serErrTimeOut if there is nothing attached to the bus*/ if (!err) { SerReceive(serialRefNum, &serBuf, 1, 0, &err); if ((err != serErrTimeOut) && (err !=0)) { return err; } if (serBuf != 0xf0) { *devicePresent = 1; } }/* reinitialize the Port */ term.baudRate = 115200; term.flags = serDefaultSettings; err = SerSetSettings(serialRefNum, &term); if (err) { return err; }/* 0 returned*/ return err;}Err OWTouchByte(UInt16 serialRefNum, UInt8* character){ return OWTouchBits(serialRefNum, 8, character);}/* Name: OWTouchBits Purpose: Send/Receive a bit to/from 1-wire Devices Input: UInt16 serialRefNum reference to the serial library UInt8* <->character character to send, returns character received Result: Err error Name: Christof Klaiber <cklaiber@users.sourceforge.net> Versions: 25.01.2001 First working release 06.12.2003 adaption for version 4 09.12.2003 code cleanups 11.12.2003 error handling This function is taken from digitemp-1.3/onewire.c by Brian C. Lane <bcl@brianlane.com>*/Err OWTouchBits(UInt16 serialRefNum, UInt8 nbits, UInt8* character) { UInt8 sendbit; UInt8 mask = 0x01; UInt8 outChar = *character; UInt8 inch = 0; UInt8 serBuf; UInt16 bit; Err err; UInt32 numBytes; Int32 recTimeout = ((SysTicksPerSecond() / kRecTimeoutScale) == 0) ? 1 : (SysTicksPerSecond() / kRecTimeoutScale); *character = 0;/* Flush Input and Output Queue */ SerReceiveFlush(serialRefNum, 0); err = SerSendFlush(serialRefNum); if (err) { return err; } /* Initialize the Mask to get the Bits one by one */ mask = 0x01; /* Result of this Function */ inch = 0; /* Iterrate through the Bits to send */ for (bit = 0; bit < nbits; bit++) { sendbit = (outChar & mask) ? 0xff : 0x00; inch >>= 1;/* Send the Bit */ numBytes = SerSend(serialRefNum, &sendbit, 1, &err); if (err) { return err; } /* Get Output from the Queue if there is one */ err = SerReceiveWait(serialRefNum, 1, recTimeout); if ((err != serErrTimeOut) && (err != 0)) { return err; } if (!err) { numBytes = SerReceive(serialRefNum, &serBuf, 1, 0, &err); if (err) { return err; } inch |= (serBuf & 0x01)? 0x80:0x00; } mask <<= 1; } *character = inch; return err;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -