Skip to content

Commit bee1f02

Browse files
committed
0.2.0 LC7822
1 parent 7754b90 commit bee1f02

15 files changed

Lines changed: 540 additions & 109 deletions

File tree

libraries/LC7822/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.2.0] - 2025-12-21
10+
- fix #2, cePin bug
11+
- fix #3, add address as parameter to **begin()**
12+
- fix #5, addresses of the devices.
13+
- fix shift order in updateDevice()
14+
- refactor class hierarchy, for address check.
15+
- make **sPin** optional, needed for hardwired sPins.
16+
- add **getAddress()** convenience function.
17+
- add **LC7822_fixed_address.ino** example
18+
- add **LC7822_two_device.ino** example
19+
- update readme.md a lot.
20+
- minor edits
21+
22+
----
23+
924
## [0.1.0] - 2025-12-20
1025
- initial version
1126

libraries/LC7822/LC7822.cpp

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
// FILE: LC7822.cpp
33
// AUTHOR: Rob Tillaart
44
// DATE: 2025-12-20
5-
// VERSION: 0.1.0
5+
// VERSION: 0.2.0
66
// PURPOSE: Arduino library for the LC7822 8 channel analogue switch.
77
// URL: https://github.com/RobTillaart/LC7822
88

99

1010
#include "LC7822.h"
1111

1212

13-
LC7822::LC7822(uint8_t dataPin, uint8_t clockPin, uint8_t cePin, uint8_t sPin, uint8_t resetPin)
13+
LC782X::LC782X(uint8_t dataPin, uint8_t clockPin, uint8_t cePin, uint8_t sPin, uint8_t resetPin)
1414
{
15-
_address = 11;
15+
_address = 0;
1616
_dataPin = dataPin;
1717
_clockPin = clockPin;
1818
_cePin = cePin;
@@ -22,17 +22,22 @@ LC7822::LC7822(uint8_t dataPin, uint8_t clockPin, uint8_t cePin, uint8_t sPin, u
2222
}
2323

2424

25-
bool LC7822::begin()
25+
bool LC782X::begin(uint8_t address)
2626
{
27+
_address = address;
28+
2729
pinMode(_dataPin, OUTPUT);
2830
digitalWrite(_dataPin, LOW);
2931
pinMode(_clockPin, OUTPUT);
3032
digitalWrite(_clockPin, LOW);
3133
pinMode(_cePin, OUTPUT);
3234
digitalWrite(_cePin, LOW);
33-
pinMode(_sPin, OUTPUT);
34-
digitalWrite(_sPin, LOW);
35-
35+
// optional pins.
36+
if (_sPin != 255)
37+
{
38+
pinMode(_sPin, OUTPUT);
39+
digitalWrite(_sPin, LOW);
40+
}
3641
if (_resetPin != 255)
3742
{
3843
pinMode(_resetPin, OUTPUT);
@@ -42,7 +47,7 @@ bool LC7822::begin()
4247
}
4348

4449

45-
bool LC7822::reset()
50+
bool LC782X::reset()
4651
{
4752
if (_resetPin != 255)
4853
{
@@ -58,21 +63,21 @@ bool LC7822::reset()
5863
}
5964

6065

61-
bool LC7822::setAll(uint8_t value)
66+
bool LC782X::setAll(uint8_t value)
6267
{
6368
_switches = value;
6469
_updateDevice();
6570
return true;
6671
}
6772

6873

69-
uint8_t LC7822::getAll()
74+
uint8_t LC782X::getAll()
7075
{
7176
return _switches;
7277
}
7378

7479

75-
bool LC7822::setSwitch(uint8_t sw, bool val)
80+
bool LC782X::setSwitch(uint8_t sw, bool val)
7681
{
7782
if (sw > 7) return false;
7883
if (val) // ON
@@ -88,7 +93,7 @@ bool LC7822::setSwitch(uint8_t sw, bool val)
8893
}
8994

9095

91-
bool LC7822::getSwitch(uint8_t sw)
96+
bool LC782X::getSwitch(uint8_t sw)
9297
{
9398
if (sw > 7) return false;
9499
return _switches & (1 << sw);
@@ -99,13 +104,13 @@ bool LC7822::getSwitch(uint8_t sw)
99104
//
100105
// PROTECTED
101106
//
102-
void LC7822::_updateDevice()
107+
void LC782X::_updateDevice()
103108
{
104-
// select this device
105-
digitalWrite(_sPin, HIGH);
109+
// select this device only if sPin is defined.
110+
if (_sPin != 255) digitalWrite(_sPin, HIGH);
106111

107-
// send address == 4 bits
108-
for (uint8_t mask = 0x08; mask > 0x00; mask >>= 1)
112+
// send address == 4 bits - A0 first
113+
for (uint8_t mask = 0x01; mask < 0x10; mask <<= 1)
109114
{
110115
digitalWrite(_dataPin, (_address & mask));
111116
if (_microDelay) delayMicroseconds(_microDelay);
@@ -114,40 +119,73 @@ void LC7822::_updateDevice()
114119
digitalWrite(_clockPin, LOW);
115120
}
116121

117-
// send data == 8 bits
122+
// send data == 8 bits - SW1 first
118123
if (_microDelay) delayMicroseconds(_microDelay);
119124
digitalWrite(_cePin, HIGH);
120125
if (_microDelay) delayMicroseconds(_microDelay);
121126

122-
for (uint8_t mask = 0x80; mask > 0x00; mask >>= 1)
127+
for (uint8_t mask = 0x01; mask != 0x00; mask <<= 1)
123128
{
124129
digitalWrite(_dataPin, (_switches & mask));
125130
if (_microDelay) delayMicroseconds(_microDelay);
126131
digitalWrite(_clockPin, HIGH);
127132
if (_microDelay) delayMicroseconds(_microDelay);
128133
digitalWrite(_clockPin, LOW);
129134
}
130-
digitalWrite(_cePin, HIGH);
135+
digitalWrite(_cePin, LOW);
131136

132-
digitalWrite(_sPin, LOW);
137+
if (_sPin != 255) digitalWrite(_sPin, LOW);
133138
}
134139

135140

136141
//////////////////////////////////////////////////
137142
//
138-
// DERIVED
143+
// DERIVED LC7821
139144
//
140145
LC7821::LC7821(uint8_t dataPin, uint8_t clockPin, uint8_t cePin, uint8_t sPin, uint8_t resetPin)
141-
:LC7822(dataPin, clockPin, cePin, sPin, resetPin)
146+
:LC782X(dataPin, clockPin, cePin, sPin, resetPin)
142147
{
143-
_address = 13;
148+
_address = 0x0B;
144149
}
145150

151+
bool LC7821::begin(uint8_t address)
152+
{
153+
if ((address != 0x0A) && (address != 0x0B)) return false;
154+
return LC782X::begin(address);
155+
}
146156

157+
158+
//////////////////////////////////////////////////
159+
//
160+
// DERIVED LC7822
161+
//
162+
LC7822::LC7822(uint8_t dataPin, uint8_t clockPin, uint8_t cePin, uint8_t sPin, uint8_t resetPin)
163+
:LC782X(dataPin, clockPin, cePin, sPin, resetPin)
164+
{
165+
_address = 0x0D;
166+
}
167+
168+
bool LC7822::begin(uint8_t address)
169+
{
170+
if ((address != 0x0C) && (address != 0x0D)) return false;
171+
return LC782X::begin(address);
172+
}
173+
174+
175+
//////////////////////////////////////////////////
176+
//
177+
// DERIVED LC7823
178+
//
147179
LC7823::LC7823(uint8_t dataPin, uint8_t clockPin, uint8_t cePin, uint8_t sPin, uint8_t resetPin)
148-
:LC7822(dataPin, clockPin, cePin, sPin, resetPin)
180+
:LC782X(dataPin, clockPin, cePin, sPin, resetPin)
181+
{
182+
_address = 0x0F;
183+
}
184+
185+
bool LC7823::begin(uint8_t address)
149186
{
150-
_address = 15;
187+
if ((address != 0x0E) && (address != 0x0F)) return false;
188+
return LC782X::begin(address);
151189
}
152190

153191

libraries/LC7822/LC7822.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
// FILE: LC7822.h
44
// AUTHOR: Rob Tillaart
55
// DATE: 2025-12-20
6-
// VERSION: 0.1.0
6+
// VERSION: 0.2.0
77
// PURPOSE: Arduino library for the LC7822 8 channel analogue switch.
88
// URL: https://github.com/RobTillaart/LC7822
99

1010

1111
#include "Arduino.h"
1212

13-
#define LC7822_LIB_VERSION (F("0.1.0"))
13+
#define LC7822_LIB_VERSION (F("0.2.0"))
1414

1515

16-
class LC7822
16+
// base class
17+
class LC782X
1718
{
1819
public:
19-
LC7822(uint8_t dataPin, uint8_t clockPin, uint8_t cePin,
20-
uint8_t sPin, uint8_t resetPin = 255);
20+
LC782X(uint8_t dataPin, uint8_t clockPin, uint8_t cePin,
21+
uint8_t sPin = 255, uint8_t resetPin = 255);
2122

22-
bool begin();
23+
bool begin(uint8_t address);
2324
bool reset();
2425
uint8_t getAddress() { return _address; };
2526

@@ -53,19 +54,30 @@ class LC7822
5354
//
5455
// DERIVED
5556
//
56-
class LC7821 : public LC7822
57+
class LC7821 : public LC782X
5758
{
5859
public:
5960
LC7821(uint8_t dataPin, uint8_t clockPin, uint8_t cePin,
60-
uint8_t sPin, uint8_t resetPin = 255);
61+
uint8_t sPin = 255, uint8_t resetPin = 255);
62+
bool begin(uint8_t address = 0x0B);
63+
};
64+
65+
66+
class LC7822 : public LC782X
67+
{
68+
public:
69+
LC7822(uint8_t dataPin, uint8_t clockPin, uint8_t cePin,
70+
uint8_t sPin = 255, uint8_t resetPin = 255);
71+
bool begin(uint8_t address = 0x0D);
6172
};
6273

6374

64-
class LC7823 : public LC7822
75+
class LC7823 : public LC782X
6576
{
6677
public:
6778
LC7823(uint8_t dataPin, uint8_t clockPin, uint8_t cePin,
68-
uint8_t sPin, uint8_t resetPin = 255);
79+
uint8_t sPin = 255, uint8_t resetPin = 255);
80+
bool begin(uint8_t address = 0x0F);
6981
};
7082

7183

0 commit comments

Comments
 (0)