Add 8575 examples.
This commit is contained in:
parent
6539ca328a
commit
19aaf5e28d
5 changed files with 143 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,3 +2,4 @@ Doxyfile
|
|||
.vscode/
|
||||
html/
|
||||
.DS_Store
|
||||
.idea
|
||||
30
examples/pcf8575_blink16leds/pcf8574_blink8leds.ino
Normal file
30
examples/pcf8575_blink16leds/pcf8574_blink8leds.ino
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <Adafruit_PCF8575.h>
|
||||
|
||||
/* Example for 16 output LEDs that are connected from power to the GPIO expander pins
|
||||
* Note the LEDs must be connected with the CATHODES to the expander, to SINK current!
|
||||
* The PCF8575 cannot SOURCE current!
|
||||
*/
|
||||
|
||||
Adafruit_PCF8574 pcf;
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("Adafruit PCF8575 LED blink test");
|
||||
|
||||
if (!pcf.begin(0x20, &Wire)) {
|
||||
Serial.println("Couldn't find PCF8575");
|
||||
while (1);
|
||||
}
|
||||
for (uint8_t p=0; p<16; p++) {
|
||||
pcf.pinMode(p, OUTPUT);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
for (uint8_t p=0; p<16; p++) {
|
||||
pcf.digitalWrite(p, LOW); // turn LED on by sinking current to ground
|
||||
delay(100);
|
||||
pcf.digitalWrite(p, HIGH); // turn LED off by turning off sinking transistor
|
||||
}
|
||||
}
|
||||
27
examples/pcf8575_blinkled/pcf8574_blinkled.ino
Normal file
27
examples/pcf8575_blinkled/pcf8574_blinkled.ino
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <Adafruit_PCF8575.h>
|
||||
|
||||
/* Example for 1 output LED that is connected from power to the GPIO expander pin #7
|
||||
* Note the LEDs must be connected with the CATHODES to the expander, to SINK current!
|
||||
* The PCF8575 cannot SOURCE current!
|
||||
*/
|
||||
|
||||
Adafruit_PCF8575 pcf;
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("Adafruit PCF8575 LED blink test");
|
||||
|
||||
if (!pcf.begin(0x20, &Wire)) {
|
||||
Serial.println("Couldn't find PCF8575");
|
||||
while (1);
|
||||
}
|
||||
pcf.pinMode(7, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
pcf.digitalWrite(7, LOW); // turn LED on by sinking current to ground
|
||||
delay(100);
|
||||
pcf.digitalWrite(7, HIGH); // turn LED off by turning off sinking transistor
|
||||
delay(100);
|
||||
}
|
||||
52
examples/pcf8575_buttonledirq/pcf8574_buttonledirq.ino
Normal file
52
examples/pcf8575_buttonledirq/pcf8574_buttonledirq.ino
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include <Adafruit_PCF8575.h>
|
||||
|
||||
/* Example for 1 button that is connected from PCF GPIO #0 to ground,
|
||||
* and one LED connected from power to PCF GPIO #7
|
||||
* We also have the IRQ output connected to an Interrupt input pin on the
|
||||
* Arduino so we are not constantly polling from the PCF8575 expander
|
||||
*/
|
||||
|
||||
Adafruit_PCF8575 pcf;
|
||||
|
||||
#define PCF_BUTTON 0 // on the GPIO expander!
|
||||
#define PCF_LED 7 // on the GPIO expander!
|
||||
|
||||
#define ARDUINO_IRQ 2 // make sure this pin is possible to make IRQ input
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("Adafruit PCF8575 button/led IRQ test");
|
||||
|
||||
if (!pcf.begin(0x20, &Wire)) {
|
||||
Serial.println("Couldn't find PCF8575");
|
||||
while (1);
|
||||
}
|
||||
|
||||
pcf.pinMode(PCF_BUTTON, INPUT_PULLUP);
|
||||
pcf.pinMode(PCF_LED, OUTPUT);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
// set up the interrupt pin on IRQ signal toggle
|
||||
pinMode(ARDUINO_IRQ, INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(ARDUINO_IRQ), button_detect, CHANGE);
|
||||
}
|
||||
|
||||
// We use a flag to make sure we don't enter the interrupt more than once
|
||||
volatile bool in_irq = false;
|
||||
|
||||
// called when the button is pressed!
|
||||
void button_detect(void) {
|
||||
if (in_irq) return; // we are already handling an irq so don't collide!
|
||||
|
||||
in_irq = true;
|
||||
interrupts(); // Arduino UNO seems to require that we turn on interrupts for I2C to work!
|
||||
bool val = pcf.digitalRead(PCF_BUTTON);
|
||||
pcf.digitalWrite(PCF_LED, val);
|
||||
in_irq = false;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(100); // we do nothing here!
|
||||
}
|
||||
33
examples/pcf8575_read16buttons/pcf8574_read8buttons.ino
Normal file
33
examples/pcf8575_read16buttons/pcf8574_read8buttons.ino
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include <Adafruit_PCF8575.h>
|
||||
|
||||
/* Example for 16 input buttons that are connected from the GPIO expander pins to ground.
|
||||
* Note the buttons must be connected with the other side of the switch to GROUND. There is
|
||||
* a built in pull-up 'resistor' on each input, but no pull-down resistor capability.
|
||||
*/
|
||||
|
||||
Adafruit_PCF8575 pcf;
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("Adafruit PCF8575 button read test");
|
||||
|
||||
if (!pcf.begin(0x20, &Wire)) {
|
||||
Serial.println("Couldn't find PCF8575");
|
||||
while (1);
|
||||
}
|
||||
for (uint8_t p=0; p<16; p++) {
|
||||
pcf.pinMode(p, INPUT_PULLUP);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
for (uint8_t p=0; p<16; p++) {
|
||||
if (! pcf.digitalRead(p)) {
|
||||
Serial.print("Button on GPIO #");
|
||||
Serial.print(p);
|
||||
Serial.println(" pressed!");
|
||||
}
|
||||
}
|
||||
delay(10); // a short debounce delay
|
||||
}
|
||||
Loading…
Reference in a new issue