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//
140145LC7821 ::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+ //
147179LC7823 ::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
0 commit comments