-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2CLIB.PAS
84 lines (72 loc) · 2 KB
/
I2CLIB.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
Copyright 2021 by Mogens Bramm
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 3 of the License, or
(at your option) 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, see <https://www.gnu.org/licenses/>.
}
const
I2C_PORT = $0C;
SDA_LOW = $00;
SDA_HIGH = $80;
SCL_LOW = $00;
SCL_HIGH = $01;
SDA_MASK = $80;
function getState: byte;
begin
getState := port[I2C_PORT] and SDA_MASK;
end;
procedure setState(sda, sdc: byte);
begin
port[I2C_PORT] := sda or sdc;
end;
function readBit: byte;
begin
setState(SDA_HIGH, SCL_LOW);
setState(SDA_HIGH, SCL_HIGH);
readBit := getState;
end;
procedure writeBit(sda: byte);
begin
setState(sda, SCL_LOW);
setState(sda, SCL_HIGH);
end;
function readByte(ack: byte): byte;
var i, b: byte;
begin
b := 0;
for i := 0 to 7 do
if readBit = SDA_HIGH then
b := (b shl 1) or $01
else
b := b shl 1;
writeBit(ack);
readByte := b;
end;
function writeByte(b: byte): byte;
var i: byte;
begin
for i := 0 to 7 do begin
writeBit(b and SDA_MASK);
b := b shl 1;
end;
writeByte := readBit; { return ACK/NACK value }
end;
procedure startSequence;
begin
setState(SDA_HIGH, SCL_HIGH);
setState(SDA_LOW, SCL_HIGH);
setState(SDA_LOW, SCL_LOW);
end;
procedure endSequence;
begin
setState(SDA_LOW, SCL_LOW);
setState(SDA_LOW, SCL_HIGH);
setState(SDA_HIGH, SCL_HIGH);
end;