Advertisement
AtomSoft

SX1509 Test

Mar 10th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 KB | None | 0 0
  1. /* SX1509 Library Example 01
  2.    by: Jim Lindblom
  3.    SparkFun Electronics
  4.    license: Beerware. Please use, reuse, share, and modify this
  5.    code. I'd ask that you maintain attribution and open-source.
  6.    If you find it useful, you can buy me a beer when we meet
  7.    some day.
  8.  
  9.    This is the simplest example for the SX1509 Arduino library.
  10.    This sketch shows how you can use the following SX1509 library  
  11.    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.      pinDir(pin, INPUT/OUTPUT) - This method functions much like
  20.        the Arduino pinMode() function. pin should be an SX1509
  21.        pin between 0 and 15. INPUT and OUTPUT work like usual.
  22.      writePin(pin, HIGH/LOW) - Similar to the Arduino
  23.        digitalWrite() function. pin should be between 0 and 15,
  24.        HIGH and LOW work as usual. This function will also
  25.        activate the pull-up and down resistors if the pin is
  26.        configured as an input.
  27.      readPin(pin) - This function works like the digitalRead()
  28.        function of the Arduino. Give it a pin between 0 and 15
  29.        and a 0 or 1 will be returned representing whether the pin
  30.        is LOW or HIGH.
  31.  
  32.     Hardware: The SX1509 should be hooked up like so:
  33.     SX1509 Pin      Arduino Pin
  34.        3.3V ---------- 3.3V
  35.        GND ----------- GND
  36.        SDA ----------- A4 (or SDA on newer boards)
  37.        SCL ----------- A5 (or SCL on newer boards)
  38.        
  39.        nRST, OSCIO, and nINT are not used in this example. Though
  40.        they can be connected to any digital pin.
  41.    
  42.    See the SX1509_ADDRESS defines to decide which address you need
  43.    to send to the constructor. By default the SX1509 Breakout
  44.    sets both ADDR pins to 0 (so 0x3E I2C address).
  45.  
  46.    In addition two SX1509 i/o pins are used:
  47.      14 - Output, could be connected to an LED
  48.      1 - Input, could be connected to a button.
  49. */
  50.  
  51. #include <Wire.h>  // Wire.h library is required to use SX1509 lib
  52. #include <sx1509_library.h>  // Include the SX1509 library
  53.  
  54. // Uncomment one of the four lines to match your SX1509's address
  55. //  pin selects. SX1509 breakout defaults to [0:0] (0x3E).
  56. const byte SX1509_ADDRESS = 0x3E;  // SX1509 I2C address (00)
  57. //const byte SX1509_ADDRESS = 0x3F;  // SX1509 I2C address (01)
  58. //const byte SX1509_ADDRESS = 0x70;  // SX1509 I2C address (10)
  59. //const byte SX1509_ADDRESS = 0x71;  // SX1509 I2C address (11)
  60.  
  61. // Pin definitions, not actually used in this example
  62. const byte interruptPin = 2;
  63. const byte resetPin = 8;
  64.  
  65. // Create a new sx1509Class object. You can make it with all
  66. // of the above pins:
  67. //sx1509Class sx1509(SX1509_ADDRESS, resetPin, interruptPin);
  68. // Or make an sx1509 object with just the SX1509 I2C address:
  69. sx1509Class sx1509(SX1509_ADDRESS);
  70.  
  71. // SX1509 pin defintions:
  72. const byte buttonPin = 1;
  73. const byte buttonPin2 = 2;
  74. const byte ledPin = 14;
  75. const byte ledPin2 = 13;
  76. const byte ledPin3 = 12;
  77.  
  78. void setup()
  79. {
  80.   Serial.begin(9600);
  81.   sx1509.init();  // Initialize the SX1509, does Wire.begin()
  82.   sx1509.pinDir(buttonPin, INPUT);  // Set SX1509 pin 1 as an input
  83.   sx1509.pinDir(buttonPin2, INPUT);  // Set SX1509 pin 2 as an input
  84.   //sx1509.writePin(buttonPin, HIGH);  // Activate pull-up
  85.   sx1509.pinDir(ledPin, OUTPUT);  // Set SX1509 pin 14 as an output
  86.   sx1509.pinDir(ledPin2, OUTPUT);  // Set SX1509 pin 13 as an output
  87.   sx1509.pinDir(ledPin3, OUTPUT);  // Set SX1509 pin 12 as an output
  88.  
  89.   // Blink pin 14 a few times
  90.   for (int i=0; i<5; i++)
  91.   {
  92.     sx1509.writePin(ledPin, LOW);  // Write pin LOW
  93.     delay(100);
  94.     sx1509.writePin(ledPin, HIGH);  // Write pin HIGH
  95.     delay(100);
  96.   }
  97. }
  98.  
  99. void loop()
  100. {
  101.   int buttonValue = sx1509.readPin(buttonPin);  // read pin 1 status
  102.   int buttonValue2 = sx1509.readPin(buttonPin2);  // read pin 2 status
  103.  
  104.   if (buttonValue == 1)  // by default pin 14 should be high
  105.   {
  106.     sx1509.writePin(ledPin, LOW);  // turn pin off
  107.     sx1509.writePin(ledPin2, HIGH);  // turn pin off
  108.   } else {
  109.       if (buttonValue2 == 1)  // by default pin 13 should be high
  110.       {
  111.         sx1509.writePin(ledPin, LOW);  // turn pin off
  112.         sx1509.writePin(ledPin2, HIGH);  // turn pin off
  113.         sx1509.writePin(ledPin3, LOW);  // turn pin off
  114.       } else {
  115.         sx1509.writePin(ledPin, HIGH);  // turn pin off
  116.         sx1509.writePin(ledPin2, LOW);  // turn pin off
  117.         sx1509.writePin(ledPin3, HIGH);  // turn pin off
  118.       }
  119.   }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement