Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Wire.h>
- /***************************************************************************************** Configuration **************************************************************************************************/
- // Board: Arduino/Genuino Uno
- /***************** Links *****************/
- // Arduino Wire Library: https://www.arduino.cc/en/Reference/Wire
- // NCD I2C Guide: https://ncd.io/i2c-communications-quick-start-guide/
- // 8 Channel NCD Relay Board: https://store.ncd.io/product/8-channel-high-power-relay-controller-with-i2c-interface/
- // 16 Channel NCD Relay Board: https://store.ncd.io/product/16-channel-high-power-relay-controller-with-i2c-interface/
- // 32 Channel NCD Relay Board: https://store.ncd.io/product/32-channel-high-power-relay-controller-with-i2c-interface/
- // General
- const int baud_rate = 9600;
- const char *wire_errors[] = {
- "", // 0 (success)
- "data too long to fit in transmit buffer", // 1
- "received NACK on transmit of address ", // 2
- "received NACK on transmit of data ", // 3
- "other error" // 4
- };
- // I2C Setup
- byte num_16_channel_boards = 2; // 32 Channel Board counts as two 16 Channel Boards, (maximum = 8)
- const byte bus_addr[8] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27}; // these are the possible I2C addresses the relay boards support. The program assumes you are using sequential addresses.
- const byte bus_chan[2] = {0x12, 0x13}; // each group of 16 is made up of 2 groups of 8. Each group of 8 is communicated with individually. So a board of 32 will need 4 writes to it to set all 32 relays.
- const byte init_data[2] = {0x00, 0x01}; // each I2C address has to be initialized with this data. This is from the sample programs provided by NCD.io
- // Relay Representation Map:
- // {0-7, 16-23, 32-39, 48-55, 64-71, 80-87, 96-103, 112-119}
- // {8-15, 24-31, 40-47, 56-63, 72-79, 88-95, 104-111, 120-127}
- byte relay_data[2][8] = {
- {0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0}
- };
- /***************************************************************************************** I2C Functions **************************************************************************************************/
- // Configure the I2C bus addresses as output devices.
- void configure_I2C(byte numBanks)
- {
- // loop through the 2 channels for each activated bank of relays. This nested loop configures the relays to be I2C outputs.
- // the number of outer loops is based on the number of activated relay banks. The code won't try to write to relays that aren't connected.
- for (byte i = 0; i < numBanks; i++)
- {
- for (byte j = 0; j < 2; j++)
- {
- Wire.beginTransmission(bus_addr[i]); // write I2C address
- Wire.write(init_data[j]); // write the initialization data for the channel (first channel is 0x00, second 0x01, this is from the sample program)
- Wire.write(0x00); // always write 0x00 here. This is from the sample program.
- byte ret = Wire.endTransmission();
- if (ret)
- {
- Serial.print(F("Wire.endTransmission() >> ERROR = "));
- Serial.println(wire_errors[ret]);
- }
- delay(10);
- }
- }
- }
- // Write the contents of the I2C relay data matrix to the board with a nested loop.
- void write_I2C()
- {
- for (byte i = 0; i < num_16_channel_boards; i++)
- {
- for (byte j = 0; j < 2; j++)
- {
- Wire.beginTransmission(bus_addr[i]);
- Wire.write(bus_chan[j]);
- Wire.write(relay_data[j][i]);
- byte ret = Wire.endTransmission();
- if (ret)
- {
- Serial.print(F("Wire.endTransmission() >> ERROR = "));
- Serial.println(wire_errors[ret]);
- }
- delay(1);
- }
- }
- }
- void write_to_relay(int group_16_num, int relay_num, int state)
- {
- Wire.beginTransmission(bus_addr[group_16_num]);
- if (relay_num < 9)
- { // relays 1-8
- Wire.write(bus_chan[0]);
- }
- else
- { // relays 9-16
- Wire.write(bus_chan[1]);
- }
- if (state)
- {
- Wire.write(relay_num);
- }
- else
- {
- Wire.write(relay_num);
- }
- byte ret = Wire.endTransmission();
- if (ret)
- {
- Serial.print(F("Wire.endTransmission() >> ERROR = "));
- Serial.println(wire_errors[ret]);
- }
- }
- // splits a 16 bit unsigned int into two 8 bit sections. The 8 bit sections are 16 bits still, but the last 8 bits will be 0.
- void set_relay_data(unsigned int bus_data, int bus_num)
- {
- // [y][x]
- relay_data[0][bus_num] = (bus_data)&0xff;
- relay_data[1][bus_num] = (bus_data / 256) & 0xff;
- }
- /***************************************************************************************** Arduino Functions **************************************************************************************************/
- void loop()
- {
- set_relay_data(1158, i);
- write_I2C();
- delay(10);
- }
- void setup()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(baud_rate);
- while (!Serial)
- {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- // I2C setup
- Wire.begin();
- configure_I2C(num_16_channel_boards); // initialize relay boards as an output device
- write_I2C(); // reset relays on boot
- Serial.println(F("setup finished"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement