Skip to content

Commit bfe8e79

Browse files
author
Jim Lindblom
committed
Added much LED Driver functionality
New Functions: ledDriverInit, pwm, blink, sync New example to demo LED driver Renamed simple example
1 parent 6f7e141 commit bfe8e79

File tree

5 files changed

+396
-13
lines changed

5 files changed

+396
-13
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* SX1509 Library Example 02
2+
Driving LEDs: PWM, Blink, Breath
3+
by: Jim Lindblom
4+
SparkFun Electronics
5+
license: Beerware. Please use, reuse, share, and modify this
6+
code. I'd ask that you maintain attribution and open-source.
7+
If you find it useful, you can buy me a beer when we meet
8+
some day.
9+
10+
This example shows how you can use the following SX1509
11+
library methods:
12+
constructor - initialize an instance of the SX1509 library.
13+
The minimum required parameter is the I2C address of the
14+
SX1509. Other parameters (resetPin, interruptPin, oscPin)
15+
are optional (as shown).
16+
init() - initializes the SX1509. Will perform a reset, start
17+
up the Wire library and read some registers to make sure
18+
the SX1509 is operational.
19+
ledDriverInit
20+
pwm
21+
blink
22+
sync
23+
24+
Hardware: The SX1509 should be hooked up like so:
25+
SX1509 Pin Arduino Pin
26+
3.3V ---------- 3.3V
27+
GND ----------- GND
28+
SDA ----------- A4 (or SDA on newer boards)
29+
SCL ----------- A5 (or SCL on newer boards)
30+
nRST ---------- 8 (could be any digital pin)
31+
OSCIO, and nINT are not used in this example.
32+
33+
See the SX1509_ADDRESS defines to decide which address you need
34+
to send to the constructor. By default the SX1509 Breakout
35+
sets both ADDR pins to 0 (so 0x3E I2C address).
36+
37+
In addition SX1509 i/o pins are used to drive LEDs. These
38+
pins should be sinking current to an LED. So the LED should be
39+
pulled (through a current-limiting resistor) to 3.3V.
40+
14 - Breathing output (pins 4-7 and 12-15 can breath)
41+
3 - Blinking output (any i/o pin can blink)
42+
0 - PWM output (any i/o pin can pwm)
43+
*/
44+
45+
#include <Wire.h> // Wire.h library is required to use SX1509 lib
46+
#include <sx1509_library.h> // Include the SX1509 library
47+
48+
// Uncomment one of the four lines to match your SX1509's address
49+
// pin selects. SX1509 breakout defaults to [0:0] (0x3E).
50+
//const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address (00)
51+
const byte SX1509_ADDRESS = 0x3F; // SX1509 I2C address (01)
52+
//const byte SX1509_ADDRESS = 0x70; // SX1509 I2C address (10)
53+
//const byte SX1509_ADDRESS = 0x71; // SX1509 I2C address (11)
54+
55+
// Arduino pin definitions
56+
const byte resetPin = 8;
57+
58+
// SX1509 I/O pin definitions
59+
int breatheLED = 14;
60+
int blinkLED = 3;
61+
int pwmLED = 0;
62+
63+
// Create a new sx1509Class object. You can make it with all
64+
// of the above pins:
65+
//sx1509Class sx1509(SX1509_ADDRESS, resetPin, interruptPin);
66+
// Or make an sx1509 object with just the SX1509 I2C address and
67+
// reset pin. Reset is used to demo sync:
68+
sx1509Class sx1509(SX1509_ADDRESS, resetPin);
69+
70+
void setup()
71+
{
72+
// Must first initialize the sx1509:
73+
sx1509.init();
74+
// Now init ledDriver-ness on the LED driver pins:
75+
// By default these will set the led driver clock (ClkX) to 2MHz
76+
// and linear ramp mode.
77+
sx1509.ledDriverInit(breatheLED);
78+
sx1509.ledDriverInit(blinkLED);
79+
sx1509.ledDriverInit(pwmLED);
80+
// There are two more parameters you can send the ledDriverInit
81+
// function. One sets log vs. linear ramp mode. The other
82+
// sets the ClkX divider.
83+
// clockDivider is a 3-bit value (value between 1 and 7)
84+
// A bigger clockDivider value will lead to a slower clock
85+
// ClkX = 2MHz/(2^(clockDivider-1))
86+
byte clockDivider = 1;
87+
sx1509.ledDriverInit(pwmLED, clockDivider, LOGARITHMIC);
88+
// Note that the log/linear and ClkX divider parameters affect
89+
// the LED drivers on all pins. The last setting you make will
90+
// apply to all led driver pins.
91+
92+
// Demo breathe:
93+
// Play around with some of these variables.
94+
// onTime and offTime set Ton and Toff as follows:
95+
// 0: off
96+
// 1-15: Ton = 64 * onTime * (255 / ClkX)
97+
// 16-31: Ton = 512 * onTime * (255 / ClkX)
98+
byte onTime = 16; // should be between 1 and 31. 0 = off.
99+
byte offTime = 16; // big diff between 15 and 16
100+
byte offIntensity = 0; // 0=100% off, 7=highest (still fairly low)
101+
byte onIntensity = 255; // 0 = 0%, 255 = 100%
102+
// riseTime and fallTime set TRise and TFall as follows:
103+
// 0: off
104+
// 1-15: TRise = (onIntensity) - (4 * offIntensity) * riseTime * (255 / ClkX)
105+
// 16-31: TRise = 16 * (the 1-15 above equation)
106+
byte riseTime = 10; // 31 = longest, 1 = shortest
107+
byte fallTime = 10; // big difference between 15 and 16
108+
sx1509.blink(breatheLED, onTime, offTime,
109+
offIntensity, onIntensity,
110+
riseTime, fallTime);
111+
112+
// Demo blink:
113+
// To blink an LED, you can send just three parameters to the
114+
// blink function:
115+
sx1509.blink(blinkLED, onTime, offTime);
116+
// Or you can send two more to indicate the on and off intensity
117+
sx1509.blink(blinkLED, onTime, offTime,
118+
offIntensity, onIntensity);
119+
120+
// If you want to sync up each of the I/O's led driver
121+
// counters. Try sending a sync command. This requires nReset
122+
// to be connected:
123+
sx1509.sync();
124+
}
125+
126+
void loop()
127+
{
128+
// In the loop, we'll manually cycle this pin. What a pain!
129+
// You could just breath it and leave it.
130+
for (byte intensity=0; intensity<=254; intensity++)
131+
{
132+
// The pwm function requires two paramaters:
133+
// a pin and an intensity between 0 and 255.
134+
// It works a lot like analogWrite()!
135+
sx1509.pwm(pwmLED, intensity);
136+
delay(5);
137+
}
138+
delay(500);
139+
for (byte intensity=255; intensity>0; intensity--)
140+
{
141+
sx1509.pwm(pwmLED, intensity);
142+
delay(5);
143+
}
144+
delay(500);
145+
}

