INCLUDE rtcu.inc
VAR_OUTPUT
Connected : BOOL;
NotConnected : BOOL;
END_VAR;
FUNCTION_BLOCK owDS2408;
VAR_INPUT
device : INT := 1;
END_VAR;
VAR_OUTPUT
P : ARRAY[0..7] OF BOOL;
PL : ARRAY[0..7] OF BOOL;
AL : ARRAY[0..7] OF BOOL;
SM : ARRAY[0..7] OF BOOL;
SP : ARRAY[0..7] OF BOOL;
PLS : BOOL := FALSE;
CT : BOOL := FALSE;
ROS : BOOL := FALSE;
PORL : BOOL := FALSE;
VCCP : BOOL := FALSE;
ready : BOOL := FALSE;
END_VAR;
VAR
regs : ARRAY[16#88..16#8F] OF SINT;
crc : INT;
rc : INT;
i : SINT;
END_VAR;
ready := FALSE;
rc := owAccess(family := 16#29, device := device);
IF rc <> 0 THEN
DebugFmt(message := "owDS2408 : Failed to access device (\1)", v1 := rc);
RETURN;
END_IF;
owWrite(data := 16#F0);
owWrite(data := 16#88);
owWrite(data := 16#00);
owReadData(data := ADDR(regs), size := SIZEOF(regs));
owReadData(data := ADDR(crc), size := SIZEOF(crc));
owRelease();
IF NOT (crcCalculate(
buffer := ADDR(regs),
length := SIZEOF(regs),
order := 16,
polynom := 16#0000_8005,
preset := 16#0000_66CC,
direct := TRUE,
finalxor := NOT (crc),
reverseData := TRUE,
reverseCRC := TRUE
) = 0) THEN
DebugMsg(message := "owDS2408 : Failed to read register (CRC error)");
RETURN;
END_IF;
FOR i := 0 TO 7 DO
P[i] := (regs[16#88] AND shl8(in := 1, n := i)) <> 0;
END_FOR;
FOR i := 0 TO 7 DO
PL[i] := (regs[16#89] AND shl8(in := 1, n := i)) <> 0;
END_FOR;
FOR i := 0 TO 7 DO
AL[i] := (regs[16#8A] AND shl8(in := 1, n := i)) <> 0;
END_FOR;
FOR i := 0 TO 7 DO
SM[i] := (regs[16#8B] AND shl8(in := 1, n := i)) <> 0;
END_FOR;
FOR i := 0 TO 7 DO
SP[i] := (regs[16#8C] AND shl8(in := 1, n := i)) <> 0;
END_FOR;
PLS := (regs[16#8D] AND 16#01) <> 0;
CT := (regs[16#8D] AND 16#02) <> 0;
ROS := (regs[16#8D] AND 16#04) <> 0;
PORL := (regs[16#8D] AND 16#08) <> 0;
VCCP := (regs[16#8D] AND 16#80) <> 0;
ready := TRUE;
END_FUNCTION_BLOCK;
FUNCTION owDS2408Cfg;
VAR_INPUT
device : INT := 1;
PLS : BOOL := FALSE;
CT : BOOL := FALSE;
ROS : BOOL := FALSE;
END_VAR;
VAR
reg : SINT := 16#00;
val : SINT := 16#00;
rc : INT;
END_VAR;
IF PLS THEN reg := reg OR 16#01; END_IF;
IF CT THEN reg := reg OR 16#02; END_IF;
IF ROS THEN reg := reg OR 16#04; END_IF;
rc := owAccess(family := 16#29, device := device);
IF rc <> 0 THEN
DebugFmt(message := "owDS2408Cfg : Failed to access device (\1)", v1 := rc);
RETURN;
END_IF;
owWrite(data := 16#CC);
owWrite(data := 16#8D);
owWrite(data := 16#00);
owWrite(data := reg);
owRelease();
rc := owAccess(family := 16#29, device := device);
IF rc <> 0 THEN
DebugFmt(message := "owDS2408Cfg (verify) : Failed to access device (\1)",
v1 := rc);
RETURN;
END_IF;
owWrite(data := 16#F0);
owWrite(data := 16#8D);
owWrite(data := 16#00);
owRead(data := val);
owRelease();
IF (val AND 16#0007) <> reg THEN
DebugFmt(message := "owDS2408Cfg (validate) : Failed (\1 <> \2)",
v1 := (val AND 16#0007), v2 := (reg AND 16#00FF));
END_IF;
END_FUNCTION;
FUNCTION owDS2408ResetAL : BOOL;
VAR_INPUT
device : INT := 1;
END_VAR;
VAR
ack : SINT;
rc : INT;
END_VAR;
rc := owAccess(family := 16#29, device := device);
IF rc <> 0 THEN
DebugFmt(message := "owDS2408ResetAL : Failed to access device (\1)",
v1 := rc);
RETURN;
END_IF;
owWrite(data := 16#C3);
owRead(data := ack);
owRelease();
IF (ack AND 16#00FF) <> 16#00AA THEN
DebugFmt(message := "owDS2408ResetAL : Failed to reset latch (ack = \1)",
v1 := (ack AND 16#00FF));
RETURN;
END_IF;
owDS2408ResetAL := TRUE;
END_FUNCTION;
FUNCTION owDS2408SetOutput;
VAR_INPUT
device : INT := 1;
p0 : BOOL := OFF;
p1 : BOOL := OFF;
p2 : BOOL := OFF;
p3 : BOOL := OFF;
p4 : BOOL := OFF;
p5 : BOOL := OFF;
p6 : BOOL := OFF;
p7 : BOOL := OFF;
END_VAR;
VAR
mask : SINT := 0;
ack : SINT;
rc : INT;
i : SINT;
status : SINT;
END_VAR;
IF p0 THEN mask := mask OR 16#01; END_IF;
IF p1 THEN mask := mask OR 16#02; END_IF;
IF p2 THEN mask := mask OR 16#04; END_IF;
IF p3 THEN mask := mask OR 16#08; END_IF;
IF p4 THEN mask := mask OR 16#10; END_IF;
IF p5 THEN mask := mask OR 16#20; END_IF;
IF p6 THEN mask := mask OR 16#40; END_IF;
IF p7 THEN mask := mask OR 16#80; END_IF;
rc := owAccess(family := 16#29, device := device);
IF rc <> 0 THEN
DebugFmt(message := "owDS2408SetOutput : Failed to access device (\1)", v1 := rc);
RETURN;
END_IF;
owWrite(data := 16#5A);
owWrite(data := mask);
owWrite(data := NOT(mask));
owRead(data := ack);
owRead(data := status);
owRelease();
IF (ack AND 16#00FF) <> 16#00AA THEN
DebugFmt(message := "owDS2408SetOutput : Failed to set new pin state (reply \1)",
v1 := (ack AND 16#00FF));
END_IF;
END_FUNCTION;
PROGRAM DS2408;
VAR
DS2408 : owDS2408;
id : STRING;
cnt : INT;
i : INT;
upd : BOOL;
END_VAR;
boardDCOut(enable:=ON);
BEGIN
IF connected THEN
Sleep(delay := 500);
DS2408(device := 1);
END_IF;
IF DS2408.ready THEN
owDS2408ResetAL(device := 1);
upd := FALSE;
IF DS2408.porl THEN
DebugMsg(Message := "Power-On Reset detected.");
owDS2408Cfg(device := 1, ros := ON);
upd := TRUE;
ELSE
FOR i := 0 TO 3 DO
IF DS2408.al[i] THEN
DebugFmt(message := "DS2408 : PIN \1 = \2",
v1 := i, v2 := SINT(DS2408.p[i]));
upd := TRUE;
END_IF;
END_FOR;
END_IF;
IF upd THEN
owDS2408SetOutput(
device := 1,
p4 := DS2408.p[0],
p5 := DS2408.p[1],
p6 := DS2408.p[2],
p7 := DS2408.p[3]);
END_IF;
ELSE
IF NOT (id = "") THEN
DebugMsg(message := "Lost connection to DS2408 device 1 '" + id +"'");
id := "";
END_IF;
cnt := owSearchX(first := 16#29, last := 16#29); // only search for DS2408 devices
IF cnt > 0 THEN
DebugFmt(message := "Found \1 DS2408 1-Wire devices.", v1 := cnt);
id := owGetID(device := 1, family := 16#29);
DebugFmt(message := "Using DS2408 1-Wire device = " + id);
ELSIF cnt < 0 THEN
DebugMsg(message := "Failed to search for DS2408 1-Wire devices.");
END_IF;
END_IF;
Connected := (id <> "");
NotConnected := NOT Connected;
END;
END_PROGRAM;
|