We went to Rodulf's place for a bit of Halo and a braai, What a lovely day, but for some reason my mind seems to want to drift through my past tonight, so I dug up a few old backup CD's to remember where I've come from:
Thought I'd post a bit of embedded code from days gone by, of little use now, but it made me a living once. It was from a piece of hardware I designed to handle Modbus communication between a sensor and a PC, this snipped did Byte translating, 51 programmers might smile at it: How things have changed for me.
{$M 0 0}
unit giogen10;
interface
const
test : array[0..7] of byte = (1,2,4,8,$10,$20,$40,$80);
var
temp : string[4];
function byte_to_str(inb,len : byte) : string[8];
function Hexbyte(W : byte) : String[3];
function buff_string(instr: string[20]; num : byte) : string[20];
function TestBit(bit_number : byte; byte_on : byte) : boolean;
function TwoToOne(b1,b2 :byte) : byte;
function BitON(bit_number : byte; byte_on : byte) : byte;
function BitOFF(bit_number : byte; byte_on : byte) : byte;
implementation
function BitON(bit_number : byte; byte_on : byte) : byte;
begin
BitON := byte_on or test[bit_number];
end;
function BitOFF(bit_number : byte; byte_on : byte) : byte;
begin
BitOFF := byte_on and ($FF-test[bit_number]);
end;
function byte_to_str(inb,len : byte) : string[8];
var
loop,leni : byte;
txtemp : string[8];
begin
str(inb,txtemp);
leni := length(txtemp);
if leni < len then
for loop := (leni+1) to len do
txtemp := '0' + txtemp;
byte_to_str := txtemp;
end;
function buff_string(instr: string; num : byte) : string[20];
var
len,dud : byte;
begin
len := length(instr);
if len < num then
for dud := (len+1) to num do
instr := instr + ' ';
buff_string := instr;
end;
function TestBit(bit_number : byte; byte_on : byte) : boolean;
begin
Testbit := false;
if byte_on and (test[bit_number]) > 0 then
Testbit := true;
end;
function TwoToOne(b1,b2 :byte) : byte;
var byteval : byte;
Begin
Case B1 Of
$30 : ByteVal := 0;
$31 : ByteVal := 16;
$32 : ByteVal := 32;
$33 : ByteVal := 48;
$34 : ByteVal := 64;
$35 : ByteVal := 80;
$36 : ByteVal := 96;
$37 : ByteVal := 112;
$38 : ByteVal := 128;
$39 : ByteVal := 144;
$41 : ByteVal := 160;
$42 : ByteVal := 176;
$43 : ByteVal := 192;
$44 : ByteVal := 208;
$45 : ByteVal := 224;
$46 : ByteVal := 240;
End;
Case B2 Of
$30 : ByteVal := ByteVal + 0;
$31 : ByteVal := ByteVal + 1;
$32 : ByteVal := ByteVal + 2;
$33 : ByteVal := ByteVal + 3;
$34 : ByteVal := ByteVal + 4;
$35 : ByteVal := ByteVal + 5;
$36 : ByteVal := ByteVal + 6;
$37 : ByteVal := ByteVal + 7;
$38 : ByteVal := ByteVal + 8;
$39 : ByteVal := ByteVal + 9;
$41 : ByteVal := ByteVal + 10;
$42 : ByteVal := ByteVal + 11;
$43 : ByteVal := ByteVal + 12;
$44 : ByteVal := ByteVal + 13;
$45 : ByteVal := ByteVal + 14;
$46 : ByteVal := ByteVal + 15;
End;
twotoone := byteval;
End;
function Hexbyte(W : byte) : String[3];
const
HexDigits : ARRAY[0..15] OF Char = '0123456789ABCDEF';
var Temp : String[3];
begin
Temp[0] := #2;
Temp[1] := HexDigits[(W SHR 4) AND $F];
Temp[2] := HexDigits[W AND $F];
Hexbyte := Temp;
end;
function HexWord(W : byte) : String[4];
const
HexDigits : ARRAY[0..15] OF Char = '0123456789ABCDEF';
var Temp : String[4];
begin
Temp[0] := #4;
Temp[1] := HexDigits[(W SHR 8) AND $F];
Temp[2] := HexDigits[(W SHR 4) AND $F];
Temp[3] := HexDigits[W AND $F];
HexWord := Temp;
end;
begin
end.