Here is an example code in C:
/*************************************************************
@brief Change the communication protocol
@param[in] type The log to enable, 1-wire or i2c
@return TRUE or FALSE
*************************************************************/
int switchCommunicationBus(int type) {
switch (type) {
case one_wire:
dprintf("Using OneWire Mode\n");
if (device_type == one_wire) {
dprintf("\n1-Wire Mode\n");
return TRUE;
}
if (!DS9481_SwitchOneWireMode()) {
dprintf("\Failed to switch to 1-Wire Mode\n");
return FALSE;
}
else {
dprintf("DS9481P switched to 1 Wire mode.\n");
}
device_type = one_wire;
break;
case i2c:
if (device_type == i2c) {
dprintf("\nI2C mode\n");
return TRUE;
}
if (!DS9481_SwitchI2CMode())
{
CloseCOM();
dprintf("Failed to switch DS9481 to I2C mode.\n");
return FALSE;
}
else
dprintf("DS9481P switched to I2C mode.\n");
device_type = i2c;
break;
}
}
/**@brief DS94181P-300 Switch to I2C Mode
* Requires to keep track of DS9481-P communication state.
* Example could use a flag to store latest communication state every time this function is used or in
* conjuction with DS9481_SwitchOneWireMode
@retval TRUE On Success
@retval FALSE On Failed
*/
int DS9481_SwitchI2CMode(void)
{
uchar sendpacket[10];
short sendlen = 0;
// check for correct mode
if (UMode != MODSEL_COMMAND)
{
UMode = MODSEL_COMMAND;
sendpacket[sendlen++] = MODE_COMMAND;
}
// construct the command
sendpacket[sendlen++] = 0xE5;
// send the packet
if (WriteCOM(sendlen, sendpacket))
{
// No response
return TRUE;
}
// failed to write command
return FALSE;
}
/**@brief DS94181P-300 Switch to 1-Wire Mode
* Requires to keep track of DS9481-P communication state.
* Example could use a flag to store latest communication state every time this function is used or in
* conjuction with DS9481_SwitchI2CMode
@retval TRUE On Success
@retval FALSE On Failed
*/
int DS9481_SwitchOneWireMode(void)
{
uchar sendpacket[10];
short sendlen = 0;
//construct command
sendpacket[0] = 0x43;
WriteCOM(1, sendpacket);
sendpacket[0] = 0x4F;
if (WriteCOM(1, sendpacket)) {
OWReset();
FlushCOM();
return TRUE;
}
// failed to write command
return FALSE;
}