/* Centipede Power Driver Interface for the 16 Channel digital output board. * Copyright 2012 by Gene Buckle * All Rights Reserved * This software is provided as-is and is licensed under the * Creative Commons Attribution Share Alike license. * Please see http://creativecommons.org/about/licenses/ for details. * * I can be reached at geneb@deltasoft.com * * History: * ----------------- * 18Mar10 gwb Started. * 21Jul10 gwb First version! * 30Jul10 gwb First publicly releasable version. * 21Mar12 gwb Updated to drive the Centipede board instead of the older Gazoutta 16 board. * Provides up to 128 output channels instead of 16. * Board addressing code removed. Only the Gazatta 16 board required it. * */ #include #include #define CEN_COUNT 1 // # of Centipede boards we have to talk to. #define BANK_A 0 // Outputs 0..15 #define BANK_B 1 // Outputs 16..31 #define BANK_C 2 // Outputs 32..47 #define BANK_D 3 // Outputs 48..63 #define BANK_E 4 // Outputs 0..15 on 2nd addressable Centipede (64..79) #define BANK_F 5 // Outputs 16..31 on 2nd addressable Centipede (80..95) #define BANK_G 6 // Outputs 32..47 on 2nd addressable Centipede (96..111) #define BANK_H 7 // Outputs 48..63 on 2nd addressable Centipede (112..127) byte channels[128]; #define BUF_SIZE 32 int buffer[128]; int bufIndex = 0; Centipede CS; // create Centipede object... void setup() { int x = 0; //Serial.begin(115200); // Use this baud rate if your host can't use the faster rate below. Serial.begin(250000); // The 250k baud rate gives us a 0% error rate with a 16Mhz clock. Wire.begin(); CS.initialize(); // Initialize all the channels to be outputs and make sure they're all turned off. for(x=BANK_A; x < BANK_D+1; x++) { if(CEN_COUNT == 1) { CS.portMode(x, 0b0000000000000000); } else if(CEN_COUNT == 2) { CS.portMode(x, 0b0000000000000000); CS.portMode(x+BANK_E, 0b0000000000000000); } } for(x = 0;x < BUF_SIZE; x++) { buffer[x] = 0; } for(x = 0;x < 128; x++) channels[x] = x; } void loop() { int inByte = 0; int test = 0; if (Serial.available() > 0) { /* append the byte to the buffer */ inByte = Serial.read(); buffer[bufIndex] = inByte; bufIndex++; if ((inByte == '*') || (inByte == '\n')) { processBuffer(); clearBuffer(); bufIndex = 0; } } } void clearBuffer() { int x = 0; for(x = 0;x < BUF_SIZE; x++) { buffer[x] = 0; } } void processBuffer() { int cnt = 0; int chanBank = 0; int chanset1 = 0; int chanset2 = 0; int cmd = -1; // Byte 1 is the function byte. It tells us what we're going to do. // 0x00 - Set channels to follow bit state in channel bytes // 0x01 - Turn specific channels on // 0x02 - Turn specific channels off // 0x03 - Turn ALL channels on // 0x04 - Turn ALL channels off. cmd = buffer[0]; chanBank = buffer[1]; // What bank are we talking to? 0..7 (A..H) chanset1 = buffer[2]; // first 8 channels - binary encoded chanset2 = buffer[3]; // second 8 channels - binary encoded switch(cmd) { case 0x00: for (cnt = 0; cnt < 8; cnt++) { if (chanset1 & (1 << cnt)) { CS.digitalWrite(channels[cnt+chanBank], HIGH); } else CS.digitalWrite(channels[chanBank+cnt], LOW); if (chanset2 & (1 << cnt)) CS.digitalWrite(channels[chanBank+cnt+8], HIGH); else CS.digitalWrite(channels[chanBank+cnt+8], LOW); } break; case 0x01: for (cnt = 0; cnt < 8; cnt++) { if (chanset1 & (1 << cnt)) { CS.digitalWrite(channels[cnt+chanBank], HIGH); } if (chanset2 & (1 << cnt)) { CS.digitalWrite(channels[cnt+8+chanBank], HIGH); } } break; case 0x02: for (cnt = 0; cnt < 8; cnt++) { if (chanset1 & (1 << cnt)) CS.digitalWrite(channels[cnt+chanBank], LOW); if (chanset2 & (1 << cnt)) CS.digitalWrite(channels[cnt+8+chanBank], LOW); } break; case 0x03: if (cmd == 0x03) { allChannelsOn(); } break; case 0x04: if (cmd == 0x04) { allChannelsOff(); } break; } // // if (cmd == 'i' || buffer[0] == 'I') { // Serial.println("Centipede Power Driver v1.0"); // } } void allChannelsOn() { int x; for(x=BANK_A; x < BANK_D+1; x++) { if(CEN_COUNT == 1) { CS.portWrite(x, 0b1111111111111111); } else if(CEN_COUNT == 2) { CS.portWrite(x+BANK_E, 0b1111111111111111); } } } void allChannelsOff() { int x; for(x=BANK_A; x < BANK_D+1; x++) { if(CEN_COUNT == 1) { CS.portWrite(x, 0b0000000000000000); } else if(CEN_COUNT == 2) { CS.portWrite(x+BANK_E, 0b0000000000000000); } } }