Arduino/libraries/SX1509/sx1509_includes/sx1509_registers.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,28 @@
118118
#define REG_RESET 0x7D // RegReset Software reset register 0000 0000
119119
#define REG_TEST_1 0x7E // RegTest1 Test register 0000 0000
120120
#define REG_TEST_2 0x7F // RegTest2 Test register 0000 0000
121+
122+
byte REG_I_ON[16] = {REG_I_ON_0, REG_I_ON_1, REG_I_ON_2, REG_I_ON_3,
123+
REG_I_ON_4, REG_I_ON_5, REG_I_ON_6, REG_I_ON_7,
124+
REG_I_ON_8, REG_I_ON_9, REG_I_ON_10, REG_I_ON_11,
125+
REG_I_ON_12, REG_I_ON_13, REG_I_ON_14, REG_I_ON_15};
126+
127+
byte REG_T_ON[16] = {REG_T_ON_0, REG_T_ON_1, REG_T_ON_2, REG_T_ON_3,
128+
REG_T_ON_4, REG_T_ON_5, REG_T_ON_6, REG_T_ON_7,
129+
REG_T_ON_8, REG_T_ON_9, REG_T_ON_10, REG_T_ON_11,
130+
REG_T_ON_12, REG_T_ON_13, REG_T_ON_14, REG_T_ON_15};
131+
132+
byte REG_OFF[16] = {REG_OFF_0, REG_OFF_1, REG_OFF_2, REG_OFF_3,
133+
REG_OFF_4, REG_OFF_5, REG_OFF_6, REG_OFF_7,
134+
REG_OFF_8, REG_OFF_9, REG_OFF_10, REG_OFF_11,
135+
REG_OFF_12, REG_OFF_13, REG_OFF_14, REG_OFF_15};
136+
137+
byte REG_T_RISE[16] = {0xFF, 0xFF, 0xFF, 0xFF,
138+
REG_T_RISE_4, REG_T_RISE_5, REG_T_RISE_6, REG_T_RISE_7,
139+
0xFF, 0xFF, 0xFF, 0xFF,
140+
REG_T_RISE_12, REG_T_RISE_13, REG_T_RISE_14, REG_T_RISE_15};
141+
142+
byte REG_T_FALL[16] = {0xFF, 0xFF, 0xFF, 0xFF,
143+
REG_T_FALL_4, REG_T_FALL_5, REG_T_FALL_6, REG_T_FALL_7,
144+
0xFF, 0xFF, 0xFF, 0xFF,
145+
REG_T_FALL_12, REG_T_FALL_13, REG_T_FALL_14, REG_T_FALL_15};

0 commit comments

Comments
 (0)