-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCA9698.cpp
136 lines (122 loc) · 2.35 KB
/
PCA9698.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "Arduino.h"
#include "Wire.h"
#include "PCA9698.h"
PCA9698::PCA9698()
{
Adr = 0x0;
}
void PCA9698::begin(byte adr)
{
Adr = adr;
Wire.begin();
Wire.setClock(400000);
}
void PCA9698::pinMode(byte Port, byte Pin, bool Func)
{
if(Port >= 0 && Port <= 4 && Pin >= 0 && Pin <= 7)
{
byte Reg = 0x18 + Port;
byte Data = readReg(Reg);
if(Func == OUTPUT)
{
Data &= ~(1 << Pin);
}
if(Func == INPUT)
{
Data |= (1 << Pin);
}
writeReg(Reg, Data);
}
}
void PCA9698::digitalWrite(byte Port, byte Pin, bool Stat)
{
if(Port >= 0 && Port <= 4 && Pin >= 0 && Pin <= 7)
{
byte Reg = 0x08 + Port;
byte Data = readReg(Reg);
if(Stat == LOW)
{
Data &= ~(1 << Pin);
}
if(Stat == HIGH)
{
Data |= (1 << Pin);
}
writeReg(Reg, Data);
}
}
void PCA9698::setOutputs(byte bank0, byte bank1, byte bank2, byte bank3, byte bank4)
{
byte Reg = 0x88; //AI bit 1
int length = 5;
byte Data [] = {bank0, bank1, bank2, bank3, bank4};
writeMultiReg(Reg, Data, length);
}
bool PCA9698::digitalRead(byte Port, byte Pin)
{
if(Port >= 0 && Port <= 4 && Pin >= 0 && Pin <= 7)
{
byte Reg = 0x00 + Port;
byte Data = readReg(Reg);
return bool((Data >> Pin) & 0x01);
}
}
void PCA9698::outputEnable(void)
{
byte Reg = 0x2A;
byte Data = readReg(Reg);
Data &= ~(1 << 0);
writeReg(Reg, Data);
}
void PCA9698::outputDisable(void)
{
byte Reg = 0x2A;
byte Data = readReg(Reg);
Data |= (1 << 0);
writeReg(Reg, Data);
}
void PCA9698::digitalWriteALL(bool Stat)
{
byte Reg = 0x29;
byte Data = 0;
byte Data2 = 0;
byte Data3 = 0;
if(Stat == HIGH)
{
Data = 0b10011111;
Data3 = 0b11111111;
Data2 = 0b00011111;
}
else if(Stat == LOW)
{
Data = 0b00000000;
Data3 = 0b00000000;
Data2 = 0b10000000;
}
writeReg(Reg, Data);
for(uint8_t RegOut = 0x08; RegOut <= 0x0C; RegOut++) //refresh Output register
writeReg(RegOut, Data3);
writeReg(Reg, Data2);
}
void PCA9698::writeReg(byte Reg, byte Data)
{
Wire.beginTransmission(Adr);
Wire.write(uint8_t(Reg));
Wire.write(uint8_t(Data));
Wire.endTransmission();
}
void PCA9698::writeMultiReg(byte Reg, byte *Data, int length)
{
Wire.beginTransmission(Adr);
Wire.write(Reg);
Wire.write(Data, length);
Wire.endTransmission(Adr);
}
byte PCA9698::readReg(byte Reg)
{
Wire.beginTransmission(Adr);
Wire.write(uint8_t(Reg));
Wire.endTransmission();
Wire.requestFrom(Adr, 1);
return Wire.read();
}