📄 mainp.c
字号:
//
// Returns: 8 bytes read from 1-Wire Net
//
//SMALLINT owReadByte(int portnum)
//{
// return owTouchByte(portnum,0xFF);
//}
//--------------------------------------------------------------------------
// Set the 1-Wire Net communucation speed.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'new_speed' - new speed defined as
// MODE_NORMAL 0x00
// MODE_OVERDRIVE 0x01
//
// Returns: current 1-Wire Net speed
//
//SMALLINT owSpeed(int portnum, SMALLINT new_speed)
//{
// portnum = 0;
//
// new_speed = USpeed;
// // not supported in micro code
//
// return USpeed;
//}
//--------------------------------------------------------------------------
// Set the 1-Wire Net line level. The values for NewLevel are
// as follows:
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'new_level' - new level defined as
// MODE_NORMAL 0x00
// MODE_STRONG5 0x02
// MODE_PROGRAM 0x04
// MODE_BREAK 0x08
//
// Returns: current 1-Wire Net level
//
// Note: Strong and Program not supported on 550 target.
//SMALLINT owLevel(int portnum, SMALLINT new_level)
//{
// portnum = 0;
// switch(new_level)
// {
// case MODE_BREAK:
// OW_PORT = 0; //send a break on one-wire port
// break;
// default:
// case MODE_NORMAL:
// OW_PORT = 1;
// break;
// }
// ULevel = new_level;
// return new_level;
//}
//--------------------------------------------------------------------------
// Bit utility to read and write a bit in the buffer 'buf'.
//
// 'op' - operation (1) to set and (0) to read
// 'state' - set (1) or clear (0) if operation is write (1)
// 'loc' - bit number location to read or write
// 'buf' - pointer to array of bytes that contains the bit
// to read or write
//
// Returns: 1 if operation is set (1)
// 0/1 state of bit number 'loc' if operation is reading
//
//SMALLINT bitacc(SMALLINT op, SMALLINT state, SMALLINT loc, uchar *buf)
//{
// SMALLINT nbyt,nbit;
//
// nbyt = (loc / 8);
// nbit = loc - (nbyt * 8);
//
// if (op == WRITE_FUNCTION)
// {
// if (state)
// buf[nbyt] |= (0x01 << nbit);
// else
// buf[nbyt] &= ~(0x01 << nbit);
//
// return 1;
// }
// else
// return ((buf[nbyt] >> nbit) & 0x01);
//}
// global variables for this module to hold search state information
static SMALLINT LastDiscrepancy[1];
static SMALLINT LastFamilyDiscrepancy[1];
static SMALLINT LastDevice[1];
uchar SerialNum[1][8];
//--------------------------------------------------------------------------
// The 'owFirst' finds the first device on the 1-Wire Net This function
// contains one parameter 'alarm_only'. When
// 'alarm_only' is TRUE (1) the find alarm command 0xEC is
// sent instead of the normal search command 0xF0.
// Using the find alarm command 0xEC will limit the search to only
// 1-Wire devices that are in an 'alarm' state.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not
// perform reset before search.
// 'alarm_only' - TRUE (1) the find alarm command 0xEC is
// sent instead of the normal search command 0xF0
//
// Returns: TRUE (1) : when a 1-Wire device was found and it's
// Serial Number placed in the global SerialNum[portnum]
// FALSE (0): There are no devices on the 1-Wire Net.
//
SMALLINT owFirst(int portnum, SMALLINT do_reset, SMALLINT alarm_only)
{
// reset the search state
LastDiscrepancy[portnum] = 0;
LastDevice[portnum] = FALSE;
LastFamilyDiscrepancy[portnum] = 0;
return owNext(portnum,do_reset,alarm_only);
}
//--------------------------------------------------------------------------
// The 'owNext' function does a general search. This function
// continues from the previos search state. The search state
// can be reset by using the 'owFirst' function.
// This function contains one parameter 'alarm_only'.
// When 'alarm_only' is TRUE (1) the find alarm command
// 0xEC is sent instead of the normal search command 0xF0.
// Using the find alarm command 0xEC will limit the search to only
// 1-Wire devices that are in an 'alarm' state.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not
// perform reset before search.
// 'alarm_only' - TRUE (1) the find alarm command 0xEC is
// sent instead of the normal search command 0xF0
//
// Returns: TRUE (1) : when a 1-Wire device was found and it's
// Serial Number placed in the global SerialNum[portnum]
// FALSE (0): when no new device was found. Either the
// last search was the last device or there
// are no devices on the 1-Wire Net.
//
SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only)
{
uchar bit_test, search_direction, bit_number;
uchar last_zero, serial_byte_number, next_result;
uchar serial_byte_mask;
uchar lastcrc8;
// initialize for search
bit_number = 1;
last_zero = 0;
serial_byte_number = 0;
serial_byte_mask = 1;
next_result = 0;
setcrc8(portnum,0);
// if the last call was not the last one
if (!LastDevice[portnum])
{
// check if reset first is requested
if (do_reset)
{
// reset the 1-wire
// if there are no parts on 1-wire, return FALSE
if (!owTouchReset(portnum))
{
// printf("owTouchReset failed\r\n");
// reset the search
LastDiscrepancy[portnum] = 0;
LastFamilyDiscrepancy[portnum] = 0;
//OWERROR(OWERROR_NO_DEVICES_ON_NET);
return FALSE;
}
}
// If finding alarming devices issue a different command
if (alarm_only)
owWriteByte(portnum,0xEC); // issue the alarming search command
else
owWriteByte(portnum,0xF0); // issue the search command
//pause before beginning the search
//usDelay(100);
// loop to do the search
do
{
// read a bit and its compliment
bit_test = owTouchBit(portnum,1) << 1;
bit_test |= owTouchBit(portnum,1);
// check for no devices on 1-wire
if (bit_test == 3)
break;
else
{
// all devices coupled have 0 or 1
if (bit_test > 0)
search_direction = !(bit_test & 0x01); // 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 (bit_number < LastDiscrepancy[portnum])
search_direction = ((SerialNum[portnum][serial_byte_number] & serial_byte_mask) > 0);
else
// if equal to last pick 1, if not then pick 0
search_direction = (bit_number == LastDiscrepancy[portnum]);
// if 0 was picked then record its position in LastZero
if (search_direction == 0)
last_zero = bit_number;
// check for Last discrepancy in family
if (last_zero < 9)
LastFamilyDiscrepancy[portnum] = last_zero;
}
// set or clear the bit in the SerialNum[portnum] byte serial_byte_number
// with mask serial_byte_mask
if (search_direction == 1)
SerialNum[portnum][serial_byte_number] |= serial_byte_mask;
else
SerialNum[portnum][serial_byte_number] &= ~serial_byte_mask;
// serial number search direction write bit
owTouchBit(portnum,search_direction);
// increment the byte counter bit_number
// and shift the mask serial_byte_mask
bit_number++;
serial_byte_mask <<= 1;
// if the mask is 0 then go to new SerialNum[portnum] byte serial_byte_number
// and reset mask
if (serial_byte_mask == 0)
{
lastcrc8 = docrc8(portnum,SerialNum[portnum][serial_byte_number]); // accumulate the CRC
serial_byte_number++;
serial_byte_mask = 1;
}
}
}
while(serial_byte_number < 8); // loop until through all SerialNum[portnum] bytes 0-7
// if the search was successful then
if (!((bit_number < 65) || lastcrc8))
{
// search successful so set LastDiscrepancy[portnum],LastDevice[portnum],next_result
LastDiscrepancy[portnum] = last_zero;
LastDevice[portnum] = (LastDiscrepancy[portnum] == 0);
next_result = TRUE;
}
}
// if no device found then reset counters so next 'next' will be
// like a first
if (!next_result || !SerialNum[portnum][0])
{
LastDiscrepancy[portnum] = 0;
LastDevice[portnum] = FALSE;
LastFamilyDiscrepancy[portnum] = 0;
next_result = FALSE;
}
return next_result;
}
//--------------------------------------------------------------------------
// The 'owSerialNum' function either reads or sets the SerialNum buffer
// that is used in the search functions 'owFirst' and 'owNext'.
// This function contains two parameters, 'serialnum_buf' is a pointer
// to a buffer provided by the caller. 'serialnum_buf' should point to
// an array of 8 unsigned chars. The second parameter is a flag called
// 'do_read' that is TRUE (1) if the operation is to read and FALSE
// (0) if the operation is to set the internal SerialNum buffer from
// the data in the provided buffer.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'serialnum_buf' - buffer to that contains the serial number to set
// when do_read = FALSE (0) and buffer to get the serial
// number when do_read = TRUE (1).
// 'do_read' - flag to indicate reading (1) or setting (0) the current
// serial number.
//
void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read)
{
uchar i;
// read the internal buffer and place in 'serialnum_buf'
if (do_read)
{
for (i = 0; i < 8; i++)
serialnum_buf[i] = SerialNum[portnum][i];
}
// set the internal buffer from the data in 'serialnum_buf'
else
{
for (i = 0; i < 8; i++)
SerialNum[portnum][i] = serialnum_buf[i];
}
}
//--------------------------------------------------------------------------
// Update the Dallas Semiconductor One Wire CRC (utilcrc8) from the global
// variable utilcrc8 and the argument.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'x' - data byte to calculate the 8 bit crc from
//
// Returns: the updated utilcrc8.
//
uchar docrc8(int portnum, uchar x)
{
utilcrc8[portnum] = dscrc_table[utilcrc8[portnum] ^ x];
return utilcrc8[portnum];
}
//--------------------------------------------------------------------------
// Reset crc8 to the value passed in
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'reset' - data to set crc8 to
//
void setcrc8(int portnum, uchar reset)
{
utilcrc8[portnum] = reset;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -